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