1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /* |
---|
3 | * gaim |
---|
4 | * |
---|
5 | * Some code copyright (C) 1998-1999, Mark Spencer <markster@marko.net> |
---|
6 | * libfaim code copyright 1998, 1999 Adam Fritzler <afritz@auk.cx> |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or modify |
---|
9 | * it under the terms of the GNU General Public License as published by |
---|
10 | * the Free Software Foundation; either version 2 of the License, or |
---|
11 | * (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
21 | * |
---|
22 | */ |
---|
23 | |
---|
24 | #ifndef _WIN32 |
---|
25 | #include <sys/utsname.h> |
---|
26 | #endif |
---|
27 | #include <errno.h> |
---|
28 | #include <string.h> |
---|
29 | #include <stdlib.h> |
---|
30 | #include <stdio.h> |
---|
31 | #include <time.h> |
---|
32 | #include <sys/stat.h> |
---|
33 | #include "jabber.h" |
---|
34 | #include "nogaim.h" |
---|
35 | #include "bitlbee.h" |
---|
36 | #include "proxy.h" |
---|
37 | #include "ssl_client.h" |
---|
38 | |
---|
39 | /* The priv member of gjconn's is a gaim_connection for now. */ |
---|
40 | #define GJ_GC(x) ((struct gaim_connection *)(x)->priv) |
---|
41 | |
---|
42 | #define IQID_AUTH "__AUTH__" |
---|
43 | |
---|
44 | #define IQ_NONE -1 |
---|
45 | #define IQ_AUTH 0 |
---|
46 | #define IQ_ROSTER 1 |
---|
47 | |
---|
48 | #define UC_AWAY (0x02 | UC_UNAVAILABLE) |
---|
49 | #define UC_CHAT 0x04 |
---|
50 | #define UC_XA (0x08 | UC_UNAVAILABLE) |
---|
51 | #define UC_DND (0x10 | UC_UNAVAILABLE) |
---|
52 | |
---|
53 | #define DEFAULT_SERVER "jabber.org" |
---|
54 | #define DEFAULT_GROUPCHAT "conference.jabber.org" |
---|
55 | #define DEFAULT_PORT 5222 |
---|
56 | #define DEFAULT_PORT_SSL 5223 |
---|
57 | #define JABBER_PORT_MIN 5220 |
---|
58 | #define JABBER_PORT_MAX 5229 |
---|
59 | |
---|
60 | #define JABBER_GROUP "Friends" |
---|
61 | |
---|
62 | /* i18n disabled - Bitlbee */ |
---|
63 | #define N_(String) String |
---|
64 | |
---|
65 | /* |
---|
66 | * Note: "was_connected" may seem redundant, but it was needed and I |
---|
67 | * didn't want to touch the Jabber state stuff not specific to Gaim. |
---|
68 | */ |
---|
69 | typedef struct gjconn_struct { |
---|
70 | /* Core structure */ |
---|
71 | pool p; /* Memory allocation pool */ |
---|
72 | int state; /* Connection state flag */ |
---|
73 | int was_connected; /* We were once connected */ |
---|
74 | int fd; /* Connection file descriptor */ |
---|
75 | void *ssl; /* SSL connection */ |
---|
76 | jid user; /* User info */ |
---|
77 | char *pass; /* User passwd */ |
---|
78 | |
---|
79 | /* Stream stuff */ |
---|
80 | int id; /* id counter for jab_getid() function */ |
---|
81 | char idbuf[9]; /* temporary storage for jab_getid() */ |
---|
82 | char *sid; /* stream id from server, for digest auth */ |
---|
83 | XML_Parser parser; /* Parser instance */ |
---|
84 | xmlnode current; /* Current node in parsing instance.. */ |
---|
85 | |
---|
86 | /* Event callback ptrs */ |
---|
87 | void (*on_state)(struct gjconn_struct *gjc, int state); |
---|
88 | void (*on_packet)(struct gjconn_struct *gjc, jpacket p); |
---|
89 | |
---|
90 | GHashTable *queries; /* query tracker */ |
---|
91 | |
---|
92 | void *priv; |
---|
93 | } *gjconn, gjconn_struct; |
---|
94 | |
---|
95 | typedef void (*gjconn_state_h)(gjconn gjc, int state); |
---|
96 | typedef void (*gjconn_packet_h)(gjconn gjc, jpacket p); |
---|
97 | |
---|
98 | static gjconn gjab_new(char *user, char *pass, void *priv); |
---|
99 | static void gjab_delete(gjconn gjc); |
---|
100 | static void gjab_state_handler(gjconn gjc, gjconn_state_h h); |
---|
101 | static void gjab_packet_handler(gjconn gjc, gjconn_packet_h h); |
---|
102 | static void gjab_start(gjconn gjc); |
---|
103 | static void gjab_stop(gjconn gjc); |
---|
104 | /* |
---|
105 | static int gjab_getfd(gjconn gjc); |
---|
106 | static jid gjab_getjid(gjconn gjc); |
---|
107 | static char *gjab_getsid(gjconn gjc); |
---|
108 | */ |
---|
109 | static char *gjab_getid(gjconn gjc); |
---|
110 | static void gjab_send(gjconn gjc, xmlnode x); |
---|
111 | static void gjab_send_raw(gjconn gjc, const char *str); |
---|
112 | static void gjab_recv(gjconn gjc); |
---|
113 | static void gjab_auth(gjconn gjc); |
---|
114 | |
---|
115 | /* |
---|
116 | * It is *this* to which we point the gaim_connection proto_data |
---|
117 | */ |
---|
118 | struct jabber_data { |
---|
119 | gjconn gjc; |
---|
120 | gboolean did_import; |
---|
121 | GSList *chats; |
---|
122 | GHashTable *hash; |
---|
123 | time_t idle; |
---|
124 | gboolean die; |
---|
125 | }; |
---|
126 | |
---|
127 | /* |
---|
128 | * Jabber "chat group" info. Pointers to these go in jabber_data |
---|
129 | * pending and existing chats lists. |
---|
130 | */ |
---|
131 | struct jabber_chat { |
---|
132 | jid Jid; |
---|
133 | struct gaim_connection *gc; |
---|
134 | struct conversation *b; |
---|
135 | int id; |
---|
136 | int state; |
---|
137 | }; |
---|
138 | |
---|
139 | /* |
---|
140 | * Jabber chat states... |
---|
141 | * |
---|
142 | * Note: due to a bug in one version of the Jabber server, subscriptions |
---|
143 | * to chat groups aren't (always?) properly removed at the server. The |
---|
144 | * result is clients receive Jabber "presence" notifications for JIDs |
---|
145 | * they no longer care about. The problem with such vestigial notifies is |
---|
146 | * that we really have no way of telling if it's vestigial or if it's a |
---|
147 | * valid "buddy" presence notification. So we keep jabber_chat structs |
---|
148 | * around after leaving a chat group and simply mark them "closed." That |
---|
149 | * way we can test for such errant presence notifications. I.e.: if we |
---|
150 | * get a presence notfication from a JID that matches a chat group JID, |
---|
151 | * we disregard it. |
---|
152 | */ |
---|
153 | #define JCS_PENDING 1 /* pending */ |
---|
154 | #define JCS_ACTIVE 2 /* active */ |
---|
155 | #define JCS_CLOSED 3 /* closed */ |
---|
156 | |
---|
157 | |
---|
158 | #define STATE_EVT(arg) if(gjc->on_state) { (gjc->on_state)(gjc, (arg) ); } |
---|
159 | |
---|
160 | static void jabber_remove_buddy(struct gaim_connection *gc, char *name, char *group); |
---|
161 | static void jabber_handlevcard(gjconn gjc, xmlnode querynode, char *from); |
---|
162 | |
---|
163 | static char *create_valid_jid(const char *given, char *server, char *resource) |
---|
164 | { |
---|
165 | char *valid; |
---|
166 | |
---|
167 | if (!strchr(given, '@')) |
---|
168 | valid = g_strdup_printf("%s@%s/%s", given, server, resource); |
---|
169 | else if (!strchr(strchr(given, '@'), '/')) |
---|
170 | valid = g_strdup_printf("%s/%s", given, resource); |
---|
171 | else |
---|
172 | valid = g_strdup(given); |
---|
173 | |
---|
174 | return valid; |
---|
175 | } |
---|
176 | |
---|
177 | static gjconn gjab_new(char *user, char *pass, void *priv) |
---|
178 | { |
---|
179 | pool p; |
---|
180 | gjconn gjc; |
---|
181 | |
---|
182 | if (!user) |
---|
183 | return (NULL); |
---|
184 | |
---|
185 | p = pool_new(); |
---|
186 | if (!p) |
---|
187 | return (NULL); |
---|
188 | gjc = pmalloc_x(p, sizeof(gjconn_struct), 0); |
---|
189 | if (!gjc) { |
---|
190 | pool_free(p); /* no need for this anymore! */ |
---|
191 | return (NULL); |
---|
192 | } |
---|
193 | gjc->p = p; |
---|
194 | |
---|
195 | if((gjc->user = jid_new(p, user)) == NULL) { |
---|
196 | pool_free(p); /* no need for this anymore! */ |
---|
197 | return (NULL); |
---|
198 | } |
---|
199 | gjc->pass = pstrdup(p, pass); |
---|
200 | |
---|
201 | gjc->state = JCONN_STATE_OFF; |
---|
202 | gjc->was_connected = 0; |
---|
203 | gjc->id = 1; |
---|
204 | gjc->fd = -1; |
---|
205 | |
---|
206 | gjc->priv = priv; |
---|
207 | |
---|
208 | return gjc; |
---|
209 | } |
---|
210 | |
---|
211 | static void gjab_delete(gjconn gjc) |
---|
212 | { |
---|
213 | if (!gjc) |
---|
214 | return; |
---|
215 | |
---|
216 | gjab_stop(gjc); |
---|
217 | pool_free(gjc->p); |
---|
218 | } |
---|
219 | |
---|
220 | static void gjab_state_handler(gjconn gjc, gjconn_state_h h) |
---|
221 | { |
---|
222 | if (!gjc) |
---|
223 | return; |
---|
224 | |
---|
225 | gjc->on_state = h; |
---|
226 | } |
---|
227 | |
---|
228 | static void gjab_packet_handler(gjconn gjc, gjconn_packet_h h) |
---|
229 | { |
---|
230 | if (!gjc) |
---|
231 | return; |
---|
232 | |
---|
233 | gjc->on_packet = h; |
---|
234 | } |
---|
235 | |
---|
236 | static void gjab_stop(gjconn gjc) |
---|
237 | { |
---|
238 | if (!gjc || gjc->state == JCONN_STATE_OFF) |
---|
239 | return; |
---|
240 | |
---|
241 | gjab_send_raw(gjc, "</stream:stream>"); |
---|
242 | gjc->state = JCONN_STATE_OFF; |
---|
243 | gjc->was_connected = 0; |
---|
244 | if (gjc->ssl) { |
---|
245 | ssl_disconnect(gjc->ssl); |
---|
246 | gjc->ssl = NULL; |
---|
247 | } else { |
---|
248 | closesocket(gjc->fd); |
---|
249 | } |
---|
250 | gjc->fd = -1; |
---|
251 | XML_ParserFree(gjc->parser); |
---|
252 | gjc->parser = NULL; |
---|
253 | } |
---|
254 | |
---|
255 | /* |
---|
256 | static int gjab_getfd(gjconn gjc) |
---|
257 | { |
---|
258 | if (gjc) |
---|
259 | return gjc->fd; |
---|
260 | else |
---|
261 | return -1; |
---|
262 | } |
---|
263 | |
---|
264 | static jid gjab_getjid(gjconn gjc) |
---|
265 | { |
---|
266 | if (gjc) |
---|
267 | return (gjc->user); |
---|
268 | else |
---|
269 | return NULL; |
---|
270 | } |
---|
271 | |
---|
272 | static char *gjab_getsid(gjconn gjc) |
---|
273 | { |
---|
274 | if (gjc) |
---|
275 | return (gjc->sid); |
---|
276 | else |
---|
277 | return NULL; |
---|
278 | } |
---|
279 | */ |
---|
280 | |
---|
281 | static char *gjab_getid(gjconn gjc) |
---|
282 | { |
---|
283 | g_snprintf(gjc->idbuf, 8, "%d", gjc->id++); |
---|
284 | return &gjc->idbuf[0]; |
---|
285 | } |
---|
286 | |
---|
287 | static void gjab_send(gjconn gjc, xmlnode x) |
---|
288 | { |
---|
289 | if (gjc && gjc->state != JCONN_STATE_OFF) { |
---|
290 | char *buf = xmlnode2str(x); |
---|
291 | if (!buf) |
---|
292 | return; |
---|
293 | else if (gjc->ssl) |
---|
294 | ssl_write(gjc->ssl, buf, strlen(buf)); |
---|
295 | else |
---|
296 | write(gjc->fd, buf, strlen(buf)); |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | static void gjab_send_raw(gjconn gjc, const char *str) |
---|
301 | { |
---|
302 | if (gjc && gjc->state != JCONN_STATE_OFF) { |
---|
303 | int len; |
---|
304 | |
---|
305 | /* |
---|
306 | * JFIXME: No error detection?!?! |
---|
307 | */ |
---|
308 | if (gjc->ssl) |
---|
309 | len = ssl_write(gjc->ssl, str, strlen(str)); |
---|
310 | else |
---|
311 | len = write(gjc->fd, str, strlen(str)); |
---|
312 | |
---|
313 | if(len < 0) { |
---|
314 | /* Do NOT write to stdout/stderr directly, IRC clients |
---|
315 | might get confused, and we don't want that... |
---|
316 | fprintf(stderr, "DBG: Problem sending. Error: %d\n", errno); |
---|
317 | fflush(stderr); */ |
---|
318 | } |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | static void gjab_reqroster(gjconn gjc) |
---|
323 | { |
---|
324 | xmlnode x; |
---|
325 | |
---|
326 | x = jutil_iqnew(JPACKET__GET, NS_ROSTER); |
---|
327 | xmlnode_put_attrib(x, "id", gjab_getid(gjc)); |
---|
328 | |
---|
329 | gjab_send(gjc, x); |
---|
330 | xmlnode_free(x); |
---|
331 | } |
---|
332 | |
---|
333 | static void gjab_reqauth(gjconn gjc) |
---|
334 | { |
---|
335 | xmlnode x, y, z; |
---|
336 | char *user; |
---|
337 | |
---|
338 | if (!gjc) |
---|
339 | return; |
---|
340 | |
---|
341 | x = jutil_iqnew(JPACKET__GET, NS_AUTH); |
---|
342 | xmlnode_put_attrib(x, "id", IQID_AUTH); |
---|
343 | y = xmlnode_get_tag(x, "query"); |
---|
344 | |
---|
345 | user = gjc->user->user; |
---|
346 | |
---|
347 | if (user) { |
---|
348 | z = xmlnode_insert_tag(y, "username"); |
---|
349 | xmlnode_insert_cdata(z, user, -1); |
---|
350 | } |
---|
351 | |
---|
352 | gjab_send(gjc, x); |
---|
353 | xmlnode_free(x); |
---|
354 | } |
---|
355 | |
---|
356 | static void gjab_auth(gjconn gjc) |
---|
357 | { |
---|
358 | xmlnode x, y, z; |
---|
359 | char *hash, *user; |
---|
360 | |
---|
361 | if (!gjc) |
---|
362 | return; |
---|
363 | |
---|
364 | x = jutil_iqnew(JPACKET__SET, NS_AUTH); |
---|
365 | xmlnode_put_attrib(x, "id", IQID_AUTH); |
---|
366 | y = xmlnode_get_tag(x, "query"); |
---|
367 | |
---|
368 | user = gjc->user->user; |
---|
369 | |
---|
370 | if (user) { |
---|
371 | z = xmlnode_insert_tag(y, "username"); |
---|
372 | xmlnode_insert_cdata(z, user, -1); |
---|
373 | } |
---|
374 | |
---|
375 | z = xmlnode_insert_tag(y, "resource"); |
---|
376 | xmlnode_insert_cdata(z, gjc->user->resource, -1); |
---|
377 | |
---|
378 | if (gjc->sid) { |
---|
379 | z = xmlnode_insert_tag(y, "digest"); |
---|
380 | hash = pmalloc(x->p, strlen(gjc->sid) + strlen(gjc->pass) + 1); |
---|
381 | strcpy(hash, gjc->sid); |
---|
382 | strcat(hash, gjc->pass); |
---|
383 | hash = shahash(hash); |
---|
384 | xmlnode_insert_cdata(z, hash, 40); |
---|
385 | } else { |
---|
386 | z = xmlnode_insert_tag(y, "password"); |
---|
387 | xmlnode_insert_cdata(z, gjc->pass, -1); |
---|
388 | } |
---|
389 | |
---|
390 | gjab_send(gjc, x); |
---|
391 | xmlnode_free(x); |
---|
392 | |
---|
393 | return; |
---|
394 | } |
---|
395 | |
---|
396 | static void gjab_recv(gjconn gjc) |
---|
397 | { |
---|
398 | static char buf[4096]; |
---|
399 | int len; |
---|
400 | |
---|
401 | if (!gjc || gjc->state == JCONN_STATE_OFF) |
---|
402 | return; |
---|
403 | |
---|
404 | if (gjc->ssl) |
---|
405 | len = ssl_read(gjc->ssl, buf, sizeof(buf) - 1); |
---|
406 | else |
---|
407 | len = read(gjc->fd, buf, sizeof(buf) - 1); |
---|
408 | |
---|
409 | if (len > 0) { |
---|
410 | struct jabber_data *jd = GJ_GC(gjc)->proto_data; |
---|
411 | buf[len] = '\0'; |
---|
412 | XML_Parse(gjc->parser, buf, len, 0); |
---|
413 | if (jd->die) |
---|
414 | signoff(GJ_GC(gjc)); |
---|
415 | } else if (len == 0 || (len < 0 && (!sockerr_again() || gjc->ssl))) { |
---|
416 | STATE_EVT(JCONN_STATE_OFF) |
---|
417 | } |
---|
418 | } |
---|
419 | |
---|
420 | static void startElement(void *userdata, const char *name, const char **attribs) |
---|
421 | { |
---|
422 | xmlnode x; |
---|
423 | gjconn gjc = (gjconn) userdata; |
---|
424 | |
---|
425 | if (gjc->current) { |
---|
426 | /* Append the node to the current one */ |
---|
427 | x = xmlnode_insert_tag(gjc->current, name); |
---|
428 | xmlnode_put_expat_attribs(x, attribs); |
---|
429 | |
---|
430 | gjc->current = x; |
---|
431 | } else { |
---|
432 | x = xmlnode_new_tag(name); |
---|
433 | xmlnode_put_expat_attribs(x, attribs); |
---|
434 | if (strcmp(name, "stream:stream") == 0) { |
---|
435 | /* special case: name == stream:stream */ |
---|
436 | /* id attrib of stream is stored for digest auth */ |
---|
437 | gjc->sid = g_strdup(xmlnode_get_attrib(x, "id")); |
---|
438 | /* STATE_EVT(JCONN_STATE_AUTH) */ |
---|
439 | xmlnode_free(x); |
---|
440 | } else { |
---|
441 | gjc->current = x; |
---|
442 | } |
---|
443 | } |
---|
444 | } |
---|
445 | |
---|
446 | static void endElement(void *userdata, const char *name) |
---|
447 | { |
---|
448 | gjconn gjc = (gjconn) userdata; |
---|
449 | xmlnode x; |
---|
450 | jpacket p; |
---|
451 | |
---|
452 | if (gjc->current == NULL) { |
---|
453 | /* we got </stream:stream> */ |
---|
454 | STATE_EVT(JCONN_STATE_OFF) |
---|
455 | return; |
---|
456 | } |
---|
457 | |
---|
458 | x = xmlnode_get_parent(gjc->current); |
---|
459 | |
---|
460 | if (!x) { |
---|
461 | /* it is time to fire the event */ |
---|
462 | p = jpacket_new(gjc->current); |
---|
463 | |
---|
464 | if (gjc->on_packet) |
---|
465 | (gjc->on_packet) (gjc, p); |
---|
466 | else |
---|
467 | xmlnode_free(gjc->current); |
---|
468 | } |
---|
469 | |
---|
470 | gjc->current = x; |
---|
471 | } |
---|
472 | |
---|
473 | static gboolean jabber_callback(gpointer data, gint source, b_input_condition condition) |
---|
474 | { |
---|
475 | struct gaim_connection *gc = (struct gaim_connection *)data; |
---|
476 | struct jabber_data *jd = (struct jabber_data *)gc->proto_data; |
---|
477 | |
---|
478 | gjab_recv(jd->gjc); |
---|
479 | |
---|
480 | return TRUE; |
---|
481 | } |
---|
482 | |
---|
483 | static void charData(void *userdata, const char *s, int slen) |
---|
484 | { |
---|
485 | gjconn gjc = (gjconn) userdata; |
---|
486 | |
---|
487 | if (gjc->current) |
---|
488 | xmlnode_insert_cdata(gjc->current, s, slen); |
---|
489 | } |
---|
490 | |
---|
491 | static gboolean gjab_connected(gpointer data, gint source, b_input_condition cond) |
---|
492 | { |
---|
493 | xmlnode x; |
---|
494 | char *t, *t2; |
---|
495 | struct gaim_connection *gc = data; |
---|
496 | struct jabber_data *jd; |
---|
497 | gjconn gjc; |
---|
498 | |
---|
499 | if (!g_slist_find(get_connections(), gc)) { |
---|
500 | closesocket(source); |
---|
501 | return FALSE; |
---|
502 | } |
---|
503 | |
---|
504 | jd = gc->proto_data; |
---|
505 | gjc = jd->gjc; |
---|
506 | |
---|
507 | if (gjc->fd != source) |
---|
508 | gjc->fd = source; |
---|
509 | |
---|
510 | if (source == -1) { |
---|
511 | STATE_EVT(JCONN_STATE_OFF) |
---|
512 | return FALSE; |
---|
513 | } |
---|
514 | |
---|
515 | gjc->state = JCONN_STATE_CONNECTED; |
---|
516 | STATE_EVT(JCONN_STATE_CONNECTED) |
---|
517 | |
---|
518 | /* start stream */ |
---|
519 | x = jutil_header(NS_CLIENT, gjc->user->server); |
---|
520 | t = xmlnode2str(x); |
---|
521 | /* this is ugly, we can create the string here instead of jutil_header */ |
---|
522 | /* what do you think about it? -madcat */ |
---|
523 | t2 = strstr(t, "/>"); |
---|
524 | *t2++ = '>'; |
---|
525 | *t2 = '\0'; |
---|
526 | gjab_send_raw(gjc, "<?xml version='1.0'?>"); |
---|
527 | gjab_send_raw(gjc, t); |
---|
528 | xmlnode_free(x); |
---|
529 | |
---|
530 | gjc->state = JCONN_STATE_ON; |
---|
531 | STATE_EVT(JCONN_STATE_ON); |
---|
532 | |
---|
533 | gc = GJ_GC(gjc); |
---|
534 | gc->inpa = b_input_add(gjc->fd, GAIM_INPUT_READ, jabber_callback, gc); |
---|
535 | |
---|
536 | return FALSE; |
---|
537 | } |
---|
538 | |
---|
539 | static gboolean gjab_connected_ssl(gpointer data, void *source, b_input_condition cond) |
---|
540 | { |
---|
541 | struct gaim_connection *gc = data; |
---|
542 | struct jabber_data *jd; |
---|
543 | gjconn gjc; |
---|
544 | |
---|
545 | jd = gc->proto_data; |
---|
546 | gjc = jd->gjc; |
---|
547 | |
---|
548 | if (source == NULL) { |
---|
549 | STATE_EVT(JCONN_STATE_OFF) |
---|
550 | return FALSE; |
---|
551 | } |
---|
552 | |
---|
553 | if (!g_slist_find(get_connections(), gc)) { |
---|
554 | ssl_disconnect(source); |
---|
555 | return FALSE; |
---|
556 | } |
---|
557 | |
---|
558 | return gjab_connected(data, gjc->fd, cond); |
---|
559 | } |
---|
560 | |
---|
561 | static void gjab_start(gjconn gjc) |
---|
562 | { |
---|
563 | account_t *acc; |
---|
564 | int port = -1, ssl = 0; |
---|
565 | char *server = NULL, *s; |
---|
566 | |
---|
567 | if (!gjc || gjc->state != JCONN_STATE_OFF) |
---|
568 | return; |
---|
569 | |
---|
570 | acc = GJ_GC(gjc)->acc; |
---|
571 | if (acc->server) { |
---|
572 | /* If there's a dot, assume there's a hostname in the beginning */ |
---|
573 | if (strchr(acc->server, '.')) { |
---|
574 | server = g_strdup(acc->server); |
---|
575 | if ((s = strchr(server, ':'))) |
---|
576 | *s = 0; |
---|
577 | } |
---|
578 | |
---|
579 | /* After the hostname, there can be a port number */ |
---|
580 | s = strchr(acc->server, ':'); |
---|
581 | if (s && isdigit(s[1])) |
---|
582 | sscanf(s + 1, "%d", &port); |
---|
583 | |
---|
584 | /* And if there's the string ssl, the user wants an SSL-connection */ |
---|
585 | if (strstr(acc->server, ":ssl") || g_strcasecmp(acc->server, "ssl") == 0) |
---|
586 | ssl = 1; |
---|
587 | } |
---|
588 | |
---|
589 | if (port == -1 && !ssl) |
---|
590 | port = DEFAULT_PORT; |
---|
591 | else if (port == -1 && ssl) |
---|
592 | port = DEFAULT_PORT_SSL; |
---|
593 | else if (port < JABBER_PORT_MIN || port > JABBER_PORT_MAX) { |
---|
594 | serv_got_crap(GJ_GC(gjc), "For security reasons, the Jabber port number must be in the %d-%d range.", JABBER_PORT_MIN, JABBER_PORT_MAX); |
---|
595 | STATE_EVT(JCONN_STATE_OFF) |
---|
596 | return; |
---|
597 | } |
---|
598 | |
---|
599 | if (server == NULL) |
---|
600 | server = g_strdup(gjc->user->server); |
---|
601 | |
---|
602 | gjc->parser = XML_ParserCreate(NULL); |
---|
603 | XML_SetUserData(gjc->parser, (void *)gjc); |
---|
604 | XML_SetElementHandler(gjc->parser, startElement, endElement); |
---|
605 | XML_SetCharacterDataHandler(gjc->parser, charData); |
---|
606 | |
---|
607 | if (ssl) { |
---|
608 | if ((gjc->ssl = ssl_connect(server, port, gjab_connected_ssl, GJ_GC(gjc)))) |
---|
609 | gjc->fd = ssl_getfd(gjc->ssl); |
---|
610 | else |
---|
611 | gjc->fd = -1; |
---|
612 | } else { |
---|
613 | gjc->fd = proxy_connect(server, port, gjab_connected, GJ_GC(gjc)); |
---|
614 | } |
---|
615 | |
---|
616 | g_free(server); |
---|
617 | |
---|
618 | if (!acc->gc || (gjc->fd < 0)) { |
---|
619 | STATE_EVT(JCONN_STATE_OFF) |
---|
620 | return; |
---|
621 | } |
---|
622 | } |
---|
623 | |
---|
624 | /* |
---|
625 | * Find existing/active Jabber chat |
---|
626 | */ |
---|
627 | static struct jabber_chat *find_existing_chat(struct gaim_connection *gc, jid chat) |
---|
628 | { |
---|
629 | GSList *jcs = ((struct jabber_data *)gc->proto_data)->chats; |
---|
630 | struct jabber_chat *jc = NULL; |
---|
631 | |
---|
632 | while (jcs) { |
---|
633 | jc = jcs->data; |
---|
634 | if (jc->state == JCS_ACTIVE && !jid_cmpx(chat, jc->Jid, JID_USER | JID_SERVER)) |
---|
635 | break; |
---|
636 | jc = NULL; |
---|
637 | jcs = jcs->next; |
---|
638 | } |
---|
639 | |
---|
640 | return jc; |
---|
641 | } |
---|
642 | |
---|
643 | /* |
---|
644 | * Find pending chat |
---|
645 | */ |
---|
646 | static struct jabber_chat *find_pending_chat(struct gaim_connection *gc, jid chat) |
---|
647 | { |
---|
648 | GSList *jcs = ((struct jabber_data *)gc->proto_data)->chats; |
---|
649 | struct jabber_chat *jc = NULL; |
---|
650 | |
---|
651 | while (jcs) { |
---|
652 | jc = jcs->data; |
---|
653 | if (jc->state == JCS_PENDING && !jid_cmpx(chat, jc->Jid, JID_USER | JID_SERVER)) |
---|
654 | break; |
---|
655 | jc = NULL; |
---|
656 | jcs = jcs->next; |
---|
657 | } |
---|
658 | |
---|
659 | return jc; |
---|
660 | } |
---|
661 | |
---|
662 | static gboolean find_chat_buddy(struct conversation *b, char *name) |
---|
663 | { |
---|
664 | GList *m = b->in_room; |
---|
665 | |
---|
666 | while (m) { |
---|
667 | if (!strcmp(m->data, name)) |
---|
668 | return TRUE; |
---|
669 | m = m->next; |
---|
670 | } |
---|
671 | |
---|
672 | return FALSE; |
---|
673 | } |
---|
674 | |
---|
675 | /* |
---|
676 | * Remove a buddy from the (gaim) buddylist (if he's on it) |
---|
677 | */ |
---|
678 | static void jabber_remove_gaim_buddy(struct gaim_connection *gc, char *buddyname) |
---|
679 | { |
---|
680 | struct buddy *b; |
---|
681 | |
---|
682 | if ((b = find_buddy(gc, buddyname)) != NULL) { |
---|
683 | /* struct group *group; |
---|
684 | |
---|
685 | group = find_group_by_buddy(gc, buddyname); |
---|
686 | remove_buddy(gc, group, b); */ |
---|
687 | jabber_remove_buddy(gc, b->name, JABBER_GROUP); |
---|
688 | } |
---|
689 | } |
---|
690 | |
---|
691 | /* |
---|
692 | * keep track of away msg same as yahoo plugin |
---|
693 | */ |
---|
694 | static void jabber_track_away(gjconn gjc, jpacket p, char *name, char *type) |
---|
695 | { |
---|
696 | struct jabber_data *jd = GJ_GC(gjc)->proto_data; |
---|
697 | gpointer val = g_hash_table_lookup(jd->hash, name); |
---|
698 | char *show; |
---|
699 | char *vshow = NULL; |
---|
700 | char *status = NULL; |
---|
701 | char *msg = NULL; |
---|
702 | |
---|
703 | if (type && (g_strcasecmp(type, "unavailable") == 0)) { |
---|
704 | vshow = _("Unavailable"); |
---|
705 | } else { |
---|
706 | if((show = xmlnode_get_tag_data(p->x, "show")) != NULL) { |
---|
707 | if (!g_strcasecmp(show, "away")) { |
---|
708 | vshow = _("Away"); |
---|
709 | } else if (!g_strcasecmp(show, "chat")) { |
---|
710 | vshow = _("Online"); |
---|
711 | } else if (!g_strcasecmp(show, "xa")) { |
---|
712 | vshow = _("Extended Away"); |
---|
713 | } else if (!g_strcasecmp(show, "dnd")) { |
---|
714 | vshow = _("Do Not Disturb"); |
---|
715 | } |
---|
716 | } |
---|
717 | } |
---|
718 | |
---|
719 | status = xmlnode_get_tag_data(p->x, "status"); |
---|
720 | |
---|
721 | if(vshow != NULL || status != NULL ) { |
---|
722 | /* kinda hokey, but it works :-) */ |
---|
723 | msg = g_strdup_printf("%s%s%s", |
---|
724 | (vshow == NULL? "" : vshow), |
---|
725 | (vshow == NULL || status == NULL? "" : ": "), |
---|
726 | (status == NULL? "" : status)); |
---|
727 | } else { |
---|
728 | msg = g_strdup(_("Online")); |
---|
729 | } |
---|
730 | |
---|
731 | if (val) { |
---|
732 | g_free(val); |
---|
733 | g_hash_table_insert(jd->hash, name, msg); |
---|
734 | } else { |
---|
735 | g_hash_table_insert(jd->hash, g_strdup(name), msg); |
---|
736 | } |
---|
737 | } |
---|
738 | |
---|
739 | static time_t iso8601_to_time(char *timestamp) |
---|
740 | { |
---|
741 | struct tm t; |
---|
742 | time_t retval = 0; |
---|
743 | |
---|
744 | if(sscanf(timestamp,"%04d%02d%02dT%02d:%02d:%02d", |
---|
745 | &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec)) |
---|
746 | { |
---|
747 | t.tm_year -= 1900; |
---|
748 | t.tm_mon -= 1; |
---|
749 | t.tm_isdst = 0; |
---|
750 | retval = mktime(&t); |
---|
751 | # ifdef HAVE_TM_GMTOFF |
---|
752 | retval += t.tm_gmtoff; |
---|
753 | # else |
---|
754 | # ifdef HAVE_TIMEZONE |
---|
755 | tzset(); /* making sure */ |
---|
756 | retval -= timezone; |
---|
757 | # endif |
---|
758 | # endif |
---|
759 | } |
---|
760 | |
---|
761 | return retval; |
---|
762 | } |
---|
763 | |
---|
764 | static void jabber_handlemessage(gjconn gjc, jpacket p) |
---|
765 | { |
---|
766 | xmlnode y, xmlns, z; |
---|
767 | time_t time_sent = time(NULL); |
---|
768 | |
---|
769 | char *from = NULL, *msg = NULL, *type = NULL; |
---|
770 | char m[BUF_LONG * 2]; |
---|
771 | |
---|
772 | type = xmlnode_get_attrib(p->x, "type"); |
---|
773 | |
---|
774 | z = xmlnode_get_firstchild(p->x); |
---|
775 | |
---|
776 | while(z) |
---|
777 | { |
---|
778 | if(NSCHECK(z,NS_DELAY)) |
---|
779 | { |
---|
780 | char *timestamp = xmlnode_get_attrib(z,"stamp"); |
---|
781 | time_sent = iso8601_to_time(timestamp); |
---|
782 | } |
---|
783 | z = xmlnode_get_nextsibling(z); |
---|
784 | } |
---|
785 | |
---|
786 | if (!type || !g_strcasecmp(type, "normal") || !g_strcasecmp(type, "chat")) { |
---|
787 | |
---|
788 | /* XXX namespaces could be handled better. (mid) */ |
---|
789 | if ((xmlns = xmlnode_get_tag(p->x, "x"))) |
---|
790 | type = xmlnode_get_attrib(xmlns, "xmlns"); |
---|
791 | |
---|
792 | from = jid_full(p->from); |
---|
793 | /* |
---|
794 | if ((y = xmlnode_get_tag(p->x, "html"))) { |
---|
795 | msg = xmlnode_get_data(y); |
---|
796 | } else |
---|
797 | */ |
---|
798 | if ((y = xmlnode_get_tag(p->x, "body"))) { |
---|
799 | msg = xmlnode_get_data(y); |
---|
800 | } |
---|
801 | |
---|
802 | |
---|
803 | if (!from) |
---|
804 | return; |
---|
805 | |
---|
806 | if (type && !g_strcasecmp(type, "jabber:x:conference")) { |
---|
807 | /* do nothing */ |
---|
808 | } else if (msg) { /* whisper */ |
---|
809 | struct jabber_chat *jc; |
---|
810 | g_snprintf(m, sizeof(m), "%s", msg); |
---|
811 | if (((jc = find_existing_chat(GJ_GC(gjc), p->from)) != NULL) && jc->b) |
---|
812 | serv_got_chat_in(GJ_GC(gjc), jc->b->id, p->from->resource, 1, m, time_sent); |
---|
813 | else { |
---|
814 | int flags = 0; |
---|
815 | |
---|
816 | if(p->from->user) { |
---|
817 | from = g_strdup_printf("%s@%s", p->from->user, p->from->server); |
---|
818 | } else { |
---|
819 | /* server message? */ |
---|
820 | from = g_strdup(p->from->server); |
---|
821 | } |
---|
822 | serv_got_im(GJ_GC(gjc), from, m, flags, time_sent, -1); |
---|
823 | g_free(from); |
---|
824 | } |
---|
825 | } |
---|
826 | |
---|
827 | } else if (!g_strcasecmp(type, "error")) { |
---|
828 | if ((y = xmlnode_get_tag(p->x, "error"))) { |
---|
829 | type = xmlnode_get_attrib(y, "code"); |
---|
830 | msg = xmlnode_get_data(y); |
---|
831 | } |
---|
832 | |
---|
833 | if (msg) { |
---|
834 | from = g_strdup_printf("Error %s", type ? type : ""); |
---|
835 | do_error_dialog(GJ_GC(gjc), msg, from); |
---|
836 | g_free(from); |
---|
837 | } |
---|
838 | } else if (!g_strcasecmp(type, "headline")) { |
---|
839 | char *subject, *body, *url; |
---|
840 | |
---|
841 | y = xmlnode_get_tag( p->x, "body" ); |
---|
842 | body = y ? g_strdup( xmlnode_get_data( y ) ) : NULL; |
---|
843 | |
---|
844 | y = xmlnode_get_tag( p->x, "subject" ); |
---|
845 | subject = y ? g_strdup( xmlnode_get_data( y ) ) : NULL; |
---|
846 | |
---|
847 | url = NULL; |
---|
848 | z = xmlnode_get_firstchild(p->x); |
---|
849 | while( z ) |
---|
850 | { |
---|
851 | char *xtype = xmlnode_get_attrib( z, "xmlns" ); |
---|
852 | |
---|
853 | if( xtype && g_strcasecmp( xtype, "jabber:x:oob" ) == 0 && |
---|
854 | ( y = xmlnode_get_tag( z, "url" ) ) ) |
---|
855 | { |
---|
856 | url = g_strdup( xmlnode_get_data( y ) ); |
---|
857 | break; |
---|
858 | } |
---|
859 | |
---|
860 | z = xmlnode_get_nextsibling( z ); |
---|
861 | } |
---|
862 | |
---|
863 | g_snprintf( m, BUF_LONG, "Subject: %s\nURL: %s\nMessage:\n%s", subject ? subject : "(none)", |
---|
864 | url ? url : "(none)", body ? body : "(none)" ); |
---|
865 | |
---|
866 | if( p->from->user ) |
---|
867 | from = g_strdup_printf( "%s@%s", p->from->user, p->from->server ); |
---|
868 | else |
---|
869 | from = g_strdup( p->from->server ); |
---|
870 | |
---|
871 | serv_got_im( GJ_GC(gjc), from, m, 0, time_sent, -1 ); |
---|
872 | |
---|
873 | g_free( from ); |
---|
874 | g_free( subject ); |
---|
875 | g_free( body ); |
---|
876 | g_free( url ); |
---|
877 | } |
---|
878 | } |
---|
879 | |
---|
880 | static void jabber_handlepresence(gjconn gjc, jpacket p) |
---|
881 | { |
---|
882 | char *to, *from, *type; |
---|
883 | struct buddy *b = NULL; |
---|
884 | jid who; |
---|
885 | char *buddy; |
---|
886 | xmlnode y; |
---|
887 | char *show; |
---|
888 | int state = 0; |
---|
889 | GSList *resources; |
---|
890 | char *res; |
---|
891 | struct conversation *cnv = NULL; |
---|
892 | struct jabber_chat *jc = NULL; |
---|
893 | |
---|
894 | to = xmlnode_get_attrib(p->x, "to"); |
---|
895 | from = xmlnode_get_attrib(p->x, "from"); |
---|
896 | type = xmlnode_get_attrib(p->x, "type"); |
---|
897 | |
---|
898 | if (type && g_strcasecmp(type, "error") == 0) { |
---|
899 | return; |
---|
900 | } |
---|
901 | else if ((y = xmlnode_get_tag(p->x, "show"))) { |
---|
902 | show = xmlnode_get_data(y); |
---|
903 | if (!show) { |
---|
904 | state = 0; |
---|
905 | } else if (!g_strcasecmp(show, "away")) { |
---|
906 | state = UC_AWAY; |
---|
907 | } else if (!g_strcasecmp(show, "chat")) { |
---|
908 | state = UC_CHAT; |
---|
909 | } else if (!g_strcasecmp(show, "xa")) { |
---|
910 | state = UC_XA; |
---|
911 | } else if (!g_strcasecmp(show, "dnd")) { |
---|
912 | state = UC_DND; |
---|
913 | } |
---|
914 | } else { |
---|
915 | state = 0; |
---|
916 | } |
---|
917 | |
---|
918 | who = jid_new(gjc->p, from); |
---|
919 | if (who->user == NULL) { |
---|
920 | /* FIXME: transport */ |
---|
921 | return; |
---|
922 | } |
---|
923 | |
---|
924 | buddy = g_strdup_printf("%s@%s", who->user, who->server); |
---|
925 | |
---|
926 | /* um. we're going to check if it's a chat. if it isn't, and there are pending |
---|
927 | * chats, create the chat. if there aren't pending chats and we don't have the |
---|
928 | * buddy on our list, simply bail out. */ |
---|
929 | if ((cnv = NULL) == NULL) { |
---|
930 | static int i = 0x70; |
---|
931 | if ((jc = find_pending_chat(GJ_GC(gjc), who)) != NULL) { |
---|
932 | jc->b = cnv = serv_got_joined_chat(GJ_GC(gjc), i++, who->user); |
---|
933 | jc->id = jc->b->id; |
---|
934 | jc->state = JCS_ACTIVE; |
---|
935 | } else if ((b = find_buddy(GJ_GC(gjc), buddy)) == NULL) { |
---|
936 | g_free(buddy); |
---|
937 | return; |
---|
938 | } |
---|
939 | } |
---|
940 | |
---|
941 | if (!cnv) { |
---|
942 | resources = b->proto_data; |
---|
943 | res = who->resource; |
---|
944 | if (res) |
---|
945 | while (resources) { |
---|
946 | if (!strcmp(res, resources->data)) |
---|
947 | break; |
---|
948 | resources = resources->next; |
---|
949 | } |
---|
950 | |
---|
951 | /* keep track of away msg same as yahoo plugin */ |
---|
952 | jabber_track_away(gjc, p, normalize(b->name), type); |
---|
953 | |
---|
954 | if (type && (g_strcasecmp(type, "unavailable") == 0)) { |
---|
955 | if (resources) { |
---|
956 | g_free(resources->data); |
---|
957 | b->proto_data = g_slist_remove(b->proto_data, resources->data); |
---|
958 | } |
---|
959 | if (!b->proto_data) { |
---|
960 | serv_got_update(GJ_GC(gjc), buddy, 0, 0, 0, 0, 0, 0); |
---|
961 | } |
---|
962 | } else { |
---|
963 | if (!resources) { |
---|
964 | b->proto_data = g_slist_append(b->proto_data, g_strdup(res)); |
---|
965 | } |
---|
966 | |
---|
967 | serv_got_update(GJ_GC(gjc), buddy, 1, 0, b->signon, b->idle, state, 0); |
---|
968 | |
---|
969 | } |
---|
970 | } else { |
---|
971 | if (who->resource) { |
---|
972 | char *buf; |
---|
973 | |
---|
974 | buf = g_strdup_printf("%s@%s/%s", who->user, who->server, who->resource); |
---|
975 | jabber_track_away(gjc, p, buf, type); |
---|
976 | g_free(buf); |
---|
977 | |
---|
978 | if (type && !g_strcasecmp(type, "unavailable")) { |
---|
979 | struct jabber_data *jd; |
---|
980 | if (!jc && !(jc = find_existing_chat(GJ_GC(gjc), who))) { |
---|
981 | g_free(buddy); |
---|
982 | return; |
---|
983 | } |
---|
984 | jd = jc->gc->proto_data; |
---|
985 | /* if it's not ourselves...*/ |
---|
986 | if (strcmp(who->resource, jc->Jid->resource) && jc->b) { |
---|
987 | remove_chat_buddy(jc->b, who->resource, NULL); |
---|
988 | g_free(buddy); |
---|
989 | return; |
---|
990 | } |
---|
991 | |
---|
992 | jc->state = JCS_CLOSED; |
---|
993 | serv_got_chat_left(GJ_GC(gjc), jc->id); |
---|
994 | /* |
---|
995 | * TBD: put back some day? |
---|
996 | jd->chats = g_slist_remove(jd->chats, jc); |
---|
997 | g_free(jc); |
---|
998 | */ |
---|
999 | } else { |
---|
1000 | if ((!jc && !(jc = find_existing_chat(GJ_GC(gjc), who))) || !jc->b) { |
---|
1001 | g_free(buddy); |
---|
1002 | return; |
---|
1003 | } |
---|
1004 | if (!find_chat_buddy(jc->b, who->resource)) { |
---|
1005 | add_chat_buddy(jc->b, who->resource); |
---|
1006 | } |
---|
1007 | } |
---|
1008 | } |
---|
1009 | } |
---|
1010 | |
---|
1011 | g_free(buddy); |
---|
1012 | |
---|
1013 | return; |
---|
1014 | } |
---|
1015 | |
---|
1016 | /* |
---|
1017 | * Used only by Jabber accept/deny add stuff just below |
---|
1018 | */ |
---|
1019 | struct jabber_add_permit { |
---|
1020 | gjconn gjc; |
---|
1021 | gchar *user; |
---|
1022 | }; |
---|
1023 | |
---|
1024 | /* |
---|
1025 | * Common part for Jabber accept/deny adds |
---|
1026 | * |
---|
1027 | * "type" says whether we'll permit/deny the subscribe request |
---|
1028 | */ |
---|
1029 | static void jabber_accept_deny_add(struct jabber_add_permit *jap, const char *type) |
---|
1030 | { |
---|
1031 | xmlnode g = xmlnode_new_tag("presence"); |
---|
1032 | |
---|
1033 | xmlnode_put_attrib(g, "to", jap->user); |
---|
1034 | xmlnode_put_attrib(g, "type", type); |
---|
1035 | gjab_send(jap->gjc, g); |
---|
1036 | |
---|
1037 | xmlnode_free(g); |
---|
1038 | } |
---|
1039 | |
---|
1040 | /* |
---|
1041 | * Callback from "accept" in do_ask_dialog() invoked by jabber_handles10n() |
---|
1042 | */ |
---|
1043 | static void jabber_accept_add(gpointer w, struct jabber_add_permit *jap) |
---|
1044 | { |
---|
1045 | jabber_accept_deny_add(jap, "subscribed"); |
---|
1046 | /* |
---|
1047 | * If we don't already have the buddy on *our* buddylist, |
---|
1048 | * ask if we want him or her added. |
---|
1049 | */ |
---|
1050 | if(find_buddy(GJ_GC(jap->gjc), jap->user) == NULL) { |
---|
1051 | show_got_added(GJ_GC(jap->gjc), jap->user, NULL); |
---|
1052 | } |
---|
1053 | g_free(jap->user); |
---|
1054 | g_free(jap); |
---|
1055 | } |
---|
1056 | |
---|
1057 | /* |
---|
1058 | * Callback from "deny/cancel" in do_ask_dialog() invoked by jabber_handles10n() |
---|
1059 | */ |
---|
1060 | static void jabber_deny_add(gpointer w, struct jabber_add_permit *jap) |
---|
1061 | { |
---|
1062 | jabber_accept_deny_add(jap, "unsubscribed"); |
---|
1063 | g_free(jap->user); |
---|
1064 | g_free(jap); |
---|
1065 | } |
---|
1066 | |
---|
1067 | /* |
---|
1068 | * Handle subscription requests |
---|
1069 | */ |
---|
1070 | static void jabber_handles10n(gjconn gjc, jpacket p) |
---|
1071 | { |
---|
1072 | xmlnode g; |
---|
1073 | char *Jid = xmlnode_get_attrib(p->x, "from"); |
---|
1074 | char *type = xmlnode_get_attrib(p->x, "type"); |
---|
1075 | |
---|
1076 | g = xmlnode_new_tag("presence"); |
---|
1077 | xmlnode_put_attrib(g, "to", Jid); |
---|
1078 | |
---|
1079 | if (!strcmp(type, "subscribe")) { |
---|
1080 | /* |
---|
1081 | * A "subscribe to us" request was received - put up the approval dialog |
---|
1082 | */ |
---|
1083 | struct jabber_add_permit *jap = g_new0(struct jabber_add_permit, 1); |
---|
1084 | gchar *msg = g_strdup_printf(_("The user %s wants to add you to his/her buddy list."), |
---|
1085 | Jid); |
---|
1086 | |
---|
1087 | jap->gjc = gjc; |
---|
1088 | jap->user = g_strdup(Jid); |
---|
1089 | do_ask_dialog(GJ_GC(gjc), msg, jap, jabber_accept_add, jabber_deny_add); |
---|
1090 | |
---|
1091 | g_free(msg); |
---|
1092 | xmlnode_free(g); /* Never needed it here anyway */ |
---|
1093 | return; |
---|
1094 | |
---|
1095 | } else if (!strcmp(type, "unsubscribe")) { |
---|
1096 | /* |
---|
1097 | * An "unsubscribe to us" was received - simply "approve" it |
---|
1098 | */ |
---|
1099 | xmlnode_put_attrib(g, "type", "unsubscribed"); |
---|
1100 | } else { |
---|
1101 | /* |
---|
1102 | * Did we attempt to subscribe to somebody and they do not exist? |
---|
1103 | */ |
---|
1104 | if (!strcmp(type, "unsubscribed")) { |
---|
1105 | xmlnode y; |
---|
1106 | char *status; |
---|
1107 | if((y = xmlnode_get_tag(p->x, "status")) && (status = xmlnode_get_data(y)) && |
---|
1108 | !strcmp(status, "Not Found")) { |
---|
1109 | char *msg = g_strdup_printf("%s: \"%s\"", _("No such user"), |
---|
1110 | xmlnode_get_attrib(p->x, "from")); |
---|
1111 | do_error_dialog(GJ_GC(gjc), msg, _("Jabber Error")); |
---|
1112 | g_free(msg); |
---|
1113 | } |
---|
1114 | } |
---|
1115 | |
---|
1116 | xmlnode_free(g); |
---|
1117 | return; |
---|
1118 | } |
---|
1119 | |
---|
1120 | gjab_send(gjc, g); |
---|
1121 | xmlnode_free(g); |
---|
1122 | } |
---|
1123 | |
---|
1124 | /* |
---|
1125 | * Pending subscription to a buddy? |
---|
1126 | */ |
---|
1127 | #define BUD_SUB_TO_PEND(sub, ask) ((!g_strcasecmp((sub), "none") || !g_strcasecmp((sub), "from")) && \ |
---|
1128 | (ask) != NULL && !g_strcasecmp((ask), "subscribe")) |
---|
1129 | |
---|
1130 | /* |
---|
1131 | * Subscribed to a buddy? |
---|
1132 | */ |
---|
1133 | #define BUD_SUBD_TO(sub, ask) ((!g_strcasecmp((sub), "to") || !g_strcasecmp((sub), "both")) && \ |
---|
1134 | ((ask) == NULL || !g_strcasecmp((ask), "subscribe"))) |
---|
1135 | |
---|
1136 | /* |
---|
1137 | * Pending unsubscription to a buddy? |
---|
1138 | */ |
---|
1139 | #define BUD_USUB_TO_PEND(sub, ask) ((!g_strcasecmp((sub), "to") || !g_strcasecmp((sub), "both")) && \ |
---|
1140 | (ask) != NULL && !g_strcasecmp((ask), "unsubscribe")) |
---|
1141 | |
---|
1142 | /* |
---|
1143 | * Unsubscribed to a buddy? |
---|
1144 | */ |
---|
1145 | #define BUD_USUBD_TO(sub, ask) ((!g_strcasecmp((sub), "none") || !g_strcasecmp((sub), "from")) && \ |
---|
1146 | ((ask) == NULL || !g_strcasecmp((ask), "unsubscribe"))) |
---|
1147 | |
---|
1148 | /* |
---|
1149 | * If a buddy is added or removed from the roster on another resource |
---|
1150 | * jabber_handlebuddy is called |
---|
1151 | * |
---|
1152 | * Called with roster item node. |
---|
1153 | */ |
---|
1154 | static void jabber_handlebuddy(gjconn gjc, xmlnode x) |
---|
1155 | { |
---|
1156 | xmlnode g; |
---|
1157 | char *Jid, *name, *sub, *ask; |
---|
1158 | jid who; |
---|
1159 | struct buddy *b = NULL; |
---|
1160 | char *buddyname, *groupname = NULL; |
---|
1161 | |
---|
1162 | Jid = xmlnode_get_attrib(x, "jid"); |
---|
1163 | name = xmlnode_get_attrib(x, "name"); |
---|
1164 | sub = xmlnode_get_attrib(x, "subscription"); |
---|
1165 | ask = xmlnode_get_attrib(x, "ask"); |
---|
1166 | who = jid_new(gjc->p, Jid); |
---|
1167 | |
---|
1168 | /* JFIXME: jabber_handleroster() had a "FIXME: transport" at this |
---|
1169 | * equivilent point. So... |
---|
1170 | * |
---|
1171 | * We haven't allocated any memory or done anything interesting to |
---|
1172 | * this point, so we'll violate Good Coding Structure here by |
---|
1173 | * simply bailing out. |
---|
1174 | */ |
---|
1175 | if (!who || !who->user) { |
---|
1176 | return; |
---|
1177 | } |
---|
1178 | |
---|
1179 | buddyname = g_strdup_printf("%s@%s", who->user, who->server); |
---|
1180 | |
---|
1181 | if((g = xmlnode_get_tag(x, "group")) != NULL) { |
---|
1182 | groupname = xmlnode_get_data(g); |
---|
1183 | } |
---|
1184 | |
---|
1185 | /* |
---|
1186 | * Add or remove a buddy? Change buddy's alias or group? |
---|
1187 | */ |
---|
1188 | if (BUD_SUB_TO_PEND(sub, ask) || BUD_SUBD_TO(sub, ask)) { |
---|
1189 | if ((b = find_buddy(GJ_GC(gjc), buddyname)) == NULL) { |
---|
1190 | add_buddy(GJ_GC(gjc), groupname ? groupname : _("Buddies"), buddyname, |
---|
1191 | name ? name : buddyname); |
---|
1192 | } else { |
---|
1193 | /* struct group *c_grp = find_group_by_buddy(GJ_GC(gjc), buddyname); */ |
---|
1194 | |
---|
1195 | /* |
---|
1196 | * If the buddy's in a new group or his/her alias is changed... |
---|
1197 | */ |
---|
1198 | if(groupname) { |
---|
1199 | int present = b->present; /* save presence state */ |
---|
1200 | int uc = b->uc; /* and away state (?) */ |
---|
1201 | int idle = b->idle; |
---|
1202 | int signon = b->signon; |
---|
1203 | |
---|
1204 | /* |
---|
1205 | * seems rude, but it seems to be the only way... |
---|
1206 | */ |
---|
1207 | /* remove_buddy(GJ_GC(gjc), c_grp, b); */ |
---|
1208 | jabber_remove_buddy(GJ_GC(gjc), b->name, JABBER_GROUP); |
---|
1209 | |
---|
1210 | add_buddy(GJ_GC(gjc), groupname, buddyname, |
---|
1211 | name ? name : buddyname); |
---|
1212 | if(present) { |
---|
1213 | serv_got_update(GJ_GC(gjc), buddyname, 1, 0, signon, idle, uc, 0); |
---|
1214 | } |
---|
1215 | } else if(name != NULL && strcmp(b->show, name)) { |
---|
1216 | strncpy(b->show, name, BUDDY_ALIAS_MAXLEN); |
---|
1217 | b->show[BUDDY_ALIAS_MAXLEN - 1] = '\0'; /* cheap safety feature */ |
---|
1218 | serv_buddy_rename(GJ_GC(gjc), buddyname, b->show); |
---|
1219 | } |
---|
1220 | } |
---|
1221 | } else if (BUD_USUB_TO_PEND(sub, ask) || BUD_USUBD_TO(sub, ask) || !g_strcasecmp(sub, "remove")) { |
---|
1222 | jabber_remove_gaim_buddy(GJ_GC(gjc), buddyname); |
---|
1223 | } |
---|
1224 | g_free(buddyname); |
---|
1225 | |
---|
1226 | } |
---|
1227 | |
---|
1228 | static void jabber_handleroster(gjconn gjc, xmlnode querynode) |
---|
1229 | { |
---|
1230 | xmlnode x; |
---|
1231 | |
---|
1232 | x = xmlnode_get_firstchild(querynode); |
---|
1233 | while (x) { |
---|
1234 | jabber_handlebuddy(gjc, x); |
---|
1235 | x = xmlnode_get_nextsibling(x); |
---|
1236 | } |
---|
1237 | |
---|
1238 | account_online(GJ_GC(gjc)); |
---|
1239 | } |
---|
1240 | |
---|
1241 | static void jabber_handleauthresp(gjconn gjc, jpacket p) |
---|
1242 | { |
---|
1243 | if (jpacket_subtype(p) == JPACKET__RESULT) { |
---|
1244 | if (xmlnode_has_children(p->x)) { |
---|
1245 | xmlnode query = xmlnode_get_tag(p->x, "query"); |
---|
1246 | set_login_progress(GJ_GC(gjc), 4, _("Authenticating")); |
---|
1247 | if (!xmlnode_get_tag(query, "digest")) { |
---|
1248 | g_free(gjc->sid); |
---|
1249 | gjc->sid = NULL; |
---|
1250 | } |
---|
1251 | gjab_auth(gjc); |
---|
1252 | } else { |
---|
1253 | gjab_reqroster(gjc); |
---|
1254 | |
---|
1255 | ((struct jabber_data *)GJ_GC(gjc)->proto_data)->did_import = TRUE; |
---|
1256 | } |
---|
1257 | } else { |
---|
1258 | xmlnode xerr; |
---|
1259 | char *errmsg = NULL; |
---|
1260 | int errcode = 0; |
---|
1261 | struct jabber_data *jd = GJ_GC(gjc)->proto_data; |
---|
1262 | |
---|
1263 | xerr = xmlnode_get_tag(p->x, "error"); |
---|
1264 | if (xerr) { |
---|
1265 | char msg[BUF_LONG]; |
---|
1266 | errmsg = xmlnode_get_data(xerr); |
---|
1267 | if (xmlnode_get_attrib(xerr, "code")) { |
---|
1268 | errcode = atoi(xmlnode_get_attrib(xerr, "code")); |
---|
1269 | g_snprintf(msg, sizeof(msg), "Error %d: %s", errcode, errmsg ? errmsg : "Unknown error"); |
---|
1270 | } else |
---|
1271 | g_snprintf(msg, sizeof(msg), "%s", errmsg); |
---|
1272 | hide_login_progress(GJ_GC(gjc), msg); |
---|
1273 | } else { |
---|
1274 | hide_login_progress(GJ_GC(gjc), _("Unknown login error")); |
---|
1275 | } |
---|
1276 | |
---|
1277 | jd->die = TRUE; |
---|
1278 | } |
---|
1279 | } |
---|
1280 | |
---|
1281 | static void jabber_handleversion(gjconn gjc, xmlnode iqnode) { |
---|
1282 | xmlnode querynode, x; |
---|
1283 | char *id, *from; |
---|
1284 | char os[1024]; |
---|
1285 | #ifndef _WIN32 |
---|
1286 | struct utsname osinfo; |
---|
1287 | |
---|
1288 | uname(&osinfo); |
---|
1289 | g_snprintf(os, sizeof os, "%s %s %s", osinfo.sysname, osinfo.release, osinfo.machine); |
---|
1290 | #else |
---|
1291 | g_snprintf(os, sizeof os, "Windows %d %d", _winmajor, _winminor); |
---|
1292 | #endif |
---|
1293 | |
---|
1294 | |
---|
1295 | id = xmlnode_get_attrib(iqnode, "id"); |
---|
1296 | from = xmlnode_get_attrib(iqnode, "from"); |
---|
1297 | |
---|
1298 | x = jutil_iqnew(JPACKET__RESULT, NS_VERSION); |
---|
1299 | |
---|
1300 | xmlnode_put_attrib(x, "to", from); |
---|
1301 | xmlnode_put_attrib(x, "id", id); |
---|
1302 | querynode = xmlnode_get_tag(x, "query"); |
---|
1303 | xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "name"), PACKAGE, -1); |
---|
1304 | xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "version"), BITLBEE_VERSION, -1); |
---|
1305 | xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "os"), os, -1); |
---|
1306 | |
---|
1307 | gjab_send(gjc, x); |
---|
1308 | |
---|
1309 | xmlnode_free(x); |
---|
1310 | } |
---|
1311 | |
---|
1312 | static void jabber_handletime(gjconn gjc, xmlnode iqnode) { |
---|
1313 | xmlnode querynode, x; |
---|
1314 | char *id, *from; |
---|
1315 | time_t now_t; |
---|
1316 | struct tm *now; |
---|
1317 | char buf[1024]; |
---|
1318 | |
---|
1319 | time(&now_t); |
---|
1320 | now = localtime(&now_t); |
---|
1321 | |
---|
1322 | id = xmlnode_get_attrib(iqnode, "id"); |
---|
1323 | from = xmlnode_get_attrib(iqnode, "from"); |
---|
1324 | |
---|
1325 | x = jutil_iqnew(JPACKET__RESULT, NS_TIME); |
---|
1326 | |
---|
1327 | xmlnode_put_attrib(x, "to", from); |
---|
1328 | xmlnode_put_attrib(x, "id", id); |
---|
1329 | querynode = xmlnode_get_tag(x, "query"); |
---|
1330 | |
---|
1331 | strftime(buf, 1024, "%Y%m%dT%T", now); |
---|
1332 | xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "utc"), buf, -1); |
---|
1333 | strftime(buf, 1024, "%Z", now); |
---|
1334 | xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "tz"), buf, -1); |
---|
1335 | strftime(buf, 1024, "%d %b %Y %T", now); |
---|
1336 | xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "display"), buf, -1); |
---|
1337 | |
---|
1338 | gjab_send(gjc, x); |
---|
1339 | |
---|
1340 | xmlnode_free(x); |
---|
1341 | } |
---|
1342 | |
---|
1343 | static void jabber_handlelast(gjconn gjc, xmlnode iqnode) { |
---|
1344 | xmlnode x, querytag; |
---|
1345 | char *id, *from; |
---|
1346 | struct jabber_data *jd = GJ_GC(gjc)->proto_data; |
---|
1347 | char idle_time[32]; |
---|
1348 | |
---|
1349 | id = xmlnode_get_attrib(iqnode, "id"); |
---|
1350 | from = xmlnode_get_attrib(iqnode, "from"); |
---|
1351 | |
---|
1352 | x = jutil_iqnew(JPACKET__RESULT, "jabber:iq:last"); |
---|
1353 | |
---|
1354 | xmlnode_put_attrib(x, "to", from); |
---|
1355 | xmlnode_put_attrib(x, "id", id); |
---|
1356 | querytag = xmlnode_get_tag(x, "query"); |
---|
1357 | g_snprintf(idle_time, sizeof idle_time, "%ld", jd->idle ? time(NULL) - jd->idle : 0); |
---|
1358 | xmlnode_put_attrib(querytag, "seconds", idle_time); |
---|
1359 | |
---|
1360 | gjab_send(gjc, x); |
---|
1361 | xmlnode_free(x); |
---|
1362 | } |
---|
1363 | |
---|
1364 | /* |
---|
1365 | * delete == TRUE: delete found entry |
---|
1366 | * |
---|
1367 | * returns pointer to (local) copy of value if found, NULL otherwise |
---|
1368 | * |
---|
1369 | * Note: non-reentrant! Local static storage re-used on subsequent calls. |
---|
1370 | * If you're going to need to keep the returned value, make a copy! |
---|
1371 | */ |
---|
1372 | static gchar *jabber_track_queries(GHashTable *queries, gchar *key, gboolean delete) |
---|
1373 | { |
---|
1374 | gpointer my_key, my_val; |
---|
1375 | static gchar *ret_val = NULL; |
---|
1376 | |
---|
1377 | if(ret_val != NULL) { |
---|
1378 | g_free(ret_val); |
---|
1379 | ret_val = NULL; |
---|
1380 | } |
---|
1381 | |
---|
1382 | /* self-protection */ |
---|
1383 | if(queries != NULL && key != NULL) { |
---|
1384 | if(g_hash_table_lookup_extended(queries, key, &my_key, &my_val)) { |
---|
1385 | ret_val = g_strdup((gchar *) my_val); |
---|
1386 | if(delete) { |
---|
1387 | g_hash_table_remove(queries, key); |
---|
1388 | g_free(my_key); |
---|
1389 | g_free(my_val); |
---|
1390 | } |
---|
1391 | } |
---|
1392 | } |
---|
1393 | |
---|
1394 | return(ret_val); |
---|
1395 | } |
---|
1396 | |
---|
1397 | static void jabber_handlepacket(gjconn gjc, jpacket p) |
---|
1398 | { |
---|
1399 | char *id; |
---|
1400 | switch (p->type) { |
---|
1401 | case JPACKET_MESSAGE: |
---|
1402 | jabber_handlemessage(gjc, p); |
---|
1403 | break; |
---|
1404 | case JPACKET_PRESENCE: |
---|
1405 | jabber_handlepresence(gjc, p); |
---|
1406 | break; |
---|
1407 | case JPACKET_IQ: |
---|
1408 | id = xmlnode_get_attrib(p->x, "id"); |
---|
1409 | if (id != NULL && !strcmp(id, IQID_AUTH)) { |
---|
1410 | jabber_handleauthresp(gjc, p); |
---|
1411 | break; |
---|
1412 | } |
---|
1413 | |
---|
1414 | if (jpacket_subtype(p) == JPACKET__SET) { |
---|
1415 | xmlnode querynode; |
---|
1416 | querynode = xmlnode_get_tag(p->x, "query"); |
---|
1417 | if (NSCHECK(querynode, "jabber:iq:roster")) { |
---|
1418 | jabber_handlebuddy(gjc, xmlnode_get_firstchild(querynode)); |
---|
1419 | } |
---|
1420 | } else if (jpacket_subtype(p) == JPACKET__GET) { |
---|
1421 | xmlnode querynode; |
---|
1422 | querynode = xmlnode_get_tag(p->x, "query"); |
---|
1423 | if (NSCHECK(querynode, NS_VERSION)) { |
---|
1424 | jabber_handleversion(gjc, p->x); |
---|
1425 | } else if (NSCHECK(querynode, NS_TIME)) { |
---|
1426 | jabber_handletime(gjc, p->x); |
---|
1427 | } else if (NSCHECK(querynode, "jabber:iq:last")) { |
---|
1428 | jabber_handlelast(gjc, p->x); |
---|
1429 | } |
---|
1430 | } else if (jpacket_subtype(p) == JPACKET__RESULT) { |
---|
1431 | xmlnode querynode, vcard; |
---|
1432 | /* char *xmlns; */ |
---|
1433 | char *from; |
---|
1434 | |
---|
1435 | /* |
---|
1436 | * TBD: ISTM maybe this part could use a serious re-work? |
---|
1437 | */ |
---|
1438 | from = xmlnode_get_attrib(p->x, "from"); |
---|
1439 | querynode = xmlnode_get_tag(p->x, "query"); |
---|
1440 | vcard = xmlnode_get_tag(p->x, "vCard"); |
---|
1441 | if (!vcard) |
---|
1442 | vcard = xmlnode_get_tag(p->x, "VCARD"); |
---|
1443 | |
---|
1444 | if (NSCHECK(querynode, NS_ROSTER)) { |
---|
1445 | jabber_handleroster(gjc, querynode); |
---|
1446 | } else if (NSCHECK(querynode, NS_VCARD)) { |
---|
1447 | jabber_track_queries(gjc->queries, id, TRUE); /* delete query track */ |
---|
1448 | jabber_handlevcard(gjc, querynode, from); |
---|
1449 | } else if (vcard) { |
---|
1450 | jabber_track_queries(gjc->queries, id, TRUE); /* delete query track */ |
---|
1451 | jabber_handlevcard(gjc, vcard, from); |
---|
1452 | } else { |
---|
1453 | char *val; |
---|
1454 | |
---|
1455 | /* handle "null" query results */ |
---|
1456 | if((val = jabber_track_queries(gjc->queries, id, TRUE)) != NULL) { |
---|
1457 | if (!g_strncasecmp(val, "vcard", 5)) { |
---|
1458 | jabber_handlevcard(gjc, NULL, from); |
---|
1459 | } |
---|
1460 | |
---|
1461 | /* No-op */ |
---|
1462 | } |
---|
1463 | } |
---|
1464 | |
---|
1465 | } else if (jpacket_subtype(p) == JPACKET__ERROR) { |
---|
1466 | xmlnode xerr; |
---|
1467 | char *from, *errmsg = NULL; |
---|
1468 | int errcode = 0; |
---|
1469 | |
---|
1470 | from = xmlnode_get_attrib(p->x, "from"); |
---|
1471 | xerr = xmlnode_get_tag(p->x, "error"); |
---|
1472 | if (xerr) { |
---|
1473 | errmsg = xmlnode_get_data(xerr); |
---|
1474 | if (xmlnode_get_attrib(xerr, "code")) |
---|
1475 | errcode = atoi(xmlnode_get_attrib(xerr, "code")); |
---|
1476 | } |
---|
1477 | |
---|
1478 | from = g_strdup_printf("Error %d (%s)", errcode, from); |
---|
1479 | do_error_dialog(GJ_GC(gjc), errmsg, from); |
---|
1480 | g_free(from); |
---|
1481 | |
---|
1482 | } |
---|
1483 | |
---|
1484 | break; |
---|
1485 | case JPACKET_S10N: |
---|
1486 | jabber_handles10n(gjc, p); |
---|
1487 | break; |
---|
1488 | } |
---|
1489 | |
---|
1490 | xmlnode_free(p->x); |
---|
1491 | |
---|
1492 | return; |
---|
1493 | } |
---|
1494 | |
---|
1495 | static void jabber_handlestate(gjconn gjc, int state) |
---|
1496 | { |
---|
1497 | switch (state) { |
---|
1498 | case JCONN_STATE_OFF: |
---|
1499 | if(gjc->was_connected) { |
---|
1500 | hide_login_progress_error(GJ_GC(gjc), _("Connection lost")); |
---|
1501 | } else { |
---|
1502 | hide_login_progress(GJ_GC(gjc), _("Unable to connect")); |
---|
1503 | } |
---|
1504 | signoff(GJ_GC(gjc)); |
---|
1505 | break; |
---|
1506 | case JCONN_STATE_CONNECTED: |
---|
1507 | gjc->was_connected = 1; |
---|
1508 | set_login_progress(GJ_GC(gjc), 2, _("Connected")); |
---|
1509 | break; |
---|
1510 | case JCONN_STATE_ON: |
---|
1511 | set_login_progress(GJ_GC(gjc), 3, _("Requesting Authentication Method")); |
---|
1512 | gjab_reqauth(gjc); |
---|
1513 | break; |
---|
1514 | } |
---|
1515 | return; |
---|
1516 | } |
---|
1517 | |
---|
1518 | static void jabber_login(account_t *acc) |
---|
1519 | { |
---|
1520 | struct gaim_connection *gc = new_gaim_conn(acc); |
---|
1521 | struct jabber_data *jd = gc->proto_data = g_new0(struct jabber_data, 1); |
---|
1522 | char *loginname = create_valid_jid(acc->user, DEFAULT_SERVER, "BitlBee"); |
---|
1523 | |
---|
1524 | jd->hash = g_hash_table_new(g_str_hash, g_str_equal); |
---|
1525 | jd->chats = NULL; /* we have no chats yet */ |
---|
1526 | |
---|
1527 | set_login_progress(gc, 1, _("Connecting")); |
---|
1528 | |
---|
1529 | if (!(jd->gjc = gjab_new(loginname, acc->pass, gc))) { |
---|
1530 | g_free(loginname); |
---|
1531 | hide_login_progress(gc, _("Unable to connect")); |
---|
1532 | signoff(gc); |
---|
1533 | return; |
---|
1534 | } |
---|
1535 | |
---|
1536 | g_free(loginname); |
---|
1537 | gjab_state_handler(jd->gjc, jabber_handlestate); |
---|
1538 | gjab_packet_handler(jd->gjc, jabber_handlepacket); |
---|
1539 | jd->gjc->queries = g_hash_table_new(g_str_hash, g_str_equal); |
---|
1540 | gjab_start(jd->gjc); |
---|
1541 | } |
---|
1542 | |
---|
1543 | static gboolean jabber_destroy_hash(gpointer key, gpointer val, gpointer data) { |
---|
1544 | g_free(key); |
---|
1545 | g_free(val); |
---|
1546 | return TRUE; |
---|
1547 | } |
---|
1548 | |
---|
1549 | static gboolean jabber_free(gpointer data, gint fd, b_input_condition cond) |
---|
1550 | { |
---|
1551 | struct jabber_data *jd = data; |
---|
1552 | |
---|
1553 | if(jd->gjc != NULL) { |
---|
1554 | gjab_delete(jd->gjc); |
---|
1555 | /* YAY for modules with their own memory pool managers!... |
---|
1556 | g_free(jd->gjc->sid); |
---|
1557 | And a less sarcastic yay for valgrind. :-) */ |
---|
1558 | jd->gjc = NULL; |
---|
1559 | } |
---|
1560 | g_free(jd); |
---|
1561 | |
---|
1562 | return FALSE; |
---|
1563 | } |
---|
1564 | |
---|
1565 | static void jabber_close(struct gaim_connection *gc) |
---|
1566 | { |
---|
1567 | struct jabber_data *jd = gc->proto_data; |
---|
1568 | |
---|
1569 | if(jd) { |
---|
1570 | GSList *jcs = jd->chats; |
---|
1571 | |
---|
1572 | /* Free-up the jabber_chat struct allocs and the list */ |
---|
1573 | while (jcs) { |
---|
1574 | g_free(jcs->data); |
---|
1575 | jcs = jcs->next; |
---|
1576 | } |
---|
1577 | g_slist_free(jd->chats); |
---|
1578 | |
---|
1579 | /* Free-up the away status memories and the list */ |
---|
1580 | if(jd->hash != NULL) { |
---|
1581 | g_hash_table_foreach_remove(jd->hash, jabber_destroy_hash, NULL); |
---|
1582 | g_hash_table_destroy(jd->hash); |
---|
1583 | jd->hash = NULL; |
---|
1584 | } |
---|
1585 | |
---|
1586 | /* Free-up the pending queries memories and the list */ |
---|
1587 | if(jd->gjc != NULL && jd->gjc->queries != NULL) { |
---|
1588 | g_hash_table_foreach_remove(jd->gjc->queries, jabber_destroy_hash, NULL); |
---|
1589 | g_hash_table_destroy(jd->gjc->queries); |
---|
1590 | jd->gjc->queries = NULL; |
---|
1591 | } |
---|
1592 | } |
---|
1593 | if (gc->inpa) |
---|
1594 | b_event_remove(gc->inpa); |
---|
1595 | |
---|
1596 | if(jd) { |
---|
1597 | b_timeout_add(50, jabber_free, jd); |
---|
1598 | if(jd->gjc != NULL) |
---|
1599 | xmlnode_free(jd->gjc->current); |
---|
1600 | } |
---|
1601 | gc->proto_data = NULL; |
---|
1602 | } |
---|
1603 | |
---|
1604 | static int jabber_send_im(struct gaim_connection *gc, char *who, char *message, int len, int flags) |
---|
1605 | { |
---|
1606 | xmlnode x, y; |
---|
1607 | char *realwho; |
---|
1608 | gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; |
---|
1609 | |
---|
1610 | if (!who || !message) |
---|
1611 | return 0; |
---|
1612 | |
---|
1613 | x = xmlnode_new_tag("message"); |
---|
1614 | /* Bare username and "username" not the server itself? */ |
---|
1615 | if (!strchr(who, '@') && strcmp(who, gjc->user->server) != 0) |
---|
1616 | realwho = g_strdup_printf("%s@%s", who, gjc->user->server); |
---|
1617 | else |
---|
1618 | realwho = g_strdup(who); |
---|
1619 | xmlnode_put_attrib(x, "to", realwho); |
---|
1620 | g_free(realwho); |
---|
1621 | |
---|
1622 | xmlnode_insert_tag(x, "bitlbee"); |
---|
1623 | xmlnode_put_attrib(x, "type", "chat"); |
---|
1624 | |
---|
1625 | if (message && strlen(message)) { |
---|
1626 | y = xmlnode_insert_tag(x, "body"); |
---|
1627 | xmlnode_insert_cdata(y, message, -1); |
---|
1628 | } |
---|
1629 | |
---|
1630 | gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); |
---|
1631 | xmlnode_free(x); |
---|
1632 | return 1; |
---|
1633 | } |
---|
1634 | |
---|
1635 | /* |
---|
1636 | * Add/update buddy's roster entry on server |
---|
1637 | */ |
---|
1638 | static void jabber_roster_update(struct gaim_connection *gc, char *name) |
---|
1639 | { |
---|
1640 | xmlnode x, y; |
---|
1641 | char *realwho; |
---|
1642 | gjconn gjc; |
---|
1643 | struct buddy *buddy = NULL; |
---|
1644 | /* struct group *buddy_group = NULL; */ |
---|
1645 | |
---|
1646 | if(gc && gc->proto_data && ((struct jabber_data *)gc->proto_data)->gjc && name) { |
---|
1647 | gjc = ((struct jabber_data *)gc->proto_data)->gjc; |
---|
1648 | |
---|
1649 | if (!strchr(name, '@')) |
---|
1650 | realwho = g_strdup_printf("%s@%s", name, gjc->user->server); |
---|
1651 | else { |
---|
1652 | jid who = jid_new(gjc->p, name); |
---|
1653 | if (who->user == NULL) { |
---|
1654 | /* FIXME: transport */ |
---|
1655 | return; |
---|
1656 | } |
---|
1657 | realwho = g_strdup_printf("%s@%s", who->user, who->server); |
---|
1658 | } |
---|
1659 | |
---|
1660 | |
---|
1661 | x = jutil_iqnew(JPACKET__SET, NS_ROSTER); |
---|
1662 | y = xmlnode_insert_tag(xmlnode_get_tag(x, "query"), "item"); |
---|
1663 | xmlnode_put_attrib(y, "jid", realwho); |
---|
1664 | |
---|
1665 | |
---|
1666 | /* If we can find the buddy, there's an alias for him, it's not 0-length |
---|
1667 | * and it doesn't match his JID, add the "name" attribute. |
---|
1668 | */ |
---|
1669 | if((buddy = find_buddy(gc, realwho)) != NULL && |
---|
1670 | buddy->show != NULL && buddy->show[0] != '\0' && strcmp(realwho, buddy->show)) { |
---|
1671 | |
---|
1672 | xmlnode_put_attrib(y, "name", buddy->show); |
---|
1673 | } |
---|
1674 | |
---|
1675 | /* |
---|
1676 | * Find out what group the buddy's in and send that along |
---|
1677 | * with the roster item. |
---|
1678 | */ |
---|
1679 | /* ** Bitlbee disabled ** |
---|
1680 | if((buddy_group = NULL) != NULL) { |
---|
1681 | xmlnode z; |
---|
1682 | z = xmlnode_insert_tag(y, "group"); |
---|
1683 | xmlnode_insert_cdata(z, buddy_group->name, -1); |
---|
1684 | } |
---|
1685 | ** End - Bitlbee ** */ |
---|
1686 | |
---|
1687 | gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); |
---|
1688 | |
---|
1689 | xmlnode_free(x); |
---|
1690 | g_free(realwho); |
---|
1691 | } |
---|
1692 | } |
---|
1693 | |
---|
1694 | /* |
---|
1695 | * Change buddy's group on server roster |
---|
1696 | */ |
---|
1697 | static void jabber_group_change(struct gaim_connection *gc, char *name, char *old_group, char *new_group) |
---|
1698 | { |
---|
1699 | if(strcmp(old_group, new_group)) { |
---|
1700 | jabber_roster_update(gc, name); |
---|
1701 | } |
---|
1702 | } |
---|
1703 | |
---|
1704 | static void jabber_add_buddy(struct gaim_connection *gc, char *name) |
---|
1705 | { |
---|
1706 | xmlnode x; |
---|
1707 | char *realwho; |
---|
1708 | gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; |
---|
1709 | |
---|
1710 | if (!((struct jabber_data *)gc->proto_data)->did_import) |
---|
1711 | return; |
---|
1712 | |
---|
1713 | if (!name) |
---|
1714 | return; |
---|
1715 | |
---|
1716 | if (!strcmp(gc->username, name)) |
---|
1717 | return; |
---|
1718 | |
---|
1719 | if (!strchr(name, '@')) |
---|
1720 | realwho = g_strdup_printf("%s@%s", name, gjc->user->server); |
---|
1721 | else { |
---|
1722 | jid who; |
---|
1723 | |
---|
1724 | if((who = jid_new(gjc->p, name)) == NULL) { |
---|
1725 | char *msg = g_strdup_printf("%s: \"%s\"", _("Invalid Jabber I.D."), name); |
---|
1726 | do_error_dialog(GJ_GC(gjc), msg, _("Jabber Error")); |
---|
1727 | g_free(msg); |
---|
1728 | jabber_remove_gaim_buddy(gc, name); |
---|
1729 | return; |
---|
1730 | } |
---|
1731 | if (who->user == NULL) { |
---|
1732 | /* FIXME: transport */ |
---|
1733 | return; |
---|
1734 | } |
---|
1735 | realwho = g_strdup_printf("%s@%s", who->user, who->server); |
---|
1736 | } |
---|
1737 | |
---|
1738 | x = xmlnode_new_tag("presence"); |
---|
1739 | xmlnode_put_attrib(x, "to", realwho); |
---|
1740 | xmlnode_put_attrib(x, "type", "subscribe"); |
---|
1741 | gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); |
---|
1742 | xmlnode_free(x); |
---|
1743 | |
---|
1744 | jabber_roster_update(gc, realwho); |
---|
1745 | |
---|
1746 | g_free(realwho); |
---|
1747 | } |
---|
1748 | |
---|
1749 | static void jabber_remove_buddy(struct gaim_connection *gc, char *name, char *group) |
---|
1750 | { |
---|
1751 | xmlnode x; |
---|
1752 | char *realwho; |
---|
1753 | gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; |
---|
1754 | |
---|
1755 | if (!name) |
---|
1756 | return; |
---|
1757 | |
---|
1758 | if (!strchr(name, '@')) |
---|
1759 | realwho = g_strdup_printf("%s@%s", name, gjc->user->server); |
---|
1760 | else |
---|
1761 | realwho = g_strdup(name); |
---|
1762 | |
---|
1763 | x = xmlnode_new_tag("presence"); |
---|
1764 | xmlnode_put_attrib(x, "to", realwho); |
---|
1765 | xmlnode_put_attrib(x, "type", "unsubscribe"); |
---|
1766 | gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); |
---|
1767 | g_free(realwho); |
---|
1768 | xmlnode_free(x); |
---|
1769 | } |
---|
1770 | |
---|
1771 | static void jabber_get_info(struct gaim_connection *gc, char *who) { |
---|
1772 | xmlnode x; |
---|
1773 | char *id; |
---|
1774 | char *realwho; |
---|
1775 | struct jabber_data *jd = gc->proto_data; |
---|
1776 | gjconn gjc = jd->gjc; |
---|
1777 | |
---|
1778 | x = jutil_iqnew(JPACKET__GET, NS_VCARD); |
---|
1779 | /* Bare username? */ |
---|
1780 | if (!strchr(who, '@')) { |
---|
1781 | realwho = g_strdup_printf("%s@%s", who, gjc->user->server); |
---|
1782 | } else { |
---|
1783 | realwho = g_strdup(who); |
---|
1784 | } |
---|
1785 | xmlnode_put_attrib(x, "to", realwho); |
---|
1786 | g_free(realwho); |
---|
1787 | |
---|
1788 | id = gjab_getid(gjc); |
---|
1789 | xmlnode_put_attrib(x, "id", id); |
---|
1790 | |
---|
1791 | g_hash_table_insert(jd->gjc->queries, g_strdup(id), g_strdup("vCard")); |
---|
1792 | |
---|
1793 | gjab_send(gjc, x); |
---|
1794 | |
---|
1795 | xmlnode_free(x); |
---|
1796 | |
---|
1797 | } |
---|
1798 | |
---|
1799 | static void jabber_get_away_msg(struct gaim_connection *gc, char *who) { |
---|
1800 | struct jabber_data *jd = gc->proto_data; |
---|
1801 | gjconn gjc = jd->gjc; |
---|
1802 | char *status; |
---|
1803 | |
---|
1804 | /* space for all elements: Jabber I.D. + "status" + NULL (list terminator) */ |
---|
1805 | gchar **str_arr = (gchar **) g_new(gpointer, 3); |
---|
1806 | gchar **ap = str_arr; |
---|
1807 | gchar *realwho, *final; |
---|
1808 | |
---|
1809 | /* Bare username? */ |
---|
1810 | if (!strchr(who, '@')) { |
---|
1811 | realwho = g_strdup_printf("%s@%s", who, gjc->user->server); |
---|
1812 | } else { |
---|
1813 | realwho = g_strdup(who); |
---|
1814 | } |
---|
1815 | *ap++ = g_strdup_printf("<B>Jabber ID:</B> %s<BR>\n", realwho); |
---|
1816 | |
---|
1817 | if((status = g_hash_table_lookup(jd->hash, realwho)) == NULL) { |
---|
1818 | status = _("Unknown"); |
---|
1819 | } |
---|
1820 | *ap++ = g_strdup_printf("<B>Status:</B> %s<BR>\n", status); |
---|
1821 | |
---|
1822 | *ap = NULL; |
---|
1823 | |
---|
1824 | final= g_strjoinv(NULL, str_arr); |
---|
1825 | g_strfreev(str_arr); |
---|
1826 | |
---|
1827 | g_free(realwho); |
---|
1828 | g_free(final); |
---|
1829 | |
---|
1830 | } |
---|
1831 | |
---|
1832 | static GList *jabber_away_states(struct gaim_connection *gc) { |
---|
1833 | GList *m = NULL; |
---|
1834 | |
---|
1835 | m = g_list_append(m, "Online"); |
---|
1836 | m = g_list_append(m, "Chatty"); |
---|
1837 | m = g_list_append(m, "Away"); |
---|
1838 | m = g_list_append(m, "Extended Away"); |
---|
1839 | m = g_list_append(m, "Do Not Disturb"); |
---|
1840 | |
---|
1841 | return m; |
---|
1842 | } |
---|
1843 | |
---|
1844 | static void jabber_set_away(struct gaim_connection *gc, char *state, char *message) |
---|
1845 | { |
---|
1846 | xmlnode x, y; |
---|
1847 | struct jabber_data *jd = gc->proto_data; |
---|
1848 | gjconn gjc = jd->gjc; |
---|
1849 | |
---|
1850 | gc->away = NULL; /* never send an auto-response */ |
---|
1851 | |
---|
1852 | x = xmlnode_new_tag("presence"); |
---|
1853 | |
---|
1854 | if (!strcmp(state, GAIM_AWAY_CUSTOM)) { |
---|
1855 | /* oh goody. Gaim is telling us what to do. */ |
---|
1856 | if (message) { |
---|
1857 | /* Gaim wants us to be away */ |
---|
1858 | y = xmlnode_insert_tag(x, "show"); |
---|
1859 | xmlnode_insert_cdata(y, "away", -1); |
---|
1860 | y = xmlnode_insert_tag(x, "status"); |
---|
1861 | xmlnode_insert_cdata(y, message, -1); |
---|
1862 | gc->away = ""; |
---|
1863 | } else { |
---|
1864 | /* Gaim wants us to not be away */ |
---|
1865 | /* but for Jabber, we can just send presence with no other information. */ |
---|
1866 | } |
---|
1867 | } else { |
---|
1868 | /* state is one of our own strings. it won't be NULL. */ |
---|
1869 | if (!g_strcasecmp(state, "Online")) { |
---|
1870 | /* once again, we don't have to put anything here */ |
---|
1871 | } else if (!g_strcasecmp(state, "Chatty")) { |
---|
1872 | y = xmlnode_insert_tag(x, "show"); |
---|
1873 | xmlnode_insert_cdata(y, "chat", -1); |
---|
1874 | } else if (!g_strcasecmp(state, "Away")) { |
---|
1875 | y = xmlnode_insert_tag(x, "show"); |
---|
1876 | xmlnode_insert_cdata(y, "away", -1); |
---|
1877 | gc->away = ""; |
---|
1878 | } else if (!g_strcasecmp(state, "Extended Away")) { |
---|
1879 | y = xmlnode_insert_tag(x, "show"); |
---|
1880 | xmlnode_insert_cdata(y, "xa", -1); |
---|
1881 | gc->away = ""; |
---|
1882 | } else if (!g_strcasecmp(state, "Do Not Disturb")) { |
---|
1883 | y = xmlnode_insert_tag(x, "show"); |
---|
1884 | xmlnode_insert_cdata(y, "dnd", -1); |
---|
1885 | gc->away = ""; |
---|
1886 | } |
---|
1887 | } |
---|
1888 | |
---|
1889 | gjab_send(gjc, x); |
---|
1890 | xmlnode_free(x); |
---|
1891 | } |
---|
1892 | |
---|
1893 | static void jabber_keepalive(struct gaim_connection *gc) { |
---|
1894 | struct jabber_data *jd = (struct jabber_data *)gc->proto_data; |
---|
1895 | gjab_send_raw(jd->gjc, " \t "); |
---|
1896 | } |
---|
1897 | |
---|
1898 | /*---------------------------------------*/ |
---|
1899 | /* Jabber "set info" (vCard) support */ |
---|
1900 | /*---------------------------------------*/ |
---|
1901 | |
---|
1902 | /* |
---|
1903 | * V-Card format: |
---|
1904 | * |
---|
1905 | * <vCard prodid='' version='' xmlns=''> |
---|
1906 | * <FN></FN> |
---|
1907 | * <N> |
---|
1908 | * <FAMILY/> |
---|
1909 | * <GIVEN/> |
---|
1910 | * </N> |
---|
1911 | * <NICKNAME/> |
---|
1912 | * <URL/> |
---|
1913 | * <ADR> |
---|
1914 | * <STREET/> |
---|
1915 | * <EXTADD/> |
---|
1916 | * <LOCALITY/> |
---|
1917 | * <REGION/> |
---|
1918 | * <PCODE/> |
---|
1919 | * <COUNTRY/> |
---|
1920 | * </ADR> |
---|
1921 | * <TEL/> |
---|
1922 | * <EMAIL/> |
---|
1923 | * <ORG> |
---|
1924 | * <ORGNAME/> |
---|
1925 | * <ORGUNIT/> |
---|
1926 | * </ORG> |
---|
1927 | * <TITLE/> |
---|
1928 | * <ROLE/> |
---|
1929 | * <DESC/> |
---|
1930 | * <BDAY/> |
---|
1931 | * </vCard> |
---|
1932 | * |
---|
1933 | * See also: |
---|
1934 | * |
---|
1935 | * http://docs.jabber.org/proto/html/vcard-temp.html |
---|
1936 | * http://www.vcard-xml.org/dtd/vCard-XML-v2-20010520.dtd |
---|
1937 | */ |
---|
1938 | |
---|
1939 | /* |
---|
1940 | * Cross-reference user-friendly V-Card entry labels to vCard XML tags |
---|
1941 | * and attributes. |
---|
1942 | * |
---|
1943 | * Order is (or should be) unimportant. For example: we have no way of |
---|
1944 | * knowing in what order real data will arrive. |
---|
1945 | * |
---|
1946 | * Format: Label, Pre-set text, "visible" flag, "editable" flag, XML tag |
---|
1947 | * name, XML tag's parent tag "path" (relative to vCard node). |
---|
1948 | * |
---|
1949 | * List is terminated by a NULL label pointer. |
---|
1950 | * |
---|
1951 | * Entries with no label text, but with XML tag and parent tag |
---|
1952 | * entries, are used by V-Card XML construction routines to |
---|
1953 | * "automagically" construct the appropriate XML node tree. |
---|
1954 | * |
---|
1955 | * Thoughts on future direction/expansion |
---|
1956 | * |
---|
1957 | * This is a "simple" vCard. |
---|
1958 | * |
---|
1959 | * It is possible for nodes other than the "vCard" node to have |
---|
1960 | * attributes. Should that prove necessary/desirable, add an |
---|
1961 | * "attributes" pointer to the vcard_template struct, create the |
---|
1962 | * necessary tag_attr structs, and add 'em to the vcard_dflt_data |
---|
1963 | * array. |
---|
1964 | * |
---|
1965 | * The above changes will (obviously) require changes to the vCard |
---|
1966 | * construction routines. |
---|
1967 | */ |
---|
1968 | |
---|
1969 | static struct vcard_template { |
---|
1970 | char *label; /* label text pointer */ |
---|
1971 | char *text; /* entry text pointer */ |
---|
1972 | int visible; /* should entry field be "visible?" */ |
---|
1973 | int editable; /* should entry field be editable? */ |
---|
1974 | char *tag; /* tag text */ |
---|
1975 | char *ptag; /* parent tag "path" text */ |
---|
1976 | char *url; /* vCard display format if URL */ |
---|
1977 | } vcard_template_data[] = { |
---|
1978 | {N_("Full Name"), NULL, TRUE, TRUE, "FN", NULL, NULL}, |
---|
1979 | {N_("Family Name"), NULL, TRUE, TRUE, "FAMILY", "N", NULL}, |
---|
1980 | {N_("Given Name"), NULL, TRUE, TRUE, "GIVEN", "N", NULL}, |
---|
1981 | {N_("Nickname"), NULL, TRUE, TRUE, "NICKNAME", NULL, NULL}, |
---|
1982 | {N_("URL"), NULL, TRUE, TRUE, "URL", NULL, "<A HREF=\"%s\">%s</A>"}, |
---|
1983 | {N_("Street Address"), NULL, TRUE, TRUE, "STREET", "ADR", NULL}, |
---|
1984 | {N_("Extended Address"), NULL, TRUE, TRUE, "EXTADD", "ADR", NULL}, |
---|
1985 | {N_("Locality"), NULL, TRUE, TRUE, "LOCALITY", "ADR", NULL}, |
---|
1986 | {N_("Region"), NULL, TRUE, TRUE, "REGION", "ADR", NULL}, |
---|
1987 | {N_("Postal Code"), NULL, TRUE, TRUE, "PCODE", "ADR", NULL}, |
---|
1988 | {N_("Country"), NULL, TRUE, TRUE, "COUNTRY", "ADR", NULL}, |
---|
1989 | {N_("Telephone"), NULL, TRUE, TRUE, "TELEPHONE", NULL, NULL}, |
---|
1990 | {N_("Email"), NULL, TRUE, TRUE, "EMAIL", NULL, "<A HREF=\"mailto:%s\">%s</A>"}, |
---|
1991 | {N_("Organization Name"), NULL, TRUE, TRUE, "ORGNAME", "ORG", NULL}, |
---|
1992 | {N_("Organization Unit"), NULL, TRUE, TRUE, "ORGUNIT", "ORG", NULL}, |
---|
1993 | {N_("Title"), NULL, TRUE, TRUE, "TITLE", NULL, NULL}, |
---|
1994 | {N_("Role"), NULL, TRUE, TRUE, "ROLE", NULL, NULL}, |
---|
1995 | {N_("Birthday"), NULL, TRUE, TRUE, "BDAY", NULL, NULL}, |
---|
1996 | {N_("Description"), NULL, TRUE, TRUE, "DESC", NULL, NULL}, |
---|
1997 | {"", NULL, TRUE, TRUE, "N", NULL, NULL}, |
---|
1998 | {"", NULL, TRUE, TRUE, "ADR", NULL, NULL}, |
---|
1999 | {"", NULL, TRUE, TRUE, "ORG", NULL, NULL}, |
---|
2000 | {NULL, NULL, 0, 0, NULL, NULL, NULL} |
---|
2001 | }; |
---|
2002 | |
---|
2003 | /* |
---|
2004 | * Used by routines to parse an XML-encoded string into an xmlnode tree |
---|
2005 | */ |
---|
2006 | typedef struct { |
---|
2007 | XML_Parser parser; |
---|
2008 | xmlnode current; |
---|
2009 | } *xmlstr2xmlnode_parser, xmlstr2xmlnode_parser_struct; |
---|
2010 | |
---|
2011 | |
---|
2012 | /* |
---|
2013 | * Used by XML_Parse on parsing CDATA |
---|
2014 | */ |
---|
2015 | static void xmlstr2xmlnode_charData(void *userdata, const char *s, int slen) |
---|
2016 | { |
---|
2017 | xmlstr2xmlnode_parser xmlp = (xmlstr2xmlnode_parser) userdata; |
---|
2018 | |
---|
2019 | if (xmlp->current) |
---|
2020 | xmlnode_insert_cdata(xmlp->current, s, slen); |
---|
2021 | } |
---|
2022 | |
---|
2023 | /* |
---|
2024 | * Used by XML_Parse to start or append to an xmlnode |
---|
2025 | */ |
---|
2026 | static void xmlstr2xmlnode_startElement(void *userdata, const char *name, const char **attribs) |
---|
2027 | { |
---|
2028 | xmlnode x; |
---|
2029 | xmlstr2xmlnode_parser xmlp = (xmlstr2xmlnode_parser) userdata; |
---|
2030 | |
---|
2031 | if (xmlp->current) { |
---|
2032 | /* Append the node to the current one */ |
---|
2033 | x = xmlnode_insert_tag(xmlp->current, name); |
---|
2034 | xmlnode_put_expat_attribs(x, attribs); |
---|
2035 | |
---|
2036 | xmlp->current = x; |
---|
2037 | } else { |
---|
2038 | x = xmlnode_new_tag(name); |
---|
2039 | xmlnode_put_expat_attribs(x, attribs); |
---|
2040 | xmlp->current = x; |
---|
2041 | } |
---|
2042 | } |
---|
2043 | |
---|
2044 | /* |
---|
2045 | * Used by XML_Parse to end an xmlnode |
---|
2046 | */ |
---|
2047 | static void xmlstr2xmlnode_endElement(void *userdata, const char *name) |
---|
2048 | { |
---|
2049 | xmlstr2xmlnode_parser xmlp = (xmlstr2xmlnode_parser) userdata; |
---|
2050 | xmlnode x; |
---|
2051 | |
---|
2052 | if (xmlp->current != NULL && (x = xmlnode_get_parent(xmlp->current)) != NULL) { |
---|
2053 | xmlp->current = x; |
---|
2054 | } |
---|
2055 | } |
---|
2056 | |
---|
2057 | /* |
---|
2058 | * Parse an XML-encoded string into an xmlnode tree |
---|
2059 | * |
---|
2060 | * Caller is responsible for freeing the returned xmlnode |
---|
2061 | */ |
---|
2062 | static xmlnode xmlstr2xmlnode(char *xmlstring) |
---|
2063 | { |
---|
2064 | xmlstr2xmlnode_parser my_parser = g_new(xmlstr2xmlnode_parser_struct, 1); |
---|
2065 | xmlnode x = NULL; |
---|
2066 | |
---|
2067 | my_parser->parser = XML_ParserCreate(NULL); |
---|
2068 | my_parser->current = NULL; |
---|
2069 | |
---|
2070 | XML_SetUserData(my_parser->parser, (void *)my_parser); |
---|
2071 | XML_SetElementHandler(my_parser->parser, xmlstr2xmlnode_startElement, xmlstr2xmlnode_endElement); |
---|
2072 | XML_SetCharacterDataHandler(my_parser->parser, xmlstr2xmlnode_charData); |
---|
2073 | XML_Parse(my_parser->parser, xmlstring, strlen(xmlstring), 0); |
---|
2074 | |
---|
2075 | x = my_parser->current; |
---|
2076 | |
---|
2077 | XML_ParserFree(my_parser->parser); |
---|
2078 | g_free(my_parser); |
---|
2079 | |
---|
2080 | return(x); |
---|
2081 | } |
---|
2082 | |
---|
2083 | /* |
---|
2084 | * Insert a tag node into an xmlnode tree, recursively inserting parent tag |
---|
2085 | * nodes as necessary |
---|
2086 | * |
---|
2087 | * Returns pointer to inserted node |
---|
2088 | * |
---|
2089 | * Note to hackers: this code is designed to be re-entrant (it's recursive--it |
---|
2090 | * calls itself), so don't put any "static"s in here! |
---|
2091 | */ |
---|
2092 | static xmlnode insert_tag_to_parent_tag(xmlnode start, const char *parent_tag, const char *new_tag) |
---|
2093 | { |
---|
2094 | xmlnode x = NULL; |
---|
2095 | |
---|
2096 | /* |
---|
2097 | * If the parent tag wasn't specified, see if we can get it |
---|
2098 | * from the vCard template struct. |
---|
2099 | */ |
---|
2100 | if(parent_tag == NULL) { |
---|
2101 | struct vcard_template *vc_tp = vcard_template_data; |
---|
2102 | |
---|
2103 | while(vc_tp->label != NULL) { |
---|
2104 | if(strcmp(vc_tp->tag, new_tag) == 0) { |
---|
2105 | parent_tag = vc_tp->ptag; |
---|
2106 | break; |
---|
2107 | } |
---|
2108 | ++vc_tp; |
---|
2109 | } |
---|
2110 | } |
---|
2111 | |
---|
2112 | /* |
---|
2113 | * If we have a parent tag... |
---|
2114 | */ |
---|
2115 | if(parent_tag != NULL ) { |
---|
2116 | /* |
---|
2117 | * Try to get the parent node for a tag |
---|
2118 | */ |
---|
2119 | if((x = xmlnode_get_tag(start, parent_tag)) == NULL) { |
---|
2120 | /* |
---|
2121 | * Descend? |
---|
2122 | */ |
---|
2123 | char *grand_parent = strcpy(g_malloc(strlen(parent_tag) + 1), parent_tag); |
---|
2124 | char *parent; |
---|
2125 | |
---|
2126 | if((parent = strrchr(grand_parent, '/')) != NULL) { |
---|
2127 | *(parent++) = '\0'; |
---|
2128 | x = insert_tag_to_parent_tag(start, grand_parent, parent); |
---|
2129 | } else { |
---|
2130 | x = xmlnode_insert_tag(start, grand_parent); |
---|
2131 | } |
---|
2132 | g_free(grand_parent); |
---|
2133 | } else { |
---|
2134 | /* |
---|
2135 | * We found *something* to be the parent node. |
---|
2136 | * Note: may be the "root" node! |
---|
2137 | */ |
---|
2138 | xmlnode y; |
---|
2139 | if((y = xmlnode_get_tag(x, new_tag)) != NULL) { |
---|
2140 | return(y); |
---|
2141 | } |
---|
2142 | } |
---|
2143 | } |
---|
2144 | |
---|
2145 | /* |
---|
2146 | * insert the new tag into its parent node |
---|
2147 | */ |
---|
2148 | return(xmlnode_insert_tag((x == NULL? start : x), new_tag)); |
---|
2149 | } |
---|
2150 | |
---|
2151 | /* |
---|
2152 | * Send vCard info to Jabber server |
---|
2153 | */ |
---|
2154 | static void jabber_set_info(struct gaim_connection *gc, char *info) |
---|
2155 | { |
---|
2156 | xmlnode x, vc_node; |
---|
2157 | char *id; |
---|
2158 | struct jabber_data *jd = gc->proto_data; |
---|
2159 | gjconn gjc = jd->gjc; |
---|
2160 | |
---|
2161 | x = xmlnode_new_tag("iq"); |
---|
2162 | xmlnode_put_attrib(x,"type","set"); |
---|
2163 | |
---|
2164 | id = gjab_getid(gjc); |
---|
2165 | |
---|
2166 | xmlnode_put_attrib(x, "id", id); |
---|
2167 | |
---|
2168 | /* |
---|
2169 | * Send only if there's actually any *information* to send |
---|
2170 | */ |
---|
2171 | if((vc_node = xmlstr2xmlnode(info)) != NULL && xmlnode_get_name(vc_node) != NULL && |
---|
2172 | g_strncasecmp(xmlnode_get_name(vc_node), "vcard", 5) == 0) { |
---|
2173 | xmlnode_insert_tag_node(x, vc_node); |
---|
2174 | gjab_send(gjc, x); |
---|
2175 | } |
---|
2176 | |
---|
2177 | xmlnode_free(x); |
---|
2178 | } |
---|
2179 | |
---|
2180 | /* |
---|
2181 | * displays a Jabber vCard |
---|
2182 | */ |
---|
2183 | static void jabber_handlevcard(gjconn gjc, xmlnode querynode, char *from) |
---|
2184 | { |
---|
2185 | struct jabber_data *jd = GJ_GC(gjc)->proto_data; |
---|
2186 | jid who = jid_new(gjc->p, from); |
---|
2187 | char *status = NULL, *text = NULL; |
---|
2188 | GString *str = g_string_sized_new(100); |
---|
2189 | xmlnode child; |
---|
2190 | |
---|
2191 | gchar *buddy = NULL; |
---|
2192 | |
---|
2193 | if(querynode == NULL) { |
---|
2194 | serv_got_crap(GJ_GC(gjc), "%s - Received empty info reply from %s", _("User Info"), from); |
---|
2195 | return; |
---|
2196 | } |
---|
2197 | |
---|
2198 | if(who->resource != NULL && (who->resource)[0] != '\0') { |
---|
2199 | buddy = g_strdup_printf("%s@%s/%s", who->user, who->server, who->resource); |
---|
2200 | } else { |
---|
2201 | buddy = g_strdup_printf("%s@%s", who->user, who->server); |
---|
2202 | } |
---|
2203 | |
---|
2204 | if((status = g_hash_table_lookup(jd->hash, buddy)) == NULL) { |
---|
2205 | status = _("Unknown"); |
---|
2206 | } |
---|
2207 | |
---|
2208 | g_string_sprintfa(str, "%s: %s - %s: %s", _("Jabber ID"), buddy, _("Status"), |
---|
2209 | status); |
---|
2210 | |
---|
2211 | for(child = querynode->firstchild; child; child = child->next) |
---|
2212 | { |
---|
2213 | xmlnode child2; |
---|
2214 | |
---|
2215 | if(child->type != NTYPE_TAG) |
---|
2216 | continue; |
---|
2217 | |
---|
2218 | text = xmlnode_get_data(child); |
---|
2219 | if(text && !strcmp(child->name, "FN")) { |
---|
2220 | info_string_append(str, "\n", _("Full Name"), text); |
---|
2221 | } else if (!strcmp(child->name, "N")) { |
---|
2222 | for (child2 = child->firstchild; child2; child2 = child2->next) { |
---|
2223 | char *text2 = NULL; |
---|
2224 | |
---|
2225 | if (child2->type != NTYPE_TAG) |
---|
2226 | continue; |
---|
2227 | |
---|
2228 | text2 = xmlnode_get_data(child2); |
---|
2229 | if (text2 && !strcmp(child2->name, "FAMILY")) { |
---|
2230 | info_string_append(str, "\n", _("Family Name"), text2); |
---|
2231 | } else if (text2 && !strcmp(child2->name, "GIVEN")) { |
---|
2232 | info_string_append(str, "\n", _("Given Name"), text2); |
---|
2233 | } else if (text2 && !strcmp(child2->name, "MIDDLE")) { |
---|
2234 | info_string_append(str, "\n", _("Middle Name"), text2); |
---|
2235 | } |
---|
2236 | } |
---|
2237 | } else if (text && !strcmp(child->name, "NICKNAME")) { |
---|
2238 | info_string_append(str, "\n", _("Nickname"), text); |
---|
2239 | } else if (text && !strcmp(child->name, "BDAY")) { |
---|
2240 | info_string_append(str, "\n", _("Birthday"), text); |
---|
2241 | } else if (!strcmp(child->name, "ADR")) { |
---|
2242 | /* show wich address it is */ |
---|
2243 | /* Just for the beauty of bitlbee |
---|
2244 | if (child->firstchild) |
---|
2245 | g_string_sprintfa(str, "%s:\n", _("Address")); |
---|
2246 | */ |
---|
2247 | for(child2 = child->firstchild; child2; child2 = child2->next) { |
---|
2248 | char *text2 = NULL; |
---|
2249 | |
---|
2250 | if(child2->type != NTYPE_TAG) |
---|
2251 | continue; |
---|
2252 | |
---|
2253 | text2 = xmlnode_get_data(child2); |
---|
2254 | if(text2 && !strcmp(child2->name, "POBOX")) { |
---|
2255 | info_string_append(str, "\n", |
---|
2256 | _("P.O. Box"), text2); |
---|
2257 | } else if(text2 && !strcmp(child2->name, "EXTADR")) { |
---|
2258 | info_string_append(str, "\n", |
---|
2259 | _("Extended Address"), text2); |
---|
2260 | } else if(text2 && !strcmp(child2->name, "STREET")) { |
---|
2261 | info_string_append(str, "\n", |
---|
2262 | _("Street Address"), text2); |
---|
2263 | } else if(text2 && !strcmp(child2->name, "LOCALITY")) { |
---|
2264 | info_string_append(str, "\n", |
---|
2265 | _("Locality"), text2); |
---|
2266 | } else if(text2 && !strcmp(child2->name, "REGION")) { |
---|
2267 | info_string_append(str, "\n", |
---|
2268 | _("Region"), text2); |
---|
2269 | } else if(text2 && !strcmp(child2->name, "PCODE")) { |
---|
2270 | info_string_append(str, "\n", |
---|
2271 | _("Postal Code"), text2); |
---|
2272 | } else if(text2 && (!strcmp(child2->name, "CTRY") |
---|
2273 | || !strcmp(child2->name, "COUNTRY"))) { |
---|
2274 | info_string_append(str, "\n", _("Country"), text2); |
---|
2275 | } |
---|
2276 | } |
---|
2277 | } else if(!strcmp(child->name, "TEL")) { |
---|
2278 | char *number = NULL; |
---|
2279 | if ((child2 = xmlnode_get_tag(child, "NUMBER"))) { |
---|
2280 | /* show what kind of number it is */ |
---|
2281 | number = xmlnode_get_data(child2); |
---|
2282 | if(number) { |
---|
2283 | info_string_append(str, "\n", _("Telephone"), number); |
---|
2284 | } |
---|
2285 | } else if((number = xmlnode_get_data(child))) { |
---|
2286 | /* lots of clients (including gaim) do this, |
---|
2287 | * but it's out of spec */ |
---|
2288 | info_string_append(str, "\n", _("Telephone"), number); |
---|
2289 | } |
---|
2290 | } else if(!strcmp(child->name, "EMAIL")) { |
---|
2291 | char *userid = NULL; |
---|
2292 | if((child2 = xmlnode_get_tag(child, "USERID"))) { |
---|
2293 | /* show what kind of email it is */ |
---|
2294 | userid = xmlnode_get_data(child2); |
---|
2295 | if(userid) { |
---|
2296 | info_string_append(str, "\n", _("Email"), userid); |
---|
2297 | } |
---|
2298 | } else if((userid = xmlnode_get_data(child))) { |
---|
2299 | /* lots of clients (including gaim) do this, |
---|
2300 | * but it's out of spec */ |
---|
2301 | info_string_append(str, "\n", _("Email"), userid); |
---|
2302 | } |
---|
2303 | } else if(!strcmp(child->name, "ORG")) { |
---|
2304 | for(child2 = child->firstchild; child2; child2 = child2->next) { |
---|
2305 | char *text2 = NULL; |
---|
2306 | |
---|
2307 | if(child2->type != NTYPE_TAG) |
---|
2308 | continue; |
---|
2309 | |
---|
2310 | text2 = xmlnode_get_data(child2); |
---|
2311 | if(text2 && !strcmp(child2->name, "ORGNAME")) { |
---|
2312 | info_string_append(str, "\n", _("Organization Name"), text2); |
---|
2313 | } else if(text2 && !strcmp(child2->name, "ORGUNIT")) { |
---|
2314 | info_string_append(str, "\n", _("Organization Unit"), text2); |
---|
2315 | } |
---|
2316 | } |
---|
2317 | } else if(text && !strcmp(child->name, "TITLE")) { |
---|
2318 | info_string_append(str, "\n", _("Title"), text); |
---|
2319 | } else if(text && !strcmp(child->name, "ROLE")) { |
---|
2320 | info_string_append(str, "\n", _("Role"), text); |
---|
2321 | } else if(text && !strcmp(child->name, "DESC")) { |
---|
2322 | g_string_sprintfa(str, "\n%s:\n%s\n%s", _("Description"), |
---|
2323 | text, _("End of Description")); |
---|
2324 | } |
---|
2325 | } |
---|
2326 | |
---|
2327 | serv_got_crap(GJ_GC(gjc), "%s\n%s", _("User Info"), str->str); |
---|
2328 | |
---|
2329 | g_free(buddy); |
---|
2330 | g_string_free(str, TRUE); |
---|
2331 | } |
---|
2332 | |
---|
2333 | void jabber_init() |
---|
2334 | { |
---|
2335 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
2336 | |
---|
2337 | ret->name = "jabber"; |
---|
2338 | ret->away_states = jabber_away_states; |
---|
2339 | ret->login = jabber_login; |
---|
2340 | ret->close = jabber_close; |
---|
2341 | ret->send_im = jabber_send_im; |
---|
2342 | ret->set_info = jabber_set_info; |
---|
2343 | ret->get_info = jabber_get_info; |
---|
2344 | ret->set_away = jabber_set_away; |
---|
2345 | ret->get_away = jabber_get_away_msg; |
---|
2346 | ret->add_buddy = jabber_add_buddy; |
---|
2347 | ret->remove_buddy = jabber_remove_buddy; |
---|
2348 | ret->keepalive = jabber_keepalive; |
---|
2349 | ret->alias_buddy = jabber_roster_update; |
---|
2350 | ret->group_buddy = jabber_group_change; |
---|
2351 | ret->cmp_buddynames = g_strcasecmp; |
---|
2352 | |
---|
2353 | register_protocol (ret); |
---|
2354 | } |
---|