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