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