comparison src/protocols/jabber/jconn.c @ 2086:424a40f12a6c

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