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