Mercurial > pidgin.yaz
annotate src/protocols/jabber/jabber.c @ 3328:d183c2703508
[gaim-migrate @ 3346]
Remainder of fix for Gaim crashing on non-ASCII buddy alias (Jabber "name"
attribute) chars. (Thanks, A Ho-seok Lee)
committer: Tailor Script <tailor@pidgin.im>
author | Jim Seymour <jseymour> |
---|---|
date | Wed, 19 Jun 2002 21:20:08 +0000 |
parents | 52db87ab4083 |
children | e77fcff466a1 |
rev | line source |
---|---|
2086 | 1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
2 /* | |
3 * gaim | |
4 * | |
5 * Some code copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
6 * libfaim code copyright 1998, 1999 Adam Fritzler <afritz@auk.cx> | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 * | |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include "config.h" | |
26 #endif | |
27 | |
28 | |
29 #include <netdb.h> | |
30 #include <unistd.h> | |
31 #include <errno.h> | |
32 #include <netinet/in.h> | |
33 #include <arpa/inet.h> | |
34 #include <string.h> | |
35 #include <stdlib.h> | |
36 #include <stdio.h> | |
37 #include <time.h> | |
38 #include <sys/socket.h> | |
39 #include <sys/utsname.h> | |
40 #include <sys/stat.h> | |
41 #include "multi.h" | |
42 #include "prpl.h" | |
43 #include "gaim.h" | |
2232
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
44 #ifdef MAX |
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
45 #undef MAX |
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
46 #endif |
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
47 #ifdef MIN |
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
48 #undef MIN |
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
49 #endif |
2086 | 50 #include "jabber.h" |
51 #include "proxy.h" | |
52 | |
53 #include "pixmaps/available.xpm" | |
54 #include "pixmaps/available-away.xpm" | |
55 #include "pixmaps/available-chat.xpm" | |
56 #include "pixmaps/available-xa.xpm" | |
57 #include "pixmaps/available-dnd.xpm" | |
3259 | 58 #include "pixmaps/available-error.xpm" |
2086 | 59 |
60 /* The priv member of gjconn's is a gaim_connection for now. */ | |
61 #define GJ_GC(x) ((struct gaim_connection *)(x)->priv) | |
62 | |
63 #define IQID_AUTH "__AUTH__" | |
64 | |
65 #define IQ_NONE -1 | |
66 #define IQ_AUTH 0 | |
67 #define IQ_ROSTER 1 | |
68 | |
3259 | 69 #define UC_AWAY (0x02 | UC_UNAVAILABLE) |
70 #define UC_CHAT 0x04 | |
71 #define UC_XA (0x08 | UC_UNAVAILABLE) | |
72 #define UC_DND (0x10 | UC_UNAVAILABLE) | |
73 #define UC_ERROR (0x20 | UC_UNAVAILABLE) | |
2086 | 74 |
75 #define DEFAULT_SERVER "jabber.org" | |
76 #define DEFAULT_GROUPCHAT "conference.jabber.org" | |
77 #define DEFAULT_PORT 5222 | |
78 | |
79 #define USEROPT_PORT 0 | |
80 | |
3311 | 81 #define JABBER_TYPING_NOTIFY_INT 15 /* Delay (in seconds) between sending typing notifications */ |
82 | |
3074 | 83 /* |
84 * Note: "was_connected" may seem redundant, but it was needed and I | |
85 * didn't want to touch the Jabber state stuff not specific to Gaim. | |
86 */ | |
2086 | 87 typedef struct gjconn_struct { |
88 /* Core structure */ | |
89 pool p; /* Memory allocation pool */ | |
90 int state; /* Connection state flag */ | |
3074 | 91 int was_connected; /* We were once connected */ |
2086 | 92 int fd; /* Connection file descriptor */ |
93 jid user; /* User info */ | |
94 char *pass; /* User passwd */ | |
95 | |
96 /* Stream stuff */ | |
97 int id; /* id counter for jab_getid() function */ | |
98 char idbuf[9]; /* temporary storage for jab_getid() */ | |
99 char *sid; /* stream id from server, for digest auth */ | |
100 XML_Parser parser; /* Parser instance */ | |
101 xmlnode current; /* Current node in parsing instance.. */ | |
102 | |
103 /* Event callback ptrs */ | |
2956 | 104 void (*on_state)(struct gjconn_struct *gjc, int state); |
105 void (*on_packet)(struct gjconn_struct *gjc, jpacket p); | |
106 | |
107 GHashTable *queries; /* query tracker */ | |
2086 | 108 |
109 void *priv; | |
110 | |
111 } *gjconn, gjconn_struct; | |
112 | |
2956 | 113 typedef void (*gjconn_state_h)(gjconn gjc, int state); |
114 typedef void (*gjconn_packet_h)(gjconn gjc, jpacket p); | |
2086 | 115 |
116 static gjconn gjab_new(char *user, char *pass, void *priv); | |
2956 | 117 static void gjab_delete(gjconn gjc); |
118 static void gjab_state_handler(gjconn gjc, gjconn_state_h h); | |
119 static void gjab_packet_handler(gjconn gjc, gjconn_packet_h h); | |
120 static void gjab_start(gjconn gjc); | |
121 static void gjab_stop(gjconn gjc); | |
2086 | 122 /* |
2956 | 123 static int gjab_getfd(gjconn gjc); |
124 static jid gjab_getjid(gjconn gjc); | |
125 static char *gjab_getsid(gjconn gjc); | |
2086 | 126 */ |
2956 | 127 static char *gjab_getid(gjconn gjc); |
128 static void gjab_send(gjconn gjc, xmlnode x); | |
129 static void gjab_send_raw(gjconn gjc, const char *str); | |
130 static void gjab_recv(gjconn gjc); | |
131 static void gjab_auth(gjconn gjc); | |
132 | |
133 /* | |
134 * It is *this* to which we point the gaim_connection proto_data | |
135 */ | |
2086 | 136 struct jabber_data { |
2956 | 137 gjconn gjc; |
2086 | 138 gboolean did_import; |
2956 | 139 GSList *chats; |
2086 | 140 time_t idle; |
2800
0ad63a625eec
[gaim-migrate @ 2813]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2791
diff
changeset
|
141 gboolean die; |
3311 | 142 GHashTable *buddies; |
2086 | 143 }; |
144 | |
2956 | 145 /* |
3311 | 146 * It is *this* to which we point the buddy proto_data |
147 */ | |
148 struct jabber_buddy_data { | |
149 GSList *resources; | |
150 char *error_msg; | |
151 }; | |
152 | |
153 /* | |
154 * per-resource info | |
155 */ | |
156 typedef struct jabber_resource_info { | |
157 char *name; | |
158 int priority; | |
159 int state; | |
160 char *away_msg; | |
161 char *thread_id; | |
162 gboolean has_composing; | |
163 } *jab_res_info; | |
164 | |
165 /* | |
166 * For our own jid handling | |
167 * | |
168 * We do our own so we can cleanly parse buddy names | |
169 * (user@server/resource) and rid ourselves of the | |
170 * struct when we're done with it. The Jabber lib | |
171 * structs last the life of the pool--we frequently | |
172 * don't want that. | |
173 * | |
174 * We use the real jid structs so we can make use of | |
175 * jid_safe(), jid_cmp() and some others. | |
176 * | |
177 * BE CAREFUL using the Jabber lib routines. | |
178 * Many of them assume pool use and are not | |
179 * amenable to use with our own! | |
180 * | |
181 * We give them special names so we know, throughout | |
182 * the code, that they're not alloc'd out of pool | |
183 * memory and we can, and must, dispose of them when | |
184 * we're done with 'em. | |
185 */ | |
186 #define gaim_jid_struct jid_struct | |
187 typedef struct gaim_jid_struct *gaim_jid; | |
188 | |
189 /* | |
2956 | 190 * Jabber "chat group" info. Pointers to these go in jabber_data |
191 * pending and existing chats lists. | |
192 */ | |
2086 | 193 struct jabber_chat { |
3311 | 194 gaim_jid gjid; |
2086 | 195 struct gaim_connection *gc; |
196 struct conversation *b; | |
197 int id; | |
2956 | 198 int state; |
2086 | 199 }; |
200 | |
2956 | 201 /* |
202 * Jabber chat states... | |
203 * | |
204 * Note: due to a bug in one version of the Jabber server, subscriptions | |
205 * to chat groups aren't (always?) properly removed at the server. The | |
206 * result is clients receive Jabber "presence" notifications for JIDs | |
207 * they no longer care about. The problem with such vestigial notifies is | |
208 * that we really have no way of telling if it's vestigial or if it's a | |
209 * valid "buddy" presence notification. So we keep jabber_chat structs | |
210 * around after leaving a chat group and simply mark them "closed." That | |
211 * way we can test for such errant presence notifications. I.e.: if we | |
212 * get a presence notfication from a JID that matches a chat group JID, | |
213 * we disregard it. | |
214 */ | |
215 #define JCS_PENDING 1 /* pending */ | |
216 #define JCS_ACTIVE 2 /* active */ | |
217 #define JCS_CLOSED 3 /* closed */ | |
218 | |
219 | |
2086 | 220 static char *jabber_name() |
221 { | |
222 return "Jabber"; | |
223 } | |
224 | |
2956 | 225 #define STATE_EVT(arg) if(gjc->on_state) { (gjc->on_state)(gjc, (arg) ); } |
226 | |
227 static void jabber_handlevcard(gjconn, xmlnode, char *); | |
2086 | 228 |
229 static char *create_valid_jid(const char *given, char *server, char *resource) | |
230 { | |
231 char *valid; | |
232 | |
233 if (!strchr(given, '@')) | |
234 valid = g_strdup_printf("%s@%s/%s", given, server, resource); | |
235 else if (!strchr(strchr(given, '@'), '/')) | |
236 valid = g_strdup_printf("%s/%s", given, resource); | |
237 else | |
238 valid = g_strdup(given); | |
239 | |
240 return valid; | |
241 } | |
242 | |
3311 | 243 /* |
244 * Dispose of a gaim_jid_struct | |
245 */ | |
246 static void gaim_jid_free(gaim_jid gjid) | |
247 { | |
248 if(gjid) { | |
249 if(gjid->resource) | |
250 free(gjid->resource); | |
251 if(gjid->user) | |
252 free(gjid->user); | |
253 if(gjid->server) | |
254 free(gjid->server); | |
255 if(gjid->full) | |
256 free(gjid->full); | |
257 free(gjid); | |
258 } | |
259 } | |
260 | |
261 /* | |
262 * Create a new gjid struct | |
263 * | |
264 * Unlike jid_new(), also creates "full." | |
265 * | |
266 * Shamelessly copied, in part, from jid.c: jid_new() | |
267 * | |
268 * Caller is responsible for freeing the space allocated by this via | |
269 * gaim_jid_free(). | |
270 * | |
271 * JFIXME: Has a local declaration for jid.c:jid_safe(). I've put in a | |
272 * request to have that added to libjabber's lib.h file. (JSeymour) | |
273 */ | |
274 static gaim_jid gaim_jid_new(char *name) | |
275 { | |
276 extern jid jid_safe(jid); /* *retch* */ | |
277 | |
278 gaim_jid gjid = NULL; | |
279 | |
280 if(name && strlen(name)) { | |
281 char *server, *resource, *type, *str; | |
282 int full_len = 0; | |
283 | |
284 /* user@server/resource */ | |
285 | |
286 str = strdup(name); /* we mangle a copy */ | |
287 | |
288 gjid = calloc(1, sizeof(struct gaim_jid_struct)); | |
289 | |
290 if((resource = strstr(str, "/")) != NULL) { | |
291 *resource = '\0'; | |
292 ++resource; | |
293 if((full_len = strlen(resource)) > 0) { | |
294 gjid->resource = strdup(resource); | |
295 ++full_len; /* for later "/" addition */ | |
296 } | |
297 } else { | |
298 resource = str + strlen(str); /* point to end */ | |
299 } | |
300 | |
301 type = strstr(str, ":"); | |
302 if(type != NULL && type < resource) { | |
303 *type = '\0'; | |
304 ++type; | |
305 str = type; /* ignore the type: prefix */ | |
306 } | |
307 | |
308 server = strstr(str, "@"); | |
309 | |
310 /* | |
311 * if there's no @, it's just the server address | |
312 */ | |
313 if(server == NULL || server > resource) { | |
314 gjid->server = strdup(str); | |
315 full_len += strlen(str); | |
316 } else { | |
317 *server = '\0'; | |
318 ++server; | |
319 gjid->server = strdup(server); | |
320 full_len += strlen(server) + 1; /* account for later "@" */ | |
321 if(strlen(str) > 0) { | |
322 gjid->user = strdup(str); | |
323 full_len += strlen(str); | |
324 } | |
325 } | |
326 | |
327 free(str); | |
328 | |
329 if(!jid_safe(gjid)) { | |
330 gaim_jid_free(gjid); | |
331 gjid = NULL; | |
332 } else { | |
333 if(full_len) { | |
334 char *s = gjid->full = malloc(++full_len); | |
335 | |
336 if(gjid->user) { | |
337 strcpy(s, gjid->user); | |
338 s += strlen(gjid->user); | |
339 } | |
340 if(gjid->server) { | |
341 if(s > gjid->full) | |
342 *(s++) = '@'; | |
343 strcpy(s, gjid->server); | |
344 s += strlen(gjid->server); | |
345 } | |
346 if(gjid->resource) { | |
347 *(s++) = '/'; | |
348 strcpy(s, gjid->resource); | |
349 } | |
350 } | |
351 } | |
352 } | |
353 | |
354 return gjid; | |
355 } | |
356 | |
357 /* | |
358 * Get a "username@server" from unadorned "username" | |
359 * | |
360 * If there's no "@server" part and "who" doesn't match the | |
361 * gjconn server (which would indicate that "who" *is* the | |
362 * server in case of server messages), the gjconn server is | |
363 * appended. | |
364 * | |
365 * If incl_resource is TRUE (non-0), the returned string | |
366 * includes the "/resource" part (if it exists), otherwise not. | |
367 * | |
368 * Allocates space for returned string. Caller is | |
369 * responsible for freeing it with g_free(). | |
370 * | |
371 * If "gjid" is non-null, sets that as well. Caller is | |
372 * reponsible for freeing that via gaim_jid_free() when done | |
373 * with it. | |
374 */ | |
375 static gchar *get_realwho(gjconn gjc, char *who, int incl_resource, gaim_jid *gjid) | |
376 { | |
377 gaim_jid my_gjid; | |
378 gchar *my_who; | |
379 gchar *realwho = NULL; | |
380 | |
381 if(!(who && who[0])) { | |
382 return NULL; | |
383 } | |
384 | |
385 /* | |
386 * Bare username and "username" not the server itself? | |
387 */ | |
388 if(!strchr(who, '@') && strcasecmp(who, gjc->user->server)) { | |
389 my_who = g_strdup_printf("%s@%s", who, gjc->user->server); | |
390 } else { | |
391 my_who = g_strdup(who); | |
392 } | |
393 | |
394 if((my_gjid = gaim_jid_new(my_who)) != NULL) { | |
395 /* | |
396 * If there's no "user" part, "who" was just the server or perhaps a transport (?) | |
397 */ | |
398 if(my_gjid->user) { | |
399 /* | |
400 * Include "/resource" bit? | |
401 */ | |
402 if(incl_resource) { | |
403 realwho = g_strdup(my_gjid->full); | |
404 } else { | |
405 realwho = g_strdup_printf("%s@%s", my_gjid->user, my_gjid->server); | |
406 } | |
407 } else { | |
408 realwho = g_strdup(my_gjid->server); | |
409 } | |
410 } | |
411 | |
412 g_free(my_who); | |
413 | |
414 if(gjid) { | |
415 *gjid = my_gjid; | |
416 } else { | |
417 gaim_jid_free(my_gjid); | |
418 } | |
419 | |
420 return realwho; | |
421 } | |
422 | |
2086 | 423 static gjconn gjab_new(char *user, char *pass, void *priv) |
424 { | |
425 pool p; | |
2956 | 426 gjconn gjc; |
2086 | 427 |
428 if (!user) | |
429 return (NULL); | |
430 | |
431 p = pool_new(); | |
432 if (!p) | |
433 return (NULL); | |
2956 | 434 gjc = pmalloc_x(p, sizeof(gjconn_struct), 0); |
435 if (!gjc) { | |
436 pool_free(p); /* no need for this anymore! */ | |
2086 | 437 return (NULL); |
2956 | 438 } |
439 gjc->p = p; | |
440 | |
3236 | 441 if((gjc->user = jid_new(p, user)) == NULL) { |
442 pool_free(p); /* no need for this anymore! */ | |
443 return (NULL); | |
444 } | |
3257 | 445 |
446 gjc->pass = strdup(pass); | |
2956 | 447 |
448 gjc->state = JCONN_STATE_OFF; | |
3074 | 449 gjc->was_connected = 0; |
2956 | 450 gjc->id = 1; |
451 gjc->fd = -1; | |
452 | |
453 gjc->priv = priv; | |
454 | |
455 return gjc; | |
2086 | 456 } |
457 | |
2956 | 458 static void gjab_delete(gjconn gjc) |
2086 | 459 { |
2956 | 460 if (!gjc) |
2086 | 461 return; |
462 | |
2956 | 463 gjab_stop(gjc); |
3257 | 464 free(gjc->pass); |
2956 | 465 pool_free(gjc->p); |
2086 | 466 } |
467 | |
2956 | 468 static void gjab_state_handler(gjconn gjc, gjconn_state_h h) |
2086 | 469 { |
2956 | 470 if (!gjc) |
2086 | 471 return; |
472 | |
2956 | 473 gjc->on_state = h; |
2086 | 474 } |
475 | |
2956 | 476 static void gjab_packet_handler(gjconn gjc, gjconn_packet_h h) |
2086 | 477 { |
2956 | 478 if (!gjc) |
2086 | 479 return; |
480 | |
2956 | 481 gjc->on_packet = h; |
2086 | 482 } |
483 | |
2956 | 484 static void gjab_stop(gjconn gjc) |
2086 | 485 { |
2956 | 486 if (!gjc || gjc->state == JCONN_STATE_OFF) |
2086 | 487 return; |
488 | |
2956 | 489 gjab_send_raw(gjc, "</stream:stream>"); |
490 gjc->state = JCONN_STATE_OFF; | |
3074 | 491 gjc->was_connected = 0; |
2956 | 492 close(gjc->fd); |
493 gjc->fd = -1; | |
494 XML_ParserFree(gjc->parser); | |
495 gjc->parser = NULL; | |
2086 | 496 } |
497 | |
498 /* | |
2956 | 499 static int gjab_getfd(gjconn gjc) |
2086 | 500 { |
2956 | 501 if (gjc) |
502 return gjc->fd; | |
2086 | 503 else |
504 return -1; | |
505 } | |
506 | |
2956 | 507 static jid gjab_getjid(gjconn gjc) |
2086 | 508 { |
2956 | 509 if (gjc) |
510 return (gjc->user); | |
2086 | 511 else |
512 return NULL; | |
513 } | |
514 | |
2956 | 515 static char *gjab_getsid(gjconn gjc) |
2086 | 516 { |
2956 | 517 if (gjc) |
518 return (gjc->sid); | |
2086 | 519 else |
520 return NULL; | |
521 } | |
522 */ | |
523 | |
2956 | 524 static char *gjab_getid(gjconn gjc) |
2086 | 525 { |
2956 | 526 snprintf(gjc->idbuf, 8, "%d", gjc->id++); |
527 return &gjc->idbuf[0]; | |
2086 | 528 } |
529 | |
2956 | 530 static void gjab_send(gjconn gjc, xmlnode x) |
2086 | 531 { |
2956 | 532 if (gjc && gjc->state != JCONN_STATE_OFF) { |
2086 | 533 char *buf = xmlnode2str(x); |
534 if (buf) | |
2956 | 535 write(gjc->fd, buf, strlen(buf)); |
2086 | 536 debug_printf("gjab_send: %s\n", buf); |
537 } | |
538 } | |
539 | |
2956 | 540 static void gjab_send_raw(gjconn gjc, const char *str) |
2086 | 541 { |
2956 | 542 if (gjc && gjc->state != JCONN_STATE_OFF) { |
543 /* | |
544 * JFIXME: No error detection?!?! | |
545 */ | |
546 if(write(gjc->fd, str, strlen(str)) < 0) { | |
547 fprintf(stderr, "DBG: Problem sending. Error: %d\n", errno); | |
548 fflush(stderr); | |
549 } | |
2086 | 550 debug_printf("gjab_send_raw: %s\n", str); |
551 } | |
552 } | |
553 | |
2956 | 554 static void gjab_reqroster(gjconn gjc) |
2086 | 555 { |
556 xmlnode x; | |
557 | |
558 x = jutil_iqnew(JPACKET__GET, NS_ROSTER); | |
2956 | 559 xmlnode_put_attrib(x, "id", gjab_getid(gjc)); |
560 | |
561 gjab_send(gjc, x); | |
2086 | 562 xmlnode_free(x); |
563 } | |
564 | |
2956 | 565 static void gjab_reqauth(gjconn gjc) |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
566 { |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
567 xmlnode x, y, z; |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
568 char *user; |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
569 |
2956 | 570 if (!gjc) |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
571 return; |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
572 |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
573 x = jutil_iqnew(JPACKET__GET, NS_AUTH); |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
574 xmlnode_put_attrib(x, "id", IQID_AUTH); |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
575 y = xmlnode_get_tag(x, "query"); |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
576 |
2956 | 577 user = gjc->user->user; |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
578 |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
579 if (user) { |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
580 z = xmlnode_insert_tag(y, "username"); |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
581 xmlnode_insert_cdata(z, user, -1); |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
582 } |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
583 |
2956 | 584 gjab_send(gjc, x); |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
585 xmlnode_free(x); |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
586 } |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
587 |
2956 | 588 static void gjab_auth(gjconn gjc) |
2086 | 589 { |
590 xmlnode x, y, z; | |
591 char *hash, *user; | |
592 | |
2956 | 593 if (!gjc) |
2086 | 594 return; |
595 | |
596 x = jutil_iqnew(JPACKET__SET, NS_AUTH); | |
597 xmlnode_put_attrib(x, "id", IQID_AUTH); | |
598 y = xmlnode_get_tag(x, "query"); | |
599 | |
2956 | 600 user = gjc->user->user; |
2086 | 601 |
602 if (user) { | |
603 z = xmlnode_insert_tag(y, "username"); | |
604 xmlnode_insert_cdata(z, user, -1); | |
605 } | |
606 | |
607 z = xmlnode_insert_tag(y, "resource"); | |
2956 | 608 xmlnode_insert_cdata(z, gjc->user->resource, -1); |
609 | |
610 if (gjc->sid) { | |
611 debug_printf("digest authentication (sid %s)\n", gjc->sid); | |
2086 | 612 z = xmlnode_insert_tag(y, "digest"); |
2956 | 613 hash = pmalloc(x->p, strlen(gjc->sid) + strlen(gjc->pass) + 1); |
614 strcpy(hash, gjc->sid); | |
615 strcat(hash, gjc->pass); | |
2086 | 616 hash = shahash(hash); |
617 xmlnode_insert_cdata(z, hash, 40); | |
618 } else { | |
619 z = xmlnode_insert_tag(y, "password"); | |
2956 | 620 xmlnode_insert_cdata(z, gjc->pass, -1); |
2086 | 621 } |
622 | |
2956 | 623 gjab_send(gjc, x); |
2086 | 624 xmlnode_free(x); |
625 | |
626 return; | |
627 } | |
628 | |
2956 | 629 static void gjab_recv(gjconn gjc) |
2086 | 630 { |
631 static char buf[4096]; | |
632 int len; | |
633 | |
2956 | 634 if (!gjc || gjc->state == JCONN_STATE_OFF) |
2086 | 635 return; |
636 | |
3234 | 637 if ((len = read(gjc->fd, buf, sizeof(buf) - 1)) > 0) { |
2956 | 638 struct jabber_data *jd = GJ_GC(gjc)->proto_data; |
2086 | 639 buf[len] = '\0'; |
640 debug_printf("input (len %d): %s\n", len, buf); | |
2956 | 641 XML_Parse(gjc->parser, buf, len, 0); |
2800
0ad63a625eec
[gaim-migrate @ 2813]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2791
diff
changeset
|
642 if (jd->die) |
2956 | 643 signoff(GJ_GC(gjc)); |
3105 | 644 } else if (len < 0 || errno != EAGAIN) { |
2086 | 645 STATE_EVT(JCONN_STATE_OFF) |
646 } | |
647 } | |
648 | |
649 static void startElement(void *userdata, const char *name, const char **attribs) | |
650 { | |
651 xmlnode x; | |
2956 | 652 gjconn gjc = (gjconn) userdata; |
653 | |
654 if (gjc->current) { | |
2086 | 655 /* Append the node to the current one */ |
2956 | 656 x = xmlnode_insert_tag(gjc->current, name); |
2086 | 657 xmlnode_put_expat_attribs(x, attribs); |
658 | |
2956 | 659 gjc->current = x; |
2086 | 660 } else { |
661 x = xmlnode_new_tag(name); | |
662 xmlnode_put_expat_attribs(x, attribs); | |
663 if (strcmp(name, "stream:stream") == 0) { | |
664 /* special case: name == stream:stream */ | |
665 /* id attrib of stream is stored for digest auth */ | |
2956 | 666 gjc->sid = g_strdup(xmlnode_get_attrib(x, "id")); |
2086 | 667 /* STATE_EVT(JCONN_STATE_AUTH) */ |
2635
8c75e59e4bdf
[gaim-migrate @ 2648]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2607
diff
changeset
|
668 xmlnode_free(x); |
2086 | 669 } else { |
2956 | 670 gjc->current = x; |
2086 | 671 } |
672 } | |
673 } | |
674 | |
675 static void endElement(void *userdata, const char *name) | |
676 { | |
2956 | 677 gjconn gjc = (gjconn) userdata; |
2086 | 678 xmlnode x; |
679 jpacket p; | |
680 | |
2956 | 681 if (gjc->current == NULL) { |
2086 | 682 /* we got </stream:stream> */ |
683 STATE_EVT(JCONN_STATE_OFF) | |
684 return; | |
685 } | |
686 | |
2956 | 687 x = xmlnode_get_parent(gjc->current); |
2086 | 688 |
689 if (!x) { | |
690 /* it is time to fire the event */ | |
2956 | 691 p = jpacket_new(gjc->current); |
692 | |
693 if (gjc->on_packet) | |
694 (gjc->on_packet) (gjc, p); | |
2086 | 695 else |
2956 | 696 xmlnode_free(gjc->current); |
2086 | 697 } |
698 | |
2956 | 699 gjc->current = x; |
2086 | 700 } |
701 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
702 static void jabber_callback(gpointer data, gint source, GaimInputCondition condition) |
2086 | 703 { |
704 struct gaim_connection *gc = (struct gaim_connection *)data; | |
705 struct jabber_data *jd = (struct jabber_data *)gc->proto_data; | |
706 | |
2956 | 707 gjab_recv(jd->gjc); |
2086 | 708 } |
709 | |
710 static void charData(void *userdata, const char *s, int slen) | |
711 { | |
2956 | 712 gjconn gjc = (gjconn) userdata; |
713 | |
714 if (gjc->current) | |
715 xmlnode_insert_cdata(gjc->current, s, slen); | |
2086 | 716 } |
717 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
718 static void gjab_connected(gpointer data, gint source, GaimInputCondition cond) |
2086 | 719 { |
720 xmlnode x; | |
721 char *t, *t2; | |
722 struct gaim_connection *gc = data; | |
723 struct jabber_data *jd; | |
2956 | 724 gjconn gjc; |
2086 | 725 |
726 if (!g_slist_find(connections, gc)) { | |
727 close(source); | |
728 return; | |
729 } | |
730 | |
731 jd = gc->proto_data; | |
2956 | 732 gjc = jd->gjc; |
733 | |
734 if (gjc->fd != source) | |
735 gjc->fd = source; | |
2300
d2686f757d6e
[gaim-migrate @ 2310]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2289
diff
changeset
|
736 |
2086 | 737 if (source == -1) { |
738 STATE_EVT(JCONN_STATE_OFF) | |
739 return; | |
740 } | |
741 | |
2956 | 742 gjc->state = JCONN_STATE_CONNECTED; |
2086 | 743 STATE_EVT(JCONN_STATE_CONNECTED) |
744 | |
745 /* start stream */ | |
2956 | 746 x = jutil_header(NS_CLIENT, gjc->user->server); |
2086 | 747 t = xmlnode2str(x); |
748 /* this is ugly, we can create the string here instead of jutil_header */ | |
749 /* what do you think about it? -madcat */ | |
750 t2 = strstr(t, "/>"); | |
751 *t2++ = '>'; | |
752 *t2 = '\0'; | |
2956 | 753 gjab_send_raw(gjc, "<?xml version='1.0'?>"); |
754 gjab_send_raw(gjc, t); | |
2086 | 755 xmlnode_free(x); |
756 | |
2956 | 757 gjc->state = JCONN_STATE_ON; |
2086 | 758 STATE_EVT(JCONN_STATE_ON); |
759 | |
2956 | 760 gc = GJ_GC(gjc); |
761 gc->inpa = gaim_input_add(gjc->fd, GAIM_INPUT_READ, jabber_callback, gc); | |
2086 | 762 } |
763 | |
2956 | 764 static void gjab_start(gjconn gjc) |
2086 | 765 { |
766 struct aim_user *user; | |
767 int port; | |
768 | |
2956 | 769 if (!gjc || gjc->state != JCONN_STATE_OFF) |
2086 | 770 return; |
771 | |
2956 | 772 user = GJ_GC(gjc)->user; |
2086 | 773 port = user->proto_opt[USEROPT_PORT][0] ? atoi(user->proto_opt[USEROPT_PORT]) : DEFAULT_PORT; |
774 | |
2956 | 775 gjc->parser = XML_ParserCreate(NULL); |
776 XML_SetUserData(gjc->parser, (void *)gjc); | |
777 XML_SetElementHandler(gjc->parser, startElement, endElement); | |
778 XML_SetCharacterDataHandler(gjc->parser, charData); | |
779 | |
780 gjc->fd = proxy_connect(gjc->user->server, port, gjab_connected, GJ_GC(gjc)); | |
781 if (!user->gc || (gjc->fd < 0)) { | |
2086 | 782 STATE_EVT(JCONN_STATE_OFF) |
783 return; | |
784 } | |
785 } | |
786 | |
2956 | 787 /* |
788 * Find chat by chat group name | |
789 */ | |
2086 | 790 static struct conversation *find_chat(struct gaim_connection *gc, char *name) |
791 { | |
792 GSList *bcs = gc->buddy_chats; | |
793 struct conversation *b = NULL; | |
794 char *chat = g_strdup(normalize(name)); | |
795 | |
796 while (bcs) { | |
797 b = bcs->data; | |
798 if (!strcasecmp(normalize(b->name), chat)) | |
799 break; | |
800 b = NULL; | |
801 bcs = bcs->next; | |
802 } | |
803 | |
804 g_free(chat); | |
805 return b; | |
806 } | |
807 | |
2956 | 808 /* |
809 * Find chat by "chat id" | |
810 * | |
811 * Returns: 0 on success and jabber_chat pointer set | |
812 * or -EINVAL on error and jabber_chat pointer is | |
813 * undefined. | |
814 * | |
815 * TBD: Slogging through the buddy_chats list seems | |
816 * redundant since the chat i.d. is mirrored in the | |
817 * jabber_chat struct list. But that's the way it | |
818 * was, so that's the way I'm leaving it--for now. | |
819 */ | |
820 static int jabber_find_chat_by_convo_id(struct gaim_connection *gc, int id, struct jabber_chat **jc) | |
2086 | 821 { |
2956 | 822 GSList *bcs = gc->buddy_chats; |
823 struct conversation *b = NULL; | |
824 struct jabber_data *jd = gc->proto_data; | |
825 | |
826 *jc = NULL; | |
827 | |
828 while(bcs != NULL) { | |
829 b = bcs->data; | |
830 if (id == b->id) | |
831 break; | |
832 bcs = bcs->next; | |
833 } | |
834 | |
835 if (bcs != NULL) { | |
836 bcs = jd->chats; | |
837 while (bcs != NULL) { | |
838 *jc = bcs->data; | |
839 if ((*jc)->state == JCS_ACTIVE && (*jc)->b == b) | |
840 break; | |
841 bcs = bcs->next; | |
842 } | |
843 } | |
844 | |
845 return(bcs == NULL? -EINVAL : 0); | |
846 } | |
847 | |
848 /* | |
849 * Find any chat | |
850 */ | |
851 static struct jabber_chat *find_any_chat(struct gaim_connection *gc, jid chat) | |
852 { | |
853 GSList *jcs = ((struct jabber_data *)gc->proto_data)->chats; | |
2086 | 854 struct jabber_chat *jc = NULL; |
855 | |
2956 | 856 while (jcs) { |
857 jc = jcs->data; | |
3311 | 858 if (!jid_cmpx(chat, jc->gjid, JID_USER | JID_SERVER)) |
2086 | 859 break; |
860 jc = NULL; | |
2956 | 861 jcs = jcs->next; |
2086 | 862 } |
863 | |
864 return jc; | |
865 } | |
866 | |
2956 | 867 |
868 /* | |
869 * Find existing/active Jabber chat | |
870 */ | |
871 static struct jabber_chat *find_existing_chat(struct gaim_connection *gc, jid chat) | |
872 { | |
873 GSList *jcs = ((struct jabber_data *)gc->proto_data)->chats; | |
874 struct jabber_chat *jc = NULL; | |
875 | |
876 while (jcs) { | |
877 jc = jcs->data; | |
3311 | 878 if (jc->state == JCS_ACTIVE && !jid_cmpx(chat, jc->gjid, JID_USER | JID_SERVER)) |
2956 | 879 break; |
880 jc = NULL; | |
881 jcs = jcs->next; | |
882 } | |
883 | |
884 return jc; | |
885 } | |
886 | |
887 /* | |
888 * Find pending chat | |
889 */ | |
2086 | 890 static struct jabber_chat *find_pending_chat(struct gaim_connection *gc, jid chat) |
891 { | |
2956 | 892 GSList *jcs = ((struct jabber_data *)gc->proto_data)->chats; |
2086 | 893 struct jabber_chat *jc = NULL; |
894 | |
2956 | 895 while (jcs) { |
896 jc = jcs->data; | |
3311 | 897 if (jc->state == JCS_PENDING && !jid_cmpx(chat, jc->gjid, JID_USER | JID_SERVER)) |
2086 | 898 break; |
899 jc = NULL; | |
2956 | 900 jcs = jcs->next; |
2086 | 901 } |
902 | |
903 return jc; | |
904 } | |
905 | |
906 static gboolean find_chat_buddy(struct conversation *b, char *name) | |
907 { | |
908 GList *m = b->in_room; | |
909 | |
910 while (m) { | |
911 if (!strcmp(m->data, name)) | |
912 return TRUE; | |
913 m = m->next; | |
914 } | |
915 | |
916 return FALSE; | |
917 } | |
918 | |
2956 | 919 /* |
3236 | 920 * Remove a buddy from the (gaim) buddylist (if he's on it) |
921 */ | |
922 static void jabber_remove_gaim_buddy(struct gaim_connection *gc, char *buddyname) | |
923 { | |
924 struct buddy *b; | |
925 | |
926 if ((b = find_buddy(gc, buddyname)) != NULL) { | |
927 struct group *group; | |
928 | |
929 group = find_group_by_buddy(gc, buddyname); | |
930 debug_printf("removing buddy [1]: %s, from group: %s\n", buddyname, group->name); | |
931 remove_buddy(gc, group, b); | |
932 do_export(gc); | |
933 } | |
934 } | |
935 | |
3257 | 936 static void jabber_change_passwd(struct gaim_connection *gc, char *old, char *new) |
937 { | |
938 gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; | |
939 | |
940 if(strcmp(old, gjc->pass)) | |
941 { | |
942 do_error_dialog(_("Incorrect current password! Password NOT Changed!"), | |
943 _("Password Change Error!")); | |
944 } | |
945 else if(!strcmp(old, new)) | |
946 { | |
947 do_error_dialog(_("New password same as old password! Password NOT Changed!"), | |
948 _("Password Change Error!")); | |
949 } | |
950 else | |
951 { | |
952 xmlnode x, y, z; | |
953 char *id; | |
954 | |
955 x = jutil_iqnew(JPACKET__SET, NS_REGISTER); | |
956 xmlnode_put_attrib(x, "to", gjc->user->server); | |
957 y = xmlnode_get_tag(x, "query"); | |
958 z = xmlnode_insert_tag(y, "username"); | |
959 xmlnode_insert_cdata(z, gjc->user->user, -1); | |
960 z = xmlnode_insert_tag(y, "password"); | |
961 xmlnode_insert_cdata(z, new, -1); | |
962 | |
963 id = gjab_getid(gjc); | |
964 xmlnode_put_attrib(x, "id", id); | |
965 | |
966 free(gjc->pass); | |
967 gjc->pass = strdup(new); | |
968 | |
969 g_hash_table_insert(gjc->queries, g_strdup(id), g_strdup("change_password")); | |
970 | |
971 gjab_send(gjc, x); | |
972 xmlnode_free(x); | |
973 } | |
974 } | |
3311 | 975 |
976 static struct jabber_buddy_data* jabber_find_buddy(struct gaim_connection *gc, char *buddy) | |
977 { | |
978 struct jabber_data *jd = gc->proto_data; | |
979 gpointer val; | |
980 char *realwho; | |
981 | |
982 if((realwho = get_realwho(jd->gjc, buddy, FALSE, NULL)) == NULL) | |
983 return NULL; | |
984 | |
985 val = g_hash_table_lookup(jd->buddies, realwho); | |
986 if(val) { | |
987 g_free(realwho); | |
988 return (struct jabber_buddy_data *)val; | |
989 | |
990 } else { | |
991 struct jabber_buddy_data *jbd = g_new0(struct jabber_buddy_data, 1); | |
992 jbd->error_msg = NULL; | |
993 jbd->resources = NULL; | |
994 g_hash_table_insert(jd->buddies, g_strdup(realwho), jbd); | |
995 g_free(realwho); | |
996 return jbd; | |
997 } | |
998 } | |
3257 | 999 |
3236 | 1000 /* |
3311 | 1001 * find a resource by name, or if no name given, return the "default" resource |
1002 * default being the highest priority one. | |
1003 */ | |
1004 | |
1005 static jab_res_info jabber_find_resource(struct gaim_connection *gc, char *who) | |
1006 { | |
1007 GSList *resources; | |
1008 struct jabber_buddy_data *jbd = jabber_find_buddy(gc, who); | |
1009 jab_res_info jri = NULL; | |
1010 char *res = strstr(who, "/"); | |
1011 | |
1012 if(res) | |
1013 res++; | |
1014 | |
1015 if(jbd) | |
1016 { | |
1017 resources = jbd->resources; | |
1018 while(resources) | |
1019 { | |
1020 if(!jri && !res) { | |
1021 jri = (jab_res_info) resources->data; | |
1022 } else if(!res) { /* we're looking for the default priority, so... */ | |
1023 if(((jab_res_info) resources->data)->priority >= jri->priority) | |
1024 jri = (jab_res_info) resources->data; | |
1025 } else { | |
1026 if(!strcasecmp(((jab_res_info) resources->data)->name, res)) { | |
1027 jri = (jab_res_info) resources->data; | |
1028 break; | |
1029 } | |
1030 } | |
1031 resources = resources->next; | |
1032 } | |
1033 } | |
1034 | |
1035 return jri; | |
1036 } | |
1037 | |
1038 /* | |
1039 * if the resource doesn't exist, create it. otherwise, just update the priority | |
2956 | 1040 */ |
3311 | 1041 static void jabber_track_resource(struct gaim_connection *gc, |
1042 char *buddy, | |
1043 char *res, | |
1044 int priority, | |
1045 int state) | |
1046 { | |
1047 struct jabber_buddy_data *jbd = jabber_find_buddy(gc, buddy); | |
1048 | |
1049 if(jbd && res) { | |
1050 char *who = g_strdup_printf("%s/%s", buddy, res); | |
1051 jab_res_info jri = jabber_find_resource(gc, who); | |
1052 g_free(who); | |
1053 if(!jri) { | |
1054 jri = g_new0(struct jabber_resource_info, 1); | |
1055 jri->name = g_strdup(res); | |
1056 jri->away_msg = NULL; | |
1057 jbd->resources = g_slist_append(jbd->resources, jri); | |
1058 } | |
1059 jri->priority = priority; | |
1060 jri->state = state; | |
1061 } | |
1062 } | |
1063 | |
1064 /* | |
1065 * remove the resource, if it exists | |
1066 */ | |
1067 static void jabber_remove_resource(struct gaim_connection *gc, char *buddy, char *res) | |
2956 | 1068 { |
3311 | 1069 struct jabber_buddy_data *jbd = jabber_find_buddy(gc, buddy); |
1070 if(jbd && res) { | |
1071 char *who = g_strdup_printf("%s/%s", buddy, res); | |
1072 jab_res_info jri = jabber_find_resource(gc, who); | |
1073 g_free(who); | |
1074 if(jri) { | |
1075 g_free(jri->name); | |
1076 if(jri->away_msg) | |
1077 g_free(jri->away_msg); | |
1078 jbd->resources = g_slist_remove(jbd->resources, jri); | |
1079 g_free(jri); | |
1080 } | |
1081 } | |
1082 } | |
1083 | |
1084 /* | |
1085 * grab the away message for the default resource | |
1086 */ | |
1087 static char *jabber_lookup_away(gjconn gjc, char *name) | |
1088 { | |
1089 jab_res_info jri = jabber_find_resource(GJ_GC(gjc), name); | |
1090 | |
1091 if(!jri || !jri->away_msg) | |
1092 return _("Unknown"); | |
1093 | |
1094 return jri->away_msg; | |
1095 } | |
1096 | |
1097 static void jabber_track_away(gjconn gjc, jpacket p, char *type) | |
1098 { | |
2956 | 1099 char *show; |
1100 char *vshow = NULL; | |
1101 char *status = NULL; | |
1102 char *msg = NULL; | |
3311 | 1103 jab_res_info jri = NULL; |
1104 | |
1105 if(!p || !p->from || !p->from->resource || !p->from->user) | |
1106 return; | |
1107 | |
1108 jri = jabber_find_resource(GJ_GC(gjc), jid_full(p->from)); | |
1109 | |
1110 if(!jri) | |
1111 return; | |
1112 | |
3105 | 1113 if (type && (strcasecmp(type, "unavailable") == 0)) { |
1114 vshow = _("Unavailable"); | |
1115 } else { | |
1116 if((show = xmlnode_get_tag_data(p->x, "show")) != NULL) { | |
1117 if (!strcasecmp(show, "away")) { | |
1118 vshow = _("Away"); | |
1119 } else if (!strcasecmp(show, "chat")) { | |
1120 vshow = _("Online"); | |
1121 } else if (!strcasecmp(show, "xa")) { | |
1122 vshow = _("Extended Away"); | |
1123 } else if (!strcasecmp(show, "dnd")) { | |
1124 vshow = _("Do Not Disturb"); | |
1125 } | |
2956 | 1126 } |
1127 } | |
1128 | |
3311 | 1129 status = g_strdup(xmlnode_get_tag_data(p->x, "status")); |
2956 | 1130 |
1131 if(vshow != NULL || status != NULL ) { | |
1132 /* kinda hokey, but it works :-) */ | |
1133 msg = g_strdup_printf("%s%s%s", | |
1134 (vshow == NULL? "" : vshow), | |
1135 (vshow == NULL || status == NULL? "" : ": "), | |
1136 (status == NULL? "" : status)); | |
3105 | 1137 } else { |
1138 msg = g_strdup(_("Online")); | |
2956 | 1139 } |
1140 | |
3259 | 1141 g_free(status); |
1142 | |
3311 | 1143 if(jri->away_msg) |
1144 g_free(jri->away_msg); | |
1145 | |
1146 jri->away_msg = msg; | |
1147 } | |
1148 | |
1149 static void jabber_convo_closed(struct gaim_connection *gc, char *name) | |
1150 { | |
1151 jab_res_info jri = jabber_find_resource(gc, name); | |
1152 | |
1153 if(jri) { | |
1154 if(jri->thread_id) | |
1155 g_free(jri->thread_id); | |
1156 | |
1157 jri->thread_id = NULL; | |
2956 | 1158 } |
1159 } | |
1160 | |
3311 | 1161 static void jabber_track_convo_thread(gjconn gjc, char *name, char *thread_id) |
1162 { | |
1163 jab_res_info jri = jabber_find_resource(GJ_GC(gjc), name); | |
1164 | |
1165 if(jri) { | |
1166 if(jri->thread_id) | |
1167 g_free(jri->thread_id); | |
1168 | |
1169 jri->thread_id = g_strdup(thread_id); | |
1170 } | |
1171 } | |
1172 | |
1173 static char *jabber_get_convo_thread(gjconn gjc, char *name) | |
1174 { | |
1175 char *ct = NULL; | |
1176 jab_res_info jri = jabber_find_resource(GJ_GC(gjc), name); | |
1177 | |
1178 if(jri) { | |
1179 if(jri->thread_id) | |
1180 ct = g_strdup(jri->thread_id); | |
1181 } | |
1182 | |
1183 return ct; | |
1184 } | |
1185 | |
1186 | |
3159 | 1187 static time_t iso8601_to_time(char *timestamp) |
1188 { | |
3229 | 1189 struct tm t; |
1190 time_t retval = 0; | |
1191 | |
3311 | 1192 if(sscanf(timestamp, "%04d%02d%02dT%02d:%02d:%02d", |
3229 | 1193 &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec)) |
1194 { | |
1195 t.tm_year -= 1900; | |
1196 t.tm_mon -= 1; | |
1197 t.tm_isdst = 0; | |
1198 retval = mktime(&t); | |
1199 # ifdef HAVE_TM_GMTOFF | |
1200 retval += t.tm_gmtoff; | |
1201 # else | |
1202 # ifdef HAVE_TIMEZONE | |
1203 tzset(); /* making sure */ | |
1204 retval -= timezone; | |
1205 # endif | |
1206 # endif | |
1207 } | |
1208 | |
1209 return retval; | |
3159 | 1210 } |
1211 | |
2956 | 1212 static void jabber_handlemessage(gjconn gjc, jpacket p) |
2086 | 1213 { |
3311 | 1214 xmlnode y, subj; |
3159 | 1215 time_t time_sent = time(NULL); |
3311 | 1216 gboolean typing = FALSE; |
2086 | 1217 |
1218 char *from = NULL, *msg = NULL, *type = NULL, *topic = NULL; | |
3311 | 1219 char *thread_id = NULL; |
1220 char *conference_room = NULL; | |
2086 | 1221 char m[BUF_LONG * 2]; |
1222 | |
1223 type = xmlnode_get_attrib(p->x, "type"); | |
3311 | 1224 |
1225 if ((y = xmlnode_get_tag(p->x, "thread"))) | |
1226 thread_id = xmlnode_get_data(y); | |
1227 | |
1228 y = xmlnode_get_firstchild(p->x); | |
1229 | |
1230 while(y) { | |
1231 if(NSCHECK(y, NS_DELAY)) { | |
1232 char *timestamp = xmlnode_get_attrib(y, "stamp"); | |
1233 time_sent = iso8601_to_time(timestamp); | |
1234 } else if(NSCHECK(y, "jabber:x:event")) { | |
1235 if(xmlnode_get_tag(y, "composing")) | |
1236 typing = TRUE; | |
1237 } else if(NSCHECK(y, "jabber:x:conference")) { | |
1238 conference_room = xmlnode_get_attrib(y, "jid"); | |
1239 } | |
1240 y = xmlnode_get_nextsibling(y); | |
3159 | 1241 } |
1242 | |
2086 | 1243 if (!type || !strcasecmp(type, "normal") || !strcasecmp(type, "chat")) { |
1244 | |
1245 from = jid_full(p->from); | |
1246 /* | |
1247 if ((y = xmlnode_get_tag(p->x, "html"))) { | |
1248 msg = xmlnode_get_data(y); | |
1249 } else | |
1250 */ | |
1251 if ((y = xmlnode_get_tag(p->x, "body"))) { | |
1252 msg = xmlnode_get_data(y); | |
1253 } | |
1254 | |
1255 msg = utf8_to_str(msg); | |
1256 | |
1257 if (!from) | |
1258 return; | |
1259 | |
3311 | 1260 if (conference_room) { |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1261 GList *m = NULL; |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1262 char **data; |
2086 | 1263 |
3311 | 1264 data = g_strsplit(conference_room, "@", 2); |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1265 m = g_list_append(m, g_strdup(data[0])); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1266 m = g_list_append(m, g_strdup(data[1])); |
2956 | 1267 m = g_list_append(m, g_strdup(gjc->user->user)); |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1268 g_strfreev(data); |
2086 | 1269 |
3311 | 1270 serv_got_chat_invite(GJ_GC(gjc), conference_room, from, msg, m); |
2086 | 1271 } else if (msg) { /* whisper */ |
1272 struct jabber_chat *jc; | |
1273 g_snprintf(m, sizeof(m), "%s", msg); | |
2956 | 1274 if (((jc = find_existing_chat(GJ_GC(gjc), p->from)) != NULL) && jc->b) |
3311 | 1275 serv_got_chat_in(GJ_GC(gjc), jc->b->id, p->from->resource, 1, m, |
1276 time_sent); | |
2086 | 1277 else { |
2278
00a8b7bcef6c
[gaim-migrate @ 2288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2232
diff
changeset
|
1278 int flags = 0; |
3311 | 1279 jab_res_info jri = jabber_find_resource(GJ_GC(gjc), jid_full(p->from)); |
1280 if(jri && typing) | |
1281 jri->has_composing = TRUE; | |
2278
00a8b7bcef6c
[gaim-migrate @ 2288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2232
diff
changeset
|
1282 if (xmlnode_get_tag(p->x, "gaim")) |
00a8b7bcef6c
[gaim-migrate @ 2288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2232
diff
changeset
|
1283 flags = IM_FLAG_GAIMUSER; |
3311 | 1284 if ((y = xmlnode_get_tag(p->x, "thread"))) |
1285 jabber_track_convo_thread(gjc, jid_full(p->from), | |
1286 xmlnode_get_data(y)); | |
2086 | 1287 if (find_conversation(jid_full(p->from))) |
3311 | 1288 serv_got_im(GJ_GC(gjc), jid_full(p->from), m, flags, |
1289 time_sent, -1); | |
2086 | 1290 else { |
2956 | 1291 if(p->from->user) { |
3311 | 1292 from = g_strdup_printf("%s@%s", p->from->user, |
1293 p->from->server); | |
2956 | 1294 } else { |
3311 | 1295 /* server message? */ |
1296 from = g_strdup(p->from->server); | |
2956 | 1297 } |
3159 | 1298 serv_got_im(GJ_GC(gjc), from, m, flags, time_sent, -1); |
2086 | 1299 g_free(from); |
1300 } | |
1301 } | |
3311 | 1302 } else { |
1303 /* a non-message message! */ | |
1304 from = g_strdup_printf("%s@%s", p->from->user, p->from->server); | |
1305 if(typing) | |
1306 serv_got_typing(GJ_GC(gjc), from, 0); | |
1307 else | |
1308 serv_got_typing_stopped(GJ_GC(gjc), from); | |
1309 g_free(from); | |
2086 | 1310 } |
1311 | |
1312 if (msg) | |
1313 g_free(msg); | |
1314 | |
1315 } else if (!strcasecmp(type, "error")) { | |
1316 if ((y = xmlnode_get_tag(p->x, "error"))) { | |
1317 type = xmlnode_get_attrib(y, "code"); | |
1318 msg = xmlnode_get_data(y); | |
1319 } | |
1320 | |
1321 if (msg) { | |
1322 from = g_strdup_printf("Error %s", type ? type : ""); | |
1323 do_error_dialog(msg, from); | |
1324 g_free(from); | |
1325 } | |
1326 } else if (!strcasecmp(type, "groupchat")) { | |
1327 struct jabber_chat *jc; | |
1328 static int i = 0; | |
1329 | |
1330 /* | |
1331 if ((y = xmlnode_get_tag(p->x, "html"))) { | |
1332 msg = xmlnode_get_data(y); | |
1333 } else | |
1334 */ | |
1335 if ((y = xmlnode_get_tag(p->x, "body"))) { | |
1336 msg = xmlnode_get_data(y); | |
1337 } | |
1338 | |
1339 msg = utf8_to_str(msg); | |
1340 | |
1341 if ((subj = xmlnode_get_tag(p->x, "subject"))) { | |
1342 topic = xmlnode_get_data(subj); | |
1343 } | |
1344 topic = utf8_to_str(topic); | |
1345 | |
2956 | 1346 jc = find_existing_chat(GJ_GC(gjc), p->from); |
2086 | 1347 if (!jc) { |
1348 /* we're not in this chat. are we supposed to be? */ | |
2956 | 1349 if ((jc = find_pending_chat(GJ_GC(gjc), p->from)) != NULL) { |
2086 | 1350 /* yes, we're supposed to be. so now we are. */ |
2956 | 1351 jc->b = serv_got_joined_chat(GJ_GC(gjc), i++, p->from->user); |
2086 | 1352 jc->id = jc->b->id; |
2956 | 1353 jc->state = JCS_ACTIVE; |
2086 | 1354 } else { |
1355 /* no, we're not supposed to be. */ | |
1356 g_free(msg); | |
1357 return; | |
1358 } | |
1359 } | |
1360 if (p->from->resource) { | |
1361 if (!y) { | |
2956 | 1362 if (!find_chat_buddy(jc->b, p->from->resource)) { |
2086 | 1363 add_chat_buddy(jc->b, p->from->resource); |
2956 | 1364 } else if ((y = xmlnode_get_tag(p->x, "status"))) { |
3311 | 1365 jabber_track_away(gjc, p, NULL); |
2086 | 1366 } |
1367 } else if (jc->b && msg) { | |
1368 char buf[8192]; | |
1369 | |
1370 if (topic) { | |
1371 char tbuf[8192]; | |
1372 g_snprintf(tbuf, sizeof(tbuf), "%s", topic); | |
1373 chat_set_topic(jc->b, p->from->resource, tbuf); | |
1374 } | |
1375 | |
1376 g_snprintf(buf, sizeof(buf), "%s", msg); | |
3311 | 1377 serv_got_chat_in(GJ_GC(gjc), jc->b->id, p->from->resource, 0, buf, |
1378 time_sent); | |
2086 | 1379 } |
1380 } else { /* message from the server */ | |
1381 if(jc->b && topic) { | |
1382 char tbuf[8192]; | |
1383 g_snprintf(tbuf, sizeof(tbuf), "%s", topic); | |
1384 chat_set_topic(jc->b, "", tbuf); | |
1385 } | |
1386 } | |
1387 | |
1388 g_free(msg); | |
1389 g_free(topic); | |
1390 | |
1391 } else { | |
1392 debug_printf("unhandled message %s\n", type); | |
1393 } | |
1394 } | |
3159 | 1395 |
2956 | 1396 static void jabber_handlepresence(gjconn gjc, jpacket p) |
2086 | 1397 { |
1398 char *to, *from, *type; | |
1399 struct buddy *b = NULL; | |
3311 | 1400 gaim_jid gjid; |
2086 | 1401 char *buddy; |
3194 | 1402 xmlnode y; |
2086 | 1403 char *show; |
2501
227cc42ffa6e
[gaim-migrate @ 2514]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
1404 int state = 0; |
2086 | 1405 struct conversation *cnv = NULL; |
1406 struct jabber_chat *jc = NULL; | |
3311 | 1407 int priority = 0; |
1408 struct jabber_buddy_data *jbd; | |
1409 | |
2086 | 1410 to = xmlnode_get_attrib(p->x, "to"); |
1411 from = xmlnode_get_attrib(p->x, "from"); | |
1412 type = xmlnode_get_attrib(p->x, "type"); | |
3150 | 1413 |
3311 | 1414 if((buddy = get_realwho(gjc, from, FALSE, &gjid)) == NULL) |
1415 return; | |
1416 | |
1417 if (gjid->user == NULL) { | |
1418 /* FIXME: transport */ | |
1419 g_free(buddy); | |
1420 gaim_jid_free(gjid); | |
1421 return; | |
1422 } | |
1423 | |
1424 jbd = jabber_find_buddy(GJ_GC(gjc), buddy); | |
1425 | |
1426 if(jbd->error_msg) { | |
1427 g_free(jbd->error_msg); | |
1428 jbd->error_msg = NULL; | |
1429 } | |
1430 | |
3259 | 1431 if(type && !strcasecmp(type, "error")) { |
1432 state = UC_ERROR; | |
3311 | 1433 if((y = xmlnode_get_tag(p->x, "error")) != NULL) { |
1434 jbd->error_msg = g_strdup_printf(_("Error %s: %s"), | |
1435 xmlnode_get_attrib(y, "code"), xmlnode_get_data(y)); | |
1436 } else { | |
1437 jbd->error_msg = g_strdup(_("Unknown Error in presence")); | |
1438 } | |
3259 | 1439 } else { |
1440 if ((y = xmlnode_get_tag(p->x, "show"))) { | |
1441 show = xmlnode_get_data(y); | |
1442 if (!show) { | |
1443 state = 0; | |
1444 } else if (!strcasecmp(show, "away")) { | |
1445 state = UC_AWAY; | |
1446 } else if (!strcasecmp(show, "chat")) { | |
1447 state = UC_CHAT; | |
1448 } else if (!strcasecmp(show, "xa")) { | |
1449 state = UC_XA; | |
1450 } else if (!strcasecmp(show, "dnd")) { | |
1451 state = UC_DND; | |
1452 } | |
1453 } else { | |
2501
227cc42ffa6e
[gaim-migrate @ 2514]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
1454 state = 0; |
2086 | 1455 } |
1456 } | |
1457 | |
3311 | 1458 if ((y = xmlnode_get_tag(p->x, "priority"))) |
1459 priority = atoi(xmlnode_get_data(y)); | |
2086 | 1460 |
1461 /* um. we're going to check if it's a chat. if it isn't, and there are pending | |
2956 | 1462 * chats, create the chat. if there aren't pending chats and we don't have the |
1463 * buddy on our list, simply bail out. */ | |
3311 | 1464 if ((cnv = find_chat(GJ_GC(gjc), gjid->user)) == NULL) { |
2086 | 1465 static int i = 0x70; |
3311 | 1466 if ((jc = find_pending_chat(GJ_GC(gjc), gjid)) != NULL) { |
1467 jc->b = cnv = serv_got_joined_chat(GJ_GC(gjc), i++, gjid->user); | |
2086 | 1468 jc->id = jc->b->id; |
2956 | 1469 jc->state = JCS_ACTIVE; |
1470 } else if ((b = find_buddy(GJ_GC(gjc), buddy)) == NULL) { | |
1471 g_free(buddy); | |
3311 | 1472 gaim_jid_free(gjid); |
2956 | 1473 return; |
2086 | 1474 } |
1475 } | |
1476 | |
3311 | 1477 if (type && (strcasecmp(type, "unavailable") == 0)) |
1478 jabber_remove_resource(GJ_GC(gjc), buddy, gjid->resource); | |
1479 else { | |
1480 jabber_track_resource(GJ_GC(gjc), buddy, gjid->resource, priority, state); | |
1481 | |
1482 /* keep track of away msg somewhat the same as the yahoo plugin */ | |
1483 jabber_track_away(gjc, p, type); | |
1484 } | |
1485 | |
1486 | |
2086 | 1487 if (!cnv) { |
3311 | 1488 /* this is where we handle presence information for "regular" buddies */ |
1489 jab_res_info jri = jabber_find_resource(GJ_GC(gjc), buddy); | |
1490 if(jri) { | |
1491 serv_got_update(GJ_GC(gjc), buddy, 1, 0, b->signon, b->idle, jri->state, 0); | |
1492 } else | |
1493 serv_got_update(GJ_GC(gjc), buddy, 0, 0, 0, 0, 0, 0); | |
1494 | |
2086 | 1495 } else { |
3311 | 1496 if (gjid->resource) { |
3259 | 1497 if (type && (!strcasecmp(type, "unavailable"))) { |
2086 | 1498 struct jabber_data *jd; |
3311 | 1499 if (!jc && !(jc = find_existing_chat(GJ_GC(gjc), gjid))) { |
2086 | 1500 g_free(buddy); |
3311 | 1501 gaim_jid_free(gjid); |
2086 | 1502 return; |
1503 } | |
1504 jd = jc->gc->proto_data; | |
2956 | 1505 /* if it's not ourselves...*/ |
3311 | 1506 if (strcmp(gjid->resource, jc->gjid->resource) && jc->b) { |
1507 remove_chat_buddy(jc->b, gjid->resource, NULL); | |
2086 | 1508 g_free(buddy); |
3311 | 1509 gaim_jid_free(gjid); |
2086 | 1510 return; |
1511 } | |
2956 | 1512 |
1513 jc->state = JCS_CLOSED; | |
1514 serv_got_chat_left(GJ_GC(gjc), jc->id); | |
1515 /* | |
1516 * TBD: put back some day? | |
1517 jd->chats = g_slist_remove(jd->chats, jc); | |
1518 g_free(jc); | |
1519 */ | |
1520 } else { | |
3311 | 1521 if ((!jc && !(jc = find_existing_chat(GJ_GC(gjc), gjid))) || !jc->b) { |
2956 | 1522 g_free(buddy); |
3311 | 1523 gaim_jid_free(gjid); |
2956 | 1524 return; |
1525 } | |
3311 | 1526 if (!find_chat_buddy(jc->b, gjid->resource)) { |
1527 add_chat_buddy(jc->b, gjid->resource); | |
2086 | 1528 } |
1529 } | |
1530 } | |
1531 } | |
1532 | |
1533 g_free(buddy); | |
3311 | 1534 gaim_jid_free(gjid); |
2086 | 1535 |
1536 return; | |
1537 } | |
1538 | |
3229 | 1539 /* |
1540 * Used only by Jabber accept/deny add stuff just below | |
1541 */ | |
1542 struct jabber_add_permit { | |
1543 gjconn gjc; | |
1544 gchar *user; | |
1545 }; | |
1546 | |
1547 /* | |
1548 * Common part for Jabber accept/deny adds | |
1549 * | |
1550 * "type" says whether we'll permit/deny the subscribe request | |
1551 */ | |
1552 static void jabber_accept_deny_add(struct jabber_add_permit *jap, const char *type) | |
1553 { | |
1554 xmlnode g = xmlnode_new_tag("presence"); | |
1555 | |
1556 xmlnode_put_attrib(g, "to", jap->user); | |
1557 xmlnode_put_attrib(g, "type", type); | |
1558 gjab_send(jap->gjc, g); | |
1559 | |
1560 xmlnode_free(g); | |
1561 } | |
1562 | |
1563 /* | |
1564 * Callback from "accept" in do_ask_dialog() invoked by jabber_handles10n() | |
1565 */ | |
1566 static void jabber_accept_add(gpointer w, struct jabber_add_permit *jap) | |
1567 { | |
1568 jabber_accept_deny_add(jap, "subscribed"); | |
1569 /* | |
1570 * If we don't already have the buddy on *our* buddylist, | |
1571 * ask if we want him or her added. | |
1572 */ | |
1573 if(find_buddy(GJ_GC(jap->gjc), jap->user) == NULL) { | |
1574 show_got_added(GJ_GC(jap->gjc), NULL, jap->user, NULL, NULL); | |
1575 } | |
1576 g_free(jap->user); | |
1577 g_free(jap); | |
1578 } | |
1579 | |
1580 /* | |
1581 * Callback from "deny/cancel" in do_ask_dialog() invoked by jabber_handles10n() | |
1582 */ | |
1583 static void jabber_deny_add(gpointer w, struct jabber_add_permit *jap) | |
1584 { | |
1585 jabber_accept_deny_add(jap, "unsubscribed"); | |
1586 g_free(jap->user); | |
1587 g_free(jap); | |
1588 } | |
1589 | |
1590 /* | |
1591 * Handle subscription requests | |
1592 */ | |
2956 | 1593 static void jabber_handles10n(gjconn gjc, jpacket p) |
2086 | 1594 { |
1595 xmlnode g; | |
1596 char *Jid = xmlnode_get_attrib(p->x, "from"); | |
3136 | 1597 char *type = xmlnode_get_attrib(p->x, "type"); |
2086 | 1598 |
1599 g = xmlnode_new_tag("presence"); | |
1600 xmlnode_put_attrib(g, "to", Jid); | |
3229 | 1601 |
1602 if (!strcmp(type, "subscribe")) { | |
1603 /* | |
1604 * A "subscribe to us" request was received - put up the approval dialog | |
1605 */ | |
1606 struct jabber_add_permit *jap = g_new0(struct jabber_add_permit, 1); | |
1607 gchar *msg = g_strdup_printf(_("The user %s wants to add you to their buddy list."), | |
1608 Jid); | |
1609 | |
1610 jap->gjc = gjc; | |
1611 jap->user = g_strdup(Jid); | |
1612 do_ask_dialog(msg, jap, jabber_accept_add, jabber_deny_add); | |
1613 | |
1614 g_free(msg); | |
1615 xmlnode_free(g); /* Never needed it here anyway */ | |
1616 return; | |
1617 | |
1618 } else if (!strcmp(type, "unsubscribe")) { | |
1619 /* | |
1620 * An "unsubscribe to us" was received - simply "approve" it | |
1621 */ | |
2086 | 1622 xmlnode_put_attrib(g, "type", "unsubscribed"); |
3229 | 1623 } else { |
1624 /* | |
1625 * Did we attempt to subscribe to somebody and they do not exist? | |
1626 */ | |
3136 | 1627 if (!strcmp(type, "unsubscribed")) { |
1628 xmlnode y; | |
1629 char *status; | |
1630 if((y = xmlnode_get_tag(p->x, "status")) && (status = xmlnode_get_data(y)) && | |
1631 !strcmp(status, "Not Found")) { | |
1632 char *msg = g_strdup_printf("%s: \"%s\"", _("No such user"), | |
1633 xmlnode_get_attrib(p->x, "from")); | |
1634 do_error_dialog(msg, _("Jabber Error")); | |
1635 g_free(msg); | |
1636 } | |
1637 } | |
1638 | |
2956 | 1639 xmlnode_free(g); |
2086 | 1640 return; |
2956 | 1641 } |
1642 | |
1643 gjab_send(gjc, g); | |
1644 xmlnode_free(g); | |
2086 | 1645 } |
1646 | |
2956 | 1647 /* |
1648 * Pending subscription to a buddy? | |
1649 */ | |
1650 #define BUD_SUB_TO_PEND(sub, ask) ((!strcasecmp((sub), "none") || !strcasecmp((sub), "from")) && \ | |
1651 (ask) != NULL && !strcasecmp((ask), "subscribe")) | |
1652 | |
1653 /* | |
1654 * Subscribed to a buddy? | |
1655 */ | |
1656 #define BUD_SUBD_TO(sub, ask) ((!strcasecmp((sub), "to") || !strcasecmp((sub), "both")) && \ | |
1657 ((ask) == NULL || !strcasecmp((ask), "subscribe"))) | |
1658 | |
1659 /* | |
1660 * Pending unsubscription to a buddy? | |
1661 */ | |
1662 #define BUD_USUB_TO_PEND(sub, ask) ((!strcasecmp((sub), "to") || !strcasecmp((sub), "both")) && \ | |
1663 (ask) != NULL && !strcasecmp((ask), "unsubscribe")) | |
1664 | |
1665 /* | |
1666 * Unsubscribed to a buddy? | |
1667 */ | |
1668 #define BUD_USUBD_TO(sub, ask) ((!strcasecmp((sub), "none") || !strcasecmp((sub), "from")) && \ | |
1669 ((ask) == NULL || !strcasecmp((ask), "unsubscribe"))) | |
1670 | |
1671 /* | |
1672 * If a buddy is added or removed from the roster on another resource | |
1673 * jabber_handlebuddy is called | |
1674 * | |
1675 * Called with roster item node. | |
1676 */ | |
1677 static void jabber_handlebuddy(gjconn gjc, xmlnode x) | |
1678 { | |
1679 xmlnode g; | |
3311 | 1680 char *who, *name, *sub, *ask; |
1681 gaim_jid gjid; | |
2956 | 1682 struct buddy *b = NULL; |
3136 | 1683 char *buddyname, *groupname = NULL; |
2956 | 1684 |
3311 | 1685 who = xmlnode_get_attrib(x, "jid"); |
2956 | 1686 name = xmlnode_get_attrib(x, "name"); |
1687 sub = xmlnode_get_attrib(x, "subscription"); | |
1688 ask = xmlnode_get_attrib(x, "ask"); | |
3311 | 1689 |
1690 if((buddyname = get_realwho(gjc, who, FALSE, &gjid)) == NULL) | |
1691 return; | |
2956 | 1692 |
1693 /* JFIXME: jabber_handleroster() had a "FIXME: transport" at this | |
1694 * equivilent point. So... | |
1695 * | |
3311 | 1696 * We haven't done anything interesting to this point, so we'll |
1697 * violate Good Coding Structure here by simply bailing out. | |
2956 | 1698 */ |
3311 | 1699 if(!gjid->user) { |
1700 g_free(buddyname); | |
1701 gaim_jid_free(gjid); | |
2956 | 1702 return; |
1703 } | |
3311 | 1704 gaim_jid_free(gjid); |
2956 | 1705 |
3236 | 1706 if((g = xmlnode_get_tag(x, "group")) != NULL) { |
3136 | 1707 groupname = xmlnode_get_data(g); |
2956 | 1708 } |
1709 | |
3059 | 1710 /* |
3136 | 1711 * Add or remove a buddy? Change buddy's alias or group? |
3059 | 1712 */ |
3328 | 1713 if(name) |
1714 name = utf8_to_str(name); | |
1715 | |
2956 | 1716 if (BUD_SUB_TO_PEND(sub, ask) || BUD_SUBD_TO(sub, ask)) { |
1717 if ((b = find_buddy(GJ_GC(gjc), buddyname)) == NULL) { | |
1718 debug_printf("adding buddy [4]: %s\n", buddyname); | |
3136 | 1719 b = add_buddy(GJ_GC(gjc), groupname ? groupname : _("Buddies"), buddyname, |
2956 | 1720 name ? name : buddyname); |
1721 do_export(GJ_GC(gjc)); | |
3136 | 1722 } else { |
1723 struct group *c_grp = find_group_by_buddy(GJ_GC(gjc), buddyname); | |
1724 | |
1725 /* | |
1726 * If the buddy's in a new group or his/her alias is changed... | |
1727 */ | |
1728 if(groupname && c_grp && strcmp(c_grp->name, groupname)) { | |
1729 int present = b->present; /* save presence state */ | |
1730 int uc = b->uc; /* and away state (?) */ | |
3150 | 1731 int idle = b->idle; |
1732 int signon = b->signon; | |
3136 | 1733 |
1734 /* | |
1735 * seems rude, but it seems to be the only way... | |
1736 */ | |
1737 remove_buddy(GJ_GC(gjc), c_grp, b); | |
1738 b = add_buddy(GJ_GC(gjc), groupname, buddyname, | |
1739 name ? name : buddyname); | |
1740 do_export(GJ_GC(gjc)); | |
1741 if(present) { | |
3311 | 1742 serv_got_update(GJ_GC(gjc), buddyname, 1, 0, signon, idle, |
1743 uc, 0); | |
3136 | 1744 } |
1745 } else if(name != NULL && strcmp(b->show, name)) { | |
1746 strncpy(b->show, name, BUDDY_ALIAS_MAXLEN); | |
1747 b->show[BUDDY_ALIAS_MAXLEN - 1] = '\0'; /* cheap safety feature */ | |
1748 handle_buddy_rename(b, buddyname); | |
1749 } | |
2956 | 1750 } |
1751 } else if (BUD_USUB_TO_PEND(sub, ask) || BUD_USUBD_TO(sub, ask) || !strcasecmp(sub, "remove")) { | |
3236 | 1752 jabber_remove_gaim_buddy(GJ_GC(gjc), buddyname); |
2956 | 1753 } |
3328 | 1754 |
1755 if(name) | |
1756 g_free(name); | |
1757 | |
2956 | 1758 g_free(buddyname); |
1759 | |
1760 } | |
1761 | |
1762 static void jabber_handleroster(gjconn gjc, xmlnode querynode) | |
2086 | 1763 { |
1764 xmlnode x; | |
1765 | |
1766 x = xmlnode_get_firstchild(querynode); | |
1767 while (x) { | |
2956 | 1768 jabber_handlebuddy(gjc, x); |
2086 | 1769 x = xmlnode_get_nextsibling(x); |
1770 } | |
1771 | |
1772 x = jutil_presnew(0, NULL, "Online"); | |
2956 | 1773 gjab_send(gjc, x); |
2086 | 1774 xmlnode_free(x); |
1775 } | |
1776 | |
2956 | 1777 static void jabber_handleauthresp(gjconn gjc, jpacket p) |
2086 | 1778 { |
1779 if (jpacket_subtype(p) == JPACKET__RESULT) { | |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1780 if (xmlnode_has_children(p->x)) { |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1781 xmlnode query = xmlnode_get_tag(p->x, "query"); |
2975 | 1782 set_login_progress(GJ_GC(gjc), 4, _("Authenticating")); |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1783 if (!xmlnode_get_tag(query, "digest")) { |
2956 | 1784 g_free(gjc->sid); |
1785 gjc->sid = NULL; | |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1786 } |
2956 | 1787 gjab_auth(gjc); |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1788 } else { |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1789 debug_printf("auth success\n"); |
2086 | 1790 |
2956 | 1791 account_online(GJ_GC(gjc)); |
1792 serv_finish_login(GJ_GC(gjc)); | |
1793 | |
1794 if (bud_list_cache_exists(GJ_GC(gjc))) | |
1795 do_import(GJ_GC(gjc), NULL); | |
1796 | |
1797 ((struct jabber_data *)GJ_GC(gjc)->proto_data)->did_import = TRUE; | |
1798 | |
1799 gjab_reqroster(gjc); | |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1800 } |
2086 | 1801 } else { |
1802 xmlnode xerr; | |
1803 char *errmsg = NULL; | |
1804 int errcode = 0; | |
2956 | 1805 struct jabber_data *jd = GJ_GC(gjc)->proto_data; |
2086 | 1806 |
1807 debug_printf("auth failed\n"); | |
1808 xerr = xmlnode_get_tag(p->x, "error"); | |
1809 if (xerr) { | |
1810 char msg[BUF_LONG]; | |
1811 errmsg = xmlnode_get_data(xerr); | |
1812 if (xmlnode_get_attrib(xerr, "code")) { | |
1813 errcode = atoi(xmlnode_get_attrib(xerr, "code")); | |
1814 g_snprintf(msg, sizeof(msg), "Error %d: %s", errcode, errmsg); | |
1815 } else | |
1816 g_snprintf(msg, sizeof(msg), "%s", errmsg); | |
2956 | 1817 hide_login_progress(GJ_GC(gjc), msg); |
2086 | 1818 } else { |
2975 | 1819 hide_login_progress(GJ_GC(gjc), _("Unknown login error")); |
2086 | 1820 } |
1821 | |
2800
0ad63a625eec
[gaim-migrate @ 2813]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2791
diff
changeset
|
1822 jd->die = TRUE; |
2086 | 1823 } |
1824 } | |
1825 | |
2956 | 1826 static void jabber_handleversion(gjconn gjc, xmlnode iqnode) { |
2086 | 1827 xmlnode querynode, x; |
1828 char *id, *from; | |
1829 char os[1024]; | |
1830 struct utsname osinfo; | |
1831 | |
1832 uname(&osinfo); | |
1833 g_snprintf(os, sizeof os, "%s %s %s", osinfo.sysname, osinfo.release, osinfo.machine); | |
1834 | |
1835 id = xmlnode_get_attrib(iqnode, "id"); | |
1836 from = xmlnode_get_attrib(iqnode, "from"); | |
1837 | |
1838 x = jutil_iqnew(JPACKET__RESULT, NS_VERSION); | |
1839 | |
1840 xmlnode_put_attrib(x, "to", from); | |
1841 xmlnode_put_attrib(x, "id", id); | |
1842 querynode = xmlnode_get_tag(x, "query"); | |
1843 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "name"), PACKAGE, -1); | |
1844 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "version"), VERSION, -1); | |
1845 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "os"), os, -1); | |
1846 | |
2956 | 1847 gjab_send(gjc, x); |
2086 | 1848 |
1849 xmlnode_free(x); | |
1850 } | |
1851 | |
2956 | 1852 static void jabber_handletime(gjconn gjc, xmlnode iqnode) { |
2086 | 1853 xmlnode querynode, x; |
1854 char *id, *from; | |
1855 time_t now_t; | |
1856 struct tm *now; | |
1857 char buf[1024]; | |
1858 | |
1859 time(&now_t); | |
1860 now = localtime(&now_t); | |
1861 | |
1862 id = xmlnode_get_attrib(iqnode, "id"); | |
1863 from = xmlnode_get_attrib(iqnode, "from"); | |
1864 | |
1865 x = jutil_iqnew(JPACKET__RESULT, NS_TIME); | |
1866 | |
1867 xmlnode_put_attrib(x, "to", from); | |
1868 xmlnode_put_attrib(x, "id", id); | |
1869 querynode = xmlnode_get_tag(x, "query"); | |
1870 | |
1871 strftime(buf, 1024, "%Y%m%dT%T", now); | |
1872 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "utc"), buf, -1); | |
1873 strftime(buf, 1024, "%Z", now); | |
1874 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "tz"), buf, -1); | |
1875 strftime(buf, 1024, "%d %b %Y %T", now); | |
1876 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "display"), buf, -1); | |
1877 | |
2956 | 1878 gjab_send(gjc, x); |
2086 | 1879 |
1880 xmlnode_free(x); | |
1881 } | |
1882 | |
2956 | 1883 static void jabber_handlelast(gjconn gjc, xmlnode iqnode) { |
2086 | 1884 xmlnode x, querytag; |
1885 char *id, *from; | |
2956 | 1886 struct jabber_data *jd = GJ_GC(gjc)->proto_data; |
2086 | 1887 char idle_time[32]; |
1888 | |
1889 id = xmlnode_get_attrib(iqnode, "id"); | |
1890 from = xmlnode_get_attrib(iqnode, "from"); | |
1891 | |
1892 x = jutil_iqnew(JPACKET__RESULT, "jabber:iq:last"); | |
1893 | |
1894 xmlnode_put_attrib(x, "to", from); | |
1895 xmlnode_put_attrib(x, "id", id); | |
1896 querytag = xmlnode_get_tag(x, "query"); | |
1897 g_snprintf(idle_time, sizeof idle_time, "%ld", jd->idle ? time(NULL) - jd->idle : 0); | |
1898 xmlnode_put_attrib(querytag, "seconds", idle_time); | |
1899 | |
2956 | 1900 gjab_send(gjc, x); |
2086 | 1901 xmlnode_free(x); |
1902 } | |
1903 | |
2956 | 1904 /* |
1905 * delete == TRUE: delete found entry | |
1906 * | |
1907 * returns pointer to (local) copy of value if found, NULL otherwise | |
1908 * | |
1909 * Note: non-reentrant! Local static storage re-used on subsequent calls. | |
1910 * If you're going to need to keep the returned value, make a copy! | |
1911 */ | |
1912 static gchar *jabber_track_queries(GHashTable *queries, gchar *key, gboolean delete) | |
1913 { | |
1914 gpointer my_key, my_val; | |
1915 static gchar *ret_val = NULL; | |
1916 | |
1917 if(ret_val != NULL) { | |
1918 g_free(ret_val); | |
1919 ret_val = NULL; | |
1920 } | |
1921 | |
1922 /* self-protection */ | |
1923 if(queries != NULL && key != NULL) { | |
1924 if(g_hash_table_lookup_extended(queries, key, &my_key, &my_val)) { | |
1925 ret_val = g_strdup((gchar *) my_val); | |
1926 if(delete) { | |
1927 g_hash_table_remove(queries, key); | |
1928 g_free(my_key); | |
1929 g_free(my_val); | |
1930 } | |
1931 } | |
1932 } | |
1933 | |
1934 return(ret_val); | |
1935 } | |
1936 | |
1937 static void jabber_handlepacket(gjconn gjc, jpacket p) | |
2086 | 1938 { |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1939 char *id; |
2086 | 1940 switch (p->type) { |
1941 case JPACKET_MESSAGE: | |
2956 | 1942 jabber_handlemessage(gjc, p); |
2086 | 1943 break; |
1944 case JPACKET_PRESENCE: | |
2956 | 1945 jabber_handlepresence(gjc, p); |
2086 | 1946 break; |
1947 case JPACKET_IQ: | |
1948 debug_printf("jpacket_subtype: %d\n", jpacket_subtype(p)); | |
1949 | |
2956 | 1950 id = xmlnode_get_attrib(p->x, "id"); |
1951 if (id != NULL && !strcmp(id, IQID_AUTH)) { | |
1952 jabber_handleauthresp(gjc, p); | |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1953 break; |
2086 | 1954 } |
1955 | |
1956 if (jpacket_subtype(p) == JPACKET__SET) { | |
2956 | 1957 xmlnode querynode; |
1958 querynode = xmlnode_get_tag(p->x, "query"); | |
1959 if (NSCHECK(querynode, "jabber:iq:roster")) { | |
1960 jabber_handlebuddy(gjc, xmlnode_get_firstchild(querynode)); | |
1961 } | |
2086 | 1962 } else if (jpacket_subtype(p) == JPACKET__GET) { |
1963 xmlnode querynode; | |
1964 querynode = xmlnode_get_tag(p->x, "query"); | |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1965 if (NSCHECK(querynode, NS_VERSION)) { |
2956 | 1966 jabber_handleversion(gjc, p->x); |
2086 | 1967 } else if (NSCHECK(querynode, NS_TIME)) { |
2956 | 1968 jabber_handletime(gjc, p->x); |
2086 | 1969 } else if (NSCHECK(querynode, "jabber:iq:last")) { |
2956 | 1970 jabber_handlelast(gjc, p->x); |
2086 | 1971 } |
1972 } else if (jpacket_subtype(p) == JPACKET__RESULT) { | |
1973 xmlnode querynode, vcard; | |
1974 char *xmlns, *from; | |
1975 | |
2956 | 1976 /* |
1977 * TBD: ISTM maybe this part could use a serious re-work? | |
1978 */ | |
2086 | 1979 from = xmlnode_get_attrib(p->x, "from"); |
1980 querynode = xmlnode_get_tag(p->x, "query"); | |
1981 vcard = xmlnode_get_tag(p->x, "vCard"); | |
2316
ebb5ecb2cd5b
[gaim-migrate @ 2326]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2309
diff
changeset
|
1982 if (!vcard) |
ebb5ecb2cd5b
[gaim-migrate @ 2326]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2309
diff
changeset
|
1983 vcard = xmlnode_get_tag(p->x, "VCARD"); |
2086 | 1984 |
1985 if (NSCHECK(querynode, NS_ROSTER)) { | |
2956 | 1986 jabber_handleroster(gjc, querynode); |
2086 | 1987 } else if (NSCHECK(querynode, NS_VCARD)) { |
2956 | 1988 jabber_track_queries(gjc->queries, id, TRUE); /* delete query track */ |
1989 jabber_handlevcard(gjc, querynode, from); | |
2316
ebb5ecb2cd5b
[gaim-migrate @ 2326]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2309
diff
changeset
|
1990 } else if (vcard) { |
2956 | 1991 jabber_track_queries(gjc->queries, id, TRUE); /* delete query track */ |
1992 jabber_handlevcard(gjc, vcard, from); | |
1993 } else if((xmlns = xmlnode_get_attrib(querynode, "xmlns")) != NULL) { | |
1994 debug_printf("jabber:iq:query: %s\n", xmlns); | |
2086 | 1995 } else { |
2956 | 1996 char *val; |
1997 | |
1998 debug_printf("jabber:iq: %s\n", xmlnode2str(p->x)); | |
1999 | |
2000 /* handle "null" query results */ | |
2001 if((val = jabber_track_queries(gjc->queries, id, TRUE)) != NULL) { | |
2002 if(strcmp((char *) val, "vCard") == 0) { | |
2003 /* | |
2004 * No actual vCard, but there's other stuff. This | |
2005 * way the user always gets some kind of response. | |
2006 */ | |
2007 jabber_handlevcard(gjc, NULL, from); | |
3257 | 2008 } else if(!strcmp((char *) val, "change_password")) { |
2009 char buf[BUF_LONG]; | |
3311 | 2010 sprintf(buf, _("Password successfully changed.")); |
3257 | 2011 |
2012 do_error_dialog(buf, _("Password Change")); | |
2956 | 2013 } |
2014 } | |
2086 | 2015 } |
2016 | |
2017 } else if (jpacket_subtype(p) == JPACKET__ERROR) { | |
2018 xmlnode xerr; | |
2019 char *from, *errmsg = NULL; | |
2020 int errcode = 0; | |
2021 | |
2022 from = xmlnode_get_attrib(p->x, "from"); | |
2023 xerr = xmlnode_get_tag(p->x, "error"); | |
2024 if (xerr) { | |
2025 errmsg = xmlnode_get_data(xerr); | |
2026 if (xmlnode_get_attrib(xerr, "code")) | |
2027 errcode = atoi(xmlnode_get_attrib(xerr, "code")); | |
2028 } | |
2029 | |
2030 from = g_strdup_printf("Error %d (%s)", errcode, from); | |
2031 do_error_dialog(errmsg, from); | |
2032 g_free(from); | |
2033 | |
2034 } | |
2035 | |
2036 break; | |
2037 case JPACKET_S10N: | |
2956 | 2038 jabber_handles10n(gjc, p); |
2086 | 2039 break; |
2040 default: | |
2041 debug_printf("jabber: packet type %d (%s)\n", p->type, xmlnode2str(p->x)); | |
2042 } | |
2043 | |
2044 xmlnode_free(p->x); | |
2045 | |
2046 return; | |
2047 } | |
2048 | |
2956 | 2049 static void jabber_handlestate(gjconn gjc, int state) |
2086 | 2050 { |
2051 switch (state) { | |
2052 case JCONN_STATE_OFF: | |
3074 | 2053 if(gjc->was_connected) { |
2054 hide_login_progress_error(GJ_GC(gjc), _("Connection lost")); | |
2055 } else { | |
2056 hide_login_progress(GJ_GC(gjc), _("Unable to connect")); | |
2057 } | |
2956 | 2058 signoff(GJ_GC(gjc)); |
2086 | 2059 break; |
2060 case JCONN_STATE_CONNECTED: | |
3074 | 2061 gjc->was_connected = 1; |
2975 | 2062 set_login_progress(GJ_GC(gjc), 2, _("Connected")); |
2086 | 2063 break; |
2064 case JCONN_STATE_ON: | |
2975 | 2065 set_login_progress(GJ_GC(gjc), 3, _("Requesting Authentication Method")); |
2956 | 2066 gjab_reqauth(gjc); |
2086 | 2067 break; |
2068 default: | |
2069 debug_printf("state change: %d\n", state); | |
2070 } | |
2071 return; | |
2072 } | |
2073 | |
2074 static void jabber_login(struct aim_user *user) | |
2075 { | |
2076 struct gaim_connection *gc = new_gaim_conn(user); | |
2077 struct jabber_data *jd = gc->proto_data = g_new0(struct jabber_data, 1); | |
2078 char *loginname = create_valid_jid(user->username, DEFAULT_SERVER, "GAIM"); | |
2079 | |
3311 | 2080 jd->buddies = g_hash_table_new(g_str_hash, g_str_equal); |
2956 | 2081 jd->chats = NULL; /* we have no chats yet */ |
2086 | 2082 |
2975 | 2083 set_login_progress(gc, 1, _("Connecting")); |
2086 | 2084 |
2956 | 2085 if (!(jd->gjc = gjab_new(loginname, user->password, gc))) { |
2086 | 2086 g_free(loginname); |
2087 debug_printf("jabber: unable to connect (jab_new failed)\n"); | |
2975 | 2088 hide_login_progress(gc, _("Unable to connect")); |
2086 | 2089 signoff(gc); |
2090 return; | |
2091 } | |
2092 | |
2093 g_free(loginname); | |
2956 | 2094 gjab_state_handler(jd->gjc, jabber_handlestate); |
2095 gjab_packet_handler(jd->gjc, jabber_handlepacket); | |
2096 jd->gjc->queries = g_hash_table_new(g_str_hash, g_str_equal); | |
2097 gjab_start(jd->gjc); | |
2086 | 2098 } |
2099 | |
2100 static gboolean jabber_destroy_hash(gpointer key, gpointer val, gpointer data) { | |
2101 g_free(key); | |
2102 g_free(val); | |
2103 return TRUE; | |
2104 } | |
2105 | |
3311 | 2106 static gboolean jabber_destroy_buddy_hash(gpointer key, gpointer val, gpointer data) { |
2107 struct jabber_buddy_data *jbd = val; | |
2108 while (jbd->resources) { | |
2109 g_free(((jab_res_info) ((GSList *)jbd->resources)->data)->name); | |
2110 if(((jab_res_info) ((GSList *)jbd->resources)->data)->away_msg) | |
2111 g_free(((jab_res_info) ((GSList *)jbd->resources)->data)->away_msg); | |
2112 g_free(((GSList *)jbd->resources)->data); | |
2113 jbd->resources = g_slist_remove(jbd->resources, ((GSList *)jbd->resources)->data); | |
2114 | |
2115 } | |
2116 if(jbd->error_msg) | |
2117 g_free(jbd->error_msg); | |
2118 g_free(key); | |
2119 g_free(jbd); | |
2120 return TRUE; | |
2121 } | |
2122 | |
2123 | |
2086 | 2124 static gboolean jabber_free(gpointer data) |
2125 { | |
2956 | 2126 struct jabber_data *jd = data; |
2127 | |
3236 | 2128 if(jd->gjc != NULL) { |
2129 gjab_delete(jd->gjc); | |
2130 g_free(jd->gjc->sid); | |
2131 jd->gjc = NULL; | |
2132 } | |
2956 | 2133 g_free(jd); |
2134 | |
2086 | 2135 return FALSE; |
2136 } | |
2137 | |
2138 static void jabber_close(struct gaim_connection *gc) | |
2139 { | |
2140 struct jabber_data *jd = gc->proto_data; | |
2956 | 2141 |
2142 if(jd) { | |
2143 GSList *jcs = jd->chats; | |
2144 | |
2145 /* Free-up the jabber_chat struct allocs and the list */ | |
2146 while (jcs) { | |
3311 | 2147 gaim_jid_free(((struct jabber_chat *)jcs->data)->gjid); |
2956 | 2148 g_free(jcs->data); |
2149 jcs = jcs->next; | |
2150 } | |
2151 g_slist_free(jd->chats); | |
2152 | |
3311 | 2153 /* Free-up the buddy data hash */ |
2154 if(jd->buddies != NULL) | |
2155 { | |
2156 g_hash_table_foreach_remove(jd->buddies, jabber_destroy_buddy_hash, NULL); | |
2157 g_hash_table_destroy(jd->buddies); | |
2158 jd->buddies = NULL; | |
2956 | 2159 } |
2160 | |
2161 /* Free-up the pending queries memories and the list */ | |
3236 | 2162 if(jd->gjc != NULL && jd->gjc->queries != NULL) { |
2956 | 2163 g_hash_table_foreach_remove(jd->gjc->queries, jabber_destroy_hash, NULL); |
2164 g_hash_table_destroy(jd->gjc->queries); | |
2165 jd->gjc->queries = NULL; | |
2166 } | |
2167 } | |
2300
d2686f757d6e
[gaim-migrate @ 2310]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2289
diff
changeset
|
2168 if (gc->inpa) |
d2686f757d6e
[gaim-migrate @ 2310]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2289
diff
changeset
|
2169 gaim_input_remove(gc->inpa); |
2956 | 2170 |
2171 if(jd) { | |
2172 g_timeout_add(50, jabber_free, jd); | |
3236 | 2173 if(jd->gjc != NULL) |
2174 xmlnode_free(jd->gjc->current); | |
2956 | 2175 } |
2086 | 2176 gc->proto_data = NULL; |
2177 } | |
2178 | |
3311 | 2179 static int jabber_send_typing(struct gaim_connection *gc, char *who, int typing) |
2180 { | |
2181 xmlnode x, y; | |
2182 char *realwho; | |
2183 gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; | |
2184 jab_res_info jri = jabber_find_resource(gc, who); | |
2185 | |
2186 if(!jri || !jri->has_composing) | |
2187 return 0; | |
2188 | |
2189 if((realwho = get_realwho(gjc, who, FALSE, NULL)) == NULL) | |
2190 return 0; | |
2191 | |
2192 x = xmlnode_new_tag("message"); | |
2193 xmlnode_put_attrib(x, "to", realwho); | |
2194 xmlnode_insert_tag(x, "gaim"); | |
2195 | |
2196 y = xmlnode_insert_tag(x, "x"); | |
2197 xmlnode_put_attrib(y, "xmlns", "jabber:x:event"); | |
2198 | |
2199 if(typing) | |
2200 xmlnode_insert_tag(y, "composing"); | |
2201 | |
2202 gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); | |
2203 xmlnode_free(x); | |
2204 g_free(realwho); | |
2205 return JABBER_TYPING_NOTIFY_INT; | |
2206 } | |
2207 | |
3033 | 2208 static int jabber_send_im(struct gaim_connection *gc, char *who, char *message, int len, int flags) |
2086 | 2209 { |
2210 xmlnode x, y; | |
2211 char *realwho; | |
3311 | 2212 char *thread_id = NULL; |
2956 | 2213 gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; |
2086 | 2214 |
2215 if (!who || !message) | |
2123
56c4382f2909
[gaim-migrate @ 2133]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2112
diff
changeset
|
2216 return 0; |
2086 | 2217 |
3311 | 2218 if((realwho = get_realwho(gjc, who, FALSE, NULL)) == NULL) |
2219 return 0; | |
2220 | |
2086 | 2221 x = xmlnode_new_tag("message"); |
2222 xmlnode_put_attrib(x, "to", realwho); | |
3311 | 2223 |
2224 thread_id = jabber_get_convo_thread(gjc, realwho); | |
2225 if(thread_id) | |
2226 { | |
2227 y = xmlnode_insert_tag(x, "thread"); | |
2228 xmlnode_insert_cdata(y, thread_id, -1); | |
2229 g_free(thread_id); | |
2230 } | |
2231 | |
2086 | 2232 g_free(realwho); |
2233 | |
2278
00a8b7bcef6c
[gaim-migrate @ 2288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2232
diff
changeset
|
2234 xmlnode_insert_tag(x, "gaim"); |
2086 | 2235 xmlnode_put_attrib(x, "type", "chat"); |
2236 | |
3311 | 2237 /* let other clients know we support typing notification */ |
2238 y = xmlnode_insert_tag(x, "x"); | |
2239 xmlnode_put_attrib(y, "xmlns", "jabber:x:event"); | |
2240 xmlnode_insert_tag(y, "composing"); | |
2241 | |
2086 | 2242 if (message && strlen(message)) { |
2243 char *utf8 = str_to_utf8(message); | |
2244 y = xmlnode_insert_tag(x, "body"); | |
2245 xmlnode_insert_cdata(y, utf8, -1); | |
2246 g_free(utf8); | |
2247 } | |
2248 | |
2956 | 2249 gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); |
2086 | 2250 xmlnode_free(x); |
2303
f5bf315e6104
[gaim-migrate @ 2313]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2300
diff
changeset
|
2251 return 1; |
2086 | 2252 } |
2253 | |
3105 | 2254 /* |
2255 * Add/update buddy's roster entry on server | |
2256 */ | |
2257 static void jabber_roster_update(struct gaim_connection *gc, char *name) | |
2258 { | |
2259 xmlnode x, y; | |
2260 char *realwho; | |
2261 gjconn gjc; | |
2262 struct buddy *buddy = NULL; | |
2263 struct group *buddy_group = NULL; | |
2264 | |
2265 if(gc && gc->proto_data && ((struct jabber_data *)gc->proto_data)->gjc && name) { | |
3311 | 2266 gaim_jid gjid; |
3105 | 2267 gjc = ((struct jabber_data *)gc->proto_data)->gjc; |
2268 | |
3311 | 2269 if((realwho = get_realwho(gjc, name, FALSE, &gjid)) == NULL) |
2270 return; | |
2271 | |
2272 /* FIXME: transport */ | |
2273 if(gjid->user == NULL) { | |
2274 g_free(realwho); | |
2275 gaim_jid_free(gjid); | |
2276 return; | |
3105 | 2277 } |
3311 | 2278 gaim_jid_free(gjid); |
3105 | 2279 |
2280 x = jutil_iqnew(JPACKET__SET, NS_ROSTER); | |
2281 y = xmlnode_insert_tag(xmlnode_get_tag(x, "query"), "item"); | |
2282 xmlnode_put_attrib(y, "jid", realwho); | |
2283 | |
2284 | |
2285 /* If we can find the buddy, there's an alias for him, it's not 0-length | |
2286 * and it doesn't match his JID, add the "name" attribute. | |
2287 */ | |
2288 if((buddy = find_buddy(gc, realwho)) != NULL && | |
3311 | 2289 buddy->show != NULL && buddy->show[0] != '\0' && strcmp(realwho, buddy->show)) |
2290 { | |
2291 char *utf8 = str_to_utf8(buddy->show); | |
2292 xmlnode_put_attrib(y, "name", utf8); | |
2293 g_free(utf8); | |
3105 | 2294 } |
2295 | |
2296 /* | |
2297 * Find out what group the buddy's in and send that along | |
2298 * with the roster item. | |
2299 */ | |
2300 if((buddy_group = find_group_by_buddy(gc, realwho)) != NULL) { | |
2301 xmlnode z; | |
2302 z = xmlnode_insert_tag(y, "group"); | |
2303 xmlnode_insert_cdata(z, buddy_group->name, -1); | |
2304 } | |
2305 | |
2306 gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); | |
2307 | |
2308 xmlnode_free(x); | |
2309 g_free(realwho); | |
2310 } | |
2311 } | |
2312 | |
3136 | 2313 /* |
2314 * Change buddy's group on server roster | |
2315 */ | |
2316 static void jabber_group_change(struct gaim_connection *gc, char *name, char *old_group, char *new_group) | |
2317 { | |
2318 if(strcmp(old_group, new_group)) { | |
2319 jabber_roster_update(gc, name); | |
2320 } | |
2321 } | |
2322 | |
2086 | 2323 static void jabber_add_buddy(struct gaim_connection *gc, char *name) |
2324 { | |
3136 | 2325 xmlnode x; |
2086 | 2326 char *realwho; |
2956 | 2327 gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; |
3311 | 2328 gaim_jid gjid; |
2086 | 2329 |
2330 if (!((struct jabber_data *)gc->proto_data)->did_import) | |
2331 return; | |
2332 | |
3311 | 2333 /* |
2334 * If there's no name or the name is ourself | |
2335 */ | |
2336 if(!name || !strcmp(gc->username, name)) | |
2086 | 2337 return; |
2338 | |
3311 | 2339 if((realwho = get_realwho(gjc, name, FALSE, &gjid)) == NULL) { |
2340 char *msg = g_strdup_printf("%s: \"%s\"", _("Invalid Jabber I.D."), name); | |
2341 do_error_dialog(msg, _("Jabber Error")); | |
2342 g_free(msg); | |
2343 jabber_remove_gaim_buddy(gc, name); | |
2344 return; | |
2086 | 2345 } |
2346 | |
3311 | 2347 /* FIXME: transport */ |
2348 if(gjid->user == NULL) { | |
2349 g_free(realwho); | |
2350 gaim_jid_free(gjid); | |
2351 return; | |
2352 } | |
2353 gaim_jid_free(gjid); | |
2354 | |
2086 | 2355 x = xmlnode_new_tag("presence"); |
2356 xmlnode_put_attrib(x, "to", realwho); | |
2357 xmlnode_put_attrib(x, "type", "subscribe"); | |
2956 | 2358 gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); |
2359 xmlnode_free(x); | |
2086 | 2360 |
3105 | 2361 jabber_roster_update(gc, realwho); |
2362 | |
2086 | 2363 g_free(realwho); |
2364 } | |
2365 | |
2681
37d80035e77f
[gaim-migrate @ 2694]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2635
diff
changeset
|
2366 static void jabber_remove_buddy(struct gaim_connection *gc, char *name, char *group) |
2086 | 2367 { |
3048 | 2368 xmlnode x; |
2086 | 2369 char *realwho; |
2956 | 2370 gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; |
2086 | 2371 |
3311 | 2372 if(!name || (realwho = get_realwho(gjc, name, FALSE, NULL)) == NULL) |
2086 | 2373 return; |
2374 | |
2956 | 2375 x = xmlnode_new_tag("presence"); |
2376 xmlnode_put_attrib(x, "to", realwho); | |
2377 xmlnode_put_attrib(x, "type", "unsubscribe"); | |
2378 gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); | |
2086 | 2379 g_free(realwho); |
2380 xmlnode_free(x); | |
2381 } | |
2382 | |
3314 | 2383 /* |
2384 * Remove a buddy item from the roster entirely | |
2385 */ | |
2386 static void jabber_remove_buddy_roster_item(struct gaim_connection *gc, char *name) | |
2387 { | |
2388 xmlnode x, y; | |
2389 char *realwho; | |
2390 gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; | |
2391 | |
2392 if(!name || (realwho = get_realwho(gjc, name, FALSE, NULL)) == NULL) | |
2393 return; | |
2394 | |
2395 x = jutil_iqnew(JPACKET__SET, NS_ROSTER); | |
2396 y = xmlnode_insert_tag(xmlnode_get_tag(x, "query"), "item"); | |
2397 xmlnode_put_attrib(y, "jid", realwho); | |
2398 xmlnode_put_attrib(y, "subscription", "remove"); | |
2399 gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); | |
2400 g_free(realwho); | |
2401 xmlnode_free(x); | |
2402 } | |
2403 | |
2086 | 2404 static char **jabber_list_icon(int uc) |
2405 { | |
2406 switch (uc) { | |
2407 case UC_AWAY: | |
2408 return available_away_xpm; | |
2409 case UC_CHAT: | |
2410 return available_chat_xpm; | |
2411 case UC_XA: | |
2412 return available_xa_xpm; | |
2413 case UC_DND: | |
2414 return available_dnd_xpm; | |
3259 | 2415 case UC_ERROR: |
2416 return available_error_xpm; | |
2086 | 2417 default: |
2418 return available_xpm; | |
2419 } | |
2420 } | |
2421 | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2422 static GList *jabber_chat_info(struct gaim_connection *gc) |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2423 { |
2956 | 2424 gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; |
2425 | |
2426 static char *confserv = NULL; /* this pointer must be persistent */ | |
2427 gchar *server; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2428 |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2429 GList *m = NULL; |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2430 struct proto_chat_entry *pce; |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2431 |
2956 | 2432 /* This is a scientific wild-ass guess... |
2433 * | |
2434 * If there are more than two "components" to the current server name, | |
2435 * lop-off the left-most component and replace with "conference." | |
2436 */ | |
2437 if(confserv != NULL) { | |
2438 g_free(confserv); /* dispose of the old value */ | |
2439 } | |
2440 | |
2441 if((server = g_strdup(gjc->user->server)) == NULL) { | |
2442 confserv = g_strdup(DEFAULT_GROUPCHAT); | |
2443 } else { | |
2444 gchar **splits, **index; | |
2445 gchar *tmp; | |
2446 int cnt = 0; | |
2447 | |
2448 | |
2449 index = splits = g_strsplit(server, ".", -1); /* split the connected server */ | |
2450 | |
2451 while(*(index++)) /* index to the end--counting the parts */ | |
2452 ++cnt; | |
2453 | |
2454 /* | |
2455 * If we've more than two parts, point to the second part. Else point | |
2456 * to the start. | |
2457 */ | |
2458 if(cnt > 2) { | |
2459 index -= cnt; | |
2460 } else { | |
2461 index = splits; | |
2462 } | |
2463 | |
2464 /* Put it together */ | |
2465 confserv = g_strjoin(".", "conference", (tmp = g_strjoinv(".", index)), NULL); | |
2466 | |
2467 g_free(server); /* we don't need this stuff no more */ | |
2468 g_free(tmp); | |
2469 g_strfreev(splits); | |
2470 } | |
2471 | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2472 pce = g_new0(struct proto_chat_entry, 1); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2473 pce->label = _("Room:"); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2474 m = g_list_append(m, pce); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2475 |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2476 pce = g_new0(struct proto_chat_entry, 1); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2477 pce->label = _("Server:"); |
2956 | 2478 pce->def = confserv; |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2479 m = g_list_append(m, pce); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2480 |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2481 pce = g_new0(struct proto_chat_entry, 1); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2482 pce->label = _("Handle:"); |
2956 | 2483 pce->def = gjc->user->user; |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2484 m = g_list_append(m, pce); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2485 |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2486 return m; |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2487 } |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2488 |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2489 static void jabber_join_chat(struct gaim_connection *gc, GList *data) |
2086 | 2490 { |
2491 xmlnode x; | |
2492 char *realwho; | |
2956 | 2493 gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; |
2494 GSList *jcs = ((struct jabber_data *)gc->proto_data)->chats; | |
2086 | 2495 struct jabber_chat *jc; |
3311 | 2496 gaim_jid gjid; |
2086 | 2497 |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2498 if (!data || !data->next || !data->next->next) |
2086 | 2499 return; |
2500 | |
3311 | 2501 realwho = create_valid_jid(data->data, data->next->data, data->next->next->data); |
2086 | 2502 debug_printf("%s\n", realwho); |
2503 | |
3311 | 2504 if((gjid = gaim_jid_new(realwho)) == NULL) { |
3236 | 2505 char *msg = g_strdup_printf("%s: \"%s\"", _("Invalid Jabber I.D."), realwho); |
2506 do_error_dialog(msg, _("Jabber Error")); | |
2507 g_free(msg); | |
3311 | 2508 g_free(realwho); |
3236 | 2509 return; |
2510 } | |
2956 | 2511 |
3311 | 2512 if((jc = find_any_chat(gc, gjid)) != NULL) { |
2956 | 2513 switch(jc->state) { |
2514 case JCS_PENDING: | |
2515 debug_printf("attempt to re-join already pending Jabber chat! (ignoring)\n"); | |
2516 g_free(realwho); /* yuck! */ | |
3311 | 2517 gaim_jid_free(gjid); |
2956 | 2518 return; |
2519 case JCS_ACTIVE: | |
2520 debug_printf("attempt to re-join already active Jabber chat! (ignoring)\n"); | |
2521 g_free(realwho); /* yuck! */ | |
3311 | 2522 gaim_jid_free(gjid); |
2956 | 2523 return; |
2524 case JCS_CLOSED: | |
2525 debug_printf("rejoining previously closed Jabber chat\n"); | |
2526 break; | |
2527 default: | |
2528 debug_printf("found Jabber chat in unknown state! (ignoring)\n"); | |
2529 g_free(realwho); /* yuck! */ | |
3311 | 2530 gaim_jid_free(gjid); |
2956 | 2531 return; |
2532 } | |
2533 } else { | |
2534 debug_printf("joining completely new Jabber chat\n"); | |
2535 jc = g_new0(struct jabber_chat, 1); | |
3311 | 2536 jc->gjid = gjid; |
2956 | 2537 jc->gc = gc; |
2538 ((struct jabber_data *)gc->proto_data)->chats = g_slist_append(jcs, jc); | |
3311 | 2539 add_buddy(gc, _("Chats"), realwho, realwho); |
2956 | 2540 } |
2541 | |
2542 jc->state = JCS_PENDING; | |
2543 | |
2086 | 2544 x = jutil_presnew(0, realwho, NULL); |
2956 | 2545 gjab_send(gjc, x); |
2086 | 2546 xmlnode_free(x); |
2547 g_free(realwho); | |
2548 } | |
2549 | |
2550 static void jabber_chat_invite(struct gaim_connection *gc, int id, char *message, char *name) | |
2551 { | |
2552 xmlnode x, y; | |
2553 struct jabber_data *jd = gc->proto_data; | |
2956 | 2554 gjconn gjc = jd->gjc; |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2555 struct jabber_chat *jc = NULL; |
2086 | 2556 char *realwho, *subject; |
2557 | |
3311 | 2558 if(!name || (realwho = get_realwho(gjc, name, FALSE, NULL)) == NULL) |
2086 | 2559 return; |
2560 | |
2561 /* find which chat we're inviting to */ | |
2956 | 2562 if(jabber_find_chat_by_convo_id(gc, id, &jc) != 0) |
2086 | 2563 return; |
2564 | |
2565 x = xmlnode_new_tag("message"); | |
2566 xmlnode_put_attrib(x, "to", realwho); | |
3311 | 2567 |
2086 | 2568 g_free(realwho); |
2569 | |
2570 y = xmlnode_insert_tag(x, "x"); | |
2571 xmlnode_put_attrib(y, "xmlns", "jabber:x:conference"); | |
3311 | 2572 subject = g_strdup_printf("%s@%s", jc->gjid->user, jc->gjid->server); |
2086 | 2573 xmlnode_put_attrib(y, "jid", subject); |
2574 g_free(subject); | |
2575 | |
2576 if (message && strlen(message)) { | |
2577 char *utf8 = str_to_utf8(message); | |
2578 y = xmlnode_insert_tag(x, "body"); | |
2579 xmlnode_insert_cdata(y, utf8, -1); | |
2580 g_free(utf8); | |
2581 } | |
2582 | |
2956 | 2583 gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); |
2086 | 2584 xmlnode_free(x); |
2585 } | |
2586 | |
2587 static void jabber_chat_leave(struct gaim_connection *gc, int id) | |
2588 { | |
2589 struct jabber_data *jd = gc->proto_data; | |
2956 | 2590 gjconn gjc = jd->gjc; |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2591 struct jabber_chat *jc = NULL; |
3311 | 2592 char *chatname; |
2086 | 2593 xmlnode x; |
2594 | |
2956 | 2595 /* Find out which chat we're leaving */ |
2596 if(jabber_find_chat_by_convo_id(gc, id, &jc) != 0) | |
2086 | 2597 return; |
2598 | |
3311 | 2599 chatname = g_strdup_printf("%s@%s", jc->gjid->user, jc->gjid->server); |
2600 x = jutil_presnew(0, chatname, NULL); | |
2601 g_free(chatname); | |
2086 | 2602 xmlnode_put_attrib(x, "type", "unavailable"); |
2956 | 2603 gjab_send(gjc, x); |
2086 | 2604 xmlnode_free(x); |
2605 jc->b = NULL; | |
2606 } | |
2607 | |
2167
edf8c5a70e5b
[gaim-migrate @ 2177]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2162
diff
changeset
|
2608 static int jabber_chat_send(struct gaim_connection *gc, int id, char *message) |
2086 | 2609 { |
2610 xmlnode x, y; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2611 struct jabber_chat *jc = NULL; |
2086 | 2612 char *chatname; |
2956 | 2613 int retval = 0; |
2614 | |
2615 /* Find out which chat we're sending to */ | |
2616 if((retval = jabber_find_chat_by_convo_id(gc, id, &jc)) != 0) | |
2617 return(retval); | |
2086 | 2618 |
2619 x = xmlnode_new_tag("message"); | |
3311 | 2620 xmlnode_put_attrib(x, "from", jc->gjid->full); |
2621 chatname = g_strdup_printf("%s@%s", jc->gjid->user, jc->gjid->server); | |
2086 | 2622 xmlnode_put_attrib(x, "to", chatname); |
2623 g_free(chatname); | |
2624 xmlnode_put_attrib(x, "type", "groupchat"); | |
2625 | |
2289
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2626 if (message && strlen(message) > strlen("/topic ") && |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2627 !g_strncasecmp(message, "/topic ", strlen("/topic "))) { |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2628 char buf[8192]; |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2629 char *utf8 = str_to_utf8(message + strlen("/topic ")); |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2630 y = xmlnode_insert_tag(x, "subject"); |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2631 xmlnode_insert_cdata(y, utf8, -1); |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2632 y = xmlnode_insert_tag(x, "body"); |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2633 g_snprintf(buf, sizeof(buf), "/me has changed the subject to: %s", utf8); |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2634 xmlnode_insert_cdata(y, buf, -1); |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2635 g_free(utf8); |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2636 } else if (message && strlen(message)) { |
2086 | 2637 char *utf8 = str_to_utf8(message); |
2638 y = xmlnode_insert_tag(x, "body"); | |
2639 xmlnode_insert_cdata(y, utf8, -1); | |
2640 g_free(utf8); | |
2641 } | |
2642 | |
2956 | 2643 gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); |
2086 | 2644 xmlnode_free(x); |
2167
edf8c5a70e5b
[gaim-migrate @ 2177]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2162
diff
changeset
|
2645 return 0; |
2086 | 2646 } |
2647 | |
2648 static void jabber_chat_whisper(struct gaim_connection *gc, int id, char *who, char *message) | |
2649 { | |
2650 xmlnode x, y; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2651 struct jabber_chat *jc = NULL; |
2086 | 2652 char *chatname; |
2653 | |
2956 | 2654 /* Find out which chat we're whispering to */ |
2655 if(jabber_find_chat_by_convo_id(gc, id, &jc) != 0) | |
2086 | 2656 return; |
2657 | |
2658 x = xmlnode_new_tag("message"); | |
3311 | 2659 xmlnode_put_attrib(x, "from", jc->gjid->full); |
2660 chatname = g_strdup_printf("%s@%s/%s", jc->gjid->user, jc->gjid->server, who); | |
2086 | 2661 xmlnode_put_attrib(x, "to", chatname); |
2662 g_free(chatname); | |
2663 xmlnode_put_attrib(x, "type", "normal"); | |
2664 | |
2665 if (message && strlen(message)) { | |
2666 char *utf8 = str_to_utf8(message); | |
2667 y = xmlnode_insert_tag(x, "body"); | |
2668 xmlnode_insert_cdata(y, utf8, -1); | |
2669 g_free(utf8); | |
2670 } | |
2671 | |
2956 | 2672 gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); |
2086 | 2673 xmlnode_free(x); |
2674 } | |
2675 | |
2676 static char *jabber_normalize(const char *s) | |
2677 { | |
2678 static char buf[BUF_LEN]; | |
2679 char *t, *u; | |
2680 int x = 0; | |
2681 | |
2682 g_return_val_if_fail((s != NULL), NULL); | |
2683 | |
2956 | 2684 /* Somebody called us with s == NULL once... */ |
2685 if(s == NULL) { | |
2686 return(NULL); | |
2687 } else { | |
2688 u = t = g_strdup(s); | |
2689 | |
2690 g_strdown(t); | |
2691 | |
2692 while (*t && (x < BUF_LEN - 1)) { | |
2693 if (*t != ' ') | |
2694 buf[x++] = *t; | |
2695 t++; | |
2696 } | |
2697 buf[x] = '\0'; | |
2698 g_free(u); | |
2699 | |
2700 if (!strchr(buf, '@')) { | |
2701 strcat(buf, "@jabber.org"); /* this isn't always right, but eh */ | |
2702 } else if ((u = strchr(strchr(buf, '@'), '/')) != NULL) { | |
2703 *u = '\0'; | |
2704 } | |
2705 | |
2706 return buf; | |
2086 | 2707 } |
2708 } | |
2709 | |
2710 static void jabber_get_info(struct gaim_connection *gc, char *who) { | |
2711 xmlnode x; | |
2712 char *id; | |
2956 | 2713 char *realwho; |
2086 | 2714 struct jabber_data *jd = gc->proto_data; |
2956 | 2715 gjconn gjc = jd->gjc; |
2086 | 2716 |
3311 | 2717 if((realwho = get_realwho(gjc, who, TRUE, NULL)) == NULL) |
2718 return; | |
2719 | |
2086 | 2720 x = jutil_iqnew(JPACKET__GET, NS_VCARD); |
2956 | 2721 xmlnode_put_attrib(x, "to", realwho); |
3311 | 2722 |
2956 | 2723 g_free(realwho); |
2724 | |
2725 id = gjab_getid(gjc); | |
2086 | 2726 xmlnode_put_attrib(x, "id", id); |
2727 | |
2956 | 2728 g_hash_table_insert(jd->gjc->queries, g_strdup(id), g_strdup("vCard")); |
2729 | |
2730 gjab_send(gjc, x); | |
2086 | 2731 |
2732 xmlnode_free(x); | |
3311 | 2733 } |
2734 | |
2735 static void jabber_get_error_msg(struct gaim_connection *gc, char *who) { | |
2736 struct jabber_data *jd = gc->proto_data; | |
2737 gjconn gjc = jd->gjc; | |
2738 gchar **str_arr = (gchar **) g_new(gpointer, 3); | |
2739 gchar **ap = str_arr; | |
2740 gchar *realwho, *final; | |
2741 struct jabber_buddy_data *jbd; | |
2742 | |
2743 if((realwho = get_realwho(gjc, who, FALSE, NULL)) == NULL) { | |
2744 g_strfreev(str_arr); | |
2745 return; | |
2746 } | |
2747 | |
2748 jbd = jabber_find_buddy(gc, realwho); | |
2749 | |
2750 *ap++ = g_strdup_printf("<B>Jabber ID:</B> %s<BR>\n", realwho); | |
2751 *ap++ = g_strdup_printf("<B>Error:</B> %s<BR>\n", jbd->error_msg); | |
2752 *ap = NULL; | |
2086 | 2753 |
3311 | 2754 final= g_strjoinv(NULL, str_arr); |
2755 | |
2756 g_strfreev(str_arr); | |
2757 | |
2758 g_show_info_text(gc, realwho, 2, final, NULL); | |
2759 g_free(realwho); | |
2760 g_free(final); | |
2086 | 2761 } |
2762 | |
2956 | 2763 static void jabber_get_away_msg(struct gaim_connection *gc, char *who) { |
2764 struct jabber_data *jd = gc->proto_data; | |
2765 gjconn gjc = jd->gjc; | |
3311 | 2766 int num_resources; |
2767 gaim_jid gjid; | |
2768 char *buddy = get_realwho(gjc, who, FALSE, &gjid); | |
2769 struct jabber_buddy_data *jbd = jabber_find_buddy(gc, buddy); | |
2770 gchar **str_arr; | |
2771 gchar **ap; | |
2772 gchar *realwho, *final; | |
2773 GSList *resources; | |
2774 int i; | |
2775 | |
2776 if(!buddy) | |
2777 return; | |
2778 | |
2779 if(!gjid->resource) { | |
2780 num_resources = g_slist_length(jbd->resources); | |
2781 resources = jbd->resources; | |
2782 } else { | |
2783 num_resources = 1; | |
2784 resources = jbd->resources; | |
2785 while(strcasecmp(((jab_res_info)resources->data)->name, gjid->resource)) | |
2786 resources = resources->next; | |
2787 } | |
2788 | |
2789 gaim_jid_free(gjid); | |
2956 | 2790 |
2791 /* space for all elements: Jabber I.D. + "status" + NULL (list terminator) */ | |
3311 | 2792 str_arr = (gchar **) g_new(gpointer, num_resources*2 + 1); |
2793 ap = str_arr; | |
2794 | |
2795 for(i=0; i<num_resources; i++) | |
2796 { | |
2797 jab_res_info jri = resources->data; | |
2798 realwho = g_strdup_printf("%s/%s", buddy, jri->name); | |
2799 *ap++ = g_strdup_printf("<B>Jabber ID:</B> %s<BR>\n", realwho); | |
2800 *ap++ = g_strdup_printf("<B>Status:</B> %s<BR>\n", jabber_lookup_away(gjc, realwho)); | |
2801 g_free(realwho); | |
2802 resources = resources->next; | |
2956 | 2803 } |
2804 | |
2805 *ap = NULL; | |
3311 | 2806 |
2807 g_free(buddy); | |
2956 | 2808 |
2809 final= g_strjoinv(NULL, str_arr); | |
2810 g_strfreev(str_arr); | |
2811 | |
3311 | 2812 g_show_info_text(gc, who, 2, final, NULL); |
2956 | 2813 g_free(final); |
2814 | |
2815 } | |
2816 | |
2817 static void jabber_get_cb_info(struct gaim_connection *gc, int cid, char *who) { | |
2818 struct jabber_chat *jc = NULL; | |
2819 char *realwho; | |
2820 | |
2821 /* Find out which chat */ | |
2822 if(jabber_find_chat_by_convo_id(gc, cid, &jc) != 0) | |
2823 return; | |
2824 | |
3311 | 2825 realwho = g_strdup_printf("%s@%s/%s", jc->gjid->user, jc->gjid->server, who); |
2956 | 2826 |
2827 jabber_get_info(gc, realwho); | |
2828 g_free(realwho); | |
2829 } | |
2830 | |
2831 static void jabber_get_cb_away_msg(struct gaim_connection *gc, int cid, char *who) { | |
2832 struct jabber_chat *jc = NULL; | |
2833 char *realwho; | |
2834 | |
2835 /* Find out which chat */ | |
2836 if(jabber_find_chat_by_convo_id(gc, cid, &jc) != 0) | |
2837 return; | |
2838 | |
3311 | 2839 realwho = g_strdup_printf("%s@%s/%s", jc->gjid->user, jc->gjid->server, who); |
2956 | 2840 |
2841 jabber_get_away_msg(gc, realwho); | |
2842 g_free(realwho); | |
2843 | |
2844 } | |
2845 | |
2170
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
2846 static GList *jabber_buddy_menu(struct gaim_connection *gc, char *who) { |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
2847 GList *m = NULL; |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
2848 struct proto_buddy_menu *pbm; |
3311 | 2849 struct buddy *b = find_buddy(gc, who); |
2850 | |
2851 if(b->uc == UC_ERROR) | |
2852 { | |
2853 pbm = g_new0(struct proto_buddy_menu, 1); | |
2854 pbm->label = _("View Error Msg"); | |
2855 pbm->callback = jabber_get_error_msg; | |
2856 pbm->gc = gc; | |
2857 m = g_list_append(m, pbm); | |
2858 } else { | |
2859 pbm = g_new0(struct proto_buddy_menu, 1); | |
2860 pbm->label = _("Get Info"); | |
2861 pbm->callback = jabber_get_info; | |
2862 pbm->gc = gc; | |
2863 m = g_list_append(m, pbm); | |
2864 pbm = g_new0(struct proto_buddy_menu, 1); | |
2865 pbm->label = _("Get Away Msg"); | |
2866 pbm->callback = jabber_get_away_msg; | |
2867 pbm->gc = gc; | |
2868 m = g_list_append(m, pbm); | |
2869 } | |
2170
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
2870 |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
2871 return m; |
2086 | 2872 } |
2873 | |
3314 | 2874 /* |
2875 * Jabber protocol-specific "edit buddy menu" item(s) | |
2876 */ | |
2877 static GList *jabber_edit_buddy_menu(struct gaim_connection *gc, char *who) { | |
2878 GList *m = NULL; | |
2879 struct proto_buddy_menu *pbm; | |
2880 | |
2881 pbm = g_new0(struct proto_buddy_menu, 1); | |
2882 pbm->label = _("Remove From Roster"); | |
2883 pbm->callback = jabber_remove_buddy_roster_item; | |
2884 pbm->gc = gc; | |
2885 m = g_list_append(m, pbm); | |
2886 | |
2887 return m; | |
2888 } | |
2889 | |
2501
227cc42ffa6e
[gaim-migrate @ 2514]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
2890 static GList *jabber_away_states(struct gaim_connection *gc) { |
2086 | 2891 GList *m = NULL; |
2892 | |
2893 m = g_list_append(m, "Online"); | |
2894 m = g_list_append(m, "Chatty"); | |
2895 m = g_list_append(m, "Away"); | |
2896 m = g_list_append(m, "Extended Away"); | |
2897 m = g_list_append(m, "Do Not Disturb"); | |
2898 | |
2899 return m; | |
2900 } | |
2901 | |
2902 static void jabber_set_away(struct gaim_connection *gc, char *state, char *message) | |
2903 { | |
2904 xmlnode x, y; | |
2905 struct jabber_data *jd = gc->proto_data; | |
2956 | 2906 gjconn gjc = jd->gjc; |
3311 | 2907 GSList *jcs; |
2908 struct jabber_chat *jc; | |
2909 char *chatname; | |
2086 | 2910 |
2911 gc->away = NULL; /* never send an auto-response */ | |
2912 | |
2913 x = xmlnode_new_tag("presence"); | |
2914 | |
2915 if (!strcmp(state, GAIM_AWAY_CUSTOM)) { | |
2916 /* oh goody. Gaim is telling us what to do. */ | |
2917 if (message) { | |
2918 /* Gaim wants us to be away */ | |
2919 y = xmlnode_insert_tag(x, "show"); | |
2920 xmlnode_insert_cdata(y, "away", -1); | |
2921 y = xmlnode_insert_tag(x, "status"); | |
2922 xmlnode_insert_cdata(y, message, -1); | |
2923 gc->away = ""; | |
2924 } else { | |
2925 /* Gaim wants us to not be away */ | |
2926 /* but for Jabber, we can just send presence with no other information. */ | |
2927 } | |
2928 } else { | |
2929 /* state is one of our own strings. it won't be NULL. */ | |
2930 if (!strcmp(state, "Online")) { | |
2931 /* once again, we don't have to put anything here */ | |
2932 } else if (!strcmp(state, "Chatty")) { | |
2933 y = xmlnode_insert_tag(x, "show"); | |
2934 xmlnode_insert_cdata(y, "chat", -1); | |
2935 } else if (!strcmp(state, "Away")) { | |
2936 y = xmlnode_insert_tag(x, "show"); | |
2937 xmlnode_insert_cdata(y, "away", -1); | |
2938 gc->away = ""; | |
2939 } else if (!strcmp(state, "Extended Away")) { | |
2940 y = xmlnode_insert_tag(x, "show"); | |
2941 xmlnode_insert_cdata(y, "xa", -1); | |
2942 gc->away = ""; | |
2943 } else if (!strcmp(state, "Do Not Disturb")) { | |
2944 y = xmlnode_insert_tag(x, "show"); | |
2945 xmlnode_insert_cdata(y, "dnd", -1); | |
2946 gc->away = ""; | |
2947 } | |
2948 } | |
2949 | |
3311 | 2950 gjab_send(gjc, x); /* Notify "individuals" */ |
2951 | |
2952 /* | |
2953 * As of jabberd-1.4.2: simply sending presence to the server doesn't result in | |
2954 * it being propagated to conference rooms. So we wade thru the list of chats, | |
2955 * sending our new presence status to each and every one. | |
2956 */ | |
2957 for(jcs = jd->chats; jcs; jcs = jcs->next) { | |
2958 jc = jcs->data; | |
2959 if(jc->state == JCS_ACTIVE) { | |
2960 xmlnode_put_attrib(x, "from", jc->gjid->full); | |
2961 chatname = g_strdup_printf("%s@%s", jc->gjid->user, jc->gjid->server); | |
2962 xmlnode_put_attrib(x, "to", chatname); | |
2963 gjab_send(gjc, x); | |
2964 g_free(chatname); | |
2965 } | |
2966 } | |
2967 | |
2086 | 2968 xmlnode_free(x); |
2969 } | |
2970 | |
2971 static void jabber_set_idle(struct gaim_connection *gc, int idle) { | |
2972 struct jabber_data *jd = (struct jabber_data *)gc->proto_data; | |
2973 debug_printf("jabber_set_idle: setting idle %i\n", idle); | |
2974 jd->idle = idle ? time(NULL) - idle : idle; | |
2975 } | |
2976 | |
2977 static void jabber_keepalive(struct gaim_connection *gc) { | |
2978 struct jabber_data *jd = (struct jabber_data *)gc->proto_data; | |
2956 | 2979 gjab_send_raw(jd->gjc, " \t "); |
2086 | 2980 } |
2981 | |
2154
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2982 static GList *jabber_user_opts() |
2086 | 2983 { |
2154
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2984 GList *m = NULL; |
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2985 struct proto_user_opt *puo; |
2086 | 2986 |
2154
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2987 puo = g_new0(struct proto_user_opt, 1); |
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2988 puo->label = "Port:"; |
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2989 puo->def = "5222"; |
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2990 puo->pos = USEROPT_PORT; |
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2991 m = g_list_append(m, puo); |
2086 | 2992 |
2154
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2993 return m; |
2086 | 2994 } |
2995 | |
2956 | 2996 /*---------------------------------------*/ |
2997 /* Jabber "set info" (vCard) support */ | |
2998 /*---------------------------------------*/ | |
2999 | |
3000 /* | |
3001 * V-Card format: | |
3002 * | |
3003 * <vCard prodid='' version='' xmlns=''> | |
3004 * <FN></FN> | |
3005 * <N> | |
3006 * <FAMILY/> | |
3007 * <GIVEN/> | |
3008 * </N> | |
3009 * <NICKNAME/> | |
3010 * <URL/> | |
3011 * <ADR> | |
3012 * <STREET/> | |
3013 * <EXTADD/> | |
3014 * <LOCALITY/> | |
3015 * <REGION/> | |
3016 * <PCODE/> | |
3017 * <COUNTRY/> | |
3018 * </ADR> | |
3019 * <TEL/> | |
3020 * <EMAIL/> | |
3021 * <ORG> | |
3022 * <ORGNAME/> | |
3023 * <ORGUNIT/> | |
3024 * </ORG> | |
3025 * <TITLE/> | |
3026 * <ROLE/> | |
3027 * <DESC/> | |
3028 * <BDAY/> | |
3029 * </vCard> | |
3030 * | |
3031 * See also: | |
3032 * | |
3033 * http://docs.jabber.org/proto/html/vcard-temp.html | |
3034 * http://www.vcard-xml.org/dtd/vCard-XML-v2-20010520.dtd | |
3035 */ | |
3036 | |
3037 /* | |
3038 * Cross-reference user-friendly V-Card entry labels to vCard XML tags | |
3039 * and attributes. | |
3040 * | |
3041 * Order is (or should be) unimportant. For example: we have no way of | |
3042 * knowing in what order real data will arrive. | |
3043 * | |
3044 * Format: Label, Pre-set text, "visible" flag, "editable" flag, XML tag | |
3045 * name, XML tag's parent tag "path" (relative to vCard node). | |
3046 * | |
3047 * List is terminated by a NULL label pointer. | |
3048 * | |
3049 * Entries with no label text, but with XML tag and parent tag | |
3050 * entries, are used by V-Card XML construction routines to | |
3051 * "automagically" construct the appropriate XML node tree. | |
3052 * | |
3053 * Thoughts on future direction/expansion | |
3054 * | |
3055 * This is a "simple" vCard. | |
3056 * | |
3057 * It is possible for nodes other than the "vCard" node to have | |
3058 * attributes. Should that prove necessary/desirable, add an | |
3059 * "attributes" pointer to the vcard_template struct, create the | |
3060 * necessary tag_attr structs, and add 'em to the vcard_dflt_data | |
3061 * array. | |
3062 * | |
3063 * The above changes will (obviously) require changes to the vCard | |
3064 * construction routines. | |
3065 */ | |
3066 | |
3067 struct vcard_template { | |
3068 char *label; /* label text pointer */ | |
3069 char *text; /* entry text pointer */ | |
3070 int visible; /* should entry field be "visible?" */ | |
3071 int editable; /* should entry field be editable? */ | |
3072 char *tag; /* tag text */ | |
3073 char *ptag; /* parent tag "path" text */ | |
3074 char *url; /* vCard display format if URL */ | |
3075 } vcard_template_data[] = { | |
2975 | 3076 {N_("Full Name"), NULL, TRUE, TRUE, "FN", NULL, NULL}, |
3077 {N_("Family Name"), NULL, TRUE, TRUE, "FAMILY", "N", NULL}, | |
3078 {N_("Given Name"), NULL, TRUE, TRUE, "GIVEN", "N", NULL}, | |
3079 {N_("Nickname"), NULL, TRUE, TRUE, "NICKNAME", NULL, NULL}, | |
3080 {N_("URL"), NULL, TRUE, TRUE, "URL", NULL, "<A HREF=\"%s\">%s</A>"}, | |
3081 {N_("Street Address"), NULL, TRUE, TRUE, "STREET", "ADR", NULL}, | |
3082 {N_("Extended Address"), NULL, TRUE, TRUE, "EXTADD", "ADR", NULL}, | |
3083 {N_("Locality"), NULL, TRUE, TRUE, "LOCALITY", "ADR", NULL}, | |
3084 {N_("Region"), NULL, TRUE, TRUE, "REGION", "ADR", NULL}, | |
3085 {N_("Postal Code"), NULL, TRUE, TRUE, "PCODE", "ADR", NULL}, | |
3086 {N_("Country"), NULL, TRUE, TRUE, "COUNTRY", "ADR", NULL}, | |
3087 {N_("Telephone"), NULL, TRUE, TRUE, "TELEPHONE", NULL, NULL}, | |
3088 {N_("Email"), NULL, TRUE, TRUE, "EMAIL", NULL, "<A HREF=\"mailto:%s\">%s</A>"}, | |
3089 {N_("Organization Name"), NULL, TRUE, TRUE, "ORGNAME", "ORG", NULL}, | |
3090 {N_("Organization Unit"), NULL, TRUE, TRUE, "ORGUNIT", "ORG", NULL}, | |
3091 {N_("Title"), NULL, TRUE, TRUE, "TITLE", NULL, NULL}, | |
3092 {N_("Role"), NULL, TRUE, TRUE, "ROLE", NULL, NULL}, | |
3093 {N_("Birthday"), NULL, TRUE, TRUE, "BDAY", NULL, NULL}, | |
3094 {N_("Description"), NULL, TRUE, TRUE, "DESC", NULL, NULL}, | |
2956 | 3095 {"", NULL, TRUE, TRUE, "N", NULL, NULL}, |
3096 {"", NULL, TRUE, TRUE, "ADR", NULL, NULL}, | |
3097 {"", NULL, TRUE, TRUE, "ORG", NULL, NULL}, | |
3098 {NULL, NULL, 0, 0, NULL, NULL, NULL} | |
3099 }; | |
3100 | |
3101 /* | |
3102 * The "vCard" tag's attibute list... | |
3103 */ | |
3104 struct tag_attr { | |
3105 char *attr; | |
3106 char *value; | |
3107 } vcard_tag_attr_list[] = { | |
3108 {"prodid", "-//HandGen//NONSGML vGen v1.0//EN"}, | |
3109 {"version", "2.0", }, | |
3110 {"xmlns", "vcard-temp", }, | |
3111 {NULL, NULL}, | |
3112 }; | |
3113 | |
3114 | |
3115 /* | |
3116 * V-Card user instructions | |
3117 */ | |
3118 static char *multi_entry_instructions = | |
2975 | 3119 N_("All items below are optional. Enter only the information with which you feel comfortable"); |
3120 static char *entries_title = N_("User Identity"); | |
2956 | 3121 |
3122 /* | |
3123 * Used by routines to parse an XML-encoded string into an xmlnode tree | |
3124 */ | |
3125 typedef struct { | |
3126 XML_Parser parser; | |
3127 xmlnode current; | |
3128 } *xmlstr2xmlnode_parser, xmlstr2xmlnode_parser_struct; | |
3129 | |
3130 | |
3131 /* | |
3132 * Display a Jabber vCard | |
3133 */ | |
3134 static void jabber_handlevcard(gjconn gjc, xmlnode querynode, char *from) | |
3135 { | |
3136 struct gaim_connection *gc = GJ_GC(gjc); | |
3137 char *cdata, *status; | |
3138 struct vcard_template *vc_tp = vcard_template_data; | |
3139 | |
3140 /* space for all vCard elements + Jabber I.D. + "status" + NULL (list terminator) */ | |
3141 gchar **str_arr = (gchar **) g_new(gpointer, | |
3142 (sizeof(vcard_template_data)/sizeof(struct vcard_template)) + 3); | |
3143 gchar **ap = str_arr; | |
3144 gchar *buddy, *final; | |
3145 | |
3311 | 3146 if((buddy = get_realwho(gjc, from, TRUE, NULL)) == NULL) { |
3147 g_strfreev(str_arr); | |
3148 return; | |
2956 | 3149 } |
3311 | 3150 |
2956 | 3151 *ap++ = g_strdup_printf("<B>Jabber ID:</B> %s<BR>\n", buddy); |
3152 | |
3153 for(vc_tp = vcard_template_data; vc_tp->label != NULL; ++vc_tp) { | |
3154 if(strcmp(vc_tp->tag, "DESC") == 0) | |
3155 continue; /* special handling later */ | |
3156 if(vc_tp->ptag == NULL) { | |
3157 cdata = xmlnode_get_tag_data(querynode, vc_tp->tag); | |
3158 } else { | |
3159 gchar *tag = g_strdup_printf("%s/%s", vc_tp->ptag, vc_tp->tag); | |
3160 cdata = xmlnode_get_tag_data(querynode, tag); | |
3161 g_free(tag); | |
3162 } | |
3163 if(cdata != NULL) { | |
3164 if(vc_tp->url == NULL) { | |
3165 *ap++ = g_strdup_printf("<B>%s:</B> %s<BR>\n", vc_tp->label, cdata); | |
3166 } else { | |
3167 gchar *fmt = g_strdup_printf("<B>%%s:</B> %s<BR>\n", vc_tp->url); | |
3168 *ap++ = g_strdup_printf(fmt, vc_tp->label, cdata, cdata); | |
3169 g_free(fmt); | |
3170 } | |
3171 } | |
3172 } | |
3173 | |
3311 | 3174 status = jabber_lookup_away(gjc, buddy); |
3175 | |
2956 | 3176 *ap++ = g_strdup_printf("<B>Status:</B> %s<BR>\n", status); |
3177 | |
3178 /* | |
3179 * "Description" handled as a special case: get a copy of the | |
3180 * string and HTML-ize. | |
3181 */ | |
3182 if((cdata = xmlnode_get_tag_data(querynode, "DESC")) != NULL) { | |
3183 gchar *tmp = g_strdup_printf("<HR>%s<BR>", cdata); | |
3184 *ap++ = strdup_withhtml(tmp); | |
3185 g_free(tmp); | |
3186 } | |
3187 | |
3188 *ap = NULL; | |
3189 | |
3190 final= g_strjoinv(NULL, str_arr); | |
3191 g_strfreev(str_arr); | |
3192 | |
3193 g_show_info_text(gc, buddy, 2, final, NULL); | |
3194 g_free(buddy); | |
3195 g_free(final); | |
3196 } | |
3197 | |
3198 /* | |
3199 * Used by XML_Parse on parsing CDATA | |
3200 */ | |
3201 static void xmlstr2xmlnode_charData(void *userdata, const char *s, int slen) | |
3202 { | |
3203 xmlstr2xmlnode_parser xmlp = (xmlstr2xmlnode_parser) userdata; | |
3204 | |
3205 if (xmlp->current) | |
3206 xmlnode_insert_cdata(xmlp->current, s, slen); | |
3207 } | |
3208 | |
3209 /* | |
3210 * Used by XML_Parse to start or append to an xmlnode | |
3211 */ | |
3212 static void xmlstr2xmlnode_startElement(void *userdata, const char *name, const char **attribs) | |
3213 { | |
3214 xmlnode x; | |
3215 xmlstr2xmlnode_parser xmlp = (xmlstr2xmlnode_parser) userdata; | |
3216 | |
3217 if (xmlp->current) { | |
3218 /* Append the node to the current one */ | |
3219 x = xmlnode_insert_tag(xmlp->current, name); | |
3220 xmlnode_put_expat_attribs(x, attribs); | |
3221 | |
3222 xmlp->current = x; | |
3223 } else { | |
3224 x = xmlnode_new_tag(name); | |
3225 xmlnode_put_expat_attribs(x, attribs); | |
3226 xmlp->current = x; | |
3227 } | |
3228 } | |
3229 | |
3230 /* | |
3231 * Used by XML_Parse to end an xmlnode | |
3232 */ | |
3233 static void xmlstr2xmlnode_endElement(void *userdata, const char *name) | |
3234 { | |
3235 xmlstr2xmlnode_parser xmlp = (xmlstr2xmlnode_parser) userdata; | |
3236 xmlnode x; | |
3237 | |
3238 if (xmlp->current != NULL && (x = xmlnode_get_parent(xmlp->current)) != NULL) { | |
3239 xmlp->current = x; | |
3240 } | |
3241 } | |
3242 | |
3243 /* | |
3244 * Parse an XML-encoded string into an xmlnode tree | |
3245 * | |
3246 * Caller is responsible for freeing the returned xmlnode | |
3247 */ | |
3248 static xmlnode xmlstr2xmlnode(char *xmlstring) | |
3249 { | |
3250 xmlstr2xmlnode_parser my_parser = g_new(xmlstr2xmlnode_parser_struct, 1); | |
3251 xmlnode x = NULL; | |
3252 | |
3253 my_parser->parser = XML_ParserCreate(NULL); | |
3254 my_parser->current = NULL; | |
3255 | |
3256 XML_SetUserData(my_parser->parser, (void *)my_parser); | |
3257 XML_SetElementHandler(my_parser->parser, xmlstr2xmlnode_startElement, xmlstr2xmlnode_endElement); | |
3258 XML_SetCharacterDataHandler(my_parser->parser, xmlstr2xmlnode_charData); | |
3259 XML_Parse(my_parser->parser, xmlstring, strlen(xmlstring), 0); | |
3260 | |
3261 x = my_parser->current; | |
3262 | |
3263 XML_ParserFree(my_parser->parser); | |
3264 g_free(my_parser); | |
3265 | |
3266 return(x); | |
3267 } | |
3268 | |
3269 /* | |
3270 * Insert a tag node into an xmlnode tree, recursively inserting parent tag | |
3271 * nodes as necessary | |
3272 * | |
3273 * Returns pointer to inserted node | |
3274 * | |
3275 * Note to hackers: this code is designed to be re-entrant (it's recursive--it | |
3276 * calls itself), so don't put any "static"s in here! | |
3277 */ | |
3278 static xmlnode insert_tag_to_parent_tag(xmlnode start, const char *parent_tag, const char *new_tag) | |
3279 { | |
3280 xmlnode x = NULL; | |
3281 | |
3282 /* | |
3283 * If the parent tag wasn't specified, see if we can get it | |
3284 * from the vCard template struct. | |
3285 */ | |
3286 if(parent_tag == NULL) { | |
3287 struct vcard_template *vc_tp = vcard_template_data; | |
3288 | |
3289 while(vc_tp->label != NULL) { | |
3290 if(strcmp(vc_tp->tag, new_tag) == 0) { | |
3291 parent_tag = vc_tp->ptag; | |
3292 break; | |
3293 } | |
3294 ++vc_tp; | |
3295 } | |
3296 } | |
3297 | |
3298 /* | |
3299 * If we have a parent tag... | |
3300 */ | |
3301 if(parent_tag != NULL ) { | |
3302 /* | |
3303 * Try to get the parent node for a tag | |
3304 */ | |
3305 if((x = xmlnode_get_tag(start, parent_tag)) == NULL) { | |
3306 /* | |
3307 * Descend? | |
3308 */ | |
3309 char *grand_parent = strcpy(g_malloc(strlen(parent_tag) + 1), parent_tag); | |
3310 char *parent; | |
3311 | |
3312 if((parent = strrchr(grand_parent, '/')) != NULL) { | |
3313 *(parent++) = '\0'; | |
3314 x = insert_tag_to_parent_tag(start, grand_parent, parent); | |
3315 } else { | |
3316 x = xmlnode_insert_tag(start, grand_parent); | |
3317 } | |
3318 g_free(grand_parent); | |
3319 } else { | |
3320 /* | |
3321 * We found *something* to be the parent node. | |
3322 * Note: may be the "root" node! | |
3323 */ | |
3324 xmlnode y; | |
3325 if((y = xmlnode_get_tag(x, new_tag)) != NULL) { | |
3326 return(y); | |
3327 } | |
3328 } | |
3329 } | |
3330 | |
3331 /* | |
3332 * insert the new tag into its parent node | |
3333 */ | |
3334 return(xmlnode_insert_tag((x == NULL? start : x), new_tag)); | |
3335 } | |
3336 | |
3337 /* | |
3338 * Find the tag name for a label | |
3339 * | |
3340 * Returns NULL on not found | |
3341 */ | |
3342 static char *tag_for_label(const char *label) | |
3343 { | |
3344 struct vcard_template *vc_tp = vcard_template_data; | |
3345 char *p = NULL; | |
3346 | |
3347 for(vc_tp = vcard_template_data; vc_tp->label != NULL; ++vc_tp) { | |
3348 if(strcmp(label, vc_tp->label) == 0) { | |
3349 p = vc_tp->tag; | |
3350 break; | |
3351 } | |
3352 } | |
3353 | |
3354 return(p); | |
3355 } | |
3356 | |
3357 /* | |
3358 * Send vCard info to Jabber server | |
3359 */ | |
3360 static void jabber_set_info(struct gaim_connection *gc, char *info) | |
3361 { | |
3362 xmlnode x, vc_node; | |
3363 char *id; | |
3364 struct jabber_data *jd = gc->proto_data; | |
3365 gjconn gjc = jd->gjc; | |
3366 | |
3367 x = xmlnode_new_tag("iq"); | |
3311 | 3368 xmlnode_put_attrib(x, "type", "set"); |
2956 | 3369 |
3370 id = gjab_getid(gjc); | |
3371 | |
3372 xmlnode_put_attrib(x, "id", id); | |
3373 | |
3374 /* | |
3375 * Send only if there's actually any *information* to send | |
3376 */ | |
3377 if((vc_node = xmlstr2xmlnode(info)) != NULL && xmlnode_get_name(vc_node) != NULL && | |
3378 g_strncasecmp(xmlnode_get_name(vc_node), "vcard", 5) == 0) { | |
3379 xmlnode_insert_tag_node(x, vc_node); | |
3380 debug_printf("jabber: vCard packet: %s\n", xmlnode2str(x)); | |
3381 gjab_send(gjc, x); | |
3382 } | |
3383 | |
3384 xmlnode_free(x); | |
3385 } | |
3386 | |
3387 /* | |
3388 * This is the callback from the "ok clicked" for "set vCard" | |
3389 * | |
3390 * Formats GSList data into XML-encoded string and returns a pointer | |
3391 * to said string. | |
3392 * | |
3393 * g_free()'ing the returned string space is the responsibility of | |
3394 * the caller. | |
3395 */ | |
3396 static gchar *jabber_format_info(MultiEntryDlg *b) | |
3397 { | |
3398 xmlnode vc_node; | |
3399 GSList *list; | |
3400 MultiEntryData *med; | |
3401 MultiTextData *mtd; | |
3402 char *p; | |
3403 | |
3404 struct tag_attr *tag_attr; | |
3405 | |
3406 vc_node = xmlnode_new_tag("vCard"); | |
3407 | |
3408 for(tag_attr = vcard_tag_attr_list; tag_attr->attr != NULL; ++tag_attr) | |
3409 xmlnode_put_attrib(vc_node, tag_attr->attr, tag_attr->value); | |
3410 | |
3411 for(list = b->multi_entry_items; list != NULL; list = list->next) { | |
3412 med = (MultiEntryData *) list->data; | |
3413 if(med->label != NULL && med->text != NULL && (med->text)[0] != '\0') { | |
3414 if((p = tag_for_label(med->label)) != NULL) { | |
3415 xmlnode xp; | |
3416 if((xp = insert_tag_to_parent_tag(vc_node, NULL, p)) != NULL) { | |
3417 xmlnode_insert_cdata(xp, med->text, -1); | |
3418 } | |
3419 } | |
3420 } | |
3421 } | |
3422 | |
3423 for(list = b->multi_text_items; list != NULL; list = list->next) { | |
3424 mtd = (MultiTextData *) list->data; | |
3425 if(mtd->label != NULL && mtd->text != NULL && (mtd->text)[0] != '\0') { | |
3426 if((p = tag_for_label(mtd->label)) != NULL) { | |
3427 xmlnode xp; | |
3428 if((xp = insert_tag_to_parent_tag(vc_node, NULL, p)) != NULL) { | |
3429 xmlnode_insert_cdata(xp, mtd->text, -1); | |
3430 } | |
3431 } | |
3432 } | |
3433 } | |
3434 | |
3435 | |
3436 p = g_strdup(xmlnode2str(vc_node)); | |
3437 xmlnode_free(vc_node); | |
3438 | |
3439 return(p); | |
3440 } | |
3441 | |
3442 /* | |
3443 * This gets executed by the proto action | |
3444 * | |
3445 * Creates a new MultiEntryDlg struct, gets the XML-formatted user_info | |
3446 * string (if any) into GSLists for the (multi-entry) edit dialog and | |
3447 * calls the set_vcard dialog. | |
3448 */ | |
3449 static void jabber_setup_set_info(struct gaim_connection *gc) | |
3450 { | |
3451 MultiEntryData *data; | |
3452 const struct vcard_template *vc_tp; | |
3453 char *user_info; | |
3454 MultiEntryDlg *b = multi_entry_dialog_new(); | |
3455 char *cdata; | |
3456 xmlnode x_vc_data = NULL; | |
3457 struct aim_user *tmp = gc->user; | |
3458 b->user = tmp; | |
3459 | |
3460 | |
3461 /* | |
3462 * Get existing, XML-formatted, user info | |
3463 */ | |
3464 if((user_info = g_malloc(strlen(tmp->user_info) + 1)) != NULL) { | |
3465 strcpy(user_info, tmp->user_info); | |
3466 x_vc_data = xmlstr2xmlnode(user_info); | |
3467 } | |
3468 | |
3469 /* | |
3470 * Set up GSLists for edit with labels from "template," data from user info | |
3471 */ | |
3472 for(vc_tp = vcard_template_data; vc_tp->label != NULL; ++vc_tp) { | |
3473 if((vc_tp->label)[0] == '\0') | |
3474 continue; | |
3475 if(vc_tp->ptag == NULL) { | |
3476 cdata = xmlnode_get_tag_data(x_vc_data, vc_tp->tag); | |
3477 } else { | |
3478 gchar *tag = g_strdup_printf("%s/%s", vc_tp->ptag, vc_tp->tag); | |
3479 cdata = xmlnode_get_tag_data(x_vc_data, tag); | |
3480 g_free(tag); | |
3481 } | |
3482 if(strcmp(vc_tp->tag, "DESC") == 0) { | |
3483 multi_text_list_update(&(b->multi_text_items), | |
3484 vc_tp->label, cdata, TRUE); | |
3485 } else { | |
3486 data = multi_entry_list_update(&(b->multi_entry_items), | |
3487 vc_tp->label, cdata, TRUE); | |
3488 data->visible = vc_tp->visible; | |
3489 data->editable = vc_tp->editable; | |
3490 } | |
3491 } | |
3492 | |
3493 | |
3494 if(x_vc_data != NULL) { | |
3495 xmlnode_free(x_vc_data); | |
3496 } else { | |
3497 /* | |
3498 * Early Beta versions had a different user_info storage format--let's | |
3499 * see if that works. | |
3500 * | |
3501 * This goes away RSN. | |
3502 */ | |
3503 const char *record_separator = "<BR>"; | |
3504 const char *field_separator = ": "; | |
3505 gchar **str_list, **str_list_ptr, **str_list2; | |
3506 | |
3507 if((str_list = g_strsplit(user_info, record_separator, 0)) != NULL) { | |
3508 for(str_list_ptr = str_list; *str_list_ptr != NULL; ++str_list_ptr) { | |
3509 str_list2 = g_strsplit(*str_list_ptr, field_separator, 2); | |
3510 if(str_list2[0] != NULL && str_list2[1] != NULL) { | |
3511 g_strstrip(str_list2[0]); | |
3512 g_strstrip(str_list2[1]); | |
3513 /* this is ugly--so far */ | |
3514 if(strcmp(str_list2[0], "Description") == 0) { | |
3515 multi_text_list_update(&(b->multi_text_items), | |
3516 str_list2[0], str_list2[1], FALSE); | |
3517 } else { | |
3518 multi_entry_list_update(&(b->multi_entry_items), | |
3519 str_list2[0], str_list2[1], FALSE); | |
3520 } | |
3521 } | |
3522 g_strfreev(str_list2); | |
3523 } | |
3524 g_strfreev(str_list); | |
3525 } | |
3526 } | |
3527 | |
3528 if(user_info != NULL) { | |
3529 g_free(user_info); | |
3530 } | |
3531 | |
2975 | 3532 b->title = _("Gaim - Edit Jabber vCard"); |
2956 | 3533 b->wmclass_name = "set_info"; |
3534 b->wmclass_class = "Gaim"; | |
3535 b->instructions->text = g_strdup(multi_entry_instructions); | |
2975 | 3536 b->entries_title = g_strdup(entries_title); |
2956 | 3537 |
3538 b->custom = (void *) jabber_format_info; | |
3539 | |
3540 show_set_vcard(b); | |
3541 } | |
3542 | |
3543 /*---------------------------------------*/ | |
3544 /* End Jabber "set info" (vCard) support */ | |
3545 /*---------------------------------------*/ | |
3546 | |
3547 /*----------------------------------------*/ | |
3548 /* Jabber "user registration" support */ | |
3549 /*----------------------------------------*/ | |
3550 | |
3551 /* | |
3552 * Three of the following four functions duplicate much of what | |
3553 * exists elsewhere: | |
3554 * | |
3555 * jabber_handleregresp() | |
3556 * gjab_reqreg() | |
3557 * jabber_handle_registration_state() | |
3558 * | |
3559 * It may be that an additional flag could be added to one of | |
3560 * the "local" structs and the duplicated code modified to | |
3561 * account for it--thus eliminating the duplication. Then again: | |
3562 * doing it the way it is may be much cleaner. | |
3563 * | |
3564 * TBD: Code to support requesting additional information server | |
3565 * wants at registration--incl. dialog. | |
3566 */ | |
3567 | |
3568 /* | |
3569 * Like jabber_handlepacket(), only different | |
3570 */ | |
3571 static void jabber_handleregresp(gjconn gjc, jpacket p) | |
3572 { | |
3573 if (jpacket_subtype(p) == JPACKET__RESULT) { | |
3574 xmlnode querynode; | |
3575 | |
3576 if((querynode = xmlnode_get_tag(p->x, "query")) != NULL) { | |
3577 char *xmlns; | |
3578 | |
3579 /* we damn well *better* have this! */ | |
3580 if((xmlns = xmlnode_get_attrib(querynode, "xmlns")) != NULL && | |
3581 strcmp(xmlns, NS_REGISTER) == 0) { | |
3582 | |
3583 char *tag; | |
3584 xmlnode child = xmlnode_get_firstchild(querynode); | |
3585 | |
3586 debug_printf("got registration requirments response!\n"); | |
3587 | |
3588 while(child != NULL) { | |
3589 if((tag = xmlnode_get_name(child)) != NULL) { | |
3590 char *data; | |
3591 | |
3592 fprintf(stderr, "DBG: got node: \"%s\"\n", tag); | |
3593 fflush(stderr); | |
3594 | |
3595 if((data = xmlnode_get_data(child)) != NULL) { | |
3596 fprintf(stderr, "DBG: got data: \"%s\"\n", data); | |
3597 fflush(stderr); | |
3598 } | |
3599 } | |
3600 child = xmlnode_get_nextsibling(child); | |
3601 } | |
3602 } | |
3603 } else { | |
3604 debug_printf("registration successful!\n"); | |
3605 | |
2975 | 3606 hide_login_progress_notice(GJ_GC(gjc), _("Server Registration successful!")); |
2956 | 3607 /* |
3608 * TBD: is this the correct way to do away with a | |
3609 * gaim_connection and all its associated memory | |
3610 * allocs, etc.? | |
3611 */ | |
3612 signoff(GJ_GC(gjc)); | |
3613 } | |
3614 | |
3615 } else { | |
3616 xmlnode xerr; | |
3617 char *errmsg = NULL; | |
3618 int errcode = 0; | |
3619 struct jabber_data *jd = GJ_GC(gjc)->proto_data; | |
3620 | |
3621 debug_printf("registration failed\n"); | |
3622 xerr = xmlnode_get_tag(p->x, "error"); | |
3623 if (xerr) { | |
3624 char msg[BUF_LONG]; | |
3625 errmsg = xmlnode_get_data(xerr); | |
3626 if (xmlnode_get_attrib(xerr, "code")) { | |
3627 errcode = atoi(xmlnode_get_attrib(xerr, "code")); | |
3628 g_snprintf(msg, sizeof(msg), "Error %d: %s", errcode, errmsg); | |
3629 } else | |
3630 g_snprintf(msg, sizeof(msg), "%s", errmsg); | |
3631 hide_login_progress(GJ_GC(gjc), msg); | |
3632 } else { | |
2975 | 3633 hide_login_progress(GJ_GC(gjc), _("Unknown registration error")); |
2956 | 3634 } |
3635 | |
3636 jd->die = TRUE; | |
3637 } | |
3638 } | |
3639 | |
3640 /* | |
3641 * Like gjab_reqauth(), only different | |
3642 */ | |
3643 static void gjab_reqreg(gjconn gjc) | |
3644 { | |
3645 xmlnode x, y, z; | |
3646 char *user; | |
3647 | |
3648 if (!gjc) | |
3649 return; | |
3650 | |
3651 x = jutil_iqnew(JPACKET__SET, NS_REGISTER); | |
3652 y = xmlnode_get_tag(x, "query"); | |
3653 | |
3654 user = gjc->user->user; | |
3655 | |
3656 if (user) { | |
3657 z = xmlnode_insert_tag(y, "username"); | |
3658 xmlnode_insert_cdata(z, user, -1); | |
3659 } | |
3660 z = xmlnode_insert_tag(y, "password"); | |
3661 xmlnode_insert_cdata(z, gjc->pass, -1); | |
3662 | |
3663 debug_printf("jabber: registration packet: %s\n", xmlnode2str(x)); | |
3664 gjab_send(gjc, x); | |
3665 xmlnode_free(x); | |
3666 } | |
3667 | |
3668 /* | |
3669 * Like jabber_handlestate(), only different | |
3670 */ | |
3671 static void jabber_handle_registration_state(gjconn gjc, int state) | |
3672 { | |
3673 switch (state) { | |
3674 case JCONN_STATE_OFF: | |
3074 | 3675 if(gjc->was_connected) { |
3676 hide_login_progress_error(GJ_GC(gjc), _("Connection lost")); | |
3677 } else { | |
3678 hide_login_progress(GJ_GC(gjc), _("Unable to connect")); | |
3679 } | |
2956 | 3680 signoff(GJ_GC(gjc)); |
3681 break; | |
3682 case JCONN_STATE_CONNECTED: | |
3074 | 3683 gjc->was_connected = 1; |
2956 | 3684 /* |
3685 * TBD? | |
2975 | 3686 set_login_progress(GJ_GC(gjc), 2, _("Connected")); |
2956 | 3687 */ |
3688 break; | |
3689 case JCONN_STATE_ON: | |
3690 /* | |
3691 * TBD? | |
2975 | 3692 set_login_progress(GJ_GC(gjc), 3, _("Requesting Authentication Method")); |
2956 | 3693 */ |
3694 gjab_reqreg(gjc); | |
3695 /* | |
3696 * TBD: A work-in-progress | |
3697 gjab_reqregreqs(gjc); | |
3698 */ | |
3699 break; | |
3700 default: | |
3701 debug_printf("state change: %d\n", state); | |
3702 } | |
3703 return; | |
3704 } | |
3705 | |
3706 /* | |
3707 * Like jabber_login(), only different | |
3708 */ | |
3709 void jabber_register_user(struct aim_user *au) | |
3710 { | |
3711 struct gaim_connection *gc = new_gaim_conn(au); | |
3712 struct jabber_data *jd = gc->proto_data = g_new0(struct jabber_data, 1); | |
3713 char *loginname = create_valid_jid(au->username, DEFAULT_SERVER, "GAIM"); | |
3714 | |
3715 /* | |
3716 * These do nothing during registration | |
3717 */ | |
3311 | 3718 jd->buddies = NULL; |
2956 | 3719 jd->chats = NULL; |
3720 | |
3721 if ((jd->gjc = gjab_new(loginname, au->password, gc)) == NULL) { | |
3722 g_free(loginname); | |
3723 debug_printf("jabber: unable to connect (jab_new failed)\n"); | |
2975 | 3724 hide_login_progress(gc, _("Unable to connect")); |
2956 | 3725 signoff(gc); |
3726 } else { | |
3727 gjab_state_handler(jd->gjc, jabber_handle_registration_state); | |
3728 gjab_packet_handler(jd->gjc, jabber_handleregresp); | |
3729 jd->gjc->queries = NULL; | |
3730 gjab_start(jd->gjc); | |
3731 } | |
3732 | |
3733 g_free(loginname); | |
3734 } | |
3735 | |
3736 /*----------------------------------------*/ | |
3737 /* End Jabber "user registration" support */ | |
3738 /*----------------------------------------*/ | |
3739 | |
3740 static void jabber_do_action(struct gaim_connection *gc, char *act) | |
3741 { | |
2975 | 3742 if (!strcmp(act, _("Set User Info"))) { |
2956 | 3743 jabber_setup_set_info(gc); |
3744 /* | |
2975 | 3745 } else if (!strcmp(act, _("Set Dir Info"))) { |
2956 | 3746 show_set_dir(gc); |
3257 | 3747 */ |
2975 | 3748 } else if (!strcmp(act, _("Change Password"))) { |
2956 | 3749 show_change_passwd(gc); |
3750 } | |
3751 } | |
3752 | |
3753 static GList *jabber_actions() | |
3754 { | |
3755 GList *m = NULL; | |
3756 | |
2975 | 3757 m = g_list_append(m, _("Set User Info")); |
2956 | 3758 /* |
2975 | 3759 m = g_list_append(m, _("Set Dir Info")); |
3257 | 3760 */ |
2975 | 3761 m = g_list_append(m, _("Change Password")); |
2956 | 3762 |
3763 return m; | |
3764 } | |
3765 | |
2086 | 3766 static struct prpl *my_protocol = NULL; |
3767 | |
3768 void jabber_init(struct prpl *ret) | |
3769 { | |
3770 /* the NULL's aren't required but they're nice to have */ | |
3771 ret->protocol = PROTO_JABBER; | |
3772 ret->options = OPT_PROTO_UNIQUE_CHATNAME | OPT_PROTO_CHAT_TOPIC; | |
3773 ret->name = jabber_name; | |
3774 ret->list_icon = jabber_list_icon; | |
3775 ret->away_states = jabber_away_states; | |
2956 | 3776 ret->actions = jabber_actions; |
3777 ret->do_action = jabber_do_action; | |
2086 | 3778 ret->buddy_menu = jabber_buddy_menu; |
3314 | 3779 ret->edit_buddy_menu = jabber_edit_buddy_menu; |
2086 | 3780 ret->user_opts = jabber_user_opts; |
3781 ret->login = jabber_login; | |
3782 ret->close = jabber_close; | |
3783 ret->send_im = jabber_send_im; | |
2956 | 3784 ret->set_info = jabber_set_info; |
2086 | 3785 ret->get_info = jabber_get_info; |
2956 | 3786 ret->get_cb_info = jabber_get_cb_info; |
3787 ret->get_cb_away = jabber_get_cb_away_msg; | |
2086 | 3788 ret->set_away = jabber_set_away; |
3789 ret->set_dir = NULL; | |
3790 ret->get_dir = NULL; | |
3791 ret->dir_search = NULL; | |
3792 ret->set_idle = jabber_set_idle; | |
3257 | 3793 ret->change_passwd = jabber_change_passwd; |
2086 | 3794 ret->add_buddy = jabber_add_buddy; |
3795 ret->add_buddies = NULL; | |
3796 ret->remove_buddy = jabber_remove_buddy; | |
3797 ret->add_permit = NULL; | |
3798 ret->add_deny = NULL; | |
3799 ret->rem_permit = NULL; | |
3800 ret->rem_deny = NULL; | |
3801 ret->set_permit_deny = NULL; | |
3802 ret->warn = NULL; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
3803 ret->chat_info = jabber_chat_info; |
2086 | 3804 ret->join_chat = jabber_join_chat; |
3805 ret->chat_invite = jabber_chat_invite; | |
3806 ret->chat_leave = jabber_chat_leave; | |
3807 ret->chat_whisper = jabber_chat_whisper; | |
3808 ret->chat_send = jabber_chat_send; | |
3809 ret->keepalive = jabber_keepalive; | |
3810 ret->normalize = jabber_normalize; | |
2956 | 3811 ret->register_user = jabber_register_user; |
3136 | 3812 ret->alias_buddy = jabber_roster_update; |
3813 ret->group_buddy = jabber_group_change; | |
3311 | 3814 ret->send_typing = jabber_send_typing; |
3815 ret->convo_closed = jabber_convo_closed; | |
2086 | 3816 |
3817 my_protocol = ret; | |
3818 } | |
3819 | |
3820 #ifndef STATIC | |
3821 | |
3822 char *gaim_plugin_init(GModule *handle) | |
3823 { | |
3824 load_protocol(jabber_init, sizeof(struct prpl)); | |
3825 return NULL; | |
3826 } | |
3827 | |
3828 void gaim_plugin_remove() | |
3829 { | |
3830 struct prpl *p = find_prpl(PROTO_JABBER); | |
3831 if (p == my_protocol) | |
3832 unload_protocol(p); | |
3833 } | |
3834 | |
3835 char *name() | |
3836 { | |
3837 return "Jabber"; | |
3838 } | |
3839 | |
3840 char *description() | |
3841 { | |
2162
a464da684307
[gaim-migrate @ 2172]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2154
diff
changeset
|
3842 return PRPL_DESC("Jabber"); |
2086 | 3843 } |
3844 | |
3845 #endif | |
3311 | 3846 |
3847 /* | |
3848 * Local variables: | |
3849 * c-indentation-style: k&r | |
3850 * c-basic-offset: 8 | |
3851 * indent-tabs-mode: notnil | |
3852 * End: | |
3853 * | |
3854 * vim: shiftwidth=8: | |
3855 */ |