Mercurial > hg
annotate mcabber/libjabber/jconn.c @ 929:f4bf564893b5
Fix ncurses #include
The ncurses.h header file wasn't found on some systems.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sat, 08 Jul 2006 09:33:54 +0200 |
parents | 98de2d166a11 |
children | 40175f3dcef7 |
rev | line source |
---|---|
25 | 1 /* |
2 * This program is free software; you can redistribute it and/or modify | |
3 * it under the terms of the GNU General Public License as published by | |
4 * the Free Software Foundation; either version 2 of the License, or | |
5 * (at your option) any later version. | |
6 * | |
7 * This program is distributed in the hope that it will be useful, | |
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 * GNU General Public License for more details. | |
11 * | |
12 * You should have received a copy of the GNU General Public License | |
13 * along with this program; if not, write to the Free Software | |
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
15 * | |
16 * Jabber | |
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/ | |
18 */ | |
19 | |
20 #include "jabber.h" | |
21 #include "connwrap.h" | |
22 | |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
176
diff
changeset
|
23 #include "../src/logprint.h" /* For logging */ |
176 | 24 |
25 | 25 /* local macros for launching event handlers */ |
26 #define STATE_EVT(arg) if(j->on_state) { (j->on_state)(j, (arg) ); } | |
27 | |
28 /* prototypes of the local functions */ | |
29 static void startElement(void *userdata, const char *name, const char **attribs); | |
30 static void endElement(void *userdata, const char *name); | |
31 static void charData(void *userdata, const char *s, int slen); | |
32 | |
33 /* | |
34 * jab_new -- initialize a new jabber connection | |
35 * | |
36 * parameters | |
37 * user -- jabber id of the user | |
38 * pass -- password of the user | |
39 * | |
40 * results | |
41 * a pointer to the connection structure | |
42 * or NULL if allocations failed | |
43 */ | |
403
17aa60c6dc63
Allow a different server name than the jid domain name
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
44 jconn jab_new(char *user, char *pass, char *server, int port, int ssl) |
25 | 45 { |
46 pool p; | |
47 jconn j; | |
48 | |
49 if(!user) return(NULL); | |
50 | |
51 p = pool_new(); | |
52 if(!p) return(NULL); | |
53 j = pmalloc_x(p, sizeof(jconn_struct), 0); | |
54 if(!j) return(NULL); | |
55 j->p = p; | |
56 | |
57 j->user = jid_new(p, user); | |
58 j->pass = pstrdup(p, pass); | |
59 j->port = port; | |
403
17aa60c6dc63
Allow a different server name than the jid domain name
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
60 j->server = server; |
25 | 61 |
62 j->state = JCONN_STATE_OFF; | |
63 j->cw_state = 0; | |
64 j->id = 1; | |
65 j->fd = -1; | |
66 j->ssl = ssl; | |
67 | |
68 return j; | |
69 } | |
70 | |
71 /* | |
72 * jab_delete -- free a jabber connection | |
73 * | |
74 * parameters | |
75 * j -- connection | |
76 * | |
77 */ | |
78 void jab_delete(jconn j) | |
79 { | |
80 if(!j) return; | |
81 | |
82 jab_stop(j); | |
83 pool_free(j->p); | |
84 } | |
85 | |
86 /* | |
87 * jab_state_handler -- set callback handler for state change | |
88 * | |
89 * parameters | |
90 * j -- connection | |
91 * h -- name of the handler function | |
92 */ | |
93 void jab_state_handler(jconn j, jconn_state_h h) | |
94 { | |
95 if(!j) return; | |
96 | |
97 j->on_state = h; | |
98 } | |
99 | |
100 /* | |
101 * jab_packet_handler -- set callback handler for incoming packets | |
102 * | |
103 * parameters | |
104 * j -- connection | |
105 * h -- name of the handler function | |
106 */ | |
107 void jab_packet_handler(jconn j, jconn_packet_h h) | |
108 { | |
109 if(!j) return; | |
110 | |
111 j->on_packet = h; | |
112 } | |
113 | |
114 void jab_logger(jconn j, jconn_logger h) | |
115 { | |
116 if(!j) return; | |
117 | |
118 j->logger = h; | |
119 } | |
120 | |
121 | |
122 /* | |
123 * jab_start -- start connection | |
124 * | |
125 * parameters | |
126 * j -- connection | |
127 * | |
128 */ | |
129 void jab_start(jconn j) | |
130 { | |
131 xmlnode x; | |
132 char *t,*t2; | |
133 | |
134 if(!j || (j->state != JCONN_STATE_OFF && j->state != JCONN_STATE_CONNECTING) ) return; | |
414
ec86d759ed54
Trailing whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents:
403
diff
changeset
|
135 |
25 | 136 if (!(j->cw_state & CW_CONNECT_WANT_SOMETHING)) { /* same as state != JCONN_STATE_CONNECTING */ |
137 j->parser = XML_ParserCreate(NULL); | |
138 XML_SetUserData(j->parser, (void *)j); | |
139 XML_SetElementHandler(j->parser, startElement, endElement); | |
140 XML_SetCharacterDataHandler(j->parser, charData); | |
141 | |
142 if (j->cw_state & CW_CONNECT_BLOCKING) | |
403
17aa60c6dc63
Allow a different server name than the jid domain name
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
143 j->fd = make_netsocket(j->port, j->server, NETSOCKET_CLIENT, j->ssl); |
25 | 144 else |
403
17aa60c6dc63
Allow a different server name than the jid domain name
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
145 j->fd = make_nb_netsocket(j->port, j->server, NETSOCKET_CLIENT, j->ssl, &j->cw_state); |
17aa60c6dc63
Allow a different server name than the jid domain name
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
146 |
25 | 147 if(j->fd < 0) { |
148 STATE_EVT(JCONN_STATE_OFF); | |
149 return; | |
150 } | |
151 } | |
152 else { /* subsequent calls to cw_nb_connect until it finishes negociation */ | |
153 if (cw_nb_connect(j->fd, 0, 0, j->ssl, &j->cw_state)) { | |
154 STATE_EVT(JCONN_STATE_OFF); | |
155 return; | |
156 } | |
157 } | |
158 if (j->cw_state & CW_CONNECT_WANT_SOMETHING){ /* check if it finished negociation */ | |
159 j->state = JCONN_STATE_CONNECTING; | |
160 STATE_EVT(JCONN_STATE_CONNECTING); | |
161 return; | |
162 } | |
163 change_socket_to_blocking(j->fd); | |
414
ec86d759ed54
Trailing whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents:
403
diff
changeset
|
164 |
25 | 165 j->state = JCONN_STATE_CONNECTED; |
166 STATE_EVT(JCONN_STATE_CONNECTED) | |
167 | |
168 /* start stream */ | |
169 x = jutil_header(NS_CLIENT, j->user->server); | |
170 t = xmlnode2str(x); | |
171 /* this is ugly, we can create the string here instead of jutil_header */ | |
172 /* what do you think about it? -madcat */ | |
173 t2 = strstr(t,"/>"); | |
174 *t2++ = '>'; | |
175 *t2 = '\0'; | |
176 jab_send_raw(j,"<?xml version='1.0'?>"); | |
177 jab_send_raw(j,t); | |
178 xmlnode_free(x); | |
179 | |
180 j->state = JCONN_STATE_ON; | |
181 STATE_EVT(JCONN_STATE_ON) | |
182 | |
183 } | |
184 | |
185 /* | |
186 * jab_stop -- stop connection | |
187 * | |
188 * parameters | |
189 * j -- connection | |
190 */ | |
191 void jab_stop(jconn j) | |
192 { | |
193 if(!j || j->state == JCONN_STATE_OFF) return; | |
194 | |
195 j->state = JCONN_STATE_OFF; | |
196 cw_close(j->fd); | |
197 j->fd = -1; | |
198 XML_ParserFree(j->parser); | |
199 } | |
200 | |
201 /* | |
202 * jab_getfd -- get file descriptor of connection socket | |
203 * | |
204 * parameters | |
205 * j -- connection | |
206 * | |
207 * returns | |
208 * fd of the socket or -1 if socket was not connected | |
209 */ | |
210 int jab_getfd(jconn j) | |
211 { | |
212 if(j) | |
213 return j->fd; | |
214 else | |
215 return -1; | |
216 } | |
217 | |
218 /* | |
219 * jab_getjid -- get jid structure of user | |
220 * | |
221 * parameters | |
222 * j -- connection | |
223 */ | |
224 jid jab_getjid(jconn j) | |
225 { | |
226 if(j) | |
227 return(j->user); | |
228 else | |
229 return NULL; | |
230 } | |
231 | |
232 /* jab_getsid -- get stream id | |
233 * This is the id of server's <stream:stream> tag and used for | |
234 * digest authorization. | |
235 * | |
236 * parameters | |
237 * j -- connection | |
238 */ | |
239 char *jab_getsid(jconn j) | |
240 { | |
241 if(j) | |
242 return(j->sid); | |
243 else | |
244 return NULL; | |
245 } | |
246 | |
247 /* | |
248 * jab_getid -- get a unique id | |
249 * | |
250 * parameters | |
251 * j -- connection | |
252 */ | |
253 char *jab_getid(jconn j) | |
254 { | |
255 snprintf(j->idbuf, 8, "%d", j->id++); | |
256 return &j->idbuf[0]; | |
257 } | |
258 | |
259 /* | |
260 * jab_send -- send xml data | |
261 * | |
262 * parameters | |
263 * j -- connection | |
264 * x -- xmlnode structure | |
265 */ | |
266 void jab_send(jconn j, xmlnode x) | |
267 { | |
268 if (j && j->state != JCONN_STATE_OFF) | |
269 { | |
270 char *buf = xmlnode2str(x); | |
271 if (buf) { | |
272 cw_write(j->fd, buf, strlen(buf), j->ssl); | |
273 if (j->logger) | |
274 (j->logger)(j, 0, buf); | |
275 } | |
276 | |
277 #ifdef JDEBUG | |
278 printf ("out: %s\n", buf); | |
279 #endif | |
280 } | |
281 } | |
282 | |
283 /* | |
284 * jab_send_raw -- send a string | |
285 * | |
286 * parameters | |
287 * j -- connection | |
288 * str -- xml string | |
289 */ | |
290 void jab_send_raw(jconn j, const char *str) | |
291 { | |
292 if (j && j->state != JCONN_STATE_OFF) { | |
293 cw_write(j->fd, str, strlen(str), j->ssl); | |
294 | |
295 if (j->logger) | |
296 (j->logger)(j, 0, str); | |
297 } | |
298 | |
299 #ifdef JDEBUG | |
300 printf ("out: %s\n", str); | |
301 #endif | |
302 } | |
303 | |
304 /* | |
305 * jab_recv -- read and parse incoming data | |
306 * | |
307 * parameters | |
308 * j -- connection | |
309 */ | |
310 void jab_recv(jconn j) | |
311 { | |
312 static char buf[32768]; | |
313 int len; | |
314 | |
315 if(!j || j->state == JCONN_STATE_OFF) | |
316 return; | |
317 | |
318 len = cw_read(j->fd, buf, sizeof(buf)-1, j->ssl); | |
319 if(len>0) | |
320 { | |
321 buf[len] = '\0'; | |
322 | |
323 if (j->logger) | |
324 (j->logger)(j, 1, buf); | |
325 | |
326 #ifdef JDEBUG | |
327 printf (" in: %s\n", buf); | |
328 #endif | |
329 XML_Parse(j->parser, buf, len, 0); | |
330 } | |
331 else if(len<=0) | |
332 { | |
333 STATE_EVT(JCONN_STATE_OFF); | |
334 jab_stop(j); | |
335 } | |
336 } | |
337 | |
338 /* | |
339 * jab_poll -- check socket for incoming data | |
340 * | |
341 * parameters | |
342 * j -- connection | |
343 * timeout -- poll timeout | |
344 */ | |
345 void jab_poll(jconn j, int timeout) | |
346 { | |
347 fd_set fds; | |
348 struct timeval tv; | |
349 int r; | |
350 | |
351 if (!j || j->state == JCONN_STATE_OFF) | |
352 return; | |
353 | |
354 FD_ZERO(&fds); | |
355 FD_SET(j->fd, &fds); | |
356 | |
357 if(timeout <= 0) { | |
358 r = select(j->fd + 1, &fds, NULL, NULL, NULL); | |
359 | |
360 } else { | |
361 tv.tv_sec = 0; | |
362 tv.tv_usec = timeout; | |
363 r = select(j->fd + 1, &fds, NULL, NULL, &tv); | |
364 | |
365 } | |
366 | |
367 if(r > 0) { | |
368 jab_recv(j); | |
369 | |
370 } else if(r) { | |
172 | 371 /* Don't disconnect for interrupted system call */ |
372 if(errno == EINTR) return; | |
171 | 373 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
176
diff
changeset
|
374 scr_LogPrint(LPRINT_LOGNORM, "jab_poll: select returned errno=%d", |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
176
diff
changeset
|
375 errno); |
25 | 376 STATE_EVT(JCONN_STATE_OFF); |
377 jab_stop(j); | |
378 | |
379 } | |
380 } | |
381 | |
382 /* | |
383 * jab_auth -- authorize user | |
384 * | |
385 * parameters | |
386 * j -- connection | |
387 * | |
388 * returns | |
389 * id of the iq packet | |
390 */ | |
391 char *jab_auth(jconn j) | |
392 { | |
393 xmlnode x,y,z; | |
394 char *hash, *user, *id; | |
395 | |
396 if(!j) return(NULL); | |
397 | |
398 x = jutil_iqnew(JPACKET__SET, NS_AUTH); | |
399 id = jab_getid(j); | |
400 xmlnode_put_attrib(x, "id", id); | |
401 y = xmlnode_get_tag(x,"query"); | |
402 | |
403 user = j->user->user; | |
404 | |
405 if (user) | |
406 { | |
407 z = xmlnode_insert_tag(y, "username"); | |
408 xmlnode_insert_cdata(z, user, -1); | |
409 } | |
410 | |
411 z = xmlnode_insert_tag(y, "resource"); | |
412 xmlnode_insert_cdata(z, j->user->resource, -1); | |
413 | |
414 if (j->sid) | |
415 { | |
416 z = xmlnode_insert_tag(y, "digest"); | |
417 hash = pmalloc(x->p, strlen(j->sid)+strlen(j->pass)+1); | |
418 strcpy(hash, j->sid); | |
419 strcat(hash, j->pass); | |
420 hash = shahash(hash); | |
421 xmlnode_insert_cdata(z, hash, 40); | |
422 } | |
423 else | |
424 { | |
425 z = xmlnode_insert_tag(y, "password"); | |
426 xmlnode_insert_cdata(z, j->pass, -1); | |
427 } | |
428 | |
429 jab_send(j, x); | |
430 xmlnode_free(x); | |
431 return id; | |
432 } | |
433 | |
434 /* | |
686
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
435 * jab_auth_mcabber -- authorize user |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
436 * |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
437 * parameters |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
438 * j -- connection |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
439 * x -- xmlnode iq packet |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
440 * |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
441 * returns |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
442 * non-zero in case of failure |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
443 */ |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
444 int jab_auth_mcabber(jconn j, xmlnode x) |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
445 { |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
446 xmlnode y,z; |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
447 char *hash, *user; |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
448 |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
449 if(!j) return -1; |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
450 |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
451 y = xmlnode_get_tag(x, "query"); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
452 |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
453 user = j->user->user; |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
454 |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
455 if (user) |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
456 { |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
457 z = xmlnode_insert_tag(y, "username"); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
458 xmlnode_insert_cdata(z, user, -1); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
459 } |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
460 |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
461 z = xmlnode_insert_tag(y, "resource"); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
462 xmlnode_insert_cdata(z, j->user->resource, -1); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
463 |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
464 if (j->sid) |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
465 { |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
466 z = xmlnode_insert_tag(y, "digest"); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
467 hash = pmalloc(x->p, strlen(j->sid)+strlen(j->pass)+1); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
468 strcpy(hash, j->sid); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
469 strcat(hash, j->pass); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
470 hash = shahash(hash); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
471 xmlnode_insert_cdata(z, hash, 40); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
472 } |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
473 else |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
474 { |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
475 z = xmlnode_insert_tag(y, "password"); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
476 xmlnode_insert_cdata(z, j->pass, -1); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
477 } |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
478 return 0; |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
479 } |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
480 |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
481 /* |
25 | 482 * jab_reg -- register user |
483 * | |
484 * parameters | |
485 * j -- connection | |
486 * | |
487 * returns | |
488 * id of the iq packet | |
489 */ | |
490 char *jab_reg(jconn j) | |
491 { | |
492 xmlnode x,y,z; | |
176 | 493 char *user, *id; |
25 | 494 |
495 if (!j) return(NULL); | |
496 | |
497 x = jutil_iqnew(JPACKET__SET, NS_REGISTER); | |
498 id = jab_getid(j); | |
499 xmlnode_put_attrib(x, "id", id); | |
500 y = xmlnode_get_tag(x,"query"); | |
501 | |
502 user = j->user->user; | |
503 | |
504 if (user) | |
505 { | |
506 z = xmlnode_insert_tag(y, "username"); | |
507 xmlnode_insert_cdata(z, user, -1); | |
508 } | |
509 | |
510 z = xmlnode_insert_tag(y, "resource"); | |
511 xmlnode_insert_cdata(z, j->user->resource, -1); | |
512 | |
513 if (j->pass) | |
514 { | |
515 z = xmlnode_insert_tag(y, "password"); | |
516 xmlnode_insert_cdata(z, j->pass, -1); | |
517 } | |
518 | |
519 jab_send(j, x); | |
520 xmlnode_free(x); | |
521 j->state = JCONN_STATE_ON; | |
522 STATE_EVT(JCONN_STATE_ON) | |
523 return id; | |
524 } | |
525 | |
526 | |
527 /* local functions */ | |
528 | |
529 static void startElement(void *userdata, const char *name, const char **attribs) | |
530 { | |
531 xmlnode x; | |
532 jconn j = (jconn)userdata; | |
533 | |
534 if(j->current) | |
535 { | |
536 /* Append the node to the current one */ | |
537 x = xmlnode_insert_tag(j->current, name); | |
538 xmlnode_put_expat_attribs(x, attribs); | |
539 | |
540 j->current = x; | |
541 } | |
542 else | |
543 { | |
544 x = xmlnode_new_tag(name); | |
545 xmlnode_put_expat_attribs(x, attribs); | |
546 if(strcmp(name, "stream:stream") == 0) { | |
547 /* special case: name == stream:stream */ | |
548 /* id attrib of stream is stored for digest auth */ | |
549 j->sid = xmlnode_get_attrib(x, "id"); | |
550 /* STATE_EVT(JCONN_STATE_AUTH) */ | |
551 } else { | |
552 j->current = x; | |
553 } | |
554 } | |
555 } | |
556 | |
557 static void endElement(void *userdata, const char *name) | |
558 { | |
559 jconn j = (jconn)userdata; | |
560 xmlnode x; | |
561 jpacket p; | |
562 | |
563 if(j->current == NULL) { | |
564 /* we got </stream:stream> */ | |
565 STATE_EVT(JCONN_STATE_OFF) | |
566 return; | |
567 } | |
568 | |
569 x = xmlnode_get_parent(j->current); | |
570 | |
571 if(x == NULL) | |
572 { | |
573 /* it is time to fire the event */ | |
574 p = jpacket_new(j->current); | |
575 | |
576 if(j->on_packet) | |
577 (j->on_packet)(j, p); | |
578 else | |
579 xmlnode_free(j->current); | |
580 } | |
581 | |
582 j->current = x; | |
583 } | |
584 | |
585 static void charData(void *userdata, const char *s, int slen) | |
586 { | |
587 jconn j = (jconn)userdata; | |
588 | |
589 if (j->current) | |
590 xmlnode_insert_cdata(j->current, s, slen); | |
591 } |