]> git.evergreen-ils.org Git - working/Evergreen.git/blob - OpenSRF/src/jserver/jserver-c_session.c
4652ccbfc66dba6a655a59c3805a70b8ae687919
[working/Evergreen.git] / OpenSRF / src / jserver / jserver-c_session.c
1 #include "jserver-c_session.h"
2
3 static int xml_error_occured = 0;
4 static int client_sent_disconnect = 0;
5
6 jserver_session* jserver_session_init() {
7
8         jserver_session* session = safe_malloc(sizeof(jserver_session));
9         session->parser_ctxt = xmlCreatePushParserCtxt(sax_handler, session, "", 0, NULL);
10         session->current_msg = xmlNewDoc(BAD_CAST "1.0");
11         session->current_to = strdup("");
12         session->current_from = strdup("");
13         session->state = 0;
14         xmlKeepBlanksDefault(0);
15         return session;
16 }
17
18 void jserver_session_free(jserver_session* session) {
19         if(session == NULL) return;
20
21         if( session->parser_ctxt) {
22                 xmlFreeDoc(session->parser_ctxt->myDoc);
23                 xmlFreeParserCtxt(session->parser_ctxt);
24         }
25
26         free(session->current_username);
27         free(session->current_resource);
28         free(session->current_domain);
29
30         xmlCleanupCharEncodingHandlers();
31         xmlFreeDoc(session->current_msg);
32         xmlCleanupParser();
33
34         free(session);
35 }
36
37
38 int jserver_session_push_data(jserver_session* session, char* data) {
39         if(session == NULL || data == NULL) return -1;  
40         debug_handler("pushing data into xml parser: %s", data);
41         xmlParseChunk(session->parser_ctxt, data, strlen(data), 0);
42         if(xml_error_occured) {
43                 xml_error_occured = 0;
44                 return -1;
45         }
46
47         if(client_sent_disconnect) {
48                 client_sent_disconnect = 0;
49                 if(session->on_client_finish)
50                         session->on_client_finish(session->blob);
51         }
52
53         return 0;
54 }
55
56 void sax_start_doc(void* blob) {
57         debug_handler("Starting new session doc");
58 }
59
60 // ---------------------------------------------------------------------------------
61 // Our SAX handlers 
62 // ---------------------------------------------------------------------------------
63 void sax_start_element( 
64                 void* blob, const xmlChar *name, const xmlChar **atts) {
65
66         jserver_session* session = (jserver_session*) blob;
67         if(!session) return;
68
69         debug_handler("jserver-c_session received opening XML node %s", name);
70
71         if(!strcmp(name, "stream:stream")) {
72
73                 /* opening a new session */     
74                 free(session->current_domain);
75                 session->current_domain = strdup(sax_xml_attr(atts, "to"));
76                 char* from_domain = sax_xml_attr(atts, "from");
77                 if(!from_domain) from_domain = "";
78
79                 if(session->current_domain == NULL) {
80                         sax_warning(session->blob, "No 'to' specified in stream opener");
81
82                 }       else {
83                         debug_handler("jserver-c_session received opening stream from client on domain %s", 
84                                 session->current_domain);
85
86                         char buf[512];
87                         memset(buf,0,512);
88
89                         /* reply with the stock jabber login response */
90                         sprintf(buf, "<?xml version='1.0'?><stream:stream xmlns:stream='http://etherx.jabber.org/streams' " 
91                                 "xmlns='jabber:client' from='%s' to='%s' version='1.0' id='d253et09iw1fv8a2noqc38f28sb0y5fc7kfmegvx'>",
92                                 session->current_domain, from_domain);
93
94                         debug_handler("Session Sending: %s", buf);
95
96                         session->state = JABBER_STATE_CONNECTING;
97                         if(session->on_login_init)
98                                 session->on_login_init(session->blob, buf);
99                 }
100                 return;
101         }
102
103         if(session->state & JABBER_STATE_CONNECTING) {
104                 /* during the connect shuffle, we have to store off the
105                         username and resource to determine the routing address */
106                 if(!strcmp(name,"iq"))
107                         session->in_iq = 1;
108                 if(!strcmp(name,"username"))
109                         session->in_uname = 1;
110                 if(!strcmp(name,"resource"))
111                         session->in_resource = 1;
112         }
113
114         if(session->state & JABBER_STATE_CONNECTED) {
115
116                 if(!strcmp(name, "message")) {
117                         /* opening of a new message, build a new doc */
118                         xmlNodePtr root = xmlNewNode(NULL, name);
119                         dom_add_attrs(root, atts);
120                         xmlNodePtr old_root = xmlDocSetRootElement(session->current_msg, root);
121
122
123                         free(session->current_to);
124
125                         char* from = sax_xml_attr(atts, "from");
126                         if(from == NULL) from = "";
127                         char* to = sax_xml_attr(atts, "to");
128                         if(to == NULL) to = "";
129
130                         session->current_to = strdup(to);
131
132                         /* free the old message tree */
133                         if(old_root) xmlFreeNode(old_root);
134
135                 } else {
136                         xmlNodePtr node = xmlNewNode(NULL, name);
137                         dom_add_attrs(node, atts);
138                         xmlAddChild(xmlDocGetRootElement(session->current_msg), node);
139                 }
140         }
141 }
142
143 void sax_end_element( void* blob, const xmlChar *name) {
144         jserver_session* session = (jserver_session*) blob;
145         if(!session) return;
146
147         if(session->state & JABBER_STATE_CONNECTED) {
148                 if(!strcmp(name, "message")) {
149                         if(session->on_msg_complete) {
150
151                                 debug_handler("Message is complete, finishing DOC");
152
153                                 /* we have to make sure the 'from' address is set.. */
154                                 xmlNodePtr msg = xmlDocGetRootElement(session->current_msg);
155                                 if(msg) xmlSetProp(msg, BAD_CAST "from", BAD_CAST session->current_from );
156                                 char* string = _xml_to_string(session->current_msg);
157
158                                 session->on_msg_complete(session->blob, string, 
159                                         session->current_from, session->current_to );
160                                 free(string);
161                         }
162                 }
163         }
164
165         if(session->state & JABBER_STATE_CONNECTING) {
166                 if(session->in_iq) {
167                         if(!strcmp(name, "iq")) {
168                                 session->in_iq = 0;
169
170                                 char buf[1024];
171                                 memset(buf, 0, 1024);
172                                 snprintf(buf, 1023, "%s@%s/%s", session->current_username,
173                                                 session->current_domain, session->current_resource );
174                                 if(session->on_from_discovered) 
175                                         session->on_from_discovered(session->blob, buf);
176
177                                 free(session->current_from);
178                                 session->current_from = strdup(buf);
179                                 debug_handler("Set from address to %s", session->current_from);
180                                 session->state = JABBER_STATE_CONNECTED;
181                                 if(session->on_login_ok) 
182                                         session->on_login_ok(session->blob);
183
184                         }
185                 }
186         }
187
188         if(!strcmp(name,"stream:stream")) {
189                 debug_handler("receive end of client session doc");
190                 client_sent_disconnect = 1;
191         }
192 }
193
194 void sax_character( void* blob, const xmlChar *ch, int len) {
195         jserver_session* session = (jserver_session*) blob;
196         if(!session) return;
197
198         if(session->state & JABBER_STATE_CONNECTED) {
199                 xmlNodePtr last = xmlGetLastChild(
200                         xmlDocGetRootElement(session->current_msg));
201         
202                 xmlNodePtr txt = xmlNewTextLen(ch, len);
203                 xmlAddChild(last, txt);
204                 return;
205         } 
206
207         if(session->state & JABBER_STATE_CONNECTING) {
208                 if(session->in_iq) {
209                         if(session->in_uname) {
210                                 free(session->current_username);
211                                 session->current_username = strndup((char*) ch, len);
212                                 session->in_uname = 0;
213                         }
214         
215                         if(session->in_resource) {
216                                 free(session->current_resource);
217                                 session->current_resource = strndup((char*) ch, len);
218                                 session->in_resource = 0;
219                         }
220                 }
221                 
222         }
223 }
224
225 void  sax_warning( void* blob, const char* msg, ... ) {
226
227         jserver_session* session = (jserver_session*) blob;
228         if(!session) return;
229
230         char buf[1024];
231         memset(buf, 0,  1024);
232
233         va_list args;
234         va_start(args, msg);
235         vsnprintf(buf, 1023, msg, args);
236         va_end(args);
237         warning_handler("XML Warning : %s", buf);
238         xml_error_occured = 1;
239 }
240
241
242 void dom_add_attrs(xmlNodePtr node, const xmlChar** atts) {
243         int i;
244         if (node != NULL && atts != NULL) {
245                 for(i = 0; (atts[i] != NULL); i++) {
246                         if(atts[i+1]) {
247                                 xmlNewProp(node, atts[i], atts[i+1]);
248                                 i++;
249                         }
250                 }
251         }
252 }
253
254 char* sax_xml_attr( const xmlChar** atts, char* attr_name ) {
255         int i;
256         if(attr_name == NULL) return NULL;
257
258         if (atts != NULL) {
259                 for(i = 0;(atts[i] != NULL);i++) {
260                         if(!strcmp(atts[i], attr_name)) 
261                                 if(atts[++i])
262                                         return (char*) atts[i];
263                 }
264         }
265         return NULL;
266 }
267
268
269
270 char* _xml_to_string( xmlDocPtr doc ) {
271         
272         xmlBufferPtr xmlbuf = xmlBufferCreate();
273         xmlNodeDump( xmlbuf, doc, xmlDocGetRootElement(doc), 0, 0);
274
275         char* xml = strdup( (char*) (xmlBufferContent(xmlbuf)));
276         xmlBufferFree(xmlbuf);
277
278         int l = strlen(xml)-1;
279         if( xml[l] == 10 || xml[l] == 13 )
280                 xml[l] = '\0';
281
282         return xml;
283
284 }
285