Mercurial > pidgin.yaz
annotate src/protocols/jabber/jabber.c @ 2232:14e8978f86bb
[gaim-migrate @ 2242]
heh.
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Fri, 07 Sep 2001 08:58:24 +0000 |
parents | 8c4ff1a368bd |
children | 00a8b7bcef6c |
rev | line source |
---|---|
2086 | 1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
2 /* | |
3 * gaim | |
4 * | |
5 * Some code copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
6 * libfaim code copyright 1998, 1999 Adam Fritzler <afritz@auk.cx> | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 * | |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include "config.h" | |
26 #endif | |
27 | |
28 | |
29 #include <netdb.h> | |
30 #include <unistd.h> | |
31 #include <errno.h> | |
32 #include <netinet/in.h> | |
33 #include <arpa/inet.h> | |
34 #include <string.h> | |
35 #include <stdlib.h> | |
36 #include <stdio.h> | |
37 #include <time.h> | |
38 #include <sys/socket.h> | |
39 #include <sys/utsname.h> | |
40 #include <sys/stat.h> | |
41 #include "multi.h" | |
42 #include "prpl.h" | |
43 #include "gaim.h" | |
2232
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
44 #ifdef MAX |
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
45 #undef MAX |
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
46 #endif |
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
47 #ifdef MIN |
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
48 #undef MIN |
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
49 #endif |
2086 | 50 #include "jabber.h" |
51 #include "proxy.h" | |
52 | |
53 #include "pixmaps/available.xpm" | |
54 #include "pixmaps/available-away.xpm" | |
55 #include "pixmaps/available-chat.xpm" | |
56 #include "pixmaps/available-xa.xpm" | |
57 #include "pixmaps/available-dnd.xpm" | |
58 | |
59 /* The priv member of gjconn's is a gaim_connection for now. */ | |
60 #define GJ_GC(x) ((struct gaim_connection *)(x)->priv) | |
61 | |
62 #define IQID_AUTH "__AUTH__" | |
63 | |
64 #define IQ_NONE -1 | |
65 #define IQ_AUTH 0 | |
66 #define IQ_ROSTER 1 | |
67 | |
68 #define UC_AWAY 0x38 | |
69 #define UC_CHAT 0x48 | |
70 #define UC_XA 0x98 | |
71 #define UC_DND 0x118 | |
72 | |
73 #define DEFAULT_SERVER "jabber.org" | |
74 #define DEFAULT_GROUPCHAT "conference.jabber.org" | |
75 #define DEFAULT_PORT 5222 | |
76 | |
77 #define USEROPT_PORT 0 | |
78 | |
79 typedef struct gjconn_struct { | |
80 /* Core structure */ | |
81 pool p; /* Memory allocation pool */ | |
82 int state; /* Connection state flag */ | |
83 int fd; /* Connection file descriptor */ | |
84 jid user; /* User info */ | |
85 char *pass; /* User passwd */ | |
86 | |
87 /* Stream stuff */ | |
88 int id; /* id counter for jab_getid() function */ | |
89 char idbuf[9]; /* temporary storage for jab_getid() */ | |
90 char *sid; /* stream id from server, for digest auth */ | |
91 XML_Parser parser; /* Parser instance */ | |
92 xmlnode current; /* Current node in parsing instance.. */ | |
93 | |
94 /* Event callback ptrs */ | |
95 void (*on_state)(struct gjconn_struct *j, int state); | |
96 void (*on_packet)(struct gjconn_struct *j, jpacket p); | |
97 | |
98 void *priv; | |
99 | |
100 } *gjconn, gjconn_struct; | |
101 | |
102 typedef void (*gjconn_state_h)(gjconn j, int state); | |
103 typedef void (*gjconn_packet_h)(gjconn j, jpacket p); | |
104 | |
105 static gjconn gjab_new(char *user, char *pass, void *priv); | |
106 static void gjab_delete(gjconn j); | |
107 static void gjab_state_handler(gjconn j, gjconn_state_h h); | |
108 static void gjab_packet_handler(gjconn j, gjconn_packet_h h); | |
109 static void gjab_start(gjconn j); | |
110 static void gjab_stop(gjconn j); | |
111 /* | |
112 static int gjab_getfd(gjconn j); | |
113 static jid gjab_getjid(gjconn j); | |
114 static char *gjab_getsid(gjconn j); | |
115 */ | |
116 static char *gjab_getid(gjconn j); | |
117 static void gjab_send(gjconn j, xmlnode x); | |
118 static void gjab_send_raw(gjconn j, const char *str); | |
119 static void gjab_recv(gjconn j); | |
120 static void gjab_auth(gjconn j); | |
121 | |
122 struct jabber_data { | |
123 gjconn jc; | |
124 gboolean did_import; | |
125 GSList *pending_chats; | |
126 GSList *existing_chats; | |
127 GHashTable *hash; | |
128 time_t idle; | |
129 }; | |
130 | |
131 struct jabber_chat { | |
132 jid Jid; | |
133 struct gaim_connection *gc; | |
134 struct conversation *b; | |
135 int id; | |
136 }; | |
137 | |
138 static char *jabber_name() | |
139 { | |
140 return "Jabber"; | |
141 } | |
142 | |
143 #define STATE_EVT(arg) if(j->on_state) { (j->on_state)(j, (arg) ); } | |
144 | |
145 static char *create_valid_jid(const char *given, char *server, char *resource) | |
146 { | |
147 char *valid; | |
148 | |
149 if (!strchr(given, '@')) | |
150 valid = g_strdup_printf("%s@%s/%s", given, server, resource); | |
151 else if (!strchr(strchr(given, '@'), '/')) | |
152 valid = g_strdup_printf("%s/%s", given, resource); | |
153 else | |
154 valid = g_strdup(given); | |
155 | |
156 return valid; | |
157 } | |
158 | |
159 static gjconn gjab_new(char *user, char *pass, void *priv) | |
160 { | |
161 pool p; | |
162 gjconn j; | |
163 | |
164 if (!user) | |
165 return (NULL); | |
166 | |
167 p = pool_new(); | |
168 if (!p) | |
169 return (NULL); | |
170 j = pmalloc_x(p, sizeof(gjconn_struct), 0); | |
171 if (!j) | |
172 return (NULL); | |
173 j->p = p; | |
174 | |
175 j->user = jid_new(p, user); | |
176 j->pass = pstrdup(p, pass); | |
177 | |
178 j->state = JCONN_STATE_OFF; | |
179 j->id = 1; | |
180 j->fd = -1; | |
181 | |
182 j->priv = priv; | |
183 | |
184 return j; | |
185 } | |
186 | |
187 static void gjab_delete(gjconn j) | |
188 { | |
189 if (!j) | |
190 return; | |
191 | |
192 gjab_stop(j); | |
193 pool_free(j->p); | |
194 } | |
195 | |
196 static void gjab_state_handler(gjconn j, gjconn_state_h h) | |
197 { | |
198 if (!j) | |
199 return; | |
200 | |
201 j->on_state = h; | |
202 } | |
203 | |
204 static void gjab_packet_handler(gjconn j, gjconn_packet_h h) | |
205 { | |
206 if (!j) | |
207 return; | |
208 | |
209 j->on_packet = h; | |
210 } | |
211 | |
212 static void gjab_stop(gjconn j) | |
213 { | |
214 if (!j || j->state == JCONN_STATE_OFF) | |
215 return; | |
216 | |
217 j->state = JCONN_STATE_OFF; | |
218 gjab_send_raw(j, "</stream:stream>"); | |
219 close(j->fd); | |
220 j->fd = -1; | |
221 XML_ParserFree(j->parser); | |
222 j->parser = NULL; | |
223 } | |
224 | |
225 /* | |
226 static int gjab_getfd(gjconn j) | |
227 { | |
228 if (j) | |
229 return j->fd; | |
230 else | |
231 return -1; | |
232 } | |
233 | |
234 static jid gjab_getjid(gjconn j) | |
235 { | |
236 if (j) | |
237 return (j->user); | |
238 else | |
239 return NULL; | |
240 } | |
241 | |
242 static char *gjab_getsid(gjconn j) | |
243 { | |
244 if (j) | |
245 return (j->sid); | |
246 else | |
247 return NULL; | |
248 } | |
249 */ | |
250 | |
251 static char *gjab_getid(gjconn j) | |
252 { | |
253 snprintf(j->idbuf, 8, "%d", j->id++); | |
254 return &j->idbuf[0]; | |
255 } | |
256 | |
257 static void gjab_send(gjconn j, xmlnode x) | |
258 { | |
259 if (j && j->state != JCONN_STATE_OFF) { | |
260 char *buf = xmlnode2str(x); | |
261 if (buf) | |
262 write(j->fd, buf, strlen(buf)); | |
263 debug_printf("gjab_send: %s\n", buf); | |
264 } | |
265 } | |
266 | |
267 static void gjab_send_raw(gjconn j, const char *str) | |
268 { | |
269 if (j && j->state != JCONN_STATE_OFF) { | |
270 write(j->fd, str, strlen(str)); | |
271 debug_printf("gjab_send_raw: %s\n", str); | |
272 } | |
273 } | |
274 | |
275 static void gjab_reqroster(gjconn j) | |
276 { | |
277 xmlnode x; | |
278 | |
279 x = jutil_iqnew(JPACKET__GET, NS_ROSTER); | |
280 xmlnode_put_attrib(x, "id", gjab_getid(j)); | |
281 | |
282 gjab_send(j, x); | |
283 xmlnode_free(x); | |
284 } | |
285 | |
286 static void gjab_auth(gjconn j) | |
287 { | |
288 xmlnode x, y, z; | |
289 char *hash, *user; | |
290 | |
291 if (!j) | |
292 return; | |
293 | |
294 x = jutil_iqnew(JPACKET__SET, NS_AUTH); | |
295 xmlnode_put_attrib(x, "id", IQID_AUTH); | |
296 y = xmlnode_get_tag(x, "query"); | |
297 | |
298 user = j->user->user; | |
299 | |
300 if (user) { | |
301 z = xmlnode_insert_tag(y, "username"); | |
302 xmlnode_insert_cdata(z, user, -1); | |
303 } | |
304 | |
305 z = xmlnode_insert_tag(y, "resource"); | |
306 xmlnode_insert_cdata(z, j->user->resource, -1); | |
307 | |
308 if (j->sid) { | |
309 z = xmlnode_insert_tag(y, "digest"); | |
310 hash = pmalloc(x->p, strlen(j->sid) + strlen(j->pass) + 1); | |
311 strcpy(hash, j->sid); | |
312 strcat(hash, j->pass); | |
313 hash = shahash(hash); | |
314 xmlnode_insert_cdata(z, hash, 40); | |
315 } else { | |
316 z = xmlnode_insert_tag(y, "password"); | |
317 xmlnode_insert_cdata(z, j->pass, -1); | |
318 } | |
319 | |
320 gjab_send(j, x); | |
321 xmlnode_free(x); | |
322 | |
323 return; | |
324 } | |
325 | |
326 static void gjab_recv(gjconn j) | |
327 { | |
328 static char buf[4096]; | |
329 int len; | |
330 | |
331 if (!j || j->state == JCONN_STATE_OFF) | |
332 return; | |
333 | |
334 if ((len = read(j->fd, buf, sizeof(buf) - 1))) { | |
335 buf[len] = '\0'; | |
336 debug_printf("input (len %d): %s\n", len, buf); | |
337 XML_Parse(j->parser, buf, len, 0); | |
338 } else if (len <= 0) { | |
339 STATE_EVT(JCONN_STATE_OFF) | |
340 } | |
341 } | |
342 | |
343 static void startElement(void *userdata, const char *name, const char **attribs) | |
344 { | |
345 xmlnode x; | |
346 gjconn j = (gjconn) userdata; | |
347 | |
348 if (j->current) { | |
349 /* Append the node to the current one */ | |
350 x = xmlnode_insert_tag(j->current, name); | |
351 xmlnode_put_expat_attribs(x, attribs); | |
352 | |
353 j->current = x; | |
354 } else { | |
355 x = xmlnode_new_tag(name); | |
356 xmlnode_put_expat_attribs(x, attribs); | |
357 if (strcmp(name, "stream:stream") == 0) { | |
358 /* special case: name == stream:stream */ | |
359 /* id attrib of stream is stored for digest auth */ | |
360 j->sid = xmlnode_get_attrib(x, "id"); | |
361 /* STATE_EVT(JCONN_STATE_AUTH) */ | |
362 } else { | |
363 j->current = x; | |
364 } | |
365 } | |
366 } | |
367 | |
368 static void endElement(void *userdata, const char *name) | |
369 { | |
370 gjconn j = (gjconn) userdata; | |
371 xmlnode x; | |
372 jpacket p; | |
373 | |
374 if (j->current == NULL) { | |
375 /* we got </stream:stream> */ | |
376 STATE_EVT(JCONN_STATE_OFF) | |
377 return; | |
378 } | |
379 | |
380 x = xmlnode_get_parent(j->current); | |
381 | |
382 if (!x) { | |
383 /* it is time to fire the event */ | |
384 p = jpacket_new(j->current); | |
385 | |
386 if (j->on_packet) | |
387 (j->on_packet) (j, p); | |
388 else | |
389 xmlnode_free(j->current); | |
390 } | |
391 | |
392 j->current = x; | |
393 } | |
394 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
395 static void jabber_callback(gpointer data, gint source, GaimInputCondition condition) |
2086 | 396 { |
397 struct gaim_connection *gc = (struct gaim_connection *)data; | |
398 struct jabber_data *jd = (struct jabber_data *)gc->proto_data; | |
399 | |
400 gjab_recv(jd->jc); | |
401 } | |
402 | |
403 static void charData(void *userdata, const char *s, int slen) | |
404 { | |
405 gjconn j = (gjconn) userdata; | |
406 | |
407 if (j->current) | |
408 xmlnode_insert_cdata(j->current, s, slen); | |
409 } | |
410 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
411 static void gjab_connected(gpointer data, gint source, GaimInputCondition cond) |
2086 | 412 { |
413 xmlnode x; | |
414 char *t, *t2; | |
415 struct gaim_connection *gc = data; | |
416 struct jabber_data *jd; | |
417 gjconn j; | |
418 | |
419 if (!g_slist_find(connections, gc)) { | |
420 close(source); | |
421 return; | |
422 } | |
423 | |
424 jd = gc->proto_data; | |
425 j = jd->jc; | |
426 | |
427 if (source == -1) { | |
428 STATE_EVT(JCONN_STATE_OFF) | |
429 return; | |
430 } | |
431 | |
432 if (j->fd != source) | |
433 j->fd = source; | |
434 | |
435 j->state = JCONN_STATE_CONNECTED; | |
436 STATE_EVT(JCONN_STATE_CONNECTED) | |
437 | |
438 /* start stream */ | |
439 x = jutil_header(NS_CLIENT, j->user->server); | |
440 t = xmlnode2str(x); | |
441 /* this is ugly, we can create the string here instead of jutil_header */ | |
442 /* what do you think about it? -madcat */ | |
443 t2 = strstr(t, "/>"); | |
444 *t2++ = '>'; | |
445 *t2 = '\0'; | |
446 gjab_send_raw(j, "<?xml version='1.0'?>"); | |
447 gjab_send_raw(j, t); | |
448 xmlnode_free(x); | |
449 | |
450 j->state = JCONN_STATE_ON; | |
451 STATE_EVT(JCONN_STATE_ON); | |
452 | |
453 gc = GJ_GC(j); | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
454 gc->inpa = gaim_input_add(j->fd, GAIM_INPUT_READ, jabber_callback, gc); |
2086 | 455 } |
456 | |
457 static void gjab_start(gjconn j) | |
458 { | |
459 struct aim_user *user; | |
460 int port; | |
461 | |
462 if (!j || j->state != JCONN_STATE_OFF) | |
463 return; | |
464 | |
465 user = GJ_GC(j)->user; | |
466 port = user->proto_opt[USEROPT_PORT][0] ? atoi(user->proto_opt[USEROPT_PORT]) : DEFAULT_PORT; | |
467 | |
468 j->parser = XML_ParserCreate(NULL); | |
469 XML_SetUserData(j->parser, (void *)j); | |
470 XML_SetElementHandler(j->parser, startElement, endElement); | |
471 XML_SetCharacterDataHandler(j->parser, charData); | |
472 | |
473 j->fd = proxy_connect(j->user->server, port, gjab_connected, GJ_GC(j)); | |
474 if (!user->gc || (j->fd < 0)) { | |
475 STATE_EVT(JCONN_STATE_OFF) | |
476 return; | |
477 } | |
478 } | |
479 | |
480 static struct conversation *find_chat(struct gaim_connection *gc, char *name) | |
481 { | |
482 GSList *bcs = gc->buddy_chats; | |
483 struct conversation *b = NULL; | |
484 char *chat = g_strdup(normalize(name)); | |
485 | |
486 while (bcs) { | |
487 b = bcs->data; | |
488 if (!strcasecmp(normalize(b->name), chat)) | |
489 break; | |
490 b = NULL; | |
491 bcs = bcs->next; | |
492 } | |
493 | |
494 g_free(chat); | |
495 return b; | |
496 } | |
497 | |
498 static struct jabber_chat *find_existing_chat(struct gaim_connection *gc, jid chat) | |
499 { | |
500 GSList *bcs = ((struct jabber_data *)gc->proto_data)->existing_chats; | |
501 struct jabber_chat *jc = NULL; | |
502 | |
503 while (bcs) { | |
504 jc = bcs->data; | |
505 if (!jid_cmpx(chat, jc->Jid, JID_USER | JID_SERVER)) | |
506 break; | |
507 jc = NULL; | |
508 bcs = bcs->next; | |
509 } | |
510 | |
511 return jc; | |
512 } | |
513 | |
514 static struct jabber_chat *find_pending_chat(struct gaim_connection *gc, jid chat) | |
515 { | |
516 GSList *bcs = ((struct jabber_data *)gc->proto_data)->pending_chats; | |
517 struct jabber_chat *jc = NULL; | |
518 | |
519 while (bcs) { | |
520 jc = bcs->data; | |
521 if (!jid_cmpx(chat, jc->Jid, JID_USER | JID_SERVER)) | |
522 break; | |
523 jc = NULL; | |
524 bcs = bcs->next; | |
525 } | |
526 | |
527 return jc; | |
528 } | |
529 | |
530 static gboolean find_chat_buddy(struct conversation *b, char *name) | |
531 { | |
532 GList *m = b->in_room; | |
533 | |
534 while (m) { | |
535 if (!strcmp(m->data, name)) | |
536 return TRUE; | |
537 m = m->next; | |
538 } | |
539 | |
540 return FALSE; | |
541 } | |
542 | |
543 static void jabber_handlemessage(gjconn j, jpacket p) | |
544 { | |
545 xmlnode y, xmlns, subj; | |
546 | |
547 char *from = NULL, *msg = NULL, *type = NULL, *topic = NULL; | |
548 char m[BUF_LONG * 2]; | |
549 | |
550 type = xmlnode_get_attrib(p->x, "type"); | |
551 | |
552 if (!type || !strcasecmp(type, "normal") || !strcasecmp(type, "chat")) { | |
553 | |
554 /* XXX namespaces could be handled better. (mid) */ | |
555 if ((xmlns = xmlnode_get_tag(p->x, "x"))) | |
556 type = xmlnode_get_attrib(xmlns, "xmlns"); | |
557 | |
558 from = jid_full(p->from); | |
559 /* | |
560 if ((y = xmlnode_get_tag(p->x, "html"))) { | |
561 msg = xmlnode_get_data(y); | |
562 } else | |
563 */ | |
564 if ((y = xmlnode_get_tag(p->x, "body"))) { | |
565 msg = xmlnode_get_data(y); | |
566 } | |
567 | |
568 msg = utf8_to_str(msg); | |
569 | |
570 if (!from) | |
571 return; | |
572 | |
573 if (type && !strcasecmp(type, "jabber:x:conference")) { | |
574 char *room; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
575 GList *m = NULL; |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
576 char **data; |
2086 | 577 |
578 room = xmlnode_get_attrib(xmlns, "jid"); | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
579 data = g_strsplit(room, "@", 2); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
580 m = g_list_append(m, g_strdup(data[0])); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
581 m = g_list_append(m, g_strdup(data[1])); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
582 m = g_list_append(m, g_strdup(j->user->user)); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
583 g_strfreev(data); |
2086 | 584 |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
585 serv_got_chat_invite(GJ_GC(j), room, from, msg, m); |
2086 | 586 } else if (msg) { /* whisper */ |
587 struct jabber_chat *jc; | |
588 g_snprintf(m, sizeof(m), "%s", msg); | |
589 if (((jc = find_existing_chat(GJ_GC(j), p->from)) != NULL) && jc->b) | |
590 serv_got_chat_in(GJ_GC(j), jc->b->id, p->from->resource, 1, m, time(NULL)); | |
591 else { | |
592 if (find_conversation(jid_full(p->from))) | |
593 serv_got_im(GJ_GC(j), jid_full(p->from), m, 0, time(NULL)); | |
594 else { | |
595 from = g_strdup_printf("%s@%s", p->from->user, p->from->server); | |
596 serv_got_im(GJ_GC(j), from, m, 0, time(NULL)); | |
597 g_free(from); | |
598 } | |
599 } | |
600 } | |
601 | |
602 if (msg) | |
603 g_free(msg); | |
604 | |
605 } else if (!strcasecmp(type, "error")) { | |
606 if ((y = xmlnode_get_tag(p->x, "error"))) { | |
607 type = xmlnode_get_attrib(y, "code"); | |
608 msg = xmlnode_get_data(y); | |
609 } | |
610 | |
611 if (msg) { | |
612 from = g_strdup_printf("Error %s", type ? type : ""); | |
613 do_error_dialog(msg, from); | |
614 g_free(from); | |
615 } | |
616 } else if (!strcasecmp(type, "groupchat")) { | |
617 struct jabber_chat *jc; | |
618 static int i = 0; | |
619 | |
620 /* | |
621 if ((y = xmlnode_get_tag(p->x, "html"))) { | |
622 msg = xmlnode_get_data(y); | |
623 } else | |
624 */ | |
625 if ((y = xmlnode_get_tag(p->x, "body"))) { | |
626 msg = xmlnode_get_data(y); | |
627 } | |
628 | |
629 msg = utf8_to_str(msg); | |
630 | |
631 if ((subj = xmlnode_get_tag(p->x, "subject"))) { | |
632 topic = xmlnode_get_data(subj); | |
633 } | |
634 topic = utf8_to_str(topic); | |
635 | |
636 jc = find_existing_chat(GJ_GC(j), p->from); | |
637 if (!jc) { | |
638 /* we're not in this chat. are we supposed to be? */ | |
639 struct jabber_data *jd = GJ_GC(j)->proto_data; | |
640 if ((jc = find_pending_chat(GJ_GC(j), p->from)) != NULL) { | |
641 /* yes, we're supposed to be. so now we are. */ | |
642 jc->b = serv_got_joined_chat(GJ_GC(j), i++, p->from->user); | |
643 jc->id = jc->b->id; | |
644 jd->existing_chats = g_slist_append(jd->existing_chats, jc); | |
645 jd->pending_chats = g_slist_remove(jd->pending_chats, jc); | |
646 } else { | |
647 /* no, we're not supposed to be. */ | |
648 g_free(msg); | |
649 return; | |
650 } | |
651 } | |
652 if (p->from->resource) { | |
653 if (!y) { | |
654 if (!find_chat_buddy(jc->b, p->from->resource)) | |
655 add_chat_buddy(jc->b, p->from->resource); | |
656 else if ((y = xmlnode_get_tag(p->x, "status"))) { | |
657 char buf[8192]; | |
658 msg = xmlnode_get_data(y); | |
659 g_snprintf(buf, sizeof(buf), "%s now has status: %s", | |
660 p->from->resource, msg); | |
661 write_to_conv(jc->b, buf, WFLAG_SYSTEM, NULL, time(NULL)); | |
662 } | |
663 } else if (jc->b && msg) { | |
664 char buf[8192]; | |
665 | |
666 if (topic) { | |
667 char tbuf[8192]; | |
668 g_snprintf(tbuf, sizeof(tbuf), "%s", topic); | |
669 chat_set_topic(jc->b, p->from->resource, tbuf); | |
670 } | |
671 | |
672 | |
673 g_snprintf(buf, sizeof(buf), "%s", msg); | |
674 serv_got_chat_in(GJ_GC(j), jc->b->id, p->from->resource, 0, buf, time(NULL)); | |
675 } | |
676 } else { /* message from the server */ | |
677 if(jc->b && topic) { | |
678 char tbuf[8192]; | |
679 g_snprintf(tbuf, sizeof(tbuf), "%s", topic); | |
680 chat_set_topic(jc->b, "", tbuf); | |
681 } | |
682 } | |
683 | |
684 g_free(msg); | |
685 g_free(topic); | |
686 | |
687 } else { | |
688 debug_printf("unhandled message %s\n", type); | |
689 } | |
690 } | |
691 | |
692 static void jabber_handlepresence(gjconn j, jpacket p) | |
693 { | |
694 char *to, *from, *type; | |
695 struct buddy *b = NULL; | |
696 jid who; | |
697 char *buddy; | |
698 xmlnode y; | |
699 char *show; | |
700 int state = UC_NORMAL; | |
701 GSList *resources; | |
702 char *res; | |
703 struct conversation *cnv = NULL; | |
704 struct jabber_chat *jc = NULL; | |
705 | |
706 to = xmlnode_get_attrib(p->x, "to"); | |
707 from = xmlnode_get_attrib(p->x, "from"); | |
708 type = xmlnode_get_attrib(p->x, "type"); | |
709 | |
710 if ((y = xmlnode_get_tag(p->x, "show"))) { | |
711 show = xmlnode_get_data(y); | |
712 if (!show) { | |
713 state = UC_NORMAL; | |
714 } else if (!strcasecmp(show, "away")) { | |
715 state = UC_AWAY; | |
716 } else if (!strcasecmp(show, "chat")) { | |
717 state = UC_CHAT; | |
718 } else if (!strcasecmp(show, "xa")) { | |
719 state = UC_XA; | |
720 } else if (!strcasecmp(show, "dnd")) { | |
721 state = UC_DND; | |
722 } | |
723 } else { | |
724 state = UC_NORMAL; | |
725 } | |
726 | |
727 who = jid_new(j->p, from); | |
728 if (who->user == NULL) { | |
729 /* FIXME: transport */ | |
730 return; | |
731 } | |
732 | |
733 buddy = g_strdup_printf("%s@%s", who->user, who->server); | |
734 | |
735 /* um. we're going to check if it's a chat. if it isn't, and there are pending | |
736 * chats, create the chat. if there aren't pending chats, add the buddy. */ | |
737 if ((cnv = find_chat(GJ_GC(j), who->user)) == NULL) { | |
738 static int i = 0x70; | |
739 struct jabber_data *jd = GJ_GC(j)->proto_data; | |
740 if ((jc = find_pending_chat(GJ_GC(j), who)) != NULL) { | |
741 jc->b = cnv = serv_got_joined_chat(GJ_GC(j), i++, who->user); | |
742 jc->id = jc->b->id; | |
743 jd->existing_chats = g_slist_append(jd->existing_chats, jc); | |
744 jd->pending_chats = g_slist_remove(jd->pending_chats, jc); | |
745 } else if (!(b = find_buddy(GJ_GC(j), buddy))) { | |
746 b = add_buddy(GJ_GC(j), "Buddies", buddy, buddy); | |
747 do_export(GJ_GC(j)); | |
748 } | |
749 } | |
750 | |
751 if (!cnv) { | |
752 resources = b->proto_data; | |
753 res = who->resource; | |
754 if (res) | |
755 while (resources) { | |
756 if (!strcmp(res, resources->data)) | |
757 break; | |
758 resources = resources->next; | |
759 } | |
760 | |
761 if (type && (strcasecmp(type, "unavailable") == 0)) { | |
762 if (resources) { | |
763 g_free(resources->data); | |
764 b->proto_data = g_slist_remove(b->proto_data, resources->data); | |
765 } | |
766 if (!b->proto_data) { | |
767 serv_got_update(GJ_GC(j), buddy, 0, 0, 0, 0, 0, 0); | |
768 } | |
769 } else { | |
770 /* keep track of away msg same as yahoo plugin */ | |
771 struct jabber_data *jd = GJ_GC(j)->proto_data; | |
772 gpointer val = g_hash_table_lookup(jd->hash, b->name); | |
773 if (val) | |
774 g_free(val); | |
775 g_hash_table_insert(jd->hash, g_strdup(b->name), g_strdup(xmlnode_get_tag_data(p->x, "status"))); | |
776 | |
777 | |
778 if (!resources) { | |
779 b->proto_data = g_slist_append(b->proto_data, g_strdup(res)); | |
780 } | |
781 | |
782 serv_got_update(GJ_GC(j), buddy, 1, 0, 0, 0, state, 0); | |
783 | |
784 } | |
785 } else { | |
786 if (who->resource) { | |
787 if (type && !strcasecmp(type, "unavailable")) { | |
788 struct jabber_data *jd; | |
789 if (!jc && !(jc = find_existing_chat(GJ_GC(j), who))) { | |
790 g_free(buddy); | |
791 return; | |
792 } | |
793 jd = jc->gc->proto_data; | |
794 if (strcmp(who->resource, jc->Jid->resource) && jc->b) { | |
795 remove_chat_buddy(jc->b, who->resource); | |
796 return; | |
797 } | |
798 | |
799 jd->existing_chats = g_slist_remove(jd->existing_chats, jc); | |
800 serv_got_chat_left(GJ_GC(j), jc->id); | |
801 g_free(jc); | |
802 } else { | |
803 if ((!jc && !(jc = find_existing_chat(GJ_GC(j), who))) || !jc->b) { | |
804 g_free(buddy); | |
805 return; | |
806 } | |
807 if (!find_chat_buddy(jc->b, who->resource)) | |
808 add_chat_buddy(jc->b, who->resource); | |
809 else if ((y = xmlnode_get_tag(p->x, "status"))) { | |
810 char buf[8192]; | |
811 char *msg = xmlnode_get_data(y); | |
812 g_snprintf(buf, sizeof(buf), "%s now has status: %s", | |
813 p->from->resource, msg); | |
814 write_to_conv(jc->b, buf, WFLAG_SYSTEM, NULL, time(NULL)); | |
815 } | |
816 } | |
817 } | |
818 } | |
819 | |
820 g_free(buddy); | |
821 | |
822 return; | |
823 } | |
824 | |
825 static void jabber_handles10n(gjconn j, jpacket p) | |
826 { | |
827 xmlnode g; | |
828 char *Jid = xmlnode_get_attrib(p->x, "from"); | |
829 char *ask = xmlnode_get_attrib(p->x, "type"); | |
830 | |
831 g = xmlnode_new_tag("presence"); | |
832 xmlnode_put_attrib(g, "to", Jid); | |
833 if (!strcmp(ask, "subscribe")) | |
834 xmlnode_put_attrib(g, "type", "subscribed"); | |
835 else if (!strcmp(ask, "unsubscribe")) | |
836 xmlnode_put_attrib(g, "type", "unsubscribed"); | |
837 else | |
838 return; | |
839 | |
840 gjab_send(j, g); | |
841 } | |
842 | |
843 static void jabber_handleroster(gjconn j, xmlnode querynode) | |
844 { | |
845 xmlnode x; | |
846 | |
847 x = xmlnode_get_firstchild(querynode); | |
848 while (x) { | |
849 xmlnode g; | |
850 char *Jid, *name, *sub, *ask; | |
851 jid who; | |
852 | |
853 Jid = xmlnode_get_attrib(x, "jid"); | |
854 name = xmlnode_get_attrib(x, "name"); | |
855 sub = xmlnode_get_attrib(x, "subscription"); | |
856 ask = xmlnode_get_attrib(x, "ask"); | |
857 who = jid_new(j->p, Jid); | |
858 | |
859 if ((g = xmlnode_get_firstchild(x))) { | |
860 while (g) { | |
2112
0ad5f2cf6728
[gaim-migrate @ 2122]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2111
diff
changeset
|
861 if (xmlnode_get_name(g) && |
0ad5f2cf6728
[gaim-migrate @ 2122]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2111
diff
changeset
|
862 g_strncasecmp(xmlnode_get_name(g), "group", 5) == 0) { |
2086 | 863 struct buddy *b = NULL; |
864 char *groupname, *buddyname; | |
865 | |
2111
109669b3887e
[gaim-migrate @ 2121]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
866 if (!who || !who->user) { |
2086 | 867 /* FIXME: transport */ |
868 g = xmlnode_get_nextsibling(g); | |
869 continue; | |
870 } | |
871 buddyname = g_strdup_printf("%s@%s", who->user, who->server); | |
872 groupname = xmlnode_get_data(xmlnode_get_firstchild(g)); | |
873 if (groupname == NULL) | |
874 groupname = "Buddies"; | |
875 if (strcasecmp(sub, "from") && strcasecmp(sub, "none") && | |
876 !(b = find_buddy(GJ_GC(j), buddyname))) { | |
877 debug_printf("adding buddy: %s\n", buddyname); | |
878 b = add_buddy(GJ_GC(j), groupname, buddyname, | |
879 name ? name : buddyname); | |
880 do_export(GJ_GC(j)); | |
881 /* | |
882 } else if (b) { | |
883 debug_printf("updating buddy: %s/%s\n", buddyname, name); | |
884 g_snprintf(b->name, sizeof(b->name), "%s", buddyname); | |
885 g_snprintf(b->show, sizeof(b->show), "%s", | |
886 name ? name : buddyname); | |
887 */ | |
888 } | |
889 g_free(buddyname); | |
890 } | |
891 g = xmlnode_get_nextsibling(g); | |
892 } | |
893 } else { | |
894 struct buddy *b; | |
895 char *buddyname; | |
896 | |
2111
109669b3887e
[gaim-migrate @ 2121]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
897 if (!who || !who->user) { |
2086 | 898 /* FIXME: transport */ |
899 x = xmlnode_get_nextsibling(x); | |
900 continue; | |
901 } | |
902 buddyname = g_strdup_printf("%s@%s", who->user, who->server); | |
903 if (strcasecmp(sub, "from") && strcasecmp(sub, "none") && | |
904 !(b = find_buddy(GJ_GC(j), buddyname))) { | |
905 debug_printf("adding buddy: %s\n", buddyname); | |
906 b = add_buddy(GJ_GC(j), "Buddies", buddyname, name ? name : Jid); | |
907 do_export(GJ_GC(j)); | |
908 } | |
909 g_free(buddyname); | |
910 } | |
911 | |
912 x = xmlnode_get_nextsibling(x); | |
913 } | |
914 | |
915 x = jutil_presnew(0, NULL, "Online"); | |
916 gjab_send(j, x); | |
917 xmlnode_free(x); | |
918 } | |
919 | |
920 static void jabber_handlevcard(gjconn j, xmlnode querynode, char *from) { | |
921 char buf[1024]; | |
922 char *fn, *url, *email, *nickname, *status, *desc; | |
923 jid who; | |
924 char *buddy; | |
925 struct jabber_data *jd = GJ_GC(j)->proto_data; | |
926 int at = 0; | |
927 | |
928 who = jid_new(j->p, from); | |
929 buddy = g_strdup_printf("%s@%s", who->user, who->server); | |
930 | |
931 fn = xmlnode_get_tag_data(querynode, "FN"); | |
932 url = xmlnode_get_tag_data(querynode, "URL"); | |
933 email = xmlnode_get_tag_data(querynode, "EMAIL"); | |
934 nickname = xmlnode_get_tag_data(querynode, "NICKNAME"); | |
935 desc = xmlnode_get_tag_data(querynode, "DESC"); | |
936 status = g_hash_table_lookup(jd->hash, buddy); | |
937 if (!status) | |
938 status = "Online"; | |
939 | |
940 at = g_snprintf(buf, sizeof buf, "<B>Jabber ID:</B> %s<BR>", buddy); | |
941 if (fn) | |
942 at += g_snprintf(buf + at, sizeof(buf) - at, "<B>Full Name:</B> %s<BR>", fn); | |
943 if (nickname) | |
944 at += g_snprintf(buf + at, sizeof(buf) - at, "<B>Nickname:</B> %s<BR>", nickname); | |
945 if (url) | |
946 at += g_snprintf(buf + at, sizeof(buf) - at, "<B>URL:</B> <A HREF=\"%s\">%s</A><BR>", | |
947 url, url); | |
948 if (email) | |
949 at += g_snprintf(buf + at, sizeof(buf) - at, | |
950 "<B>Email:</B> <A HREF=\"mailto:%s\">%s</A><BR>", email, email); | |
951 at += g_snprintf(buf + at, sizeof(buf) - at, "<B>Status:</B> %s\n", status); | |
952 if (desc) | |
953 at += g_snprintf(buf + at, sizeof(buf) - at, "<HR>%s<br>\n", desc); | |
954 | |
2137
18722ae5b882
[gaim-migrate @ 2147]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2131
diff
changeset
|
955 g_show_info_text(buf, NULL); |
2086 | 956 g_free(buddy); |
957 } | |
958 | |
959 static void jabber_handleauthresp(gjconn j, jpacket p) | |
960 { | |
961 if (jpacket_subtype(p) == JPACKET__RESULT) { | |
962 debug_printf("auth success\n"); | |
963 | |
964 account_online(GJ_GC(j)); | |
965 serv_finish_login(GJ_GC(j)); | |
966 | |
967 if (bud_list_cache_exists(GJ_GC(j))) | |
968 do_import(NULL, GJ_GC(j)); | |
969 | |
970 ((struct jabber_data *)GJ_GC(j)->proto_data)->did_import = TRUE; | |
971 | |
972 gjab_reqroster(j); | |
973 } else { | |
974 xmlnode xerr; | |
975 char *errmsg = NULL; | |
976 int errcode = 0; | |
977 | |
978 debug_printf("auth failed\n"); | |
979 xerr = xmlnode_get_tag(p->x, "error"); | |
980 if (xerr) { | |
981 char msg[BUF_LONG]; | |
982 errmsg = xmlnode_get_data(xerr); | |
983 if (xmlnode_get_attrib(xerr, "code")) { | |
984 errcode = atoi(xmlnode_get_attrib(xerr, "code")); | |
985 g_snprintf(msg, sizeof(msg), "Error %d: %s", errcode, errmsg); | |
986 } else | |
987 g_snprintf(msg, sizeof(msg), "%s", errmsg); | |
988 hide_login_progress(GJ_GC(j), msg); | |
989 } else { | |
990 hide_login_progress(GJ_GC(j), "Unknown login error"); | |
991 } | |
992 | |
993 signoff(GJ_GC(j)); | |
994 } | |
995 } | |
996 | |
997 static void jabber_handleversion(gjconn j, xmlnode iqnode) { | |
998 xmlnode querynode, x; | |
999 char *id, *from; | |
1000 char os[1024]; | |
1001 struct utsname osinfo; | |
1002 | |
1003 uname(&osinfo); | |
1004 g_snprintf(os, sizeof os, "%s %s %s", osinfo.sysname, osinfo.release, osinfo.machine); | |
1005 | |
1006 id = xmlnode_get_attrib(iqnode, "id"); | |
1007 from = xmlnode_get_attrib(iqnode, "from"); | |
1008 | |
1009 x = jutil_iqnew(JPACKET__RESULT, NS_VERSION); | |
1010 | |
1011 xmlnode_put_attrib(x, "to", from); | |
1012 xmlnode_put_attrib(x, "id", id); | |
1013 querynode = xmlnode_get_tag(x, "query"); | |
1014 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "name"), PACKAGE, -1); | |
1015 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "version"), VERSION, -1); | |
1016 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "os"), os, -1); | |
1017 | |
1018 gjab_send(j, x); | |
1019 | |
1020 xmlnode_free(x); | |
1021 } | |
1022 | |
1023 static void jabber_handletime(gjconn j, xmlnode iqnode) { | |
1024 xmlnode querynode, x; | |
1025 char *id, *from; | |
1026 time_t now_t; | |
1027 struct tm *now; | |
1028 char buf[1024]; | |
1029 | |
1030 time(&now_t); | |
1031 now = localtime(&now_t); | |
1032 | |
1033 id = xmlnode_get_attrib(iqnode, "id"); | |
1034 from = xmlnode_get_attrib(iqnode, "from"); | |
1035 | |
1036 x = jutil_iqnew(JPACKET__RESULT, NS_TIME); | |
1037 | |
1038 xmlnode_put_attrib(x, "to", from); | |
1039 xmlnode_put_attrib(x, "id", id); | |
1040 querynode = xmlnode_get_tag(x, "query"); | |
1041 | |
1042 strftime(buf, 1024, "%Y%m%dT%T", now); | |
1043 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "utc"), buf, -1); | |
1044 strftime(buf, 1024, "%Z", now); | |
1045 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "tz"), buf, -1); | |
1046 strftime(buf, 1024, "%d %b %Y %T", now); | |
1047 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "display"), buf, -1); | |
1048 | |
1049 gjab_send(j, x); | |
1050 | |
1051 xmlnode_free(x); | |
1052 } | |
1053 | |
1054 static void jabber_handlelast(gjconn j, xmlnode iqnode) { | |
1055 xmlnode x, querytag; | |
1056 char *id, *from; | |
1057 struct jabber_data *jd = GJ_GC(j)->proto_data; | |
1058 char idle_time[32]; | |
1059 | |
1060 id = xmlnode_get_attrib(iqnode, "id"); | |
1061 from = xmlnode_get_attrib(iqnode, "from"); | |
1062 | |
1063 x = jutil_iqnew(JPACKET__RESULT, "jabber:iq:last"); | |
1064 | |
1065 xmlnode_put_attrib(x, "to", from); | |
1066 xmlnode_put_attrib(x, "id", id); | |
1067 querytag = xmlnode_get_tag(x, "query"); | |
1068 g_snprintf(idle_time, sizeof idle_time, "%ld", jd->idle ? time(NULL) - jd->idle : 0); | |
1069 xmlnode_put_attrib(querytag, "seconds", idle_time); | |
1070 | |
1071 gjab_send(j, x); | |
1072 xmlnode_free(x); | |
1073 } | |
1074 | |
1075 static void jabber_handlepacket(gjconn j, jpacket p) | |
1076 { | |
1077 switch (p->type) { | |
1078 case JPACKET_MESSAGE: | |
1079 jabber_handlemessage(j, p); | |
1080 break; | |
1081 case JPACKET_PRESENCE: | |
1082 jabber_handlepresence(j, p); | |
1083 break; | |
1084 case JPACKET_IQ: | |
1085 debug_printf("jpacket_subtype: %d\n", jpacket_subtype(p)); | |
1086 | |
1087 if (xmlnode_get_attrib(p->x, "id") && (strcmp(xmlnode_get_attrib(p->x, "id"), IQID_AUTH) == 0)) { | |
1088 jabber_handleauthresp(j, p); | |
1089 break; /* I'm not sure if you like this style, Eric. */ | |
1090 } | |
1091 | |
1092 if (jpacket_subtype(p) == JPACKET__SET) { | |
1093 } else if (jpacket_subtype(p) == JPACKET__GET) { | |
1094 xmlnode querynode; | |
1095 querynode = xmlnode_get_tag(p->x, "query"); | |
1096 if(NSCHECK(querynode, NS_VERSION)) { | |
1097 jabber_handleversion(j, p->x); | |
1098 } else if (NSCHECK(querynode, NS_TIME)) { | |
1099 jabber_handletime(j, p->x); | |
1100 } else if (NSCHECK(querynode, "jabber:iq:last")) { | |
1101 jabber_handlelast(j, p->x); | |
1102 } | |
1103 } else if (jpacket_subtype(p) == JPACKET__RESULT) { | |
1104 xmlnode querynode, vcard; | |
1105 char *xmlns, *from; | |
1106 | |
1107 from = xmlnode_get_attrib(p->x, "from"); | |
1108 querynode = xmlnode_get_tag(p->x, "query"); | |
1109 xmlns = xmlnode_get_attrib(querynode, "xmlns"); | |
1110 vcard = xmlnode_get_tag(p->x, "vCard"); | |
1111 | |
1112 if (NSCHECK(querynode, NS_ROSTER)) { | |
1113 jabber_handleroster(j, querynode); | |
1114 } else if (NSCHECK(querynode, NS_VCARD)) { | |
1115 jabber_handlevcard(j, querynode, from); | |
1116 } else if(vcard) { | |
1117 jabber_handlevcard(j, vcard, from); | |
1118 } else { | |
1119 /* debug_printf("jabber:iq:query: %s\n", xmlns); */ | |
1120 } | |
1121 | |
1122 } else if (jpacket_subtype(p) == JPACKET__ERROR) { | |
1123 xmlnode xerr; | |
1124 char *from, *errmsg = NULL; | |
1125 int errcode = 0; | |
1126 | |
1127 from = xmlnode_get_attrib(p->x, "from"); | |
1128 xerr = xmlnode_get_tag(p->x, "error"); | |
1129 if (xerr) { | |
1130 errmsg = xmlnode_get_data(xerr); | |
1131 if (xmlnode_get_attrib(xerr, "code")) | |
1132 errcode = atoi(xmlnode_get_attrib(xerr, "code")); | |
1133 } | |
1134 | |
1135 from = g_strdup_printf("Error %d (%s)", errcode, from); | |
1136 do_error_dialog(errmsg, from); | |
1137 g_free(from); | |
1138 | |
1139 } | |
1140 | |
1141 break; | |
1142 | |
1143 case JPACKET_S10N: | |
1144 jabber_handles10n(j, p); | |
1145 break; | |
1146 default: | |
1147 debug_printf("jabber: packet type %d (%s)\n", p->type, xmlnode2str(p->x)); | |
1148 } | |
1149 | |
1150 xmlnode_free(p->x); | |
1151 | |
1152 return; | |
1153 } | |
1154 | |
1155 static void jabber_handlestate(gjconn j, int state) | |
1156 { | |
1157 switch (state) { | |
1158 case JCONN_STATE_OFF: | |
1159 hide_login_progress(GJ_GC(j), "Unable to connect"); | |
1160 signoff(GJ_GC(j)); | |
1161 break; | |
1162 case JCONN_STATE_CONNECTED: | |
1163 set_login_progress(GJ_GC(j), 3, "Connected"); | |
1164 break; | |
1165 case JCONN_STATE_ON: | |
1166 set_login_progress(GJ_GC(j), 5, "Logging in..."); | |
1167 gjab_auth(j); | |
1168 break; | |
1169 default: | |
1170 debug_printf("state change: %d\n", state); | |
1171 } | |
1172 return; | |
1173 } | |
1174 | |
1175 static void jabber_login(struct aim_user *user) | |
1176 { | |
1177 struct gaim_connection *gc = new_gaim_conn(user); | |
1178 struct jabber_data *jd = gc->proto_data = g_new0(struct jabber_data, 1); | |
1179 char *loginname = create_valid_jid(user->username, DEFAULT_SERVER, "GAIM"); | |
1180 | |
1181 jd->hash = g_hash_table_new(g_str_hash, g_str_equal); | |
1182 | |
1183 set_login_progress(gc, 1, "Connecting"); | |
1184 | |
1185 if (!(jd->jc = gjab_new(loginname, user->password, gc))) { | |
1186 g_free(loginname); | |
1187 debug_printf("jabber: unable to connect (jab_new failed)\n"); | |
1188 hide_login_progress(gc, "Unable to connect"); | |
1189 signoff(gc); | |
1190 return; | |
1191 } | |
1192 | |
1193 g_free(loginname); | |
1194 gjab_state_handler(jd->jc, jabber_handlestate); | |
1195 gjab_packet_handler(jd->jc, jabber_handlepacket); | |
1196 gjab_start(jd->jc); | |
1197 } | |
1198 | |
1199 static gboolean jabber_destroy_hash(gpointer key, gpointer val, gpointer data) { | |
1200 g_free(key); | |
1201 g_free(val); | |
1202 return TRUE; | |
1203 } | |
1204 | |
1205 static gboolean jabber_free(gpointer data) | |
1206 { | |
1207 gjab_delete(data); | |
1208 return FALSE; | |
1209 } | |
1210 | |
1211 static void jabber_close(struct gaim_connection *gc) | |
1212 { | |
1213 struct jabber_data *jd = gc->proto_data; | |
1214 g_hash_table_foreach_remove(jd->hash, jabber_destroy_hash, NULL); | |
1215 g_hash_table_destroy(jd->hash); | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
1216 gaim_input_remove(gc->inpa); |
2086 | 1217 close(jd->jc->fd); |
2131
acc11216ec5d
[gaim-migrate @ 2141]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2123
diff
changeset
|
1218 g_timeout_add(50, jabber_free, jd->jc); |
2086 | 1219 jd->jc = NULL; |
1220 g_free(jd); | |
1221 gc->proto_data = NULL; | |
1222 } | |
1223 | |
2231
8c4ff1a368bd
[gaim-migrate @ 2241]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2205
diff
changeset
|
1224 static int jabber_send_im(struct gaim_connection *gc, char *who, char *message, int flags) |
2086 | 1225 { |
1226 xmlnode x, y; | |
1227 char *realwho; | |
1228 gjconn j = ((struct jabber_data *)gc->proto_data)->jc; | |
1229 | |
1230 if (!who || !message) | |
2123
56c4382f2909
[gaim-migrate @ 2133]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2112
diff
changeset
|
1231 return 0; |
2086 | 1232 |
1233 x = xmlnode_new_tag("message"); | |
1234 if (!strchr(who, '@')) | |
1235 realwho = g_strdup_printf("%s@%s", who, j->user->server); | |
1236 else | |
1237 realwho = g_strdup(who); | |
1238 xmlnode_put_attrib(x, "to", realwho); | |
1239 g_free(realwho); | |
1240 | |
1241 xmlnode_put_attrib(x, "type", "chat"); | |
1242 | |
1243 if (message && strlen(message)) { | |
1244 char *utf8 = str_to_utf8(message); | |
1245 y = xmlnode_insert_tag(x, "body"); | |
1246 xmlnode_insert_cdata(y, utf8, -1); | |
1247 g_free(utf8); | |
1248 } | |
1249 | |
1250 gjab_send(((struct jabber_data *)gc->proto_data)->jc, x); | |
1251 xmlnode_free(x); | |
2123
56c4382f2909
[gaim-migrate @ 2133]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2112
diff
changeset
|
1252 return 0; |
2086 | 1253 } |
1254 | |
1255 static void jabber_add_buddy(struct gaim_connection *gc, char *name) | |
1256 { | |
1257 xmlnode x, y; | |
1258 char *realwho; | |
1259 gjconn j = ((struct jabber_data *)gc->proto_data)->jc; | |
1260 | |
1261 if (!((struct jabber_data *)gc->proto_data)->did_import) | |
1262 return; | |
1263 | |
1264 if (!name) | |
1265 return; | |
1266 | |
1267 if (!strcmp(gc->username, name)) | |
1268 return; | |
1269 | |
1270 if (!strchr(name, '@')) | |
1271 realwho = g_strdup_printf("%s@%s", name, j->user->server); | |
1272 else { | |
1273 jid who = jid_new(j->p, name); | |
1274 if (who->user == NULL) { | |
1275 /* FIXME: transport */ | |
1276 return; | |
1277 } | |
1278 realwho = g_strdup_printf("%s@%s", who->user, who->server); | |
1279 } | |
1280 | |
1281 x = jutil_iqnew(JPACKET__SET, NS_ROSTER); | |
1282 y = xmlnode_insert_tag(xmlnode_get_tag(x, "query"), "item"); | |
1283 xmlnode_put_attrib(y, "jid", realwho); | |
1284 gjab_send(((struct jabber_data *)gc->proto_data)->jc, x); | |
1285 xmlnode_free(x); | |
1286 | |
1287 x = xmlnode_new_tag("presence"); | |
1288 xmlnode_put_attrib(x, "to", realwho); | |
1289 xmlnode_put_attrib(x, "type", "subscribe"); | |
1290 gjab_send(((struct jabber_data *)gc->proto_data)->jc, x); | |
1291 | |
1292 g_free(realwho); | |
1293 } | |
1294 | |
1295 static void jabber_remove_buddy(struct gaim_connection *gc, char *name) | |
1296 { | |
1297 xmlnode x, y; | |
1298 char *realwho; | |
1299 gjconn j = ((struct jabber_data *)gc->proto_data)->jc; | |
1300 | |
1301 if (!name) | |
1302 return; | |
1303 | |
1304 if (!strchr(name, '@')) | |
1305 realwho = g_strdup_printf("%s@%s", name, j->user->server); | |
1306 else | |
1307 realwho = g_strdup(name); | |
1308 | |
1309 x = jutil_iqnew(JPACKET__SET, NS_ROSTER); | |
1310 y = xmlnode_insert_tag(xmlnode_get_tag(x, "query"), "item"); | |
1311 xmlnode_put_attrib(y, "jid", realwho); | |
1312 xmlnode_put_attrib(y, "subscription", "remove"); | |
1313 gjab_send(((struct jabber_data *)gc->proto_data)->jc, x); | |
1314 | |
1315 g_free(realwho); | |
1316 xmlnode_free(x); | |
1317 } | |
1318 | |
1319 static char **jabber_list_icon(int uc) | |
1320 { | |
1321 switch (uc) { | |
1322 case UC_AWAY: | |
1323 return available_away_xpm; | |
1324 case UC_CHAT: | |
1325 return available_chat_xpm; | |
1326 case UC_XA: | |
1327 return available_xa_xpm; | |
1328 case UC_DND: | |
1329 return available_dnd_xpm; | |
1330 default: | |
1331 return available_xpm; | |
1332 } | |
1333 } | |
1334 | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1335 static GList *jabber_chat_info(struct gaim_connection *gc) |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1336 { |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1337 gjconn j = ((struct jabber_data *)gc->proto_data)->jc; |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1338 |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1339 GList *m = NULL; |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1340 struct proto_chat_entry *pce; |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1341 |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1342 pce = g_new0(struct proto_chat_entry, 1); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1343 pce->label = _("Room:"); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1344 m = g_list_append(m, pce); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1345 |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1346 pce = g_new0(struct proto_chat_entry, 1); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1347 pce->label = _("Server:"); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1348 pce->def = DEFAULT_GROUPCHAT; |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1349 m = g_list_append(m, pce); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1350 |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1351 pce = g_new0(struct proto_chat_entry, 1); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1352 pce->label = _("Handle:"); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1353 pce->def = j->user->user; |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1354 m = g_list_append(m, pce); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1355 |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1356 return m; |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1357 } |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1358 |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1359 static void jabber_join_chat(struct gaim_connection *gc, GList *data) |
2086 | 1360 { |
1361 xmlnode x; | |
1362 char *realwho; | |
1363 gjconn j = ((struct jabber_data *)gc->proto_data)->jc; | |
1364 GSList *pc = ((struct jabber_data *)gc->proto_data)->pending_chats; | |
1365 struct jabber_chat *jc; | |
1366 | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1367 if (!data || !data->next || !data->next->next) |
2086 | 1368 return; |
1369 | |
1370 jc = g_new0(struct jabber_chat, 1); | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1371 realwho = create_valid_jid(data->data, data->next->data, |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1372 data->next->next->data); |
2086 | 1373 jc->Jid = jid_new(j->p, realwho); |
1374 jc->gc = gc; | |
1375 debug_printf("%s\n", realwho); | |
1376 | |
1377 x = jutil_presnew(0, realwho, NULL); | |
1378 gjab_send(j, x); | |
1379 xmlnode_free(x); | |
1380 g_free(realwho); | |
1381 | |
1382 ((struct jabber_data *)gc->proto_data)->pending_chats = g_slist_append(pc, jc); | |
1383 } | |
1384 | |
1385 static void jabber_chat_invite(struct gaim_connection *gc, int id, char *message, char *name) | |
1386 { | |
1387 xmlnode x, y; | |
1388 GSList *bcs = gc->buddy_chats; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1389 struct conversation *b = NULL; |
2086 | 1390 struct jabber_data *jd = gc->proto_data; |
1391 gjconn j = jd->jc; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1392 struct jabber_chat *jc = NULL; |
2086 | 1393 char *realwho, *subject; |
1394 | |
1395 if (!name) | |
1396 return; | |
1397 | |
1398 /* find which chat we're inviting to */ | |
1399 while (bcs) { | |
1400 b = bcs->data; | |
1401 if (id == b->id) | |
1402 break; | |
1403 bcs = bcs->next; | |
1404 } | |
1405 if (!bcs) | |
1406 return; | |
1407 | |
1408 bcs = jd->existing_chats; | |
1409 while (bcs) { | |
1410 jc = bcs->data; | |
1411 if (jc->b == b) | |
1412 break; | |
1413 bcs = bcs->next; | |
1414 } | |
1415 if (!bcs) | |
1416 return; | |
1417 | |
1418 x = xmlnode_new_tag("message"); | |
1419 if (!strchr(name, '@')) | |
1420 realwho = g_strdup_printf("%s@%s", name, j->user->server); | |
1421 else | |
1422 realwho = g_strdup(name); | |
1423 xmlnode_put_attrib(x, "to", realwho); | |
1424 g_free(realwho); | |
1425 | |
1426 y = xmlnode_insert_tag(x, "x"); | |
1427 xmlnode_put_attrib(y, "xmlns", "jabber:x:conference"); | |
1428 subject = g_strdup_printf("%s@%s", jc->Jid->user, jc->Jid->server); | |
1429 xmlnode_put_attrib(y, "jid", subject); | |
1430 g_free(subject); | |
1431 | |
1432 if (message && strlen(message)) { | |
1433 char *utf8 = str_to_utf8(message); | |
1434 y = xmlnode_insert_tag(x, "body"); | |
1435 xmlnode_insert_cdata(y, utf8, -1); | |
1436 g_free(utf8); | |
1437 } | |
1438 | |
1439 gjab_send(((struct jabber_data *)gc->proto_data)->jc, x); | |
1440 xmlnode_free(x); | |
1441 } | |
1442 | |
1443 static void jabber_chat_leave(struct gaim_connection *gc, int id) | |
1444 { | |
1445 GSList *bcs = gc->buddy_chats; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1446 struct conversation *b = NULL; |
2086 | 1447 struct jabber_data *jd = gc->proto_data; |
1448 gjconn j = jd->jc; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1449 struct jabber_chat *jc = NULL; |
2086 | 1450 char *realwho; |
1451 xmlnode x; | |
1452 | |
1453 while (bcs) { | |
1454 b = bcs->data; | |
1455 if (id == b->id) | |
1456 break; | |
1457 bcs = bcs->next; | |
1458 } | |
1459 if (!bcs) | |
1460 return; | |
1461 | |
1462 bcs = jd->existing_chats; | |
1463 while (bcs) { | |
1464 jc = bcs->data; | |
1465 if (jc->b == b) | |
1466 break; | |
1467 bcs = bcs->next; | |
1468 } | |
1469 if (!bcs) | |
1470 return; | |
1471 | |
1472 realwho = g_strdup_printf("%s@%s", jc->Jid->user, jc->Jid->server); | |
1473 x = jutil_presnew(0, realwho, NULL); | |
1474 g_free(realwho); | |
1475 xmlnode_put_attrib(x, "type", "unavailable"); | |
1476 gjab_send(j, x); | |
1477 xmlnode_free(x); | |
1478 jc->b = NULL; | |
1479 } | |
1480 | |
2167
edf8c5a70e5b
[gaim-migrate @ 2177]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2162
diff
changeset
|
1481 static int jabber_chat_send(struct gaim_connection *gc, int id, char *message) |
2086 | 1482 { |
1483 GSList *bcs = gc->buddy_chats; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1484 struct conversation *b = NULL; |
2086 | 1485 struct jabber_data *jd = gc->proto_data; |
1486 xmlnode x, y; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1487 struct jabber_chat *jc = NULL; |
2086 | 1488 char *chatname; |
1489 | |
1490 while (bcs) { | |
1491 b = bcs->data; | |
1492 if (id == b->id) | |
1493 break; | |
1494 bcs = bcs->next; | |
1495 } | |
1496 if (!bcs) | |
2167
edf8c5a70e5b
[gaim-migrate @ 2177]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2162
diff
changeset
|
1497 return -EINVAL; |
2086 | 1498 |
1499 bcs = jd->existing_chats; | |
1500 while (bcs) { | |
1501 jc = bcs->data; | |
1502 if (jc->b == b) | |
1503 break; | |
1504 bcs = bcs->next; | |
1505 } | |
1506 if (!bcs) | |
2167
edf8c5a70e5b
[gaim-migrate @ 2177]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2162
diff
changeset
|
1507 return -EINVAL; |
2086 | 1508 |
1509 x = xmlnode_new_tag("message"); | |
1510 xmlnode_put_attrib(x, "from", jid_full(jc->Jid)); | |
1511 chatname = g_strdup_printf("%s@%s", jc->Jid->user, jc->Jid->server); | |
1512 xmlnode_put_attrib(x, "to", chatname); | |
1513 g_free(chatname); | |
1514 xmlnode_put_attrib(x, "type", "groupchat"); | |
1515 | |
1516 if (message && strlen(message)) { | |
1517 char *utf8 = str_to_utf8(message); | |
1518 y = xmlnode_insert_tag(x, "body"); | |
1519 xmlnode_insert_cdata(y, utf8, -1); | |
1520 g_free(utf8); | |
1521 } | |
1522 | |
1523 gjab_send(((struct jabber_data *)gc->proto_data)->jc, x); | |
1524 xmlnode_free(x); | |
2167
edf8c5a70e5b
[gaim-migrate @ 2177]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2162
diff
changeset
|
1525 return 0; |
2086 | 1526 } |
1527 | |
1528 static void jabber_chat_set_topic(struct gaim_connection *gc, int id, char *topic) | |
1529 { | |
1530 GSList *bcs = gc->buddy_chats; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1531 struct conversation *b = NULL; |
2086 | 1532 struct jabber_data *jd = gc->proto_data; |
1533 xmlnode x, y; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1534 struct jabber_chat *jc = NULL; |
2086 | 1535 char *chatname; |
1536 char buf[8192]; | |
1537 | |
1538 while (bcs) { | |
1539 b = bcs->data; | |
1540 if (id == b->id) | |
1541 break; | |
1542 bcs = bcs->next; | |
1543 } | |
1544 if (!bcs) | |
1545 return; | |
1546 | |
1547 bcs = jd->existing_chats; | |
1548 while (bcs) { | |
1549 jc = bcs->data; | |
1550 if (jc->b == b) | |
1551 break; | |
1552 bcs = bcs->next; | |
1553 } | |
1554 if (!bcs) | |
1555 return; | |
1556 | |
1557 x = xmlnode_new_tag("message"); | |
1558 xmlnode_put_attrib(x, "from", jid_full(jc->Jid)); | |
1559 chatname = g_strdup_printf("%s@%s", jc->Jid->user, jc->Jid->server); | |
1560 xmlnode_put_attrib(x, "to", chatname); | |
1561 g_free(chatname); | |
1562 xmlnode_put_attrib(x, "type", "groupchat"); | |
1563 | |
1564 if (topic && strlen(topic)) { | |
1565 char *utf8 = str_to_utf8(topic); | |
1566 y = xmlnode_insert_tag(x, "subject"); | |
1567 xmlnode_insert_cdata(y, utf8, -1); | |
1568 y = xmlnode_insert_tag(x, "body"); | |
1569 g_snprintf(buf, sizeof(buf), "/me has changed the subject to: %s", utf8); | |
1570 xmlnode_insert_cdata(y, buf, -1); | |
1571 g_free(utf8); | |
1572 } | |
1573 | |
1574 gjab_send(((struct jabber_data *)gc->proto_data)->jc, x); | |
1575 xmlnode_free(x); | |
1576 } | |
1577 | |
1578 static void jabber_chat_whisper(struct gaim_connection *gc, int id, char *who, char *message) | |
1579 { | |
1580 GSList *bcs = gc->buddy_chats; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1581 struct conversation *b = NULL; |
2086 | 1582 struct jabber_data *jd = gc->proto_data; |
1583 xmlnode x, y; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1584 struct jabber_chat *jc = NULL; |
2086 | 1585 char *chatname; |
1586 | |
1587 while (bcs) { | |
1588 b = bcs->data; | |
1589 if (id == b->id) | |
1590 break; | |
1591 bcs = bcs->next; | |
1592 } | |
1593 if (!bcs) | |
1594 return; | |
1595 | |
1596 bcs = jd->existing_chats; | |
1597 while (bcs) { | |
1598 jc = bcs->data; | |
1599 if (jc->b == b) | |
1600 break; | |
1601 bcs = bcs->next; | |
1602 } | |
1603 if (!bcs) | |
1604 return; | |
1605 | |
1606 x = xmlnode_new_tag("message"); | |
1607 xmlnode_put_attrib(x, "from", jid_full(jc->Jid)); | |
1608 chatname = g_strdup_printf("%s@%s/%s", jc->Jid->user, jc->Jid->server, who); | |
1609 xmlnode_put_attrib(x, "to", chatname); | |
1610 g_free(chatname); | |
1611 xmlnode_put_attrib(x, "type", "normal"); | |
1612 | |
1613 if (message && strlen(message)) { | |
1614 char *utf8 = str_to_utf8(message); | |
1615 y = xmlnode_insert_tag(x, "body"); | |
1616 xmlnode_insert_cdata(y, utf8, -1); | |
1617 g_free(utf8); | |
1618 } | |
1619 | |
1620 gjab_send(((struct jabber_data *)gc->proto_data)->jc, x); | |
1621 xmlnode_free(x); | |
1622 } | |
1623 | |
1624 static char *jabber_normalize(const char *s) | |
1625 { | |
1626 static char buf[BUF_LEN]; | |
1627 char *t, *u; | |
1628 int x = 0; | |
1629 | |
1630 g_return_val_if_fail((s != NULL), NULL); | |
1631 | |
1632 u = t = g_strdup(s); | |
1633 | |
1634 g_strdown(t); | |
1635 | |
1636 while (*t && (x < BUF_LEN - 1)) { | |
1637 if (*t != ' ') | |
1638 buf[x++] = *t; | |
1639 t++; | |
1640 } | |
1641 buf[x] = '\0'; | |
1642 g_free(u); | |
1643 | |
1644 if (!strchr(buf, '@')) { | |
1645 strcat(buf, "@jabber.org"); /* this isn't always right, but eh */ | |
1646 } else if ((u = strchr(strchr(buf, '@'), '/')) != NULL) { | |
1647 *u = '\0'; | |
1648 } | |
1649 | |
1650 return buf; | |
1651 } | |
1652 | |
1653 static void jabber_get_info(struct gaim_connection *gc, char *who) { | |
1654 xmlnode x; | |
1655 char *id; | |
1656 struct jabber_data *jd = gc->proto_data; | |
1657 gjconn j = jd->jc; | |
1658 | |
1659 x = jutil_iqnew(JPACKET__GET, NS_VCARD); | |
1660 xmlnode_put_attrib(x, "to", who); | |
1661 id = gjab_getid(j); | |
1662 xmlnode_put_attrib(x, "id", id); | |
1663 | |
1664 gjab_send(j, x); | |
1665 | |
1666 xmlnode_free(x); | |
1667 | |
1668 } | |
1669 | |
2170
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
1670 static GList *jabber_buddy_menu(struct gaim_connection *gc, char *who) { |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
1671 GList *m = NULL; |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
1672 struct proto_buddy_menu *pbm; |
2086 | 1673 |
2170
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
1674 pbm = g_new0(struct proto_buddy_menu, 1); |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
1675 pbm->label = _("Get Info"); |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
1676 pbm->callback = jabber_get_info; |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
1677 pbm->gc = gc; |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
1678 m = g_list_append(m, pbm); |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
1679 |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
1680 return m; |
2086 | 1681 } |
1682 | |
1683 static GList *jabber_away_states() { | |
1684 GList *m = NULL; | |
1685 | |
1686 m = g_list_append(m, "Online"); | |
1687 m = g_list_append(m, "Chatty"); | |
1688 m = g_list_append(m, "Away"); | |
1689 m = g_list_append(m, "Extended Away"); | |
1690 m = g_list_append(m, "Do Not Disturb"); | |
1691 | |
1692 return m; | |
1693 } | |
1694 | |
1695 static void jabber_set_away(struct gaim_connection *gc, char *state, char *message) | |
1696 { | |
1697 xmlnode x, y; | |
1698 struct jabber_data *jd = gc->proto_data; | |
1699 gjconn j = jd->jc; | |
1700 | |
1701 gc->away = NULL; /* never send an auto-response */ | |
1702 | |
1703 x = xmlnode_new_tag("presence"); | |
1704 | |
1705 if (!strcmp(state, GAIM_AWAY_CUSTOM)) { | |
1706 /* oh goody. Gaim is telling us what to do. */ | |
1707 if (message) { | |
1708 /* Gaim wants us to be away */ | |
1709 y = xmlnode_insert_tag(x, "show"); | |
1710 xmlnode_insert_cdata(y, "away", -1); | |
1711 y = xmlnode_insert_tag(x, "status"); | |
1712 xmlnode_insert_cdata(y, message, -1); | |
1713 gc->away = ""; | |
1714 } else { | |
1715 /* Gaim wants us to not be away */ | |
1716 /* but for Jabber, we can just send presence with no other information. */ | |
1717 } | |
1718 } else { | |
1719 /* state is one of our own strings. it won't be NULL. */ | |
1720 if (!strcmp(state, "Online")) { | |
1721 /* once again, we don't have to put anything here */ | |
1722 } else if (!strcmp(state, "Chatty")) { | |
1723 y = xmlnode_insert_tag(x, "show"); | |
1724 xmlnode_insert_cdata(y, "chat", -1); | |
1725 } else if (!strcmp(state, "Away")) { | |
1726 y = xmlnode_insert_tag(x, "show"); | |
1727 xmlnode_insert_cdata(y, "away", -1); | |
1728 gc->away = ""; | |
1729 } else if (!strcmp(state, "Extended Away")) { | |
1730 y = xmlnode_insert_tag(x, "show"); | |
1731 xmlnode_insert_cdata(y, "xa", -1); | |
1732 gc->away = ""; | |
1733 } else if (!strcmp(state, "Do Not Disturb")) { | |
1734 y = xmlnode_insert_tag(x, "show"); | |
1735 xmlnode_insert_cdata(y, "dnd", -1); | |
1736 gc->away = ""; | |
1737 } | |
1738 } | |
1739 | |
1740 gjab_send(j, x); | |
1741 xmlnode_free(x); | |
1742 } | |
1743 | |
1744 static void jabber_set_idle(struct gaim_connection *gc, int idle) { | |
1745 struct jabber_data *jd = (struct jabber_data *)gc->proto_data; | |
1746 debug_printf("jabber_set_idle: setting idle %i\n", idle); | |
1747 jd->idle = idle ? time(NULL) - idle : idle; | |
1748 } | |
1749 | |
1750 static void jabber_keepalive(struct gaim_connection *gc) { | |
1751 struct jabber_data *jd = (struct jabber_data *)gc->proto_data; | |
1752 gjab_send_raw(jd->jc, " \t "); | |
1753 } | |
1754 | |
2154
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
1755 static GList *jabber_user_opts() |
2086 | 1756 { |
2154
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
1757 GList *m = NULL; |
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
1758 struct proto_user_opt *puo; |
2086 | 1759 |
2154
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
1760 puo = g_new0(struct proto_user_opt, 1); |
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
1761 puo->label = "Port:"; |
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
1762 puo->def = "5222"; |
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
1763 puo->pos = USEROPT_PORT; |
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
1764 m = g_list_append(m, puo); |
2086 | 1765 |
2154
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
1766 return m; |
2086 | 1767 } |
1768 | |
1769 static struct prpl *my_protocol = NULL; | |
1770 | |
1771 void jabber_init(struct prpl *ret) | |
1772 { | |
1773 /* the NULL's aren't required but they're nice to have */ | |
1774 ret->protocol = PROTO_JABBER; | |
1775 ret->options = OPT_PROTO_UNIQUE_CHATNAME | OPT_PROTO_CHAT_TOPIC; | |
1776 ret->name = jabber_name; | |
1777 ret->list_icon = jabber_list_icon; | |
1778 ret->away_states = jabber_away_states; | |
1779 ret->buddy_menu = jabber_buddy_menu; | |
1780 ret->user_opts = jabber_user_opts; | |
1781 ret->login = jabber_login; | |
1782 ret->close = jabber_close; | |
1783 ret->send_im = jabber_send_im; | |
1784 ret->set_info = NULL; | |
1785 ret->get_info = jabber_get_info; | |
1786 ret->set_away = jabber_set_away; | |
1787 ret->get_away_msg = NULL; | |
1788 ret->set_dir = NULL; | |
1789 ret->get_dir = NULL; | |
1790 ret->dir_search = NULL; | |
1791 ret->set_idle = jabber_set_idle; | |
1792 ret->change_passwd = NULL; | |
1793 ret->add_buddy = jabber_add_buddy; | |
1794 ret->add_buddies = NULL; | |
1795 ret->remove_buddy = jabber_remove_buddy; | |
1796 ret->add_permit = NULL; | |
1797 ret->add_deny = NULL; | |
1798 ret->rem_permit = NULL; | |
1799 ret->rem_deny = NULL; | |
1800 ret->set_permit_deny = NULL; | |
1801 ret->warn = NULL; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1802 ret->chat_info = jabber_chat_info; |
2086 | 1803 ret->join_chat = jabber_join_chat; |
1804 ret->chat_invite = jabber_chat_invite; | |
1805 ret->chat_leave = jabber_chat_leave; | |
1806 ret->chat_whisper = jabber_chat_whisper; | |
1807 ret->chat_set_topic = jabber_chat_set_topic; | |
1808 ret->chat_send = jabber_chat_send; | |
1809 ret->keepalive = jabber_keepalive; | |
1810 ret->normalize = jabber_normalize; | |
1811 | |
1812 my_protocol = ret; | |
1813 } | |
1814 | |
1815 #ifndef STATIC | |
1816 | |
1817 char *gaim_plugin_init(GModule *handle) | |
1818 { | |
1819 load_protocol(jabber_init, sizeof(struct prpl)); | |
1820 return NULL; | |
1821 } | |
1822 | |
1823 void gaim_plugin_remove() | |
1824 { | |
1825 struct prpl *p = find_prpl(PROTO_JABBER); | |
1826 if (p == my_protocol) | |
1827 unload_protocol(p); | |
1828 } | |
1829 | |
1830 char *name() | |
1831 { | |
1832 return "Jabber"; | |
1833 } | |
1834 | |
1835 char *description() | |
1836 { | |
2162
a464da684307
[gaim-migrate @ 2172]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2154
diff
changeset
|
1837 return PRPL_DESC("Jabber"); |
2086 | 1838 } |
1839 | |
1840 #endif |