source: protocols/jabber/jabber.c @ 5c09a59

Last change on this file since 5c09a59 was 5c09a59, checked in by Wilmer van der Gaast <wilmer@…>, at 2005-11-15T23:16:39Z

Got rid of raw B's in source,
Replaced most irc_usermsg() calls in nogaim.c with serv_got_crap(),
Slightly clearer handling of buddy add requests.

Thanks to timing for the patch.

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