source: protocols/jabber/jabber.c @ 626b446

Last change on this file since 626b446 was 626b446, checked in by Wilmer van der Gaast <wilmer@…>, at 2005-12-02T11:30:03Z

The Jabber module now only accepts a limited range of ports (5222 and 5223),
so it can't be abused as a portscanner. Thanks to Peter van Dijk (Habbie)
for the report.

  • Property mode set to 100644
File size: 60.0 KB
Line 
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#ifndef _WIN32
25#include <sys/utsname.h>
26#endif
27#include <errno.h>
28#include <string.h>
29#include <stdlib.h>
30#include <stdio.h>
31#include <time.h>
32#include <sys/stat.h>
33#include "jabber.h"
34#include "nogaim.h"
35#include "bitlbee.h"
36#include "proxy.h"
37#include "ssl_client.h"
38
39/* The priv member of gjconn's is a gaim_connection for now. */
40#define GJ_GC(x) ((struct gaim_connection *)(x)->priv)
41
42#define IQID_AUTH "__AUTH__"
43
44#define IQ_NONE -1
45#define IQ_AUTH 0
46#define IQ_ROSTER 1
47
48#define UC_AWAY (0x02 | UC_UNAVAILABLE)
49#define UC_CHAT  0x04
50#define UC_XA   (0x08 | UC_UNAVAILABLE)
51#define UC_DND  (0x10 | UC_UNAVAILABLE)
52
53#define DEFAULT_SERVER "jabber.org"
54#define DEFAULT_GROUPCHAT "conference.jabber.org"
55#define DEFAULT_PORT 5222
56#define DEFAULT_PORT_SSL 5223
57
58#define JABBER_GROUP "Friends"
59
60/* i18n disabled - Bitlbee */
61#define N_(String) String
62
63/*
64 * Note: "was_connected" may seem redundant, but it was needed and I
65 * didn't want to touch the Jabber state stuff not specific to Gaim.
66 */
67typedef struct gjconn_struct {
68        /* Core structure */
69        pool p;                 /* Memory allocation pool */
70        int state;              /* Connection state flag */
71        int was_connected;      /* We were once connected */
72        int fd;                 /* Connection file descriptor */
73        void *ssl;              /* SSL connection */
74        jid user;               /* User info */
75        char *pass;             /* User passwd */
76
77        /* Stream stuff */
78        int id;                 /* id counter for jab_getid() function */
79        char idbuf[9];          /* temporary storage for jab_getid() */
80        char *sid;              /* stream id from server, for digest auth */
81        XML_Parser parser;      /* Parser instance */
82        xmlnode current;        /* Current node in parsing instance.. */
83
84        /* Event callback ptrs */
85        void (*on_state)(struct gjconn_struct *gjc, int state);
86        void (*on_packet)(struct gjconn_struct *gjc, jpacket p);
87
88        GHashTable *queries;    /* query tracker */
89
90        void *priv;
91} *gjconn, gjconn_struct;
92
93typedef void (*gjconn_state_h)(gjconn gjc, int state);
94typedef void (*gjconn_packet_h)(gjconn gjc, jpacket p);
95
96static gjconn gjab_new(char *user, char *pass, void *priv);
97static void gjab_delete(gjconn gjc);
98static void gjab_state_handler(gjconn gjc, gjconn_state_h h);
99static void gjab_packet_handler(gjconn gjc, gjconn_packet_h h);
100static void gjab_start(gjconn gjc);
101static void gjab_stop(gjconn gjc);
102/*
103static int gjab_getfd(gjconn gjc);
104static jid gjab_getjid(gjconn gjc);
105static char *gjab_getsid(gjconn gjc);
106*/
107static char *gjab_getid(gjconn gjc);
108static void gjab_send(gjconn gjc, xmlnode x);
109static void gjab_send_raw(gjconn gjc, const char *str);
110static void gjab_recv(gjconn gjc);
111static void gjab_auth(gjconn gjc);
112
113/*
114 * It is *this* to which we point the gaim_connection proto_data
115 */
116struct jabber_data {
117        gjconn gjc;
118        gboolean did_import;
119        GSList *chats;
120        GHashTable *hash;
121        time_t idle;
122        gboolean die;
123};
124
125/*
126 * Jabber "chat group" info.  Pointers to these go in jabber_data
127 * pending and existing chats lists.
128 */
129struct jabber_chat {
130        jid Jid;
131        struct gaim_connection *gc;
132        struct conversation *b;
133        int id;
134        int state;
135};
136
137/*
138 * Jabber chat states...
139 *
140 * Note: due to a bug in one version of the Jabber server, subscriptions
141 * to chat groups aren't (always?) properly removed at the server.  The
142 * result is clients receive Jabber "presence" notifications for JIDs
143 * they no longer care about.  The problem with such vestigial notifies is
144 * that we really have no way of telling if it's vestigial or if it's a
145 * valid "buddy" presence notification.  So we keep jabber_chat structs
146 * around after leaving a chat group and simply mark them "closed."  That
147 * way we can test for such errant presence notifications.  I.e.: if we
148 * get a presence notfication from a JID that matches a chat group JID,
149 * we disregard it.
150 */
151#define JCS_PENDING 1   /* pending */
152#define JCS_ACTIVE  2   /* active */
153#define JCS_CLOSED  3   /* closed */
154
155
156static char *jabber_name()
157{
158        return "Jabber";
159}
160
161#define STATE_EVT(arg) if(gjc->on_state) { (gjc->on_state)(gjc, (arg) ); }
162
163static void jabber_remove_buddy(struct gaim_connection *gc, char *name, char *group);
164static void jabber_handlevcard(gjconn gjc, xmlnode querynode, char *from);
165
166static char *create_valid_jid(const char *given, char *server, char *resource)
167{
168        char *valid;
169
170        if (!strchr(given, '@'))
171                valid = g_strdup_printf("%s@%s/%s", given, server, resource);
172        else if (!strchr(strchr(given, '@'), '/'))
173                valid = g_strdup_printf("%s/%s", given, resource);
174        else
175                valid = g_strdup(given);
176
177        return valid;
178}
179
180static gjconn gjab_new(char *user, char *pass, void *priv)
181{
182        pool p;
183        gjconn gjc;
184
185        if (!user)
186                return (NULL);
187
188        p = pool_new();
189        if (!p)
190                return (NULL);
191        gjc = pmalloc_x(p, sizeof(gjconn_struct), 0);
192        if (!gjc) {
193                pool_free(p);   /* no need for this anymore! */
194                return (NULL);
195        }
196        gjc->p = p;
197
198        if((gjc->user = jid_new(p, user)) == NULL) {
199                pool_free(p);   /* no need for this anymore! */
200                return (NULL);
201        }
202        gjc->pass = pstrdup(p, pass);
203
204        gjc->state = JCONN_STATE_OFF;
205        gjc->was_connected = 0;
206        gjc->id = 1;
207        gjc->fd = -1;
208
209        gjc->priv = priv;
210
211        return gjc;
212}
213
214static void gjab_delete(gjconn gjc)
215{
216        if (!gjc)
217                return;
218
219        gjab_stop(gjc);
220        pool_free(gjc->p);
221}
222
223static void gjab_state_handler(gjconn gjc, gjconn_state_h h)
224{
225        if (!gjc)
226                return;
227
228        gjc->on_state = h;
229}
230
231static void gjab_packet_handler(gjconn gjc, gjconn_packet_h h)
232{
233        if (!gjc)
234                return;
235
236        gjc->on_packet = h;
237}
238
239static void gjab_stop(gjconn gjc)
240{
241        if (!gjc || gjc->state == JCONN_STATE_OFF)
242                return;
243
244        gjab_send_raw(gjc, "</stream:stream>");
245        gjc->state = JCONN_STATE_OFF;
246        gjc->was_connected = 0;
247        if (gjc->ssl) {
248                ssl_disconnect(gjc->ssl);
249                gjc->ssl = NULL;
250        } else {
251                closesocket(gjc->fd);
252        }
253        gjc->fd = -1;
254        XML_ParserFree(gjc->parser);
255        gjc->parser = NULL;
256}
257
258/*
259static int gjab_getfd(gjconn gjc)
260{
261        if (gjc)
262                return gjc->fd;
263        else
264                return -1;
265}
266
267static jid gjab_getjid(gjconn gjc)
268{
269        if (gjc)
270                return (gjc->user);
271        else
272                return NULL;
273}
274
275static char *gjab_getsid(gjconn gjc)
276{
277        if (gjc)
278                return (gjc->sid);
279        else
280                return NULL;
281}
282*/
283
284static char *gjab_getid(gjconn gjc)
285{
286        g_snprintf(gjc->idbuf, 8, "%d", gjc->id++);
287        return &gjc->idbuf[0];
288}
289
290static void gjab_send(gjconn gjc, xmlnode x)
291{
292        if (gjc && gjc->state != JCONN_STATE_OFF) {
293                char *buf = xmlnode2str(x);
294                if (!buf)
295                        return;
296                else if (gjc->ssl)
297                        ssl_write(gjc->ssl, buf, strlen(buf));
298                else
299                        write(gjc->fd, buf, strlen(buf));
300        }
301}
302
303static void gjab_send_raw(gjconn gjc, const char *str)
304{
305        if (gjc && gjc->state != JCONN_STATE_OFF) {
306                int len;
307               
308                /*
309                 * JFIXME: No error detection?!?!
310                 */
311                if (gjc->ssl)
312                        len = ssl_write(gjc->ssl, str, strlen(str));
313                else
314                        len = write(gjc->fd, str, strlen(str));
315                       
316                if(len < 0) {
317                        /* Do NOT write to stdout/stderr directly, IRC clients
318                           might get confused, and we don't want that...
319                        fprintf(stderr, "DBG: Problem sending.  Error: %d\n", errno);
320                        fflush(stderr); */
321                }
322        }
323}
324
325static void gjab_reqroster(gjconn gjc)
326{
327        xmlnode x;
328
329        x = jutil_iqnew(JPACKET__GET, NS_ROSTER);
330        xmlnode_put_attrib(x, "id", gjab_getid(gjc));
331
332        gjab_send(gjc, x);
333        xmlnode_free(x);
334}
335
336static void gjab_reqauth(gjconn gjc)
337{
338        xmlnode x, y, z;
339        char *user;
340
341        if (!gjc)
342                return;
343
344        x = jutil_iqnew(JPACKET__GET, NS_AUTH);
345        xmlnode_put_attrib(x, "id", IQID_AUTH);
346        y = xmlnode_get_tag(x, "query");
347
348        user = gjc->user->user;
349
350        if (user) {
351                z = xmlnode_insert_tag(y, "username");
352                xmlnode_insert_cdata(z, user, -1);
353        }
354
355        gjab_send(gjc, x);
356        xmlnode_free(x);
357}
358
359static void gjab_auth(gjconn gjc)
360{
361        xmlnode x, y, z;
362        char *hash, *user;
363
364        if (!gjc)
365                return;
366
367        x = jutil_iqnew(JPACKET__SET, NS_AUTH);
368        xmlnode_put_attrib(x, "id", IQID_AUTH);
369        y = xmlnode_get_tag(x, "query");
370
371        user = gjc->user->user;
372
373        if (user) {
374                z = xmlnode_insert_tag(y, "username");
375                xmlnode_insert_cdata(z, user, -1);
376        }
377
378        z = xmlnode_insert_tag(y, "resource");
379        xmlnode_insert_cdata(z, gjc->user->resource, -1);
380
381        if (gjc->sid) {
382                z = xmlnode_insert_tag(y, "digest");
383                hash = pmalloc(x->p, strlen(gjc->sid) + strlen(gjc->pass) + 1);
384                strcpy(hash, gjc->sid);
385                strcat(hash, gjc->pass);
386                hash = shahash(hash);
387                xmlnode_insert_cdata(z, hash, 40);
388        } else {
389                z = xmlnode_insert_tag(y, "password");
390                xmlnode_insert_cdata(z, gjc->pass, -1);
391        }
392
393        gjab_send(gjc, x);
394        xmlnode_free(x);
395
396        return;
397}
398
399static void gjab_recv(gjconn gjc)
400{
401        static char buf[4096];
402        int len;
403
404        if (!gjc || gjc->state == JCONN_STATE_OFF)
405                return;
406       
407        if (gjc->ssl)
408                len = ssl_read(gjc->ssl, buf, sizeof(buf) - 1);
409        else
410                len = read(gjc->fd, buf, sizeof(buf) - 1);
411       
412        if (len > 0) {
413                struct jabber_data *jd = GJ_GC(gjc)->proto_data;
414                buf[len] = '\0';
415                XML_Parse(gjc->parser, buf, len, 0);
416                if (jd->die)
417                        signoff(GJ_GC(gjc));
418        } else if (len < 0 || errno != EAGAIN) {
419                STATE_EVT(JCONN_STATE_OFF)
420        }
421}
422
423static void startElement(void *userdata, const char *name, const char **attribs)
424{
425        xmlnode x;
426        gjconn gjc = (gjconn) userdata;
427
428        if (gjc->current) {
429                /* Append the node to the current one */
430                x = xmlnode_insert_tag(gjc->current, name);
431                xmlnode_put_expat_attribs(x, attribs);
432
433                gjc->current = x;
434        } else {
435                x = xmlnode_new_tag(name);
436                xmlnode_put_expat_attribs(x, attribs);
437                if (strcmp(name, "stream:stream") == 0) {
438                        /* special case: name == stream:stream */
439                        /* id attrib of stream is stored for digest auth */
440                        gjc->sid = g_strdup(xmlnode_get_attrib(x, "id"));
441                        /* STATE_EVT(JCONN_STATE_AUTH) */
442                        xmlnode_free(x);
443                } else {
444                        gjc->current = x;
445                }
446        }
447}
448
449static void endElement(void *userdata, const char *name)
450{
451        gjconn gjc = (gjconn) userdata;
452        xmlnode x;
453        jpacket p;
454
455        if (gjc->current == NULL) {
456                /* we got </stream:stream> */
457                STATE_EVT(JCONN_STATE_OFF)
458                    return;
459        }
460
461        x = xmlnode_get_parent(gjc->current);
462
463        if (!x) {
464                /* it is time to fire the event */
465                p = jpacket_new(gjc->current);
466
467                if (gjc->on_packet)
468                        (gjc->on_packet) (gjc, p);
469                else
470                        xmlnode_free(gjc->current);
471        }
472
473        gjc->current = x;
474}
475
476static void jabber_callback(gpointer data, gint source, GaimInputCondition condition)
477{
478        struct gaim_connection *gc = (struct gaim_connection *)data;
479        struct jabber_data *jd = (struct jabber_data *)gc->proto_data;
480
481        gjab_recv(jd->gjc);
482}
483
484static void charData(void *userdata, const char *s, int slen)
485{
486        gjconn gjc = (gjconn) userdata;
487
488        if (gjc->current)
489                xmlnode_insert_cdata(gjc->current, s, slen);
490}
491
492static void gjab_connected(gpointer data, gint source, GaimInputCondition cond)
493{
494        xmlnode x;
495        char *t, *t2;
496        struct gaim_connection *gc = data;
497        struct jabber_data *jd;
498        gjconn gjc;
499
500        if (!g_slist_find(get_connections(), gc)) {
501                closesocket(source);
502                return;
503        }
504
505        jd = gc->proto_data;
506        gjc = jd->gjc;
507
508        if (gjc->fd != source)
509                gjc->fd = source;
510
511        if (source == -1) {
512                STATE_EVT(JCONN_STATE_OFF)
513                return;
514        }
515
516        gjc->state = JCONN_STATE_CONNECTED;
517        STATE_EVT(JCONN_STATE_CONNECTED)
518
519        /* start stream */
520        x = jutil_header(NS_CLIENT, gjc->user->server);
521        t = xmlnode2str(x);
522        /* this is ugly, we can create the string here instead of jutil_header */
523        /* what do you think about it? -madcat */
524        t2 = strstr(t, "/>");
525        *t2++ = '>';
526        *t2 = '\0';
527        gjab_send_raw(gjc, "<?xml version='1.0'?>");
528        gjab_send_raw(gjc, t);
529        xmlnode_free(x);
530
531        gjc->state = JCONN_STATE_ON;
532        STATE_EVT(JCONN_STATE_ON);
533
534        gc = GJ_GC(gjc);
535        gc->inpa = gaim_input_add(gjc->fd, GAIM_INPUT_READ, jabber_callback, gc);
536}
537
538static void gjab_connected_ssl(gpointer data, void *source, GaimInputCondition cond)
539{
540        struct gaim_connection *gc = data;
541        struct jabber_data *jd;
542        gjconn gjc;
543       
544        jd = gc->proto_data;
545        gjc = jd->gjc;
546       
547        if (source == NULL) {
548                STATE_EVT(JCONN_STATE_OFF)
549                return;
550        }
551       
552        if (!g_slist_find(get_connections(), gc)) {
553                ssl_disconnect(source);
554                return;
555        }
556       
557        gjab_connected(data, gjc->fd, cond);
558}
559
560static void gjab_start(gjconn gjc)
561{
562        struct aim_user *user;
563        int port = -1, ssl = 0;
564        char *server = NULL, *s;
565
566        if (!gjc || gjc->state != JCONN_STATE_OFF)
567                return;
568
569        user = GJ_GC(gjc)->user;
570        if (*user->proto_opt[0]) {
571                /* If there's a dot, assume there's a hostname in the beginning */
572                if (strchr(user->proto_opt[0], '.')) {
573                        server = g_strdup(user->proto_opt[0]);
574                        if ((s = strchr(server, ':')))
575                                *s = 0;
576                }
577               
578                /* After the hostname, there can be a port number */
579                s = strchr(user->proto_opt[0], ':');
580                if (s && isdigit(s[1]))
581                        sscanf(s + 1, "%d", &port);
582               
583                /* And if there's the string ssl, the user wants an SSL-connection */
584                if (strstr(user->proto_opt[0], ":ssl") || g_strcasecmp(user->proto_opt[0], "ssl") == 0)
585                        ssl = 1;
586        }
587       
588        if (port == -1 && !ssl)
589                port = DEFAULT_PORT;
590        else if (port == -1 && ssl)
591                port = DEFAULT_PORT_SSL;
592        else if (port != 5222 && port != 5223) {
593                serv_got_crap(GJ_GC(gjc), "Only port numbers 5222 and 5223 are allowed for Jabber connections.");
594                STATE_EVT(JCONN_STATE_OFF)
595                return;
596        }
597       
598        if (server == NULL)
599                server = g_strdup(gjc->user->server);
600
601        gjc->parser = XML_ParserCreate(NULL);
602        XML_SetUserData(gjc->parser, (void *)gjc);
603        XML_SetElementHandler(gjc->parser, startElement, endElement);
604        XML_SetCharacterDataHandler(gjc->parser, charData);
605       
606        if (ssl) {
607                if ((gjc->ssl = ssl_connect(server, port, gjab_connected_ssl, GJ_GC(gjc))))
608                        gjc->fd = ssl_getfd(gjc->ssl);
609                else
610                        gjc->fd = -1;
611        } else {
612                gjc->fd = proxy_connect(server, port, gjab_connected, GJ_GC(gjc));
613        }
614       
615        g_free(server);
616       
617        if (!user->gc || (gjc->fd < 0)) {
618                STATE_EVT(JCONN_STATE_OFF)
619                return;
620        }
621}
622
623/*
624 * Find existing/active Jabber chat
625 */
626static struct jabber_chat *find_existing_chat(struct gaim_connection *gc, jid chat)
627{
628        GSList *jcs = ((struct jabber_data *)gc->proto_data)->chats;
629        struct jabber_chat *jc = NULL;
630
631        while (jcs) {
632                jc = jcs->data;
633                if (jc->state == JCS_ACTIVE && !jid_cmpx(chat, jc->Jid, JID_USER | JID_SERVER))
634                        break;
635                jc = NULL;
636                jcs = jcs->next;
637        }
638
639        return jc;
640}
641
642/*
643 * Find pending chat
644 */
645static struct jabber_chat *find_pending_chat(struct gaim_connection *gc, jid chat)
646{
647        GSList *jcs = ((struct jabber_data *)gc->proto_data)->chats;
648        struct jabber_chat *jc = NULL;
649
650        while (jcs) {
651                jc = jcs->data;
652                if (jc->state == JCS_PENDING && !jid_cmpx(chat, jc->Jid, JID_USER | JID_SERVER))
653                        break;
654                jc = NULL;
655                jcs = jcs->next;
656        }
657
658        return jc;
659}
660
661static gboolean find_chat_buddy(struct conversation *b, char *name)
662{
663        GList *m = b->in_room;
664
665        while (m) {
666                if (!strcmp(m->data, name))
667                        return TRUE;
668                m = m->next;
669        }
670
671        return FALSE;
672}
673
674/*
675 * Remove a buddy from the (gaim) buddylist (if he's on it)
676 */
677static void jabber_remove_gaim_buddy(struct gaim_connection *gc, char *buddyname)
678{
679        struct buddy *b;
680
681        if ((b = find_buddy(gc, buddyname)) != NULL) {
682                /* struct group *group;
683
684                group = find_group_by_buddy(gc, buddyname);
685                remove_buddy(gc, group, b); */
686                jabber_remove_buddy(gc, b->name, JABBER_GROUP);
687        }
688}
689
690/*
691 * keep track of away msg same as yahoo plugin
692 */
693static void jabber_track_away(gjconn gjc, jpacket p, char *name, char *type)
694{
695        struct jabber_data *jd = GJ_GC(gjc)->proto_data;
696        gpointer val = g_hash_table_lookup(jd->hash, name);
697        char *show;
698        char *vshow = NULL;
699        char *status = NULL;
700        char *msg = NULL;
701
702        if (type && (g_strcasecmp(type, "unavailable") == 0)) {
703                vshow = _("Unavailable");
704        } else {
705                if((show = xmlnode_get_tag_data(p->x, "show")) != NULL) {
706                        if (!g_strcasecmp(show, "away")) {
707                                vshow = _("Away");
708                        } else if (!g_strcasecmp(show, "chat")) {
709                                vshow = _("Online");
710                        } else if (!g_strcasecmp(show, "xa")) {
711                                vshow = _("Extended Away");
712                        } else if (!g_strcasecmp(show, "dnd")) {
713                                vshow = _("Do Not Disturb");
714                        }
715                }
716        }
717
718        status = xmlnode_get_tag_data(p->x, "status");
719
720        if(vshow != NULL || status != NULL ) {
721                /* kinda hokey, but it works :-) */
722                msg = g_strdup_printf("%s%s%s",
723                        (vshow == NULL? "" : vshow),
724                        (vshow == NULL || status == NULL? "" : ": "),
725                        (status == NULL? "" : status));
726        } else {
727                msg = g_strdup(_("Online"));
728        }
729
730        if (val) {
731                g_free(val);
732                g_hash_table_insert(jd->hash, name, msg);
733        } else {
734                g_hash_table_insert(jd->hash, g_strdup(name), msg);
735        }
736}
737
738static time_t iso8601_to_time(char *timestamp)
739{
740        struct tm t;
741        time_t retval = 0;
742
743        if(sscanf(timestamp,"%04d%02d%02dT%02d:%02d:%02d",
744                &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec))
745        {
746                t.tm_year -= 1900;
747                t.tm_mon -= 1;
748                t.tm_isdst = 0;
749                retval = mktime(&t);
750#               ifdef HAVE_TM_GMTOFF
751                        retval += t.tm_gmtoff;
752#               else
753#                       ifdef HAVE_TIMEZONE
754                                tzset();        /* making sure */
755                                retval -= timezone;
756#                       endif
757#               endif
758        }
759
760        return retval;
761}
762
763static void jabber_handlemessage(gjconn gjc, jpacket p)
764{
765        xmlnode y, xmlns, z;
766        time_t time_sent = time(NULL);
767
768        char *from = NULL, *msg = NULL, *type = NULL;
769        char m[BUF_LONG * 2];
770
771        type = xmlnode_get_attrib(p->x, "type");
772
773        z = xmlnode_get_firstchild(p->x);
774
775        while(z)
776        {
777           if(NSCHECK(z,NS_DELAY))
778           {
779              char *timestamp = xmlnode_get_attrib(z,"stamp");
780              time_sent = iso8601_to_time(timestamp);
781           }
782           z = xmlnode_get_nextsibling(z);
783        }
784
785        if (!type || !g_strcasecmp(type, "normal") || !g_strcasecmp(type, "chat")) {
786
787                /* XXX namespaces could be handled better. (mid) */
788                if ((xmlns = xmlnode_get_tag(p->x, "x")))
789                        type = xmlnode_get_attrib(xmlns, "xmlns");
790
791                from = jid_full(p->from);
792                /*
793                if ((y = xmlnode_get_tag(p->x, "html"))) {
794                        msg = xmlnode_get_data(y);
795                } else
796                */
797                if ((y = xmlnode_get_tag(p->x, "body"))) {
798                        msg = xmlnode_get_data(y);
799                }
800
801
802                if (!from)
803                        return;
804
805                if (type && !g_strcasecmp(type, "jabber:x:conference")) {
806                        /* do nothing */
807                } else if (msg) { /* whisper */
808                        struct jabber_chat *jc;
809                        g_snprintf(m, sizeof(m), "%s", msg);
810                        if (((jc = find_existing_chat(GJ_GC(gjc), p->from)) != NULL) && jc->b)
811                                serv_got_chat_in(GJ_GC(gjc), jc->b->id, p->from->resource, 1, m, time_sent);
812                        else {
813                                int flags = 0;
814                               
815                                if(p->from->user) {
816                                    from = g_strdup_printf("%s@%s", p->from->user, p->from->server);
817                                } else {
818                                    /* server message? */
819                                    from = g_strdup(p->from->server);
820                                }
821                                serv_got_im(GJ_GC(gjc), from, m, flags, time_sent, -1);
822                                g_free(from);
823                        }
824                }
825
826        } else if (!g_strcasecmp(type, "error")) {
827                if ((y = xmlnode_get_tag(p->x, "error"))) {
828                        type = xmlnode_get_attrib(y, "code");
829                        msg = xmlnode_get_data(y);
830                }
831
832                if (msg) {
833                        from = g_strdup_printf("Error %s", type ? type : "");
834                        do_error_dialog(GJ_GC(gjc), msg, from);
835                        g_free(from);
836                }
837        } else if (!g_strcasecmp(type, "headline")) {
838                char *subject, *body, *url;
839               
840                y = xmlnode_get_tag( p->x, "body" );
841                body = y ? g_strdup( xmlnode_get_data( y ) ) : NULL;
842               
843                y = xmlnode_get_tag( p->x, "subject" );
844                subject = y ? g_strdup( xmlnode_get_data( y ) ) : NULL;
845               
846                url = NULL;
847                z = xmlnode_get_firstchild(p->x);
848                while( z )
849                {
850                        char *xtype = xmlnode_get_attrib( z, "xmlns" );
851                       
852                        if( xtype && g_strcasecmp( xtype, "jabber:x:oob" ) == 0 &&
853                                     ( y = xmlnode_get_tag( z, "url" ) ) )
854                        {
855                                url = g_strdup( xmlnode_get_data( y ) );
856                                break;
857                        }
858                       
859                        z = xmlnode_get_nextsibling( z );
860                }
861               
862                g_snprintf( m, BUF_LONG, "Subject: %s\nURL: %s\nMessage:\n%s", subject ? subject : "(none)",
863                                     url ? url : "(none)", body ? body : "(none)" );
864
865                if( p->from->user )
866                        from = g_strdup_printf( "%s@%s", p->from->user, p->from->server );
867                else
868                        from = g_strdup( p->from->server );
869               
870                serv_got_im( GJ_GC(gjc), from, m, 0, time_sent, -1 );
871               
872                g_free( from );
873                g_free( subject );
874                g_free( body );
875                g_free( url );
876        }
877}
878           
879static void jabber_handlepresence(gjconn gjc, jpacket p)
880{
881        char *to, *from, *type;
882        struct buddy *b = NULL;
883        jid who;
884        char *buddy;
885        xmlnode y;
886        char *show;
887        int state = 0;
888        GSList *resources;
889        char *res;
890        struct conversation *cnv = NULL;
891        struct jabber_chat *jc = NULL;
892
893        to = xmlnode_get_attrib(p->x, "to");
894        from = xmlnode_get_attrib(p->x, "from");
895        type = xmlnode_get_attrib(p->x, "type");
896       
897        if (type && g_strcasecmp(type, "error") == 0) {
898                return;
899        }
900        else if ((y = xmlnode_get_tag(p->x, "show"))) {
901                show = xmlnode_get_data(y);
902                if (!show) {
903                        state = 0;
904                } else if (!g_strcasecmp(show, "away")) {
905                        state = UC_AWAY;
906                } else if (!g_strcasecmp(show, "chat")) {
907                        state = UC_CHAT;
908                } else if (!g_strcasecmp(show, "xa")) {
909                        state = UC_XA;
910                } else if (!g_strcasecmp(show, "dnd")) {
911                        state = UC_DND;
912                }
913        } else {
914                state = 0;
915        }
916
917        who = jid_new(gjc->p, from);
918        if (who->user == NULL) {
919                /* FIXME: transport */
920                return;
921        }
922
923        buddy = g_strdup_printf("%s@%s", who->user, who->server);
924
925        /* um. we're going to check if it's a chat. if it isn't, and there are pending
926         * chats, create the chat. if there aren't pending chats and we don't have the
927         * buddy on our list, simply bail out. */
928        if ((cnv = NULL) == NULL) {
929                static int i = 0x70;
930                if ((jc = find_pending_chat(GJ_GC(gjc), who)) != NULL) {
931                        jc->b = cnv = serv_got_joined_chat(GJ_GC(gjc), i++, who->user);
932                        jc->id = jc->b->id;
933                        jc->state = JCS_ACTIVE;
934                } else if ((b = find_buddy(GJ_GC(gjc), buddy)) == NULL) {
935                        g_free(buddy);
936                        return;
937                }
938        }
939
940        if (!cnv) {
941                resources = b->proto_data;
942                res = who->resource;
943                if (res)
944                        while (resources) {
945                                if (!strcmp(res, resources->data))
946                                        break;
947                                resources = resources->next;
948                        }
949
950                /* keep track of away msg same as yahoo plugin */
951                jabber_track_away(gjc, p, normalize(b->name), type);
952
953                if (type && (g_strcasecmp(type, "unavailable") == 0)) {
954                        if (resources) {
955                                g_free(resources->data);
956                                b->proto_data = g_slist_remove(b->proto_data, resources->data);
957                        }
958                        if (!b->proto_data) {
959                                serv_got_update(GJ_GC(gjc), buddy, 0, 0, 0, 0, 0, 0);
960                        }
961                } else {
962                        if (!resources) {
963                                b->proto_data = g_slist_append(b->proto_data, g_strdup(res));
964                        }
965
966                        serv_got_update(GJ_GC(gjc), buddy, 1, 0, b->signon, b->idle, state, 0);
967
968                }
969        } else {
970                if (who->resource) {
971                        char *buf;
972
973                        buf = g_strdup_printf("%s@%s/%s", who->user, who->server, who->resource);
974                        jabber_track_away(gjc, p, buf, type);
975                        g_free(buf);
976
977                        if (type && !g_strcasecmp(type, "unavailable")) {
978                                struct jabber_data *jd;
979                                if (!jc && !(jc = find_existing_chat(GJ_GC(gjc), who))) {
980                                        g_free(buddy);
981                                        return;
982                                }
983                                jd = jc->gc->proto_data;
984                                /* if it's not ourselves...*/
985                                if (strcmp(who->resource, jc->Jid->resource) && jc->b) {
986                                        remove_chat_buddy(jc->b, who->resource, NULL);
987                                        g_free(buddy);
988                                        return;
989                                }
990
991                                jc->state = JCS_CLOSED;
992                                serv_got_chat_left(GJ_GC(gjc), jc->id);
993                                /*
994                                 * TBD: put back some day?
995                                jd->chats = g_slist_remove(jd->chats, jc);
996                                g_free(jc);
997                                 */
998                        } else {
999                                if ((!jc && !(jc = find_existing_chat(GJ_GC(gjc), who))) || !jc->b) {
1000                                        g_free(buddy);
1001                                        return;
1002                                }
1003                                if (!find_chat_buddy(jc->b, who->resource)) {
1004                                        add_chat_buddy(jc->b, who->resource);
1005                                }
1006                        }
1007                }
1008        }
1009
1010        g_free(buddy);
1011
1012        return;
1013}
1014
1015/*
1016 * Used only by Jabber accept/deny add stuff just below
1017 */
1018struct jabber_add_permit {
1019        gjconn gjc;
1020        gchar *user;
1021};
1022
1023/*
1024 * Common part for Jabber accept/deny adds
1025 *
1026 * "type" says whether we'll permit/deny the subscribe request
1027 */
1028static void jabber_accept_deny_add(struct jabber_add_permit *jap, const char *type)
1029{
1030        xmlnode g = xmlnode_new_tag("presence");
1031
1032        xmlnode_put_attrib(g, "to", jap->user);
1033        xmlnode_put_attrib(g, "type", type);
1034        gjab_send(jap->gjc, g);
1035
1036        xmlnode_free(g);
1037}
1038
1039/*
1040 * Callback from "accept" in do_ask_dialog() invoked by jabber_handles10n()
1041 */
1042static void jabber_accept_add(gpointer w, struct jabber_add_permit *jap)
1043{
1044        jabber_accept_deny_add(jap, "subscribed");
1045        /*
1046         * If we don't already have the buddy on *our* buddylist,
1047         * ask if we want him or her added.
1048         */
1049        if(find_buddy(GJ_GC(jap->gjc), jap->user) == NULL) {
1050                show_got_added(GJ_GC(jap->gjc), NULL, jap->user, NULL, NULL);
1051        }
1052        g_free(jap->user);
1053        g_free(jap);
1054}
1055
1056/*
1057 * Callback from "deny/cancel" in do_ask_dialog() invoked by jabber_handles10n()
1058 */
1059static void jabber_deny_add(gpointer w, struct jabber_add_permit *jap)
1060{
1061        jabber_accept_deny_add(jap, "unsubscribed");
1062        g_free(jap->user);
1063        g_free(jap);
1064}
1065
1066/*
1067 * Handle subscription requests
1068 */
1069static void jabber_handles10n(gjconn gjc, jpacket p)
1070{
1071        xmlnode g;
1072        char *Jid = xmlnode_get_attrib(p->x, "from");
1073        char *type = xmlnode_get_attrib(p->x, "type");
1074
1075        g = xmlnode_new_tag("presence");
1076        xmlnode_put_attrib(g, "to", Jid);
1077
1078        if (!strcmp(type, "subscribe")) {
1079                /*
1080                 * A "subscribe to us" request was received - put up the approval dialog
1081                 */
1082                struct jabber_add_permit *jap = g_new0(struct jabber_add_permit, 1);
1083                gchar *msg = g_strdup_printf(_("The user %s wants to add you to his/her buddy list."),
1084                                Jid);
1085
1086                jap->gjc = gjc;
1087                jap->user = g_strdup(Jid);
1088                do_ask_dialog(GJ_GC(gjc), msg, jap, jabber_accept_add, jabber_deny_add);
1089
1090                g_free(msg);
1091                xmlnode_free(g);        /* Never needed it here anyway */
1092                return;
1093
1094        } else if (!strcmp(type, "unsubscribe")) {
1095                /*
1096                 * An "unsubscribe to us" was received - simply "approve" it
1097                 */
1098                xmlnode_put_attrib(g, "type", "unsubscribed");
1099        } else {
1100                /*
1101                 * Did we attempt to subscribe to somebody and they do not exist?
1102                 */
1103                if (!strcmp(type, "unsubscribed")) {
1104                        xmlnode y;
1105                        char *status;
1106                        if((y = xmlnode_get_tag(p->x, "status")) && (status = xmlnode_get_data(y)) &&
1107                                        !strcmp(status, "Not Found")) {
1108                                char *msg = g_strdup_printf("%s: \"%s\"", _("No such user"), 
1109                                        xmlnode_get_attrib(p->x, "from"));
1110                                do_error_dialog(GJ_GC(gjc), msg, _("Jabber Error"));
1111                                g_free(msg);
1112                        }
1113                }
1114
1115                xmlnode_free(g);
1116                return;
1117        }
1118
1119        gjab_send(gjc, g);
1120        xmlnode_free(g);
1121}
1122
1123/*
1124 * Pending subscription to a buddy?
1125 */
1126#define BUD_SUB_TO_PEND(sub, ask) ((!g_strcasecmp((sub), "none") || !g_strcasecmp((sub), "from")) && \
1127                                        (ask) != NULL && !g_strcasecmp((ask), "subscribe"))
1128
1129/*
1130 * Subscribed to a buddy?
1131 */
1132#define BUD_SUBD_TO(sub, ask) ((!g_strcasecmp((sub), "to") || !g_strcasecmp((sub), "both")) && \
1133                                        ((ask) == NULL || !g_strcasecmp((ask), "subscribe")))
1134
1135/*
1136 * Pending unsubscription to a buddy?
1137 */
1138#define BUD_USUB_TO_PEND(sub, ask) ((!g_strcasecmp((sub), "to") || !g_strcasecmp((sub), "both")) && \
1139                                        (ask) != NULL && !g_strcasecmp((ask), "unsubscribe"))
1140
1141/*
1142 * Unsubscribed to a buddy?
1143 */
1144#define BUD_USUBD_TO(sub, ask) ((!g_strcasecmp((sub), "none") || !g_strcasecmp((sub), "from")) && \
1145                                        ((ask) == NULL || !g_strcasecmp((ask), "unsubscribe")))
1146
1147/*
1148 * If a buddy is added or removed from the roster on another resource
1149 * jabber_handlebuddy is called
1150 *
1151 * Called with roster item node.
1152 */
1153static void jabber_handlebuddy(gjconn gjc, xmlnode x)
1154{
1155        xmlnode g;
1156        char *Jid, *name, *sub, *ask;
1157        jid who;
1158        struct buddy *b = NULL;
1159        char *buddyname, *groupname = NULL;
1160
1161        Jid = xmlnode_get_attrib(x, "jid");
1162        name = xmlnode_get_attrib(x, "name");
1163        sub = xmlnode_get_attrib(x, "subscription");
1164        ask = xmlnode_get_attrib(x, "ask");
1165        who = jid_new(gjc->p, Jid);
1166
1167        /* JFIXME: jabber_handleroster() had a "FIXME: transport" at this
1168         * equivilent point.  So...
1169         *
1170         * We haven't allocated any memory or done anything interesting to
1171         * this point, so we'll violate Good Coding Structure here by
1172         * simply bailing out.
1173         */
1174        if (!who || !who->user) {
1175                return;
1176        }
1177
1178        buddyname = g_strdup_printf("%s@%s", who->user, who->server);
1179
1180        if((g = xmlnode_get_tag(x, "group")) != NULL) {
1181                groupname = xmlnode_get_data(g);
1182        }
1183
1184        /*
1185         * Add or remove a buddy?  Change buddy's alias or group?
1186         */
1187        if (BUD_SUB_TO_PEND(sub, ask) || BUD_SUBD_TO(sub, ask)) {
1188                if ((b = find_buddy(GJ_GC(gjc), buddyname)) == NULL) {
1189                        add_buddy(GJ_GC(gjc), groupname ? groupname : _("Buddies"), buddyname,
1190                                name ? name : buddyname);
1191                } else {
1192                        /* struct group *c_grp = find_group_by_buddy(GJ_GC(gjc), buddyname); */
1193
1194                        /*
1195                         * If the buddy's in a new group or his/her alias is changed...
1196                         */
1197                        if(groupname) {
1198                                int present = b->present;       /* save presence state */
1199                                int uc = b->uc;                 /* and away state (?) */
1200                                int idle = b->idle;
1201                                int signon = b->signon;
1202
1203                                /*
1204                                 * seems rude, but it seems to be the only way...
1205                                 */
1206                                /* remove_buddy(GJ_GC(gjc), c_grp, b); */
1207                                jabber_remove_buddy(GJ_GC(gjc), b->name, JABBER_GROUP);
1208                               
1209                                add_buddy(GJ_GC(gjc), groupname, buddyname,
1210                                        name ? name : buddyname);
1211                                if(present) {
1212                                        serv_got_update(GJ_GC(gjc), buddyname, 1, 0, signon, idle, uc, 0);
1213                                }
1214                        } else if(name != NULL && strcmp(b->show, name)) {
1215                                strncpy(b->show, name, BUDDY_ALIAS_MAXLEN);
1216                                b->show[BUDDY_ALIAS_MAXLEN - 1] = '\0'; /* cheap safety feature */
1217                                serv_buddy_rename(GJ_GC(gjc), buddyname, b->show);
1218                        }
1219                }
1220        }  else if (BUD_USUB_TO_PEND(sub, ask) || BUD_USUBD_TO(sub, ask) || !g_strcasecmp(sub, "remove")) {
1221                jabber_remove_gaim_buddy(GJ_GC(gjc), buddyname);
1222        }
1223        g_free(buddyname);
1224
1225}
1226
1227static void jabber_handleroster(gjconn gjc, xmlnode querynode)
1228{
1229        xmlnode x;
1230
1231        x = xmlnode_get_firstchild(querynode);
1232        while (x) {
1233                jabber_handlebuddy(gjc, x);
1234                x = xmlnode_get_nextsibling(x);
1235        }
1236
1237        x = jutil_presnew(0, NULL, "Online");
1238        gjab_send(gjc, x);
1239        xmlnode_free(x);
1240}
1241
1242static void jabber_handleauthresp(gjconn gjc, jpacket p)
1243{
1244        if (jpacket_subtype(p) == JPACKET__RESULT) {
1245                if (xmlnode_has_children(p->x)) {
1246                        xmlnode query = xmlnode_get_tag(p->x, "query");
1247                        set_login_progress(GJ_GC(gjc), 4, _("Authenticating"));
1248                        if (!xmlnode_get_tag(query, "digest")) {
1249                                g_free(gjc->sid);
1250                                gjc->sid = NULL;
1251                        }
1252                        gjab_auth(gjc);
1253                } else {
1254                        account_online(GJ_GC(gjc));
1255
1256                        if (bud_list_cache_exists(GJ_GC(gjc)))
1257                                do_import(GJ_GC(gjc), NULL);
1258
1259                        ((struct jabber_data *)GJ_GC(gjc)->proto_data)->did_import = TRUE;
1260
1261                        gjab_reqroster(gjc);
1262                }
1263        } else {
1264                xmlnode xerr;
1265                char *errmsg = NULL;
1266                int errcode = 0;
1267                struct jabber_data *jd = GJ_GC(gjc)->proto_data;
1268
1269                xerr = xmlnode_get_tag(p->x, "error");
1270                if (xerr) {
1271                        char msg[BUF_LONG];
1272                        errmsg = xmlnode_get_data(xerr);
1273                        if (xmlnode_get_attrib(xerr, "code")) {
1274                                errcode = atoi(xmlnode_get_attrib(xerr, "code"));
1275                                g_snprintf(msg, sizeof(msg), "Error %d: %s", errcode, errmsg ? errmsg : "Unknown error");
1276                        } else
1277                                g_snprintf(msg, sizeof(msg), "%s", errmsg);
1278                        hide_login_progress(GJ_GC(gjc), msg);
1279                } else {
1280                        hide_login_progress(GJ_GC(gjc), _("Unknown login error"));
1281                }
1282
1283                jd->die = TRUE;
1284        }
1285}
1286
1287static void jabber_handleversion(gjconn gjc, xmlnode iqnode) {
1288        xmlnode querynode, x;
1289        char *id, *from;
1290        char os[1024];
1291#ifndef _WIN32
1292        struct utsname osinfo;
1293
1294        uname(&osinfo);
1295        g_snprintf(os, sizeof os, "%s %s %s", osinfo.sysname, osinfo.release, osinfo.machine);
1296#else
1297        g_snprintf(os, sizeof os, "Windows %d %d", _winmajor, _winminor);
1298#endif
1299
1300
1301        id = xmlnode_get_attrib(iqnode, "id");
1302        from = xmlnode_get_attrib(iqnode, "from");
1303
1304        x = jutil_iqnew(JPACKET__RESULT, NS_VERSION);
1305
1306        xmlnode_put_attrib(x, "to", from);
1307        xmlnode_put_attrib(x, "id", id);
1308        querynode = xmlnode_get_tag(x, "query");
1309        xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "name"), PACKAGE, -1);
1310        xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "version"), BITLBEE_VERSION, -1);
1311        xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "os"), os, -1);
1312
1313        gjab_send(gjc, x);
1314
1315        xmlnode_free(x);
1316}
1317
1318static void jabber_handletime(gjconn gjc, xmlnode iqnode) {
1319        xmlnode querynode, x;
1320        char *id, *from;
1321        time_t now_t; 
1322        struct tm *now;
1323        char buf[1024];
1324
1325        time(&now_t);
1326        now = localtime(&now_t);
1327
1328        id = xmlnode_get_attrib(iqnode, "id");
1329        from = xmlnode_get_attrib(iqnode, "from");
1330
1331        x = jutil_iqnew(JPACKET__RESULT, NS_TIME);
1332
1333        xmlnode_put_attrib(x, "to", from);
1334        xmlnode_put_attrib(x, "id", id);
1335        querynode = xmlnode_get_tag(x, "query");
1336
1337        strftime(buf, 1024, "%Y%m%dT%T", now);
1338        xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "utc"), buf, -1);
1339        strftime(buf, 1024, "%Z", now);
1340        xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "tz"), buf, -1);
1341        strftime(buf, 1024, "%d %b %Y %T", now);
1342        xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "display"), buf, -1);
1343       
1344        gjab_send(gjc, x);
1345
1346        xmlnode_free(x);
1347}
1348
1349static void jabber_handlelast(gjconn gjc, xmlnode iqnode) {
1350        xmlnode x, querytag;
1351        char *id, *from;
1352        struct jabber_data *jd = GJ_GC(gjc)->proto_data;
1353        char idle_time[32];
1354       
1355        id = xmlnode_get_attrib(iqnode, "id");
1356        from = xmlnode_get_attrib(iqnode, "from");
1357
1358        x = jutil_iqnew(JPACKET__RESULT, "jabber:iq:last");
1359
1360        xmlnode_put_attrib(x, "to", from);
1361        xmlnode_put_attrib(x, "id", id);
1362        querytag = xmlnode_get_tag(x, "query");
1363        g_snprintf(idle_time, sizeof idle_time, "%ld", jd->idle ? time(NULL) - jd->idle : 0);
1364        xmlnode_put_attrib(querytag, "seconds", idle_time);
1365
1366        gjab_send(gjc, x);
1367        xmlnode_free(x);
1368}
1369
1370/*
1371 * delete == TRUE: delete found entry
1372 *
1373 * returns pointer to (local) copy of value if found, NULL otherwise
1374 *
1375 * Note: non-reentrant!  Local static storage re-used on subsequent calls.
1376 * If you're going to need to keep the returned value, make a copy!
1377 */
1378static gchar *jabber_track_queries(GHashTable *queries, gchar *key, gboolean delete)
1379{
1380        gpointer my_key, my_val;
1381        static gchar *ret_val = NULL;
1382
1383        if(ret_val != NULL) {
1384                g_free(ret_val);
1385                ret_val = NULL;
1386        }
1387
1388        /* self-protection */
1389        if(queries != NULL && key != NULL) {
1390                if(g_hash_table_lookup_extended(queries, key, &my_key, &my_val)) {
1391                        ret_val = g_strdup((gchar *) my_val);
1392                        if(delete) {
1393                                g_hash_table_remove(queries, key);
1394                                g_free(my_key);
1395                                g_free(my_val);
1396                        }
1397                }
1398        }
1399
1400        return(ret_val);
1401}
1402
1403static void jabber_handlepacket(gjconn gjc, jpacket p)
1404{
1405        char *id;
1406        switch (p->type) {
1407        case JPACKET_MESSAGE:
1408                jabber_handlemessage(gjc, p);
1409                break;
1410        case JPACKET_PRESENCE:
1411                jabber_handlepresence(gjc, p);
1412                break;
1413        case JPACKET_IQ:
1414                id = xmlnode_get_attrib(p->x, "id");
1415                if (id != NULL && !strcmp(id, IQID_AUTH)) {
1416                        jabber_handleauthresp(gjc, p);
1417                        break;
1418                }
1419
1420                if (jpacket_subtype(p) == JPACKET__SET) {
1421                        xmlnode querynode;
1422                        querynode = xmlnode_get_tag(p->x, "query");
1423                        if (NSCHECK(querynode, "jabber:iq:roster")) {
1424                                jabber_handlebuddy(gjc, xmlnode_get_firstchild(querynode));
1425                        }
1426                } else if (jpacket_subtype(p) == JPACKET__GET) {
1427                        xmlnode querynode;
1428                        querynode = xmlnode_get_tag(p->x, "query");
1429                        if (NSCHECK(querynode, NS_VERSION)) {
1430                                jabber_handleversion(gjc, p->x);
1431                        } else if (NSCHECK(querynode, NS_TIME)) {
1432                                jabber_handletime(gjc, p->x);
1433                        } else if (NSCHECK(querynode, "jabber:iq:last")) {
1434                                jabber_handlelast(gjc, p->x);
1435                        }
1436                } else if (jpacket_subtype(p) == JPACKET__RESULT) {
1437                        xmlnode querynode, vcard;
1438                        /* char *xmlns; */
1439                        char *from;
1440
1441                        /*
1442                         * TBD: ISTM maybe this part could use a serious re-work?
1443                         */
1444                        from = xmlnode_get_attrib(p->x, "from");
1445                        querynode = xmlnode_get_tag(p->x, "query");
1446                        vcard = xmlnode_get_tag(p->x, "vCard");
1447                        if (!vcard)
1448                                vcard = xmlnode_get_tag(p->x, "VCARD");
1449
1450                        if (NSCHECK(querynode, NS_ROSTER)) {
1451                                jabber_handleroster(gjc, querynode);
1452                        } else if (NSCHECK(querynode, NS_VCARD)) {
1453                                jabber_track_queries(gjc->queries, id, TRUE);   /* delete query track */
1454                                jabber_handlevcard(gjc, querynode, from);
1455                        } else if (vcard) {
1456                                jabber_track_queries(gjc->queries, id, TRUE);   /* delete query track */
1457                                jabber_handlevcard(gjc, vcard, from);
1458                        } else {
1459                                char *val;
1460
1461                                /* handle "null" query results */
1462                                if((val = jabber_track_queries(gjc->queries, id, TRUE)) != NULL) {
1463                                        if (!g_strncasecmp(val, "vcard", 5)) {
1464                                                jabber_handlevcard(gjc, NULL, from);
1465                                        }
1466
1467                                        /* No-op */
1468                                }
1469                        }
1470
1471                } else if (jpacket_subtype(p) == JPACKET__ERROR) {
1472                        xmlnode xerr;
1473                        char *from, *errmsg = NULL;
1474                        int errcode = 0;
1475
1476                        from = xmlnode_get_attrib(p->x, "from");
1477                        xerr = xmlnode_get_tag(p->x, "error");
1478                        if (xerr) {
1479                                errmsg = xmlnode_get_data(xerr);
1480                                if (xmlnode_get_attrib(xerr, "code"))
1481                                        errcode = atoi(xmlnode_get_attrib(xerr, "code"));
1482                        }
1483
1484                        from = g_strdup_printf("Error %d (%s)", errcode, from);
1485                        do_error_dialog(GJ_GC(gjc), errmsg, from);
1486                        g_free(from);
1487
1488                }
1489
1490                break;
1491        case JPACKET_S10N:
1492                jabber_handles10n(gjc, p);
1493                break;
1494        }
1495
1496        xmlnode_free(p->x);
1497
1498        return;
1499}
1500
1501static void jabber_handlestate(gjconn gjc, int state)
1502{
1503        switch (state) {
1504        case JCONN_STATE_OFF:
1505                if(gjc->was_connected) {
1506                        hide_login_progress_error(GJ_GC(gjc), _("Connection lost"));
1507                } else {
1508                        hide_login_progress(GJ_GC(gjc), _("Unable to connect"));
1509                }
1510                signoff(GJ_GC(gjc));
1511                break;
1512        case JCONN_STATE_CONNECTED:
1513                gjc->was_connected = 1;
1514                set_login_progress(GJ_GC(gjc), 2, _("Connected"));
1515                break;
1516        case JCONN_STATE_ON:
1517                set_login_progress(GJ_GC(gjc), 3, _("Requesting Authentication Method"));
1518                gjab_reqauth(gjc);
1519                break;
1520        }
1521        return;
1522}
1523
1524static void jabber_login(struct aim_user *user)
1525{
1526        struct gaim_connection *gc = new_gaim_conn(user);
1527        struct jabber_data *jd = gc->proto_data = g_new0(struct jabber_data, 1);
1528        char *loginname = create_valid_jid(user->username, DEFAULT_SERVER, "BitlBee");
1529
1530        jd->hash = g_hash_table_new(g_str_hash, g_str_equal);
1531        jd->chats = NULL;       /* we have no chats yet */
1532
1533        set_login_progress(gc, 1, _("Connecting"));
1534
1535        if (!(jd->gjc = gjab_new(loginname, user->password, gc))) {
1536                g_free(loginname);
1537                hide_login_progress(gc, _("Unable to connect"));
1538                signoff(gc);
1539                return;
1540        }
1541
1542        g_free(loginname);
1543        gjab_state_handler(jd->gjc, jabber_handlestate);
1544        gjab_packet_handler(jd->gjc, jabber_handlepacket);
1545        jd->gjc->queries = g_hash_table_new(g_str_hash, g_str_equal);
1546        gjab_start(jd->gjc);
1547}
1548
1549static gboolean jabber_destroy_hash(gpointer key, gpointer val, gpointer data) {
1550        g_free(key);
1551        g_free(val);
1552        return TRUE;
1553}
1554
1555static gboolean jabber_free(gpointer data)
1556{
1557        struct jabber_data *jd = data;
1558
1559        if(jd->gjc != NULL) {
1560                gjab_delete(jd->gjc);
1561                g_free(jd->gjc->sid);
1562                jd->gjc = NULL;
1563        }
1564        g_free(jd);
1565
1566        return FALSE;
1567}
1568
1569static void jabber_close(struct gaim_connection *gc)
1570{
1571        struct jabber_data *jd = gc->proto_data;
1572
1573        if(jd) {
1574                GSList *jcs = jd->chats;
1575
1576                /* Free-up the jabber_chat struct allocs and the list */
1577                while (jcs) {
1578                        g_free(jcs->data);
1579                        jcs = jcs->next;
1580                }
1581                g_slist_free(jd->chats);
1582
1583                /* Free-up the away status memories and the list */
1584                if(jd->hash != NULL) {
1585                        g_hash_table_foreach_remove(jd->hash, jabber_destroy_hash, NULL);
1586                        g_hash_table_destroy(jd->hash);
1587                        jd->hash = NULL;
1588                }
1589
1590                /* Free-up the pending queries memories and the list */
1591                if(jd->gjc != NULL && jd->gjc->queries != NULL) {
1592                        g_hash_table_foreach_remove(jd->gjc->queries, jabber_destroy_hash, NULL);
1593                        g_hash_table_destroy(jd->gjc->queries);
1594                        jd->gjc->queries = NULL;
1595                }
1596        }
1597        if (gc->inpa)
1598                gaim_input_remove(gc->inpa);
1599
1600        if(jd) {
1601                g_timeout_add(50, jabber_free, jd);
1602                if(jd->gjc != NULL)
1603                        xmlnode_free(jd->gjc->current);
1604        }
1605        gc->proto_data = NULL;
1606}
1607
1608static int jabber_send_im(struct gaim_connection *gc, char *who, char *message, int len, int flags)
1609{
1610        xmlnode x, y;
1611        char *realwho;
1612        gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc;
1613
1614        if (!who || !message)
1615                return 0;
1616
1617        x = xmlnode_new_tag("message");
1618        /* Bare username and "username" not the server itself? */
1619        if (!strchr(who, '@') && strcmp(who, gjc->user->server) != 0)
1620                realwho = g_strdup_printf("%s@%s", who, gjc->user->server);
1621        else
1622                realwho = g_strdup(who);
1623        xmlnode_put_attrib(x, "to", realwho);
1624        g_free(realwho);
1625
1626        xmlnode_insert_tag(x, "bitlbee");
1627        xmlnode_put_attrib(x, "type", "chat");
1628
1629        if (message && strlen(message)) {
1630                y = xmlnode_insert_tag(x, "body");
1631                xmlnode_insert_cdata(y, message, -1);
1632        }
1633
1634        gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x);
1635        xmlnode_free(x);
1636        return 1;
1637}
1638
1639/*
1640 * Add/update buddy's roster entry on server
1641 */
1642static void jabber_roster_update(struct gaim_connection *gc, char *name)
1643{
1644        xmlnode x, y;
1645        char *realwho;
1646        gjconn gjc;
1647        struct buddy *buddy = NULL;
1648        /* struct group *buddy_group = NULL; */
1649       
1650        if(gc && gc->proto_data && ((struct jabber_data *)gc->proto_data)->gjc && name) {
1651                gjc = ((struct jabber_data *)gc->proto_data)->gjc;
1652
1653                if (!strchr(name, '@'))
1654                        realwho = g_strdup_printf("%s@%s", name, gjc->user->server);
1655                else {
1656                        jid who = jid_new(gjc->p, name);
1657                        if (who->user == NULL) {
1658                                /* FIXME: transport */
1659                                return;
1660                        }
1661                        realwho = g_strdup_printf("%s@%s", who->user, who->server);
1662                }
1663
1664
1665                x = jutil_iqnew(JPACKET__SET, NS_ROSTER);
1666                y = xmlnode_insert_tag(xmlnode_get_tag(x, "query"), "item");
1667                xmlnode_put_attrib(y, "jid", realwho);
1668
1669
1670                /* If we can find the buddy, there's an alias for him, it's not 0-length
1671                 * and it doesn't match his JID, add the "name" attribute.
1672                 */
1673                if((buddy = find_buddy(gc, realwho)) != NULL &&
1674                        buddy->show != NULL && buddy->show[0] != '\0' && strcmp(realwho, buddy->show)) {
1675
1676                        xmlnode_put_attrib(y, "name", buddy->show);
1677                }
1678
1679                /*
1680                 * Find out what group the buddy's in and send that along
1681                 * with the roster item.
1682                 */
1683                /* ** Bitlbee disabled **
1684                if((buddy_group = NULL) != NULL) {
1685                        xmlnode z;
1686                        z = xmlnode_insert_tag(y, "group");
1687                        xmlnode_insert_cdata(z, buddy_group->name, -1);
1688                }
1689                ** End - Bitlbee ** */
1690
1691                gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x);
1692
1693                xmlnode_free(x);
1694                g_free(realwho);
1695        }
1696}
1697
1698/*
1699 * Change buddy's group on server roster
1700 */
1701static void jabber_group_change(struct gaim_connection *gc, char *name, char *old_group, char *new_group)
1702{
1703        if(strcmp(old_group, new_group)) {
1704                jabber_roster_update(gc, name);
1705        }
1706}
1707
1708static void jabber_add_buddy(struct gaim_connection *gc, char *name)
1709{
1710        xmlnode x;
1711        char *realwho;
1712        gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc;
1713
1714        if (!((struct jabber_data *)gc->proto_data)->did_import)
1715                return;
1716
1717        if (!name)
1718                return;
1719
1720        if (!strcmp(gc->username, name))
1721                return;
1722
1723        if (!strchr(name, '@'))
1724                realwho = g_strdup_printf("%s@%s", name, gjc->user->server);
1725        else {
1726                jid who;
1727               
1728                if((who = jid_new(gjc->p, name)) == NULL) {
1729                        char *msg = g_strdup_printf("%s: \"%s\"", _("Invalid Jabber I.D."), name);
1730                        do_error_dialog(GJ_GC(gjc), msg, _("Jabber Error"));
1731                        g_free(msg);
1732                        jabber_remove_gaim_buddy(gc, name);
1733                        return;
1734                }
1735                if (who->user == NULL) {
1736                        /* FIXME: transport */
1737                        return;
1738                }
1739                realwho = g_strdup_printf("%s@%s", who->user, who->server);
1740        }
1741
1742        x = xmlnode_new_tag("presence");
1743        xmlnode_put_attrib(x, "to", realwho);
1744        xmlnode_put_attrib(x, "type", "subscribe");
1745        gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x);
1746        xmlnode_free(x);
1747
1748        jabber_roster_update(gc, realwho);
1749
1750        g_free(realwho);
1751}
1752
1753static void jabber_remove_buddy(struct gaim_connection *gc, char *name, char *group)
1754{
1755        xmlnode x;
1756        char *realwho;
1757        gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc;
1758
1759        if (!name)
1760                return;
1761
1762        if (!strchr(name, '@'))
1763                realwho = g_strdup_printf("%s@%s", name, gjc->user->server);
1764        else
1765                realwho = g_strdup(name);
1766
1767        x = xmlnode_new_tag("presence");
1768        xmlnode_put_attrib(x, "to", realwho);
1769        xmlnode_put_attrib(x, "type", "unsubscribe");
1770        gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x);
1771        g_free(realwho);
1772        xmlnode_free(x);
1773}
1774
1775static void jabber_get_info(struct gaim_connection *gc, char *who) {
1776        xmlnode x;
1777        char *id;
1778        char *realwho;
1779        struct jabber_data *jd = gc->proto_data;
1780        gjconn gjc = jd->gjc;
1781
1782        x = jutil_iqnew(JPACKET__GET, NS_VCARD);
1783        /* Bare username? */
1784        if (!strchr(who, '@')) {
1785                realwho = g_strdup_printf("%s@%s", who, gjc->user->server);
1786        } else {
1787                realwho = g_strdup(who);
1788        }
1789        xmlnode_put_attrib(x, "to", realwho);
1790        g_free(realwho);
1791
1792        id = gjab_getid(gjc);
1793        xmlnode_put_attrib(x, "id", id);
1794
1795        g_hash_table_insert(jd->gjc->queries, g_strdup(id), g_strdup("vCard"));
1796
1797        gjab_send(gjc, x);
1798
1799        xmlnode_free(x);
1800       
1801}
1802
1803static void jabber_get_away_msg(struct gaim_connection *gc, char *who) {
1804        struct jabber_data *jd = gc->proto_data;
1805        gjconn gjc = jd->gjc;
1806        char *status;
1807
1808        /* space for all elements: Jabber I.D. + "status" + NULL (list terminator) */
1809        gchar **str_arr = (gchar **) g_new(gpointer, 3);
1810        gchar **ap = str_arr;
1811        gchar *realwho, *final;
1812
1813        /* Bare username? */
1814        if (!strchr(who, '@')) {
1815                realwho = g_strdup_printf("%s@%s", who, gjc->user->server);
1816        } else {
1817                realwho = g_strdup(who);
1818        }
1819        *ap++ = g_strdup_printf("<B>Jabber ID:</B> %s<BR>\n", realwho);
1820
1821        if((status = g_hash_table_lookup(jd->hash, realwho)) == NULL) {
1822                status = _("Unknown");
1823        }
1824        *ap++ = g_strdup_printf("<B>Status:</B> %s<BR>\n", status);
1825
1826        *ap = NULL;
1827
1828        final= g_strjoinv(NULL, str_arr);
1829        g_strfreev(str_arr);
1830
1831        g_free(realwho);
1832        g_free(final);
1833       
1834}
1835
1836static GList *jabber_away_states(struct gaim_connection *gc) {
1837        GList *m = NULL;
1838
1839        m = g_list_append(m, "Online");
1840        m = g_list_append(m, "Chatty");
1841        m = g_list_append(m, "Away");
1842        m = g_list_append(m, "Extended Away");
1843        m = g_list_append(m, "Do Not Disturb");
1844
1845        return m;
1846}
1847
1848static void jabber_set_away(struct gaim_connection *gc, char *state, char *message)
1849{
1850        xmlnode x, y;
1851        struct jabber_data *jd = gc->proto_data;
1852        gjconn gjc = jd->gjc;
1853
1854        gc->away = NULL; /* never send an auto-response */
1855
1856        x = xmlnode_new_tag("presence");
1857
1858        if (!strcmp(state, GAIM_AWAY_CUSTOM)) {
1859                /* oh goody. Gaim is telling us what to do. */
1860                if (message) {
1861                        /* Gaim wants us to be away */
1862                        y = xmlnode_insert_tag(x, "show");
1863                        xmlnode_insert_cdata(y, "away", -1);
1864                        y = xmlnode_insert_tag(x, "status");
1865                        {
1866                                char *utf8 = str_to_utf8(message);
1867                                xmlnode_insert_cdata(y, utf8, -1);
1868                                g_free(utf8);
1869                        }
1870                        gc->away = "";
1871                } else {
1872                        /* Gaim wants us to not be away */
1873                        /* but for Jabber, we can just send presence with no other information. */
1874                }
1875        } else {
1876                /* state is one of our own strings. it won't be NULL. */
1877                if (!g_strcasecmp(state, "Online")) {
1878                        /* once again, we don't have to put anything here */
1879                } else if (!g_strcasecmp(state, "Chatty")) {
1880                        y = xmlnode_insert_tag(x, "show");
1881                        xmlnode_insert_cdata(y, "chat", -1);
1882                } else if (!g_strcasecmp(state, "Away")) {
1883                        y = xmlnode_insert_tag(x, "show");
1884                        xmlnode_insert_cdata(y, "away", -1);
1885                        gc->away = "";
1886                } else if (!g_strcasecmp(state, "Extended Away")) {
1887                        y = xmlnode_insert_tag(x, "show");
1888                        xmlnode_insert_cdata(y, "xa", -1);
1889                        gc->away = "";
1890                } else if (!g_strcasecmp(state, "Do Not Disturb")) {
1891                        y = xmlnode_insert_tag(x, "show");
1892                        xmlnode_insert_cdata(y, "dnd", -1);
1893                        gc->away = "";
1894                }
1895        }
1896
1897        gjab_send(gjc, x);
1898        xmlnode_free(x);
1899}
1900
1901static void jabber_set_idle(struct gaim_connection *gc, int idle) {
1902        struct jabber_data *jd = (struct jabber_data *)gc->proto_data;
1903        jd->idle = idle ? time(NULL) - idle : idle;
1904}
1905
1906static void jabber_keepalive(struct gaim_connection *gc) {
1907        struct jabber_data *jd = (struct jabber_data *)gc->proto_data;
1908        gjab_send_raw(jd->gjc, \t  ");
1909}
1910
1911static void jabber_buddy_free(struct buddy *b)
1912{
1913        while (b->proto_data) {
1914                g_free(((GSList *)b->proto_data)->data);
1915                b->proto_data = g_slist_remove(b->proto_data, ((GSList *)b->proto_data)->data);
1916        }
1917}
1918
1919/*---------------------------------------*/
1920/* Jabber "set info" (vCard) support     */
1921/*---------------------------------------*/
1922
1923/*
1924 * V-Card format:
1925 *
1926 *  <vCard prodid='' version='' xmlns=''>
1927 *    <FN></FN>
1928 *    <N>
1929 *      <FAMILY/>
1930 *      <GIVEN/>
1931 *    </N>
1932 *    <NICKNAME/>
1933 *    <URL/>
1934 *    <ADR>
1935 *      <STREET/>
1936 *      <EXTADD/>
1937 *      <LOCALITY/>
1938 *      <REGION/>
1939 *      <PCODE/>
1940 *      <COUNTRY/>
1941 *    </ADR>
1942 *    <TEL/>
1943 *    <EMAIL/>
1944 *    <ORG>
1945 *      <ORGNAME/>
1946 *      <ORGUNIT/>
1947 *    </ORG>
1948 *    <TITLE/>
1949 *    <ROLE/>
1950 *    <DESC/>
1951 *    <BDAY/>
1952 *  </vCard>
1953 *
1954 * See also:
1955 *
1956 *      http://docs.jabber.org/proto/html/vcard-temp.html
1957 *      http://www.vcard-xml.org/dtd/vCard-XML-v2-20010520.dtd
1958 */
1959
1960/*
1961 * Cross-reference user-friendly V-Card entry labels to vCard XML tags
1962 * and attributes.
1963 *
1964 * Order is (or should be) unimportant.  For example: we have no way of
1965 * knowing in what order real data will arrive.
1966 *
1967 * Format: Label, Pre-set text, "visible" flag, "editable" flag, XML tag
1968 *         name, XML tag's parent tag "path" (relative to vCard node).
1969 *
1970 *         List is terminated by a NULL label pointer.
1971 *
1972 *         Entries with no label text, but with XML tag and parent tag
1973 *         entries, are used by V-Card XML construction routines to
1974 *         "automagically" construct the appropriate XML node tree.
1975 *
1976 * Thoughts on future direction/expansion
1977 *
1978 *      This is a "simple" vCard.
1979 *
1980 *      It is possible for nodes other than the "vCard" node to have
1981 *      attributes.  Should that prove necessary/desirable, add an
1982 *      "attributes" pointer to the vcard_template struct, create the
1983 *      necessary tag_attr structs, and add 'em to the vcard_dflt_data
1984 *      array.
1985 *
1986 *      The above changes will (obviously) require changes to the vCard
1987 *      construction routines.
1988 */
1989
1990static struct vcard_template {
1991        char *label;                    /* label text pointer */
1992        char *text;                     /* entry text pointer */
1993        int  visible;                   /* should entry field be "visible?" */
1994        int  editable;                  /* should entry field be editable? */
1995        char *tag;                      /* tag text */
1996        char *ptag;                     /* parent tag "path" text */
1997        char *url;                      /* vCard display format if URL */
1998} vcard_template_data[] = {
1999        {N_("Full Name"),          NULL, TRUE, TRUE, "FN",        NULL,  NULL},
2000        {N_("Family Name"),        NULL, TRUE, TRUE, "FAMILY",    "N",   NULL},
2001        {N_("Given Name"),         NULL, TRUE, TRUE, "GIVEN",     "N",   NULL},
2002        {N_("Nickname"),           NULL, TRUE, TRUE, "NICKNAME",  NULL,  NULL},
2003        {N_("URL"),                NULL, TRUE, TRUE, "URL",       NULL,  "<A HREF=\"%s\">%s</A>"},
2004        {N_("Street Address"),     NULL, TRUE, TRUE, "STREET",    "ADR", NULL},
2005        {N_("Extended Address"),   NULL, TRUE, TRUE, "EXTADD",    "ADR", NULL},
2006        {N_("Locality"),           NULL, TRUE, TRUE, "LOCALITY",  "ADR", NULL},
2007        {N_("Region"),             NULL, TRUE, TRUE, "REGION",    "ADR", NULL},
2008        {N_("Postal Code"),        NULL, TRUE, TRUE, "PCODE",     "ADR", NULL},
2009        {N_("Country"),            NULL, TRUE, TRUE, "COUNTRY",   "ADR", NULL},
2010        {N_("Telephone"),          NULL, TRUE, TRUE, "TELEPHONE", NULL,  NULL},
2011        {N_("Email"),              NULL, TRUE, TRUE, "EMAIL",     NULL,  "<A HREF=\"mailto:%s\">%s</A>"},
2012        {N_("Organization Name"),  NULL, TRUE, TRUE, "ORGNAME",   "ORG", NULL},
2013        {N_("Organization Unit"),  NULL, TRUE, TRUE, "ORGUNIT",   "ORG", NULL},
2014        {N_("Title"),              NULL, TRUE, TRUE, "TITLE",     NULL,  NULL},
2015        {N_("Role"),               NULL, TRUE, TRUE, "ROLE",      NULL,  NULL},
2016        {N_("Birthday"),           NULL, TRUE, TRUE, "BDAY",      NULL,  NULL},
2017        {N_("Description"),        NULL, TRUE, TRUE, "DESC",      NULL,  NULL},
2018        {"", NULL, TRUE, TRUE, "N",     NULL, NULL},
2019        {"", NULL, TRUE, TRUE, "ADR",   NULL, NULL},
2020        {"", NULL, TRUE, TRUE, "ORG",   NULL, NULL},
2021        {NULL, NULL, 0, 0, NULL, NULL, NULL}
2022};
2023
2024/*
2025 * Used by routines to parse an XML-encoded string into an xmlnode tree
2026 */
2027typedef struct {
2028        XML_Parser parser;
2029        xmlnode current;
2030} *xmlstr2xmlnode_parser, xmlstr2xmlnode_parser_struct;
2031
2032
2033/*
2034 * Used by XML_Parse on parsing CDATA
2035 */
2036static void xmlstr2xmlnode_charData(void *userdata, const char *s, int slen)
2037{
2038        xmlstr2xmlnode_parser xmlp = (xmlstr2xmlnode_parser) userdata;
2039
2040        if (xmlp->current)
2041                xmlnode_insert_cdata(xmlp->current, s, slen);
2042}
2043
2044/*
2045 * Used by XML_Parse to start or append to an xmlnode
2046 */
2047static void xmlstr2xmlnode_startElement(void *userdata, const char *name, const char **attribs)
2048{
2049        xmlnode x;
2050        xmlstr2xmlnode_parser xmlp = (xmlstr2xmlnode_parser) userdata;
2051
2052        if (xmlp->current) {
2053                /* Append the node to the current one */
2054                x = xmlnode_insert_tag(xmlp->current, name);
2055                xmlnode_put_expat_attribs(x, attribs);
2056
2057                xmlp->current = x;
2058        } else {
2059                x = xmlnode_new_tag(name);
2060                xmlnode_put_expat_attribs(x, attribs);
2061                xmlp->current = x;
2062        }
2063}
2064
2065/*
2066 * Used by XML_Parse to end an xmlnode
2067 */
2068static void xmlstr2xmlnode_endElement(void *userdata, const char *name)
2069{
2070        xmlstr2xmlnode_parser xmlp = (xmlstr2xmlnode_parser) userdata;
2071        xmlnode x;
2072
2073        if (xmlp->current != NULL && (x = xmlnode_get_parent(xmlp->current)) != NULL) {
2074                xmlp->current = x;
2075        }
2076}
2077
2078/*
2079 * Parse an XML-encoded string into an xmlnode tree
2080 *
2081 * Caller is responsible for freeing the returned xmlnode
2082 */
2083static xmlnode xmlstr2xmlnode(char *xmlstring)
2084{
2085        xmlstr2xmlnode_parser my_parser = g_new(xmlstr2xmlnode_parser_struct, 1);
2086        xmlnode x = NULL;
2087
2088        my_parser->parser = XML_ParserCreate(NULL);
2089        my_parser->current = NULL;
2090
2091        XML_SetUserData(my_parser->parser, (void *)my_parser);
2092        XML_SetElementHandler(my_parser->parser, xmlstr2xmlnode_startElement, xmlstr2xmlnode_endElement);
2093        XML_SetCharacterDataHandler(my_parser->parser, xmlstr2xmlnode_charData);
2094        XML_Parse(my_parser->parser, xmlstring, strlen(xmlstring), 0);
2095
2096        x = my_parser->current;
2097
2098        XML_ParserFree(my_parser->parser);
2099        g_free(my_parser);
2100
2101        return(x);
2102}
2103
2104/*
2105 * Insert a tag node into an xmlnode tree, recursively inserting parent tag
2106 * nodes as necessary
2107 *
2108 * Returns pointer to inserted node
2109 *
2110 * Note to hackers: this code is designed to be re-entrant (it's recursive--it
2111 * calls itself), so don't put any "static"s in here!
2112 */
2113static xmlnode insert_tag_to_parent_tag(xmlnode start, const char *parent_tag, const char *new_tag)
2114{
2115        xmlnode x = NULL;
2116
2117        /*
2118         * If the parent tag wasn't specified, see if we can get it
2119         * from the vCard template struct.
2120         */
2121        if(parent_tag == NULL) {
2122                struct vcard_template *vc_tp = vcard_template_data;
2123
2124                while(vc_tp->label != NULL) {
2125                        if(strcmp(vc_tp->tag, new_tag) == 0) {
2126                                parent_tag = vc_tp->ptag;
2127                                break;
2128                        }
2129                        ++vc_tp;
2130                }
2131        }
2132
2133        /*
2134         * If we have a parent tag...
2135         */
2136        if(parent_tag != NULL ) {
2137                /*
2138                 * Try to get the parent node for a tag
2139                 */
2140                if((x = xmlnode_get_tag(start, parent_tag)) == NULL) {
2141                        /*
2142                         * Descend?
2143                         */
2144                        char *grand_parent = strcpy(g_malloc(strlen(parent_tag) + 1), parent_tag);
2145                        char *parent;
2146
2147                        if((parent = strrchr(grand_parent, '/')) != NULL) {
2148                                *(parent++) = '\0';
2149                                x = insert_tag_to_parent_tag(start, grand_parent, parent);
2150                        } else {
2151                                x = xmlnode_insert_tag(start, grand_parent);
2152                        }
2153                        g_free(grand_parent);
2154                } else {
2155                        /*
2156                         * We found *something* to be the parent node.
2157                         * Note: may be the "root" node!
2158                         */
2159                        xmlnode y;
2160                        if((y = xmlnode_get_tag(x, new_tag)) != NULL) {
2161                                return(y);
2162                        }
2163                }
2164        }
2165
2166        /*
2167         * insert the new tag into its parent node
2168         */
2169        return(xmlnode_insert_tag((x == NULL? start : x), new_tag));
2170}
2171
2172/*
2173 * Send vCard info to Jabber server
2174 */
2175static void jabber_set_info(struct gaim_connection *gc, char *info)
2176{
2177        xmlnode x, vc_node;
2178        char *id;
2179        struct jabber_data *jd = gc->proto_data;
2180        gjconn gjc = jd->gjc;
2181
2182        x = xmlnode_new_tag("iq");
2183        xmlnode_put_attrib(x,"type","set");
2184
2185        id = gjab_getid(gjc);
2186       
2187        xmlnode_put_attrib(x, "id", id);
2188
2189        /*
2190         * Send only if there's actually any *information* to send
2191         */
2192        if((vc_node = xmlstr2xmlnode(info)) != NULL && xmlnode_get_name(vc_node) != NULL &&
2193                        g_strncasecmp(xmlnode_get_name(vc_node), "vcard", 5) == 0) {
2194                xmlnode_insert_tag_node(x, vc_node);
2195                gjab_send(gjc, x);
2196        }
2197
2198        xmlnode_free(x);
2199}
2200
2201/*
2202 * displays a Jabber vCard
2203 */
2204static void jabber_handlevcard(gjconn gjc, xmlnode querynode, char *from)
2205{
2206        struct jabber_data *jd = GJ_GC(gjc)->proto_data;
2207        jid who = jid_new(gjc->p, from);
2208        char *status = NULL, *text = NULL;
2209        GString *str = g_string_sized_new(100);
2210        xmlnode child;
2211
2212        gchar *buddy = NULL;
2213       
2214        if(querynode == NULL) {
2215                serv_got_crap(GJ_GC(gjc), "%s - Received empty info reply from %s", _("User Info"), from);
2216                return;
2217        }
2218
2219        if(who->resource != NULL && (who->resource)[0] != '\0') {
2220                buddy = g_strdup_printf("%s@%s/%s", who->user, who->server, who->resource);
2221        } else {
2222                buddy = g_strdup_printf("%s@%s", who->user, who->server);
2223        }
2224
2225        if((status = g_hash_table_lookup(jd->hash, buddy)) == NULL) {
2226                status = _("Unknown");
2227        }
2228
2229        g_string_sprintfa(str, "%s: %s - %s: %s", _("Jabber ID"), buddy, _("Status"),
2230                               status);
2231
2232        for(child = querynode->firstchild; child; child = child->next)
2233        {
2234                xmlnode child2;
2235
2236                if(child->type != NTYPE_TAG)
2237                        continue;
2238
2239                text = xmlnode_get_data(child);
2240                if(text && !strcmp(child->name, "FN")) {
2241                        info_string_append(str, "\n", _("Full Name"), text);
2242                } else if (!strcmp(child->name, "N")) {
2243                        for (child2 = child->firstchild; child2; child2 = child2->next) {
2244                                char *text2 = NULL;
2245
2246                                if (child2->type != NTYPE_TAG)
2247                                        continue;
2248
2249                                text2 = xmlnode_get_data(child2);
2250                                if (text2 && !strcmp(child2->name, "FAMILY")) {
2251                                        info_string_append(str, "\n", _("Family Name"), text2);
2252                                } else if (text2 && !strcmp(child2->name, "GIVEN")) {
2253                                        info_string_append(str, "\n", _("Given Name"), text2);
2254                                } else if (text2 && !strcmp(child2->name, "MIDDLE")) {
2255                                        info_string_append(str, "\n", _("Middle Name"), text2);
2256                                }
2257                        }
2258                } else if (text && !strcmp(child->name, "NICKNAME")) {
2259                        info_string_append(str, "\n", _("Nickname"), text);
2260                } else if (text && !strcmp(child->name, "BDAY")) {
2261                        info_string_append(str, "\n", _("Birthday"), text);
2262                } else if (!strcmp(child->name, "ADR")) {
2263                        /* show wich address it is */
2264                        /* Just for the beauty of bitlbee
2265                        if (child->firstchild)
2266                                g_string_sprintfa(str, "%s:\n", _("Address"));
2267                        */
2268                        for(child2 = child->firstchild; child2; child2 = child2->next) {
2269                                char *text2 = NULL;
2270
2271                                if(child2->type != NTYPE_TAG)
2272                                        continue;
2273
2274                                text2 = xmlnode_get_data(child2);
2275                                if(text2 && !strcmp(child2->name, "POBOX")) {
2276                                        info_string_append(str, "\n",
2277                                                        _("P.O. Box"), text2);
2278                                } else if(text2 && !strcmp(child2->name, "EXTADR")) {
2279                                        info_string_append(str, "\n",
2280                                                        _("Extended Address"), text2);
2281                                } else if(text2 && !strcmp(child2->name, "STREET")) {
2282                                        info_string_append(str, "\n",
2283                                                        _("Street Address"), text2);
2284                                } else if(text2 && !strcmp(child2->name, "LOCALITY")) {
2285                                        info_string_append(str, "\n",
2286                                                        _("Locality"), text2);
2287                                } else if(text2 && !strcmp(child2->name, "REGION")) {
2288                                        info_string_append(str, "\n",
2289                                                        _("Region"), text2);
2290                                } else if(text2 && !strcmp(child2->name, "PCODE")) {
2291                                        info_string_append(str, "\n",
2292                                                        _("Postal Code"), text2);
2293                                } else if(text2 && (!strcmp(child2->name, "CTRY")
2294                                                        || !strcmp(child2->name, "COUNTRY"))) {
2295                                        info_string_append(str, "\n", _("Country"), text2);
2296                                }
2297                        }
2298                } else if(!strcmp(child->name, "TEL")) {
2299                        char *number = NULL;
2300                        if ((child2 = xmlnode_get_tag(child, "NUMBER"))) {
2301                                /* show what kind of number it is */
2302                                number = xmlnode_get_data(child2);
2303                                if(number) {
2304                                        info_string_append(str, "\n", _("Telephone"), number);
2305                                }
2306                        } else if((number = xmlnode_get_data(child))) {
2307                                /* lots of clients (including gaim) do this,
2308                                 * but it's out of spec */
2309                                info_string_append(str, "\n", _("Telephone"), number);
2310                        }
2311                } else if(!strcmp(child->name, "EMAIL")) {
2312                        char *userid = NULL;
2313                        if((child2 = xmlnode_get_tag(child, "USERID"))) {
2314                                /* show what kind of email it is */
2315                                userid = xmlnode_get_data(child2);
2316                                if(userid) {
2317                                        info_string_append(str, "\n", _("Email"), userid);
2318                                }
2319                        } else if((userid = xmlnode_get_data(child))) {
2320                                /* lots of clients (including gaim) do this,
2321                                 * but it's out of spec */
2322                                info_string_append(str, "\n", _("Email"), userid);
2323                        }
2324                } else if(!strcmp(child->name, "ORG")) {
2325                        for(child2 = child->firstchild; child2; child2 = child2->next) {
2326                                char *text2 = NULL;
2327
2328                                if(child2->type != NTYPE_TAG)
2329                                        continue;
2330
2331                                text2 = xmlnode_get_data(child2);
2332                                if(text2 && !strcmp(child2->name, "ORGNAME")) {
2333                                        info_string_append(str, "\n", _("Organization Name"), text2);
2334                                } else if(text2 && !strcmp(child2->name, "ORGUNIT")) {
2335                                        info_string_append(str, "\n", _("Organization Unit"), text2);
2336                                }
2337                        }
2338                } else if(text && !strcmp(child->name, "TITLE")) {
2339                        info_string_append(str, "\n", _("Title"), text);
2340                } else if(text && !strcmp(child->name, "ROLE")) {
2341                        info_string_append(str, "\n", _("Role"), text);
2342                } else if(text && !strcmp(child->name, "DESC")) {
2343                        g_string_sprintfa(str, "\n%s:\n%s\n%s", _("Description"), 
2344                                        text, _("End of Description"));
2345                }
2346        }
2347
2348        serv_got_crap(GJ_GC(gjc), "%s\n%s", _("User Info"), str->str);
2349
2350        g_free(buddy);
2351        g_string_free(str, TRUE);
2352}
2353
2354
2355static GList *jabber_actions()
2356{
2357        GList *m = NULL;
2358
2359        m = g_list_append(m, _("Set User Info"));
2360        /*
2361        m = g_list_append(m, _("Set Dir Info"));
2362        m = g_list_append(m, _("Change Password"));
2363         */
2364
2365        return m;
2366}
2367
2368static struct prpl *my_protocol = NULL;
2369
2370void jabber_init(struct prpl *ret)
2371{
2372        /* the NULL's aren't required but they're nice to have */
2373        ret->protocol = PROTO_JABBER;
2374        ret->name = jabber_name;
2375        ret->away_states = jabber_away_states;
2376        ret->actions = jabber_actions;
2377        ret->login = jabber_login;
2378        ret->close = jabber_close;
2379        ret->send_im = jabber_send_im;
2380        ret->set_info = jabber_set_info;
2381        ret->get_info = jabber_get_info;
2382        ret->set_away = jabber_set_away;
2383        ret->get_away = jabber_get_away_msg;
2384        ret->set_idle = jabber_set_idle;
2385        ret->add_buddy = jabber_add_buddy;
2386        ret->remove_buddy = jabber_remove_buddy;
2387        ret->add_permit = NULL;
2388        ret->add_deny = NULL;
2389        ret->rem_permit = NULL;
2390        ret->rem_deny = NULL;
2391        ret->set_permit_deny = NULL;
2392        ret->keepalive = jabber_keepalive;
2393        ret->buddy_free = jabber_buddy_free;
2394        ret->alias_buddy = jabber_roster_update;
2395        ret->group_buddy = jabber_group_change;
2396        ret->cmp_buddynames = g_strcasecmp;
2397
2398        my_protocol = ret;
2399}
Note: See TracBrowser for help on using the repository browser.