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