Mercurial > hg
annotate mcabber/libjabber/jconn.c @ 982:9cd7d00812f9
Add UK (Ukrainian) help files
author | Myhailo Danylenko <isbear@ukrpost.net> |
---|---|
date | Sun, 22 Oct 2006 23:27:05 +0200 |
parents | 40175f3dcef7 |
children | b33ca4e1c37d |
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)) { | |
938
40175f3dcef7
SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents:
686
diff
changeset
|
154 if (cw_get_ssl_error()) |
40175f3dcef7
SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents:
686
diff
changeset
|
155 scr_LogPrint(LPRINT_LOGNORM, "jab_start: SSL negotiation failed: %s", cw_get_ssl_error()); |
25 | 156 STATE_EVT(JCONN_STATE_OFF); |
157 return; | |
158 } | |
159 } | |
160 if (j->cw_state & CW_CONNECT_WANT_SOMETHING){ /* check if it finished negociation */ | |
161 j->state = JCONN_STATE_CONNECTING; | |
162 STATE_EVT(JCONN_STATE_CONNECTING); | |
163 return; | |
164 } | |
165 change_socket_to_blocking(j->fd); | |
414
ec86d759ed54
Trailing whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents:
403
diff
changeset
|
166 |
25 | 167 j->state = JCONN_STATE_CONNECTED; |
168 STATE_EVT(JCONN_STATE_CONNECTED) | |
169 | |
170 /* start stream */ | |
171 x = jutil_header(NS_CLIENT, j->user->server); | |
172 t = xmlnode2str(x); | |
173 /* this is ugly, we can create the string here instead of jutil_header */ | |
174 /* what do you think about it? -madcat */ | |
175 t2 = strstr(t,"/>"); | |
176 *t2++ = '>'; | |
177 *t2 = '\0'; | |
178 jab_send_raw(j,"<?xml version='1.0'?>"); | |
179 jab_send_raw(j,t); | |
180 xmlnode_free(x); | |
181 | |
182 j->state = JCONN_STATE_ON; | |
183 STATE_EVT(JCONN_STATE_ON) | |
184 | |
185 } | |
186 | |
187 /* | |
188 * jab_stop -- stop connection | |
189 * | |
190 * parameters | |
191 * j -- connection | |
192 */ | |
193 void jab_stop(jconn j) | |
194 { | |
195 if(!j || j->state == JCONN_STATE_OFF) return; | |
196 | |
197 j->state = JCONN_STATE_OFF; | |
198 cw_close(j->fd); | |
199 j->fd = -1; | |
200 XML_ParserFree(j->parser); | |
201 } | |
202 | |
203 /* | |
204 * jab_getfd -- get file descriptor of connection socket | |
205 * | |
206 * parameters | |
207 * j -- connection | |
208 * | |
209 * returns | |
210 * fd of the socket or -1 if socket was not connected | |
211 */ | |
212 int jab_getfd(jconn j) | |
213 { | |
214 if(j) | |
215 return j->fd; | |
216 else | |
217 return -1; | |
218 } | |
219 | |
220 /* | |
221 * jab_getjid -- get jid structure of user | |
222 * | |
223 * parameters | |
224 * j -- connection | |
225 */ | |
226 jid jab_getjid(jconn j) | |
227 { | |
228 if(j) | |
229 return(j->user); | |
230 else | |
231 return NULL; | |
232 } | |
233 | |
234 /* jab_getsid -- get stream id | |
235 * This is the id of server's <stream:stream> tag and used for | |
236 * digest authorization. | |
237 * | |
238 * parameters | |
239 * j -- connection | |
240 */ | |
241 char *jab_getsid(jconn j) | |
242 { | |
243 if(j) | |
244 return(j->sid); | |
245 else | |
246 return NULL; | |
247 } | |
248 | |
249 /* | |
250 * jab_getid -- get a unique id | |
251 * | |
252 * parameters | |
253 * j -- connection | |
254 */ | |
255 char *jab_getid(jconn j) | |
256 { | |
257 snprintf(j->idbuf, 8, "%d", j->id++); | |
258 return &j->idbuf[0]; | |
259 } | |
260 | |
261 /* | |
262 * jab_send -- send xml data | |
263 * | |
264 * parameters | |
265 * j -- connection | |
266 * x -- xmlnode structure | |
267 */ | |
268 void jab_send(jconn j, xmlnode x) | |
269 { | |
270 if (j && j->state != JCONN_STATE_OFF) | |
271 { | |
272 char *buf = xmlnode2str(x); | |
273 if (buf) { | |
274 cw_write(j->fd, buf, strlen(buf), j->ssl); | |
275 if (j->logger) | |
276 (j->logger)(j, 0, buf); | |
277 } | |
278 | |
279 #ifdef JDEBUG | |
280 printf ("out: %s\n", buf); | |
281 #endif | |
282 } | |
283 } | |
284 | |
285 /* | |
286 * jab_send_raw -- send a string | |
287 * | |
288 * parameters | |
289 * j -- connection | |
290 * str -- xml string | |
291 */ | |
292 void jab_send_raw(jconn j, const char *str) | |
293 { | |
294 if (j && j->state != JCONN_STATE_OFF) { | |
295 cw_write(j->fd, str, strlen(str), j->ssl); | |
296 | |
297 if (j->logger) | |
298 (j->logger)(j, 0, str); | |
299 } | |
300 | |
301 #ifdef JDEBUG | |
302 printf ("out: %s\n", str); | |
303 #endif | |
304 } | |
305 | |
306 /* | |
307 * jab_recv -- read and parse incoming data | |
308 * | |
309 * parameters | |
310 * j -- connection | |
311 */ | |
312 void jab_recv(jconn j) | |
313 { | |
314 static char buf[32768]; | |
315 int len; | |
316 | |
317 if(!j || j->state == JCONN_STATE_OFF) | |
318 return; | |
319 | |
320 len = cw_read(j->fd, buf, sizeof(buf)-1, j->ssl); | |
321 if(len>0) | |
322 { | |
323 buf[len] = '\0'; | |
324 | |
325 if (j->logger) | |
326 (j->logger)(j, 1, buf); | |
327 | |
328 #ifdef JDEBUG | |
329 printf (" in: %s\n", buf); | |
330 #endif | |
331 XML_Parse(j->parser, buf, len, 0); | |
332 } | |
333 else if(len<=0) | |
334 { | |
335 STATE_EVT(JCONN_STATE_OFF); | |
336 jab_stop(j); | |
337 } | |
338 } | |
339 | |
340 /* | |
341 * jab_poll -- check socket for incoming data | |
342 * | |
343 * parameters | |
344 * j -- connection | |
345 * timeout -- poll timeout | |
346 */ | |
347 void jab_poll(jconn j, int timeout) | |
348 { | |
349 fd_set fds; | |
350 struct timeval tv; | |
351 int r; | |
352 | |
353 if (!j || j->state == JCONN_STATE_OFF) | |
354 return; | |
355 | |
356 FD_ZERO(&fds); | |
357 FD_SET(j->fd, &fds); | |
358 | |
359 if(timeout <= 0) { | |
360 r = select(j->fd + 1, &fds, NULL, NULL, NULL); | |
361 | |
362 } else { | |
363 tv.tv_sec = 0; | |
364 tv.tv_usec = timeout; | |
365 r = select(j->fd + 1, &fds, NULL, NULL, &tv); | |
366 | |
367 } | |
368 | |
369 if(r > 0) { | |
370 jab_recv(j); | |
371 | |
372 } else if(r) { | |
172 | 373 /* Don't disconnect for interrupted system call */ |
374 if(errno == EINTR) return; | |
171 | 375 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
176
diff
changeset
|
376 scr_LogPrint(LPRINT_LOGNORM, "jab_poll: select returned errno=%d", |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
176
diff
changeset
|
377 errno); |
25 | 378 STATE_EVT(JCONN_STATE_OFF); |
379 jab_stop(j); | |
380 | |
381 } | |
382 } | |
383 | |
384 /* | |
385 * jab_auth -- authorize user | |
386 * | |
387 * parameters | |
388 * j -- connection | |
389 * | |
390 * returns | |
391 * id of the iq packet | |
392 */ | |
393 char *jab_auth(jconn j) | |
394 { | |
395 xmlnode x,y,z; | |
396 char *hash, *user, *id; | |
397 | |
398 if(!j) return(NULL); | |
399 | |
400 x = jutil_iqnew(JPACKET__SET, NS_AUTH); | |
401 id = jab_getid(j); | |
402 xmlnode_put_attrib(x, "id", id); | |
403 y = xmlnode_get_tag(x,"query"); | |
404 | |
405 user = j->user->user; | |
406 | |
407 if (user) | |
408 { | |
409 z = xmlnode_insert_tag(y, "username"); | |
410 xmlnode_insert_cdata(z, user, -1); | |
411 } | |
412 | |
413 z = xmlnode_insert_tag(y, "resource"); | |
414 xmlnode_insert_cdata(z, j->user->resource, -1); | |
415 | |
416 if (j->sid) | |
417 { | |
418 z = xmlnode_insert_tag(y, "digest"); | |
419 hash = pmalloc(x->p, strlen(j->sid)+strlen(j->pass)+1); | |
420 strcpy(hash, j->sid); | |
421 strcat(hash, j->pass); | |
422 hash = shahash(hash); | |
423 xmlnode_insert_cdata(z, hash, 40); | |
424 } | |
425 else | |
426 { | |
427 z = xmlnode_insert_tag(y, "password"); | |
428 xmlnode_insert_cdata(z, j->pass, -1); | |
429 } | |
430 | |
431 jab_send(j, x); | |
432 xmlnode_free(x); | |
433 return id; | |
434 } | |
435 | |
436 /* | |
686
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
437 * jab_auth_mcabber -- authorize user |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
438 * |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
439 * parameters |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
440 * j -- connection |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
441 * x -- xmlnode iq packet |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
442 * |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
443 * returns |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
444 * non-zero in case of failure |
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 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
|
447 { |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
448 xmlnode y,z; |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
449 char *hash, *user; |
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 if(!j) return -1; |
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 y = xmlnode_get_tag(x, "query"); |
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 user = j->user->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 if (user) |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
458 { |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
459 z = xmlnode_insert_tag(y, "username"); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
460 xmlnode_insert_cdata(z, user, -1); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
461 } |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
462 |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
463 z = xmlnode_insert_tag(y, "resource"); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
464 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
|
465 |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
466 if (j->sid) |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
467 { |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
468 z = xmlnode_insert_tag(y, "digest"); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
469 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
|
470 strcpy(hash, j->sid); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
471 strcat(hash, j->pass); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
472 hash = shahash(hash); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
473 xmlnode_insert_cdata(z, hash, 40); |
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 else |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
476 { |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
477 z = xmlnode_insert_tag(y, "password"); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
478 xmlnode_insert_cdata(z, j->pass, -1); |
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 return 0; |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
481 } |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
482 |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
483 /* |
25 | 484 * jab_reg -- register user |
485 * | |
486 * parameters | |
487 * j -- connection | |
488 * | |
489 * returns | |
490 * id of the iq packet | |
491 */ | |
492 char *jab_reg(jconn j) | |
493 { | |
494 xmlnode x,y,z; | |
176 | 495 char *user, *id; |
25 | 496 |
497 if (!j) return(NULL); | |
498 | |
499 x = jutil_iqnew(JPACKET__SET, NS_REGISTER); | |
500 id = jab_getid(j); | |
501 xmlnode_put_attrib(x, "id", id); | |
502 y = xmlnode_get_tag(x,"query"); | |
503 | |
504 user = j->user->user; | |
505 | |
506 if (user) | |
507 { | |
508 z = xmlnode_insert_tag(y, "username"); | |
509 xmlnode_insert_cdata(z, user, -1); | |
510 } | |
511 | |
512 z = xmlnode_insert_tag(y, "resource"); | |
513 xmlnode_insert_cdata(z, j->user->resource, -1); | |
514 | |
515 if (j->pass) | |
516 { | |
517 z = xmlnode_insert_tag(y, "password"); | |
518 xmlnode_insert_cdata(z, j->pass, -1); | |
519 } | |
520 | |
521 jab_send(j, x); | |
522 xmlnode_free(x); | |
523 j->state = JCONN_STATE_ON; | |
524 STATE_EVT(JCONN_STATE_ON) | |
525 return id; | |
526 } | |
527 | |
528 | |
529 /* local functions */ | |
530 | |
531 static void startElement(void *userdata, const char *name, const char **attribs) | |
532 { | |
533 xmlnode x; | |
534 jconn j = (jconn)userdata; | |
535 | |
536 if(j->current) | |
537 { | |
538 /* Append the node to the current one */ | |
539 x = xmlnode_insert_tag(j->current, name); | |
540 xmlnode_put_expat_attribs(x, attribs); | |
541 | |
542 j->current = x; | |
543 } | |
544 else | |
545 { | |
546 x = xmlnode_new_tag(name); | |
547 xmlnode_put_expat_attribs(x, attribs); | |
548 if(strcmp(name, "stream:stream") == 0) { | |
549 /* special case: name == stream:stream */ | |
550 /* id attrib of stream is stored for digest auth */ | |
551 j->sid = xmlnode_get_attrib(x, "id"); | |
552 /* STATE_EVT(JCONN_STATE_AUTH) */ | |
553 } else { | |
554 j->current = x; | |
555 } | |
556 } | |
557 } | |
558 | |
559 static void endElement(void *userdata, const char *name) | |
560 { | |
561 jconn j = (jconn)userdata; | |
562 xmlnode x; | |
563 jpacket p; | |
564 | |
565 if(j->current == NULL) { | |
566 /* we got </stream:stream> */ | |
567 STATE_EVT(JCONN_STATE_OFF) | |
568 return; | |
569 } | |
570 | |
571 x = xmlnode_get_parent(j->current); | |
572 | |
573 if(x == NULL) | |
574 { | |
575 /* it is time to fire the event */ | |
576 p = jpacket_new(j->current); | |
577 | |
578 if(j->on_packet) | |
579 (j->on_packet)(j, p); | |
580 else | |
581 xmlnode_free(j->current); | |
582 } | |
583 | |
584 j->current = x; | |
585 } | |
586 | |
587 static void charData(void *userdata, const char *s, int slen) | |
588 { | |
589 jconn j = (jconn)userdata; | |
590 | |
591 if (j->current) | |
592 xmlnode_insert_cdata(j->current, s, slen); | |
593 } |