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