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