Mercurial > hg
annotate mcabber/libjabber/jconn.c @ 1573:ece4f26bf9ff
Add count parameter to roster up/down command
(Based on a patch from knyar in the issue tracker)
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Mon, 13 Apr 2009 00:26:56 +0200 |
parents | 2ac9bec53c37 |
children |
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 { | |
1125 | 195 if (!j) return; |
196 if (j->parser) { | |
197 XML_ParserFree(j->parser); | |
198 j->parser = NULL; | |
199 } | |
25 | 200 j->state = JCONN_STATE_OFF; |
1125 | 201 if (j->fd >= 0) { |
202 cw_close(j->fd); | |
203 j->fd = -1; | |
204 } | |
25 | 205 } |
206 | |
207 /* | |
208 * jab_getfd -- get file descriptor of connection socket | |
209 * | |
210 * parameters | |
211 * j -- connection | |
212 * | |
213 * returns | |
214 * fd of the socket or -1 if socket was not connected | |
215 */ | |
216 int jab_getfd(jconn j) | |
217 { | |
218 if(j) | |
219 return j->fd; | |
220 else | |
221 return -1; | |
222 } | |
223 | |
224 /* | |
225 * jab_getjid -- get jid structure of user | |
226 * | |
227 * parameters | |
228 * j -- connection | |
229 */ | |
230 jid jab_getjid(jconn j) | |
231 { | |
232 if(j) | |
233 return(j->user); | |
234 else | |
235 return NULL; | |
236 } | |
237 | |
238 /* jab_getsid -- get stream id | |
239 * This is the id of server's <stream:stream> tag and used for | |
240 * digest authorization. | |
241 * | |
242 * parameters | |
243 * j -- connection | |
244 */ | |
245 char *jab_getsid(jconn j) | |
246 { | |
247 if(j) | |
248 return(j->sid); | |
249 else | |
250 return NULL; | |
251 } | |
252 | |
253 /* | |
254 * jab_getid -- get a unique id | |
255 * | |
256 * parameters | |
257 * j -- connection | |
258 */ | |
259 char *jab_getid(jconn j) | |
260 { | |
261 snprintf(j->idbuf, 8, "%d", j->id++); | |
262 return &j->idbuf[0]; | |
263 } | |
264 | |
265 /* | |
266 * jab_send -- send xml data | |
267 * | |
268 * parameters | |
269 * j -- connection | |
270 * x -- xmlnode structure | |
271 */ | |
272 void jab_send(jconn j, xmlnode x) | |
273 { | |
274 if (j && j->state != JCONN_STATE_OFF) | |
275 { | |
276 char *buf = xmlnode2str(x); | |
277 if (buf) { | |
278 cw_write(j->fd, buf, strlen(buf), j->ssl); | |
279 if (j->logger) | |
280 (j->logger)(j, 0, buf); | |
281 } | |
282 | |
283 #ifdef JDEBUG | |
284 printf ("out: %s\n", buf); | |
285 #endif | |
286 } | |
287 } | |
288 | |
289 /* | |
290 * jab_send_raw -- send a string | |
291 * | |
292 * parameters | |
293 * j -- connection | |
294 * str -- xml string | |
295 */ | |
296 void jab_send_raw(jconn j, const char *str) | |
297 { | |
298 if (j && j->state != JCONN_STATE_OFF) { | |
299 cw_write(j->fd, str, strlen(str), j->ssl); | |
300 | |
301 if (j->logger) | |
302 (j->logger)(j, 0, str); | |
303 } | |
304 | |
305 #ifdef JDEBUG | |
306 printf ("out: %s\n", str); | |
307 #endif | |
308 } | |
309 | |
310 /* | |
311 * jab_recv -- read and parse incoming data | |
312 * | |
313 * parameters | |
314 * j -- connection | |
315 */ | |
316 void jab_recv(jconn j) | |
317 { | |
318 static char buf[32768]; | |
319 int len; | |
320 | |
321 if(!j || j->state == JCONN_STATE_OFF) | |
322 return; | |
323 | |
324 len = cw_read(j->fd, buf, sizeof(buf)-1, j->ssl); | |
325 if(len>0) | |
326 { | |
327 buf[len] = '\0'; | |
328 | |
329 if (j->logger) | |
330 (j->logger)(j, 1, buf); | |
331 | |
332 #ifdef JDEBUG | |
333 printf (" in: %s\n", buf); | |
334 #endif | |
335 XML_Parse(j->parser, buf, len, 0); | |
336 } | |
337 else if(len<=0) | |
338 { | |
339 STATE_EVT(JCONN_STATE_OFF); | |
340 jab_stop(j); | |
341 } | |
342 } | |
343 | |
344 /* | |
345 * jab_poll -- check socket for incoming data | |
346 * | |
347 * parameters | |
348 * j -- connection | |
349 * timeout -- poll timeout | |
350 */ | |
351 void jab_poll(jconn j, int timeout) | |
352 { | |
353 fd_set fds; | |
354 struct timeval tv; | |
355 int r; | |
356 | |
357 if (!j || j->state == JCONN_STATE_OFF) | |
358 return; | |
359 | |
985
b33ca4e1c37d
Fix potential libjabber issue
Mikael Berthe <mikael@lilotux.net>
parents:
938
diff
changeset
|
360 if (j->fd == -1) { |
b33ca4e1c37d
Fix potential libjabber issue
Mikael Berthe <mikael@lilotux.net>
parents:
938
diff
changeset
|
361 STATE_EVT(JCONN_STATE_OFF); |
b33ca4e1c37d
Fix potential libjabber issue
Mikael Berthe <mikael@lilotux.net>
parents:
938
diff
changeset
|
362 return; |
b33ca4e1c37d
Fix potential libjabber issue
Mikael Berthe <mikael@lilotux.net>
parents:
938
diff
changeset
|
363 } |
b33ca4e1c37d
Fix potential libjabber issue
Mikael Berthe <mikael@lilotux.net>
parents:
938
diff
changeset
|
364 |
25 | 365 FD_ZERO(&fds); |
366 FD_SET(j->fd, &fds); | |
367 | |
368 if(timeout <= 0) { | |
369 r = select(j->fd + 1, &fds, NULL, NULL, NULL); | |
370 | |
371 } else { | |
372 tv.tv_sec = 0; | |
373 tv.tv_usec = timeout; | |
374 r = select(j->fd + 1, &fds, NULL, NULL, &tv); | |
375 | |
376 } | |
377 | |
378 if(r > 0) { | |
379 jab_recv(j); | |
380 | |
381 } else if(r) { | |
172 | 382 /* Don't disconnect for interrupted system call */ |
383 if(errno == EINTR) return; | |
171 | 384 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
176
diff
changeset
|
385 scr_LogPrint(LPRINT_LOGNORM, "jab_poll: select returned errno=%d", |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
176
diff
changeset
|
386 errno); |
25 | 387 STATE_EVT(JCONN_STATE_OFF); |
388 jab_stop(j); | |
389 | |
390 } | |
391 } | |
392 | |
393 /* | |
394 * jab_auth -- authorize user | |
395 * | |
396 * parameters | |
397 * j -- connection | |
398 * | |
399 * returns | |
400 * id of the iq packet | |
401 */ | |
402 char *jab_auth(jconn j) | |
403 { | |
404 xmlnode x,y,z; | |
405 char *hash, *user, *id; | |
406 | |
407 if(!j) return(NULL); | |
408 | |
409 x = jutil_iqnew(JPACKET__SET, NS_AUTH); | |
410 id = jab_getid(j); | |
411 xmlnode_put_attrib(x, "id", id); | |
412 y = xmlnode_get_tag(x,"query"); | |
413 | |
414 user = j->user->user; | |
415 | |
416 if (user) | |
417 { | |
418 z = xmlnode_insert_tag(y, "username"); | |
419 xmlnode_insert_cdata(z, user, -1); | |
420 } | |
421 | |
422 z = xmlnode_insert_tag(y, "resource"); | |
423 xmlnode_insert_cdata(z, j->user->resource, -1); | |
424 | |
425 if (j->sid) | |
426 { | |
427 z = xmlnode_insert_tag(y, "digest"); | |
428 hash = pmalloc(x->p, strlen(j->sid)+strlen(j->pass)+1); | |
429 strcpy(hash, j->sid); | |
430 strcat(hash, j->pass); | |
431 hash = shahash(hash); | |
432 xmlnode_insert_cdata(z, hash, 40); | |
433 } | |
434 else | |
435 { | |
436 z = xmlnode_insert_tag(y, "password"); | |
437 xmlnode_insert_cdata(z, j->pass, -1); | |
438 } | |
439 | |
440 jab_send(j, x); | |
441 xmlnode_free(x); | |
442 return id; | |
443 } | |
444 | |
445 /* | |
686
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
446 * jab_auth_mcabber -- authorize user |
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 * parameters |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
449 * j -- connection |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
450 * x -- xmlnode iq packet |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
451 * |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
452 * returns |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
453 * non-zero in case of failure |
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 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
|
456 { |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
457 xmlnode y,z; |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
458 char *hash, *user; |
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 if(!j) return -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 y = xmlnode_get_tag(x, "query"); |
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 user = j->user->user; |
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 (user) |
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, "username"); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
469 xmlnode_insert_cdata(z, user, -1); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
470 } |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
471 |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
472 z = xmlnode_insert_tag(y, "resource"); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
473 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
|
474 |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
475 if (j->sid) |
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, "digest"); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
478 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
|
479 strcpy(hash, j->sid); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
480 strcat(hash, j->pass); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
481 hash = shahash(hash); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
482 xmlnode_insert_cdata(z, hash, 40); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
483 } |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
484 else |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
485 { |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
486 z = xmlnode_insert_tag(y, "password"); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
487 xmlnode_insert_cdata(z, j->pass, -1); |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
488 } |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
489 return 0; |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
490 } |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
491 |
98de2d166a11
Use the new IQ system for authentication
Mikael Berthe <mikael@lilotux.net>
parents:
414
diff
changeset
|
492 /* |
25 | 493 * jab_reg -- register user |
494 * | |
495 * parameters | |
496 * j -- connection | |
497 * | |
498 * returns | |
499 * id of the iq packet | |
500 */ | |
501 char *jab_reg(jconn j) | |
502 { | |
503 xmlnode x,y,z; | |
176 | 504 char *user, *id; |
25 | 505 |
506 if (!j) return(NULL); | |
507 | |
508 x = jutil_iqnew(JPACKET__SET, NS_REGISTER); | |
509 id = jab_getid(j); | |
510 xmlnode_put_attrib(x, "id", id); | |
511 y = xmlnode_get_tag(x,"query"); | |
512 | |
513 user = j->user->user; | |
514 | |
515 if (user) | |
516 { | |
517 z = xmlnode_insert_tag(y, "username"); | |
518 xmlnode_insert_cdata(z, user, -1); | |
519 } | |
520 | |
521 z = xmlnode_insert_tag(y, "resource"); | |
522 xmlnode_insert_cdata(z, j->user->resource, -1); | |
523 | |
524 if (j->pass) | |
525 { | |
526 z = xmlnode_insert_tag(y, "password"); | |
527 xmlnode_insert_cdata(z, j->pass, -1); | |
528 } | |
529 | |
530 jab_send(j, x); | |
531 xmlnode_free(x); | |
532 j->state = JCONN_STATE_ON; | |
533 STATE_EVT(JCONN_STATE_ON) | |
534 return id; | |
535 } | |
536 | |
537 | |
538 /* local functions */ | |
539 | |
540 static void startElement(void *userdata, const char *name, const char **attribs) | |
541 { | |
542 xmlnode x; | |
543 jconn j = (jconn)userdata; | |
544 | |
545 if(j->current) | |
546 { | |
547 /* Append the node to the current one */ | |
548 x = xmlnode_insert_tag(j->current, name); | |
549 xmlnode_put_expat_attribs(x, attribs); | |
550 | |
551 j->current = x; | |
552 } | |
553 else | |
554 { | |
555 x = xmlnode_new_tag(name); | |
556 xmlnode_put_expat_attribs(x, attribs); | |
557 if(strcmp(name, "stream:stream") == 0) { | |
558 /* special case: name == stream:stream */ | |
559 /* id attrib of stream is stored for digest auth */ | |
560 j->sid = xmlnode_get_attrib(x, "id"); | |
561 /* STATE_EVT(JCONN_STATE_AUTH) */ | |
562 } else { | |
563 j->current = x; | |
564 } | |
565 } | |
566 } | |
567 | |
568 static void endElement(void *userdata, const char *name) | |
569 { | |
570 jconn j = (jconn)userdata; | |
571 xmlnode x; | |
572 jpacket p; | |
573 | |
574 if(j->current == NULL) { | |
575 /* we got </stream:stream> */ | |
576 STATE_EVT(JCONN_STATE_OFF) | |
577 return; | |
578 } | |
579 | |
580 x = xmlnode_get_parent(j->current); | |
581 | |
582 if(x == NULL) | |
583 { | |
584 /* it is time to fire the event */ | |
585 p = jpacket_new(j->current); | |
586 | |
587 if(j->on_packet) | |
588 (j->on_packet)(j, p); | |
1124 | 589 xmlnode_free(j->current); |
25 | 590 } |
591 | |
592 j->current = x; | |
593 } | |
594 | |
595 static void charData(void *userdata, const char *s, int slen) | |
596 { | |
597 jconn j = (jconn)userdata; | |
598 | |
599 if (j->current) | |
600 xmlnode_insert_cdata(j->current, s, slen); | |
601 } |