Mercurial > pidgin.yaz
annotate src/protocols/jabber/jconn.c @ 6328:cbe2f320c214
[gaim-migrate @ 6827]
Updated the Rename Group dialog to be more like the old one.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Tue, 29 Jul 2003 05:59:58 +0000 |
parents | 988485669631 |
children |
rev | line source |
---|---|
2086 | 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 | |
3717
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3159
diff
changeset
|
22 #ifdef _WIN32 |
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3159
diff
changeset
|
23 #include "win32dep.h" |
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3159
diff
changeset
|
24 #endif |
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3159
diff
changeset
|
25 |
2086 | 26 /* local macros for launching event handlers */ |
27 #define STATE_EVT(arg) if(j->on_state) { (j->on_state)(j, (arg) ); } | |
28 | |
29 /* prototypes of the local functions */ | |
30 static void startElement(void *userdata, const char *name, const char **attribs); | |
31 static void endElement(void *userdata, const char *name); | |
32 static void charData(void *userdata, const char *s, int slen); | |
33 | |
34 /* | |
35 * jab_new -- initialize a new jabber connection | |
36 * | |
37 * parameters | |
38 * user -- jabber id of the user | |
39 * pass -- password of the user | |
40 * | |
41 * results | |
42 * a pointer to the connection structure | |
43 * or NULL if allocations failed | |
44 */ | |
45 jconn jab_new(char *user, char *pass) | |
46 { | |
47 pool p; | |
48 jconn j; | |
49 | |
50 if(!user) return(NULL); | |
51 | |
52 p = pool_new(); | |
53 if(!p) return(NULL); | |
54 j = pmalloc_x(p, sizeof(jconn_struct), 0); | |
55 if(!j) return(NULL); | |
56 j->p = p; | |
57 | |
58 j->user = jid_new(p, user); | |
59 j->pass = pstrdup(p, pass); | |
60 | |
61 j->state = JCONN_STATE_OFF; | |
62 j->id = 1; | |
63 j->fd = -1; | |
64 | |
65 return j; | |
66 } | |
67 | |
68 /* | |
69 * jab_delete -- free a jabber connection | |
70 * | |
71 * parameters | |
72 * j -- connection | |
73 * | |
74 */ | |
75 void jab_delete(jconn j) | |
76 { | |
77 if(!j) return; | |
78 | |
79 jab_stop(j); | |
80 pool_free(j->p); | |
81 } | |
82 | |
83 /* | |
84 * jab_state_handler -- set callback handler for state change | |
85 * | |
86 * parameters | |
87 * j -- connection | |
88 * h -- name of the handler function | |
89 */ | |
90 void jab_state_handler(jconn j, jconn_state_h h) | |
91 { | |
92 if(!j) return; | |
93 | |
94 j->on_state = h; | |
95 } | |
96 | |
97 /* | |
98 * jab_packet_handler -- set callback handler for incoming packets | |
99 * | |
100 * parameters | |
101 * j -- connection | |
102 * h -- name of the handler function | |
103 */ | |
104 void jab_packet_handler(jconn j, jconn_packet_h h) | |
105 { | |
106 if(!j) return; | |
107 | |
108 j->on_packet = h; | |
109 } | |
110 | |
111 | |
112 /* | |
113 * jab_start -- start connection | |
114 * | |
115 * parameters | |
116 * j -- connection | |
117 * | |
118 */ | |
119 void jab_start(jconn j) | |
120 { | |
121 xmlnode x; | |
122 char *t,*t2; | |
123 | |
124 if(!j || j->state != JCONN_STATE_OFF) return; | |
125 | |
126 j->parser = XML_ParserCreate(NULL); | |
127 XML_SetUserData(j->parser, (void *)j); | |
128 XML_SetElementHandler(j->parser, startElement, endElement); | |
129 XML_SetCharacterDataHandler(j->parser, charData); | |
130 | |
131 j->fd = make_netsocket(5222, j->user->server, NETSOCKET_CLIENT); | |
132 if(j->fd < 0) { | |
133 STATE_EVT(JCONN_STATE_OFF) | |
134 return; | |
135 } | |
136 j->state = JCONN_STATE_CONNECTED; | |
137 STATE_EVT(JCONN_STATE_CONNECTED) | |
138 | |
139 /* start stream */ | |
140 x = jutil_header(NS_CLIENT, j->user->server); | |
141 t = xmlnode2str(x); | |
142 /* this is ugly, we can create the string here instead of jutil_header */ | |
143 /* what do you think about it? -madcat */ | |
144 t2 = strstr(t,"/>"); | |
145 *t2++ = '>'; | |
146 *t2 = '\0'; | |
147 jab_send_raw(j,"<?xml version='1.0'?>"); | |
148 jab_send_raw(j,t); | |
149 xmlnode_free(x); | |
150 | |
151 j->state = JCONN_STATE_ON; | |
152 STATE_EVT(JCONN_STATE_ON) | |
153 | |
154 } | |
155 | |
156 /* | |
157 * jab_stop -- stop connection | |
158 * | |
159 * parameters | |
160 * j -- connection | |
161 */ | |
162 void jab_stop(jconn j) | |
163 { | |
164 if(!j || j->state == JCONN_STATE_OFF) return; | |
165 | |
166 j->state = JCONN_STATE_OFF; | |
167 close(j->fd); | |
168 j->fd = -1; | |
169 XML_ParserFree(j->parser); | |
170 } | |
171 | |
172 /* | |
173 * jab_getfd -- get file descriptor of connection socket | |
174 * | |
175 * parameters | |
176 * j -- connection | |
177 * | |
178 * returns | |
179 * fd of the socket or -1 if socket was not connected | |
180 */ | |
181 int jab_getfd(jconn j) | |
182 { | |
183 if(j) | |
184 return j->fd; | |
185 else | |
186 return -1; | |
187 } | |
188 | |
189 /* | |
190 * jab_getjid -- get jid structure of user | |
191 * | |
192 * parameters | |
193 * j -- connection | |
194 */ | |
195 jid jab_getjid(jconn j) | |
196 { | |
197 if(j) | |
198 return(j->user); | |
199 else | |
200 return NULL; | |
201 } | |
202 | |
203 /* jab_getsid -- get stream id | |
204 * This is the id of server's <stream:stream> tag and used for | |
205 * digest authorization. | |
206 * | |
207 * parameters | |
208 * j -- connection | |
209 */ | |
210 char *jab_getsid(jconn j) | |
211 { | |
212 if(j) | |
213 return(j->sid); | |
214 else | |
215 return NULL; | |
216 } | |
217 | |
218 /* | |
219 * jab_getid -- get a unique id | |
220 * | |
221 * parameters | |
222 * j -- connection | |
223 */ | |
224 char *jab_getid(jconn j) | |
225 { | |
226 snprintf(j->idbuf, 8, "%d", j->id++); | |
227 return &j->idbuf[0]; | |
228 } | |
229 | |
230 /* | |
231 * jab_send -- send xml data | |
232 * | |
233 * parameters | |
234 * j -- connection | |
235 * x -- xmlnode structure | |
236 */ | |
237 void jab_send(jconn j, xmlnode x) | |
238 { | |
239 if (j && j->state != JCONN_STATE_OFF) | |
240 { | |
241 char *buf = xmlnode2str(x); | |
242 if (buf) write(j->fd, buf, strlen(buf)); | |
243 #ifdef JDEBUG | |
244 printf ("out: %s\n", buf); | |
245 #endif | |
246 } | |
247 } | |
248 | |
249 /* | |
250 * jab_send_raw -- send a string | |
251 * | |
252 * parameters | |
253 * j -- connection | |
254 * str -- xml string | |
255 */ | |
256 void jab_send_raw(jconn j, const char *str) | |
257 { | |
258 if (j && j->state != JCONN_STATE_OFF) | |
259 write(j->fd, str, strlen(str)); | |
260 #ifdef JDEBUG | |
261 printf ("out: %s\n", str); | |
262 #endif | |
263 } | |
264 | |
265 /* | |
266 * jab_recv -- read and parse incoming data | |
267 * | |
268 * parameters | |
269 * j -- connection | |
270 */ | |
271 void jab_recv(jconn j) | |
272 { | |
273 static char buf[4096]; | |
274 int len; | |
275 | |
276 if(!j || j->state == JCONN_STATE_OFF) | |
277 return; | |
278 | |
279 len = read(j->fd, buf, sizeof(buf)-1); | |
280 if(len>0) | |
281 { | |
282 buf[len] = '\0'; | |
283 #ifdef JDEBUG | |
284 printf (" in: %s\n", buf); | |
285 #endif | |
286 XML_Parse(j->parser, buf, len, 0); | |
287 } | |
288 else if(len<0) | |
289 { | |
290 STATE_EVT(JCONN_STATE_OFF); | |
291 jab_stop(j); | |
292 } | |
293 } | |
294 | |
295 /* | |
296 * jab_poll -- check socket for incoming data | |
297 * | |
298 * parameters | |
299 * j -- connection | |
300 * timeout -- poll timeout | |
301 */ | |
302 void jab_poll(jconn j, int timeout) | |
303 { | |
304 fd_set fds; | |
305 struct timeval tv; | |
306 | |
307 if (!j || j->state == JCONN_STATE_OFF) | |
308 return; | |
309 | |
310 FD_ZERO(&fds); | |
311 FD_SET(j->fd, &fds); | |
312 | |
313 if (timeout < 0) | |
314 { | |
315 if (select(j->fd + 1, &fds, NULL, NULL, NULL) > 0) | |
316 jab_recv(j); | |
317 } | |
318 else | |
319 { | |
320 tv.tv_sec = 0; | |
321 tv.tv_usec = timeout; | |
322 if (select(j->fd + 1, &fds, NULL, NULL, &tv) > 0) | |
323 jab_recv(j); | |
324 } | |
325 } | |
326 | |
327 /* | |
328 * jab_auth -- authorize user | |
329 * | |
330 * parameters | |
331 * j -- connection | |
332 * | |
333 * returns | |
334 * id of the iq packet | |
335 */ | |
336 char *jab_auth(jconn j) | |
337 { | |
338 xmlnode x,y,z; | |
339 char *hash, *user, *id; | |
340 | |
341 if(!j) return(NULL); | |
342 | |
343 x = jutil_iqnew(JPACKET__SET, NS_AUTH); | |
344 id = jab_getid(j); | |
345 xmlnode_put_attrib(x, "id", id); | |
346 y = xmlnode_get_tag(x,"query"); | |
347 | |
348 user = j->user->user; | |
349 | |
350 if (user) | |
351 { | |
352 z = xmlnode_insert_tag(y, "username"); | |
353 xmlnode_insert_cdata(z, user, -1); | |
354 } | |
355 | |
356 z = xmlnode_insert_tag(y, "resource"); | |
357 xmlnode_insert_cdata(z, j->user->resource, -1); | |
358 | |
359 if (j->sid) | |
360 { | |
361 z = xmlnode_insert_tag(y, "digest"); | |
362 hash = pmalloc(x->p, strlen(j->sid)+strlen(j->pass)+1); | |
363 strcpy(hash, j->sid); | |
364 strcat(hash, j->pass); | |
365 hash = shahash(hash); | |
366 xmlnode_insert_cdata(z, hash, 40); | |
367 } | |
368 else | |
369 { | |
370 z = xmlnode_insert_tag(y, "password"); | |
371 xmlnode_insert_cdata(z, j->pass, -1); | |
372 } | |
373 | |
374 jab_send(j, x); | |
375 xmlnode_free(x); | |
376 return id; | |
377 } | |
378 | |
379 /* | |
380 * jab_reg -- register user | |
381 * | |
382 * parameters | |
383 * j -- connection | |
384 * | |
385 * returns | |
386 * id of the iq packet | |
387 */ | |
388 char *jab_reg(jconn j) | |
389 { | |
390 xmlnode x,y,z; | |
3159 | 391 char *user, *id; |
2086 | 392 |
393 if (!j) return(NULL); | |
394 | |
395 x = jutil_iqnew(JPACKET__SET, NS_REGISTER); | |
396 id = jab_getid(j); | |
397 xmlnode_put_attrib(x, "id", id); | |
398 y = xmlnode_get_tag(x,"query"); | |
399 | |
400 user = j->user->user; | |
401 | |
402 if (user) | |
403 { | |
404 z = xmlnode_insert_tag(y, "username"); | |
405 xmlnode_insert_cdata(z, user, -1); | |
406 } | |
407 | |
408 z = xmlnode_insert_tag(y, "resource"); | |
409 xmlnode_insert_cdata(z, j->user->resource, -1); | |
410 | |
411 if (j->pass) | |
412 { | |
413 z = xmlnode_insert_tag(y, "password"); | |
414 xmlnode_insert_cdata(z, j->pass, -1); | |
415 } | |
416 | |
417 jab_send(j, x); | |
418 xmlnode_free(x); | |
419 j->state = JCONN_STATE_ON; | |
420 STATE_EVT(JCONN_STATE_ON) | |
421 return id; | |
422 } | |
423 | |
424 | |
425 /* local functions */ | |
426 | |
427 static void startElement(void *userdata, const char *name, const char **attribs) | |
428 { | |
429 xmlnode x; | |
430 jconn j = (jconn)userdata; | |
431 | |
432 if(j->current) | |
433 { | |
434 /* Append the node to the current one */ | |
435 x = xmlnode_insert_tag(j->current, name); | |
436 xmlnode_put_expat_attribs(x, attribs); | |
437 | |
438 j->current = x; | |
439 } | |
440 else | |
441 { | |
442 x = xmlnode_new_tag(name); | |
443 xmlnode_put_expat_attribs(x, attribs); | |
444 if(strcmp(name, "stream:stream") == 0) { | |
445 /* special case: name == stream:stream */ | |
446 /* id attrib of stream is stored for digest auth */ | |
447 j->sid = xmlnode_get_attrib(x, "id"); | |
448 /* STATE_EVT(JCONN_STATE_AUTH) */ | |
449 } else { | |
450 j->current = x; | |
451 } | |
452 } | |
453 } | |
454 | |
455 static void endElement(void *userdata, const char *name) | |
456 { | |
457 jconn j = (jconn)userdata; | |
458 xmlnode x; | |
459 jpacket p; | |
460 | |
461 if(j->current == NULL) { | |
462 /* we got </stream:stream> */ | |
463 STATE_EVT(JCONN_STATE_OFF) | |
464 return; | |
465 } | |
466 | |
467 x = xmlnode_get_parent(j->current); | |
468 | |
469 if(x == NULL) | |
470 { | |
471 /* it is time to fire the event */ | |
472 p = jpacket_new(j->current); | |
473 | |
474 if(j->on_packet) | |
475 (j->on_packet)(j, p); | |
476 else | |
477 xmlnode_free(j->current); | |
478 } | |
479 | |
480 j->current = x; | |
481 } | |
482 | |
483 static void charData(void *userdata, const char *s, int slen) | |
484 { | |
485 jconn j = (jconn)userdata; | |
486 | |
487 if (j->current) | |
488 xmlnode_insert_cdata(j->current, s, slen); | |
489 } |