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