]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/jserver/jserver-c_session.c
added external file to hold the commonly used jabber XML
[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[1024];
87                         memset(buf,0,1024);
88                         snprintf(buf, 1023, JSTRING_START_STREAM, session->current_domain, from_domain);
89
90                         debug_handler("Session Sending: %s", buf);
91
92                         session->state = JABBER_STATE_CONNECTING;
93                         if(session->on_login_init)
94                                 session->on_login_init(session->blob, buf);
95                 }
96                 return;
97         }
98
99         if(session->state & JABBER_STATE_CONNECTING) {
100                 /* during the connect shuffle, we have to store off the
101                         username and resource to determine the routing address */
102                 if(!strcmp(name,"iq"))
103                         session->in_iq = 1;
104                 if(!strcmp(name,"username"))
105                         session->in_uname = 1;
106                 if(!strcmp(name,"resource"))
107                         session->in_resource = 1;
108         }
109
110         if(session->state & JABBER_STATE_CONNECTED) {
111
112                 if(!strcmp(name, "message")) {
113                         /* opening of a new message, build a new doc */
114                         xmlNodePtr root = xmlNewNode(NULL, name);
115                         dom_add_attrs(root, atts);
116                         xmlNodePtr old_root = xmlDocSetRootElement(session->current_msg, root);
117
118
119                         free(session->current_to);
120
121                         char* from = sax_xml_attr(atts, "from");
122                         if(from == NULL) from = "";
123                         char* to = sax_xml_attr(atts, "to");
124                         if(to == NULL) to = "";
125
126                         session->current_to = strdup(to);
127
128                         /* free the old message tree */
129                         if(old_root) xmlFreeNode(old_root);
130
131                 } else {
132                         xmlNodePtr node = xmlNewNode(NULL, name);
133                         dom_add_attrs(node, atts);
134                         xmlAddChild(xmlDocGetRootElement(session->current_msg), node);
135                 }
136         }
137 }
138
139 void sax_end_element( void* blob, const xmlChar *name) {
140         jserver_session* session = (jserver_session*) blob;
141         if(!session) return;
142
143         if(session->state & JABBER_STATE_CONNECTED) {
144                 if(!strcmp(name, "message")) {
145                         if(session->on_msg_complete) {
146
147                                 debug_handler("Message is complete, finishing DOC");
148
149                                 /* we have to make sure the 'from' address is set.. */
150                                 xmlNodePtr msg = xmlDocGetRootElement(session->current_msg);
151                                 if(msg) xmlSetProp(msg, BAD_CAST "from", BAD_CAST session->current_from );
152                                 char* string = _xml_to_string(session->current_msg);
153
154                                 session->on_msg_complete(session->blob, string, 
155                                         session->current_from, session->current_to );
156                                 free(string);
157                         }
158                 }
159         }
160
161         if(session->state & JABBER_STATE_CONNECTING) {
162                 if(session->in_iq) {
163                         if(!strcmp(name, "iq")) {
164                                 session->in_iq = 0;
165
166                                 char buf[1024];
167                                 memset(buf, 0, 1024);
168                                 snprintf(buf, 1023, "%s@%s/%s", session->current_username,
169                                                 session->current_domain, session->current_resource );
170                                 if(session->on_from_discovered) 
171                                         session->on_from_discovered(session->blob, buf);
172
173                                 free(session->current_from);
174                                 session->current_from = strdup(buf);
175                                 debug_handler("Set from address to %s", session->current_from);
176                                 session->state = JABBER_STATE_CONNECTED;
177                                 if(session->on_login_ok) 
178                                         session->on_login_ok(session->blob);
179
180                         }
181                 }
182         }
183
184         if(!strcmp(name,"stream:stream")) {
185                 debug_handler("receive end of client session doc");
186                 client_sent_disconnect = 1;
187         }
188 }
189
190 void sax_character( void* blob, const xmlChar *ch, int len) {
191         jserver_session* session = (jserver_session*) blob;
192         if(!session) return;
193
194         if(session->state & JABBER_STATE_CONNECTED) {
195                 xmlNodePtr last = xmlGetLastChild(
196                         xmlDocGetRootElement(session->current_msg));
197         
198                 xmlNodePtr txt = xmlNewTextLen(ch, len);
199                 xmlAddChild(last, txt);
200                 return;
201         } 
202
203         if(session->state & JABBER_STATE_CONNECTING) {
204                 if(session->in_iq) {
205                         if(session->in_uname) {
206                                 free(session->current_username);
207                                 session->current_username = strndup((char*) ch, len);
208                                 session->in_uname = 0;
209                         }
210         
211                         if(session->in_resource) {
212                                 free(session->current_resource);
213                                 session->current_resource = strndup((char*) ch, len);
214                                 session->in_resource = 0;
215                         }
216                 }
217                 
218         }
219 }
220
221 void  sax_warning( void* blob, const char* msg, ... ) {
222
223         jserver_session* session = (jserver_session*) blob;
224         if(!session) return;
225
226         char buf[1024];
227         memset(buf, 0,  1024);
228
229         va_list args;
230         va_start(args, msg);
231         vsnprintf(buf, 1023, msg, args);
232         va_end(args);
233         warning_handler("XML Warning : %s", buf);
234         xml_error_occured = 1;
235 }
236
237
238 void dom_add_attrs(xmlNodePtr node, const xmlChar** atts) {
239         int i;
240         if (node != NULL && atts != NULL) {
241                 for(i = 0; (atts[i] != NULL); i++) {
242                         if(atts[i+1]) {
243                                 xmlNewProp(node, atts[i], atts[i+1]);
244                                 i++;
245                         }
246                 }
247         }
248 }
249
250 char* sax_xml_attr( const xmlChar** atts, char* attr_name ) {
251         int i;
252         if(attr_name == NULL) return NULL;
253
254         if (atts != NULL) {
255                 for(i = 0;(atts[i] != NULL);i++) {
256                         if(!strcmp(atts[i], attr_name)) 
257                                 if(atts[++i])
258                                         return (char*) atts[i];
259                 }
260         }
261         return NULL;
262 }
263
264
265
266 char* _xml_to_string( xmlDocPtr doc ) {
267         
268         xmlBufferPtr xmlbuf = xmlBufferCreate();
269         xmlNodeDump( xmlbuf, doc, xmlDocGetRootElement(doc), 0, 0);
270
271         char* xml = strdup( (char*) (xmlBufferContent(xmlbuf)));
272         xmlBufferFree(xmlbuf);
273
274         int l = strlen(xml)-1;
275         if( xml[l] == 10 || xml[l] == 13 )
276                 xml[l] = '\0';
277
278         return xml;
279
280 }
281