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