[gaim-migrate @ 3063]
[pidgin-git.git] / src / protocols / jabber / jconn.c
blob52069a78ae8df7bb393bdc00ca8936926346ee0b
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.
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.
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.
16 * Jabber
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
20 #include "jabber.h"
22 /* local macros for launching event handlers */
23 #define STATE_EVT(arg) if(j->on_state) { (j->on_state)(j, (arg) ); }
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);
31 * jab_new -- initialize a new jabber connection
33 * parameters
34 * user -- jabber id of the user
35 * pass -- password of the user
37 * results
38 * a pointer to the connection structure
39 * or NULL if allocations failed
41 jconn jab_new(char *user, char *pass)
43 pool p;
44 jconn j;
46 if(!user) return(NULL);
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;
54 j->user = jid_new(p, user);
55 j->pass = pstrdup(p, pass);
57 j->state = JCONN_STATE_OFF;
58 j->id = 1;
59 j->fd = -1;
61 return j;
65 * jab_delete -- free a jabber connection
67 * parameters
68 * j -- connection
71 void jab_delete(jconn j)
73 if(!j) return;
75 jab_stop(j);
76 pool_free(j->p);
80 * jab_state_handler -- set callback handler for state change
82 * parameters
83 * j -- connection
84 * h -- name of the handler function
86 void jab_state_handler(jconn j, jconn_state_h h)
88 if(!j) return;
90 j->on_state = h;
94 * jab_packet_handler -- set callback handler for incoming packets
96 * parameters
97 * j -- connection
98 * h -- name of the handler function
100 void jab_packet_handler(jconn j, jconn_packet_h h)
102 if(!j) return;
104 j->on_packet = h;
109 * jab_start -- start connection
111 * parameters
112 * j -- connection
115 void jab_start(jconn j)
117 xmlnode x;
118 char *t,*t2;
120 if(!j || j->state != JCONN_STATE_OFF) return;
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);
127 j->fd = make_netsocket(5222, j->user->server, NETSOCKET_CLIENT);
128 if(j->fd < 0) {
129 STATE_EVT(JCONN_STATE_OFF)
130 return;
132 j->state = JCONN_STATE_CONNECTED;
133 STATE_EVT(JCONN_STATE_CONNECTED)
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);
147 j->state = JCONN_STATE_ON;
148 STATE_EVT(JCONN_STATE_ON)
153 * jab_stop -- stop connection
155 * parameters
156 * j -- connection
158 void jab_stop(jconn j)
160 if(!j || j->state == JCONN_STATE_OFF) return;
162 j->state = JCONN_STATE_OFF;
163 close(j->fd);
164 j->fd = -1;
165 XML_ParserFree(j->parser);
169 * jab_getfd -- get file descriptor of connection socket
171 * parameters
172 * j -- connection
174 * returns
175 * fd of the socket or -1 if socket was not connected
177 int jab_getfd(jconn j)
179 if(j)
180 return j->fd;
181 else
182 return -1;
186 * jab_getjid -- get jid structure of user
188 * parameters
189 * j -- connection
191 jid jab_getjid(jconn j)
193 if(j)
194 return(j->user);
195 else
196 return NULL;
199 /* jab_getsid -- get stream id
200 * This is the id of server's <stream:stream> tag and used for
201 * digest authorization.
203 * parameters
204 * j -- connection
206 char *jab_getsid(jconn j)
208 if(j)
209 return(j->sid);
210 else
211 return NULL;
215 * jab_getid -- get a unique id
217 * parameters
218 * j -- connection
220 char *jab_getid(jconn j)
222 snprintf(j->idbuf, 8, "%d", j->id++);
223 return &j->idbuf[0];
227 * jab_send -- send xml data
229 * parameters
230 * j -- connection
231 * x -- xmlnode structure
233 void jab_send(jconn j, xmlnode x)
235 if (j && j->state != JCONN_STATE_OFF)
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
246 * jab_send_raw -- send a string
248 * parameters
249 * j -- connection
250 * str -- xml string
252 void jab_send_raw(jconn j, const char *str)
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
262 * jab_recv -- read and parse incoming data
264 * parameters
265 * j -- connection
267 void jab_recv(jconn j)
269 static char buf[4096];
270 int len;
272 if(!j || j->state == JCONN_STATE_OFF)
273 return;
275 len = read(j->fd, buf, sizeof(buf)-1);
276 if(len>0)
278 buf[len] = '\0';
279 #ifdef JDEBUG
280 printf (" in: %s\n", buf);
281 #endif
282 XML_Parse(j->parser, buf, len, 0);
284 else if(len<0)
286 STATE_EVT(JCONN_STATE_OFF);
287 jab_stop(j);
292 * jab_poll -- check socket for incoming data
294 * parameters
295 * j -- connection
296 * timeout -- poll timeout
298 void jab_poll(jconn j, int timeout)
300 fd_set fds;
301 struct timeval tv;
303 if (!j || j->state == JCONN_STATE_OFF)
304 return;
306 FD_ZERO(&fds);
307 FD_SET(j->fd, &fds);
309 if (timeout < 0)
311 if (select(j->fd + 1, &fds, NULL, NULL, NULL) > 0)
312 jab_recv(j);
314 else
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);
324 * jab_auth -- authorize user
326 * parameters
327 * j -- connection
329 * returns
330 * id of the iq packet
332 char *jab_auth(jconn j)
334 xmlnode x,y,z;
335 char *hash, *user, *id;
337 if(!j) return(NULL);
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");
344 user = j->user->user;
346 if (user)
348 z = xmlnode_insert_tag(y, "username");
349 xmlnode_insert_cdata(z, user, -1);
352 z = xmlnode_insert_tag(y, "resource");
353 xmlnode_insert_cdata(z, j->user->resource, -1);
355 if (j->sid)
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);
364 else
366 z = xmlnode_insert_tag(y, "password");
367 xmlnode_insert_cdata(z, j->pass, -1);
370 jab_send(j, x);
371 xmlnode_free(x);
372 return id;
376 * jab_reg -- register user
378 * parameters
379 * j -- connection
381 * returns
382 * id of the iq packet
384 char *jab_reg(jconn j)
386 xmlnode x,y,z;
387 char *hash, *user, *id;
389 if (!j) return(NULL);
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");
396 user = j->user->user;
398 if (user)
400 z = xmlnode_insert_tag(y, "username");
401 xmlnode_insert_cdata(z, user, -1);
404 z = xmlnode_insert_tag(y, "resource");
405 xmlnode_insert_cdata(z, j->user->resource, -1);
407 if (j->pass)
409 z = xmlnode_insert_tag(y, "password");
410 xmlnode_insert_cdata(z, j->pass, -1);
413 jab_send(j, x);
414 xmlnode_free(x);
415 j->state = JCONN_STATE_ON;
416 STATE_EVT(JCONN_STATE_ON)
417 return id;
421 /* local functions */
423 static void startElement(void *userdata, const char *name, const char **attribs)
425 xmlnode x;
426 jconn j = (jconn)userdata;
428 if(j->current)
430 /* Append the node to the current one */
431 x = xmlnode_insert_tag(j->current, name);
432 xmlnode_put_expat_attribs(x, attribs);
434 j->current = x;
436 else
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;
451 static void endElement(void *userdata, const char *name)
453 jconn j = (jconn)userdata;
454 xmlnode x;
455 jpacket p;
457 if(j->current == NULL) {
458 /* we got </stream:stream> */
459 STATE_EVT(JCONN_STATE_OFF)
460 return;
463 x = xmlnode_get_parent(j->current);
465 if(x == NULL)
467 /* it is time to fire the event */
468 p = jpacket_new(j->current);
470 if(j->on_packet)
471 (j->on_packet)(j, p);
472 else
473 xmlnode_free(j->current);
476 j->current = x;
479 static void charData(void *userdata, const char *s, int slen)
481 jconn j = (jconn)userdata;
483 if (j->current)
484 xmlnode_insert_cdata(j->current, s, slen);