source: protocols/jabber/jabber.c @ 7733b8c

Last change on this file since 7733b8c was 7733b8c, checked in by dequis <dx@…>, at 2015-02-21T06:10:54Z

Add the concept of jabber sub-protocols

Currently including: jabber (none), gtalk, facebook, hipchat.

They provide a default server field and an oauth service definition.

Also the "account tag guesses" become subprotocol guesses

  • Property mode set to 100644
File size: 19.6 KB
RevLine 
[f06894d]1/***************************************************************************\
2*                                                                           *
3*  BitlBee - An IRC to IM gateway                                           *
4*  Jabber module - Main file                                                *
5*                                                                           *
[0e788f5]6*  Copyright 2006-2013 Wilmer van der Gaast <wilmer@gaast.net>              *
[f06894d]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 along  *
19*  with this program; if not, write to the Free Software Foundation, Inc.,  *
20*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.              *
21*                                                                           *
22\***************************************************************************/
23
24#include <glib.h>
25#include <string.h>
26#include <unistd.h>
27#include <ctype.h>
28#include <stdio.h>
29
[21167d2]30#include "ssl_client.h"
[f06894d]31#include "xmltree.h"
32#include "bitlbee.h"
33#include "jabber.h"
[e14b47b8]34#include "oauth.h"
[608f8cf]35#include "md5.h"
[f06894d]36
[b5c8a34]37GSList *jabber_connections;
38
[7f69740]39/* First enty is the default */
40static const int jabber_port_list[] = {
41        5222,
42        5223,
43        5220,
44        5221,
45        5224,
46        5225,
47        5226,
48        5227,
49        5228,
50        5229,
51        80,
52        443,
53        0
54};
55
[7733b8c]56static jabber_subproto_desc_t jabber_subproto_list[] = {
57        { "jabber", JSUB_NONE, NULL, NULL },
58        { "gtalk", JSUB_GTALK, &oauth2_service_google, "talk.google.com" },
59        { "fb", JSUB_FACEBOOK, &oauth2_service_facebook, "chat.facebook.com" },
60        { "hipchat", JSUB_HIPCHAT, NULL, "chat.hipchat.com" },
61        { NULL },
62};
63
[5ebff60]64static void jabber_init(account_t *acc)
[f06894d]65{
66        set_t *s;
[7f69740]67        char str[16];
[7733b8c]68        jabber_subproto_desc_t *subproto = acc->prpl->data;
[5ebff60]69
70        s = set_add(&acc->set, "activity_timeout", "600", set_eval_int, acc);
71
72        s = set_add(&acc->set, "display_name", NULL, NULL, acc);
73
[7733b8c]74        if (subproto->oauth2_service) {
75                s = set_add(&acc->set, "oauth", "false", set_eval_oauth, acc);
76        }
77
[5ebff60]78        g_snprintf(str, sizeof(str), "%d", jabber_port_list[0]);
79        s = set_add(&acc->set, "port", str, set_eval_int, acc);
[f06894d]80        s->flags |= ACC_SET_OFFLINE_ONLY;
[1c3008a]81
[5ebff60]82        s = set_add(&acc->set, "priority", "0", set_eval_priority, acc);
83
84        s = set_add(&acc->set, "proxy", "<local>;<auto>", NULL, acc);
85
86        s = set_add(&acc->set, "resource", "BitlBee", NULL, acc);
[ebe7b36]87        s->flags |= ACC_SET_OFFLINE_ONLY;
[5ebff60]88
89        s = set_add(&acc->set, "resource_select", "activity", NULL, acc);
90
91        s = set_add(&acc->set, "sasl", "true", set_eval_bool, acc);
[ce199b7]92        s->flags |= ACC_SET_OFFLINE_ONLY | SET_HIDDEN_DEFAULT;
[5ebff60]93
94        s = set_add(&acc->set, "server", NULL, set_eval_account, acc);
[bb5ce568]95        s->flags |= SET_NOSAVE | ACC_SET_OFFLINE_ONLY | SET_NULL_OK;
[5ebff60]96
97        s = set_add(&acc->set, "ssl", "false", set_eval_bool, acc);
[f06894d]98        s->flags |= ACC_SET_OFFLINE_ONLY;
[5ebff60]99
100        s = set_add(&acc->set, "tls", "true", set_eval_tls, acc);
[f06894d]101        s->flags |= ACC_SET_OFFLINE_ONLY;
[5ebff60]102
103        s = set_add(&acc->set, "tls_verify", "true", set_eval_bool, acc);
[486ddb5]104        s->flags |= ACC_SET_OFFLINE_ONLY;
[34ded90]105
[5ebff60]106        s = set_add(&acc->set, "user_agent", "BitlBee", NULL, acc);
107
108        s = set_add(&acc->set, "xmlconsole", "false", set_eval_bool, acc);
[a6df0b5]109        s->flags |= ACC_SET_OFFLINE_ONLY;
[5ebff60]110
[06eef80]111        acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE |
112                      ACC_FLAG_HANDLE_DOMAINS;
[7733b8c]113
114        if (subproto->server) {
115                set_setstr(&acc->set, "server", (char *) subproto->server);
116        }
[f06894d]117}
118
[5ebff60]119static void jabber_generate_id_hash(struct jabber_data *jd);
[608f8cf]120
[5ebff60]121static void jabber_login(account_t *acc)
[f06894d]122{
[5ebff60]123        struct im_connection *ic = imcb_new(acc);
124        struct jabber_data *jd = g_new0(struct jabber_data, 1);
[7733b8c]125        jabber_subproto_desc_t *subproto = acc->prpl->data;
[4a5d885]126        char *s;
[5ebff60]127
[b5c8a34]128        /* For now this is needed in the _connected() handlers if using
129           GLib event handling, to make sure we're not handling events
130           on dead connections. */
[5ebff60]131        jabber_connections = g_slist_prepend(jabber_connections, ic);
132
[0da65d5]133        jd->ic = ic;
134        ic->proto_data = jd;
[7733b8c]135        jd->subproto = subproto->id;
[5ebff60]136
137        jabber_set_me(ic, acc->user);
138
[de03374]139        jd->fd = jd->r_inpa = jd->w_inpa = -1;
[5ebff60]140
141        if (jd->server == NULL) {
142                imcb_error(ic, "Incomplete account name (format it like <username@jabberserver.name>)");
143                imc_logout(ic, FALSE);
[21167d2]144                return;
145        }
[5ebff60]146
147        if ((s = strchr(jd->server, '/'))) {
[3b3cd693]148                *s = 0;
[5ebff60]149                set_setstr(&acc->set, "resource", s + 1);
150
[3b3cd693]151                /* Also remove the /resource from the original variable so we
152                   won't have to do this again every time. */
[5ebff60]153                s = strchr(acc->user, '/');
[3b3cd693]154                *s = 0;
155        }
[5ebff60]156
157        jd->node_cache = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, jabber_cache_entry_free);
158        jd->buddies = g_hash_table_new(g_str_hash, g_str_equal);
159
[7733b8c]160        if (subproto->oauth2_service && set_getbool(&acc->set, "oauth")) {
[e14b47b8]161                GSList *p_in = NULL;
162                const char *tok;
[5ebff60]163
[f138bd2]164                jd->fd = jd->r_inpa = jd->w_inpa = -1;
[5ebff60]165
[7733b8c]166                jd->oauth2_service = subproto->oauth2_service;
[5ebff60]167
168                oauth_params_parse(&p_in, ic->acc->pass);
169
[64b6635]170                /* First see if we have a refresh token, in which case any
171                   access token we *might* have has probably expired already
172                   anyway. */
[5ebff60]173                if ((tok = oauth_params_get(&p_in, "refresh_token"))) {
174                        sasl_oauth2_refresh(ic, tok);
[64b6635]175                }
176                /* If we don't have a refresh token, let's hope the access
177                   token is still usable. */
[5ebff60]178                else if ((tok = oauth_params_get(&p_in, "access_token"))) {
179                        jd->oauth2_access_token = g_strdup(tok);
180                        jabber_connect(ic);
[64b6635]181                }
182                /* If we don't have any, start the OAuth process now. Don't
183                   even open an XMPP connection yet. */
[5ebff60]184                else {
185                        sasl_oauth2_init(ic);
[f988ad3]186                        ic->flags |= OPT_SLOW_LOGIN;
187                }
[5ebff60]188
189                oauth_params_free(&p_in);
190        } else {
191                jabber_connect(ic);
[4a5d885]192        }
193}
194
195/* Separate this from jabber_login() so we can do OAuth first if necessary.
196   Putting this in io.c would probably be more correct. */
[5ebff60]197void jabber_connect(struct im_connection *ic)
[4a5d885]198{
199        account_t *acc = ic->acc;
200        struct jabber_data *jd = ic->proto_data;
201        int i;
202        char *connect_to;
203        struct ns_srv_reply **srvl = NULL, *srv = NULL;
[5ebff60]204
[36e9f62]205        /* Figure out the hostname to connect to. */
[5ebff60]206        if (acc->server && *acc->server) {
[36e9f62]207                connect_to = acc->server;
[5ebff60]208        } else if ((srvl = srv_lookup("xmpp-client", "tcp", jd->server)) ||
209                   (srvl = srv_lookup("jabber-client", "tcp", jd->server))) {
[ffdf2e7]210                /* Find the lowest-priority one. These usually come
211                   back in random/shuffled order. Not looking at
212                   weights etc for now. */
213                srv = *srvl;
[5ebff60]214                for (i = 1; srvl[i]; i++) {
215                        if (srvl[i]->prio < srv->prio) {
[ffdf2e7]216                                srv = srvl[i];
[5ebff60]217                        }
218                }
219
[36e9f62]220                connect_to = srv->name;
[5ebff60]221        } else {
[36e9f62]222                connect_to = jd->server;
[5ebff60]223        }
224
225        imcb_log(ic, "Connecting");
226
227        for (i = 0; jabber_port_list[i] > 0; i++) {
228                if (set_getint(&acc->set, "port") == jabber_port_list[i]) {
[7f69740]229                        break;
[5ebff60]230                }
231        }
[7f69740]232
[5ebff60]233        if (jabber_port_list[i] == 0) {
234                imcb_log(ic, "Illegal port number");
235                imc_logout(ic, FALSE);
[0f4c1bb5]236                return;
237        }
[5ebff60]238
[36e9f62]239        /* For non-SSL connections we can try to use the port # from the SRV
240           reply, but let's not do that when using SSL, SSL usually runs on
241           non-standard ports... */
[5ebff60]242        if (set_getbool(&acc->set, "ssl")) {
243                jd->ssl = ssl_connect(connect_to, set_getint(&acc->set, "port"), set_getbool(&acc->set,
244                                                                                             "tls_verify"), jabber_connected_ssl,
245                                      ic);
246                jd->fd = jd->ssl ? ssl_getfd(jd->ssl) : -1;
247        } else {
248                jd->fd = proxy_connect(connect_to, srv ? srv->port : set_getint(&acc->set,
249                                                                                "port"), jabber_connected_plain, ic);
250        }
251        srv_free(srvl);
252
253        if (jd->fd == -1) {
254                imcb_error(ic, "Could not connect to server");
255                imc_logout(ic, TRUE);
256
[fb4ebcc5]257                return;
[35f6677]258        }
[5ebff60]259
260        if (set_getbool(&acc->set, "xmlconsole")) {
[a6df0b5]261                jd->flags |= JFLAG_XMLCONSOLE;
262                /* Shouldn't really do this at this stage already, maybe. But
263                   I think this shouldn't break anything. */
[5ebff60]264                imcb_add_buddy(ic, JABBER_XMLCONSOLE_HANDLE, NULL);
[a6df0b5]265        }
[5ebff60]266
267        jabber_generate_id_hash(jd);
[608f8cf]268}
269
[89d736a]270/* This generates an unfinished md5_state_t variable. Every time we generate
271   an ID, we finish the state by adding a sequence number and take the hash. */
[5ebff60]272static void jabber_generate_id_hash(struct jabber_data *jd)
[608f8cf]273{
[89d736a]274        md5_byte_t binbuf[4];
[608f8cf]275        char *s;
[5ebff60]276
277        md5_init(&jd->cached_id_prefix);
278        md5_append(&jd->cached_id_prefix, (unsigned char *) jd->username, strlen(jd->username));
279        md5_append(&jd->cached_id_prefix, (unsigned char *) jd->server, strlen(jd->server));
280        s = set_getstr(&jd->ic->acc->set, "resource");
281        md5_append(&jd->cached_id_prefix, (unsigned char *) s, strlen(s));
282        random_bytes(binbuf, 4);
283        md5_append(&jd->cached_id_prefix, binbuf, 4);
[f06894d]284}
285
[5ebff60]286static void jabber_logout(struct im_connection *ic)
[f06894d]287{
[0da65d5]288        struct jabber_data *jd = ic->proto_data;
[4ac647d]289
[5ebff60]290        while (jd->filetransfers) {
291                imcb_file_canceled(ic, (( struct jabber_transfer *) jd->filetransfers->data)->ft, "Logging out");
292        }
293
294        while (jd->streamhosts) {
[4ac647d]295                jabber_streamhost_t *sh = jd->streamhosts->data;
[5ebff60]296                jd->streamhosts = g_slist_remove(jd->streamhosts, sh);
297                g_free(sh->jid);
298                g_free(sh->host);
299                g_free(sh);
300        }
301
302        if (jd->fd >= 0) {
303                jabber_end_stream(ic);
304        }
305
306        while (ic->groupchats) {
307                jabber_chat_free(ic->groupchats->data);
308        }
309
310        if (jd->r_inpa >= 0) {
311                b_event_remove(jd->r_inpa);
312        }
313        if (jd->w_inpa >= 0) {
314                b_event_remove(jd->w_inpa);
315        }
316
317        if (jd->ssl) {
318                ssl_disconnect(jd->ssl);
319        }
320        if (jd->fd >= 0) {
321                closesocket(jd->fd);
322        }
323
324        if (jd->tx_len) {
325                g_free(jd->txq);
326        }
327
328        if (jd->node_cache) {
329                g_hash_table_destroy(jd->node_cache);
330        }
331
332        jabber_buddy_remove_all(ic);
333
334        xt_free(jd->xt);
335
336        md5_free(&jd->cached_id_prefix);
337
338        g_free(jd->oauth2_access_token);
339        g_free(jd->away_message);
340        g_free(jd->internal_jid);
341        g_free(jd->username);
342        g_free(jd->me);
343        g_free(jd);
344
345        jabber_connections = g_slist_remove(jabber_connections, ic);
[f06894d]346}
347
[5ebff60]348static int jabber_buddy_msg(struct im_connection *ic, char *who, char *message, int flags)
[f06894d]349{
[0da65d5]350        struct jabber_data *jd = ic->proto_data;
[a21a8ac]351        struct jabber_buddy *bud;
[cc2cb2d]352        struct xt_node *node;
[b9f8b87]353        char *s;
[4a0614e]354        int st;
[5ebff60]355
356        if (g_strcasecmp(who, JABBER_XMLCONSOLE_HANDLE) == 0) {
357                return jabber_write(ic, message, strlen(message));
358        }
359
360        if (g_strcasecmp(who, JABBER_OAUTH_HANDLE) == 0 &&
361            !(jd->flags & OPT_LOGGED_IN) && jd->fd == -1) {
362                if (sasl_oauth2_get_refresh_token(ic, message)) {
[4a5d885]363                        return 1;
[5ebff60]364                } else {
365                        imcb_error(ic, "OAuth failure");
366                        imc_logout(ic, TRUE);
[911d97a]367                        return 0;
[4a5d885]368                }
369        }
[5ebff60]370
371        if ((s = strchr(who, '=')) && jabber_chat_by_jid(ic, s + 1)) {
372                bud = jabber_buddy_by_ext_jid(ic, who, 0);
373        } else {
374                bud = jabber_buddy_by_jid(ic, who, GET_BUDDY_BARE_OK);
375        }
376
377        node = xt_new_node("body", message, NULL);
378        node = jabber_make_packet("message", "chat", bud ? bud->full_jid : who, node);
379
380        if (bud && (jd->flags & JFLAG_WANT_TYPING) &&
381            ((bud->flags & JBFLAG_DOES_XEP85) ||
382             !(bud->flags & JBFLAG_PROBED_XEP85))) {
[a21a8ac]383                struct xt_node *act;
[5ebff60]384
[a21a8ac]385                /* If the user likes typing notification and if we don't know
[788a1af]386                   (and didn't probe before) if this resource supports XEP85,
[abbd8ed]387                   include a probe in this packet now. Also, if we know this
388                   buddy does support XEP85, we have to send this <active/>
389                   tag to tell that the user stopped typing (well, that's what
390                   we guess when s/he pressed Enter...). */
[5ebff60]391                act = xt_new_node("active", NULL, NULL);
392                xt_add_attr(act, "xmlns", XMLNS_CHATSTATES);
393                xt_add_child(node, act);
394
[a21a8ac]395                /* Just make sure we do this only once. */
[788a1af]396                bud->flags |= JBFLAG_PROBED_XEP85;
[a21a8ac]397        }
[5ebff60]398
399        st = jabber_write_packet(ic, node);
400        xt_free_node(node);
401
[4a0614e]402        return st;
[f06894d]403}
404
[5ebff60]405static GList *jabber_away_states(struct im_connection *ic)
[dd788bb]406{
[5e202b0]407        static GList *l = NULL;
408        int i;
[5ebff60]409
410        if (l == NULL) {
411                for (i = 0; jabber_away_state_list[i].full_name; i++) {
412                        l = g_list_append(l, (void *) jabber_away_state_list[i].full_name);
413                }
414        }
415
[5e202b0]416        return l;
[dd788bb]417}
418
[5ebff60]419static void jabber_get_info(struct im_connection *ic, char *who)
[038d17f]420{
[6a1128d]421        struct jabber_buddy *bud;
[5ebff60]422
423        bud = jabber_buddy_by_jid(ic, who, GET_BUDDY_FIRST);
424
425        while (bud) {
426                imcb_log(ic, "Buddy %s (%d) information:", bud->full_jid, bud->priority);
427                if (bud->away_state) {
428                        imcb_log(ic, "Away state: %s", bud->away_state->full_name);
429                }
430                imcb_log(ic, "Status message: %s", bud->away_message ? bud->away_message : "(none)");
431
[6a1128d]432                bud = bud->next;
433        }
[5ebff60]434
435        jabber_get_vcard(ic, bud ? bud->full_jid : who);
[038d17f]436}
437
[5ebff60]438static void jabber_set_away(struct im_connection *ic, char *state_txt, char *message)
[dd788bb]439{
[0da65d5]440        struct jabber_data *jd = ic->proto_data;
[5ebff60]441
[840bba8]442        /* state_txt == NULL -> Not away.
443           Unknown state -> fall back to the first defined away state. */
[5ebff60]444        if (state_txt == NULL) {
[daae10f]445                jd->away_state = NULL;
[5ebff60]446        } else if ((jd->away_state = jabber_away_state_by_name(state_txt)) == NULL) {
[daae10f]447                jd->away_state = jabber_away_state_list;
[5ebff60]448        }
449
450        g_free(jd->away_message);
451        jd->away_message = (message && *message) ? g_strdup(message) : NULL;
452
453        presence_send_update(ic);
[deff040]454}
455
[5ebff60]456static void jabber_add_buddy(struct im_connection *ic, char *who, char *group)
[cfbb3a6]457{
[bb95d43]458        struct jabber_data *jd = ic->proto_data;
[5ebff60]459
460        if (g_strcasecmp(who, JABBER_XMLCONSOLE_HANDLE) == 0) {
[bb95d43]461                jd->flags |= JFLAG_XMLCONSOLE;
[5ebff60]462                imcb_add_buddy(ic, JABBER_XMLCONSOLE_HANDLE, NULL);
[bb95d43]463                return;
464        }
[5ebff60]465
466        if (jabber_add_to_roster(ic, who, NULL, group)) {
467                presence_send_request(ic, who, "subscribe");
468        }
[cfbb3a6]469}
470
[5ebff60]471static void jabber_remove_buddy(struct im_connection *ic, char *who, char *group)
[cfbb3a6]472{
[bb95d43]473        struct jabber_data *jd = ic->proto_data;
[5ebff60]474
475        if (g_strcasecmp(who, JABBER_XMLCONSOLE_HANDLE) == 0) {
[bb95d43]476                jd->flags &= ~JFLAG_XMLCONSOLE;
[a3d5766]477                /* Not necessary for now. And for now the code isn't too
478                   happy if the buddy is completely gone right after calling
479                   this function already.
[998b103]480                imcb_remove_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL );
[a3d5766]481                */
[bb95d43]482                return;
483        }
[5ebff60]484
[788a1af]485        /* We should always do this part. Clean up our administration a little bit. */
[5ebff60]486        jabber_buddy_remove_bare(ic, who);
487
488        if (jabber_remove_from_roster(ic, who)) {
489                presence_send_request(ic, who, "unsubscribe");
490        }
[cfbb3a6]491}
492
[5ebff60]493static struct groupchat *jabber_chat_join_(struct im_connection *ic, const char *room, const char *nick,
494                                           const char *password, set_t **sets)
[e35d1a1]495{
[441a67e]496        struct jabber_data *jd = ic->proto_data;
[fcb2c2e]497        char *final_nick;
[5ebff60]498
[fcb2c2e]499        /* Ignore the passed nick parameter if we have our own default */
[5ebff60]500        if (!(final_nick = set_getstr(sets, "nick")) &&
501            !(final_nick = set_getstr(&ic->acc->set, "display_name"))) {
[fcb2c2e]502                /* Well, whatever, actually use the provided default, then */
503                final_nick = (char *) nick;
504        }
505
[5ebff60]506        if (strchr(room, '@') == NULL) {
507                imcb_error(ic, "%s is not a valid Jabber room name. Maybe you mean %s@conference.%s?",
508                           room, room, jd->server);
509        } else if (jabber_chat_by_jid(ic, room)) {
510                imcb_error(ic, "Already present in chat `%s'", room);
511        } else {
512                return jabber_chat_join(ic, room, final_nick, set_getstr(sets, "password"));
513        }
514
[e35d1a1]515        return NULL;
516}
517
[5ebff60]518static struct groupchat *jabber_chat_with_(struct im_connection *ic, char *who)
[fc0640e]519{
[5ebff60]520        return jabber_chat_with(ic, who);
[fc0640e]521}
522
[5ebff60]523static void jabber_chat_msg_(struct groupchat *c, char *message, int flags)
[43671b9]524{
[5ebff60]525        if (c && message) {
526                jabber_chat_msg(c, message, flags);
527        }
[43671b9]528}
529
[5ebff60]530static void jabber_chat_topic_(struct groupchat *c, char *topic)
[ef5c185]531{
[5ebff60]532        if (c && topic) {
533                jabber_chat_topic(c, topic);
534        }
[ef5c185]535}
536
[5ebff60]537static void jabber_chat_leave_(struct groupchat *c)
[e35d1a1]538{
[5ebff60]539        if (c) {
540                jabber_chat_leave(c, NULL);
541        }
[e35d1a1]542}
543
[5ebff60]544static void jabber_chat_invite_(struct groupchat *c, char *who, char *msg)
[c058ff9]545{
[68286eb]546        struct jabber_data *jd = c->ic->proto_data;
[c058ff9]547        struct jabber_chat *jc = c->data;
548        gchar *msg_alt = NULL;
549
[5ebff60]550        if (msg == NULL) {
551                msg_alt = g_strdup_printf("%s invited you to %s", jd->me, jc->name);
552        }
553
554        if (c && who) {
555                jabber_chat_invite(c, who, msg ? msg : msg_alt);
556        }
557
558        g_free(msg_alt);
[c058ff9]559}
560
[5ebff60]561static void jabber_keepalive(struct im_connection *ic)
[deff040]562{
563        /* Just any whitespace character is enough as a keepalive for XMPP sessions. */
[5ebff60]564        if (!jabber_write(ic, "\n", 1)) {
[38ff846]565                return;
[5ebff60]566        }
567
[038d17f]568        /* This runs the garbage collection every minute, which means every packet
569           is in the cache for about a minute (which should be enough AFAIK). */
[5ebff60]570        jabber_cache_clean(ic);
[dd788bb]571}
572
[5ebff60]573static int jabber_send_typing(struct im_connection *ic, char *who, int typing)
[a21a8ac]574{
[0da65d5]575        struct jabber_data *jd = ic->proto_data;
[a21a8ac]576        struct jabber_buddy *bud;
[5ebff60]577
[a21a8ac]578        /* Enable typing notification related code from now. */
579        jd->flags |= JFLAG_WANT_TYPING;
[5ebff60]580
581        if ((bud = jabber_buddy_by_jid(ic, who, 0)) == NULL) {
[788a1af]582                /* Sending typing notifications to unknown buddies is
583                   unsupported for now. Shouldn't be a problem, I think. */
584                return 0;
585        }
[5ebff60]586
587        if (bud->flags & JBFLAG_DOES_XEP85) {
[a21a8ac]588                /* We're only allowed to send this stuff if we know the other
589                   side supports it. */
[5ebff60]590
[a21a8ac]591                struct xt_node *node;
592                char *type;
593                int st;
[5ebff60]594
595                if (typing & OPT_TYPING) {
[a21a8ac]596                        type = "composing";
[5ebff60]597                } else if (typing & OPT_THINKING) {
[df1fb67]598                        type = "paused";
[5ebff60]599                } else {
[df1fb67]600                        type = "active";
[5ebff60]601                }
602
603                node = xt_new_node(type, NULL, NULL);
604                xt_add_attr(node, "xmlns", XMLNS_CHATSTATES);
605                node = jabber_make_packet("message", "chat", bud->full_jid, node);
606
607                st = jabber_write_packet(ic, node);
608                xt_free_node(node);
609
[a21a8ac]610                return st;
611        }
[5ebff60]612
[a21a8ac]613        return 1;
614}
615
[5ebff60]616void jabber_chat_add_settings(account_t *acc, set_t **head)
[6d544a1]617{
618        /* Meh. Stupid room passwords. Not trying to obfuscate/hide
619           them from the user for now. */
[5ebff60]620        set_add(head, "password", NULL, NULL, NULL);
[6d544a1]621}
622
[5ebff60]623void jabber_chat_free_settings(account_t *acc, set_t **head)
[6d544a1]624{
[5ebff60]625        set_del(head, "password");
[6d544a1]626}
627
[5ebff60]628GList *jabber_buddy_action_list(bee_user_t *bu)
[d88c92a]629{
630        static GList *ret = NULL;
[5ebff60]631
632        if (ret == NULL) {
[a97a336]633                static const struct buddy_action ba[2] = {
[d88c92a]634                        { "VERSION", "Get client (version) information" },
635                };
[5ebff60]636
637                ret = g_list_prepend(ret, (void *) ba + 0);
[d88c92a]638        }
[5ebff60]639
[d88c92a]640        return ret;
641}
642
[5ebff60]643void *jabber_buddy_action(struct bee_user *bu, const char *action, char * const args[], void *data)
[d88c92a]644{
[5ebff60]645        if (g_strcasecmp(action, "VERSION") == 0) {
[d88c92a]646                struct jabber_buddy *bud;
[5ebff60]647
648                if ((bud = jabber_buddy_by_ext_jid(bu->ic, bu->handle, 0)) == NULL) {
649                        bud = jabber_buddy_by_jid(bu->ic, bu->handle, GET_BUDDY_FIRST);
650                }
651                for (; bud; bud = bud->next) {
652                        jabber_iq_version_send(bu->ic, bud, data);
653                }
[d88c92a]654        }
[5ebff60]655
[d88c92a]656        return NULL;
657}
658
[5ebff60]659gboolean jabber_handle_is_self(struct im_connection *ic, const char *who)
660{
[be1efa3]661        struct jabber_data *jd = ic->proto_data;
[5ebff60]662
663        return ((g_strcasecmp(who, ic->acc->user) == 0) ||
664                (jd->internal_jid &&
665                 g_strcasecmp(who, jd->internal_jid) == 0));
[be1efa3]666}
667
[0da65d5]668void jabber_initmodule()
[f06894d]669{
[7733b8c]670        int i;
671        struct prpl funcs;
672
673        memset(&funcs, 0, sizeof(funcs));
674
675        funcs.mms = 0;                        /* no limit */
676        funcs.login = jabber_login;
677        funcs.init = jabber_init;
678        funcs.logout = jabber_logout;
679        funcs.buddy_msg = jabber_buddy_msg;
680        funcs.away_states = jabber_away_states;
681        funcs.set_away = jabber_set_away;
682        funcs.get_info = jabber_get_info;
683        funcs.add_buddy = jabber_add_buddy;
684        funcs.remove_buddy = jabber_remove_buddy;
685        funcs.chat_msg = jabber_chat_msg_;
686        funcs.chat_topic = jabber_chat_topic_;
687        funcs.chat_invite = jabber_chat_invite_;
688        funcs.chat_leave = jabber_chat_leave_;
689        funcs.chat_join = jabber_chat_join_;
690        funcs.chat_with = jabber_chat_with_;
691        funcs.chat_add_settings = jabber_chat_add_settings;
692        funcs.chat_free_settings = jabber_chat_free_settings;
693        funcs.keepalive = jabber_keepalive;
694        funcs.send_typing = jabber_send_typing;
695        funcs.handle_cmp = g_strcasecmp;
696        funcs.handle_is_self = jabber_handle_is_self;
697        funcs.transfer_request = jabber_si_transfer_request;
698        funcs.buddy_action_list = jabber_buddy_action_list;
699        funcs.buddy_action = jabber_buddy_action;
700
701        for (i = 0; jabber_subproto_list[i].name; i++) {
702                struct prpl *subproto = g_memdup(&funcs, sizeof(funcs));
703                subproto->name = jabber_subproto_list[i].name;
704                subproto->data = &jabber_subproto_list[i];
705                register_protocol(subproto);
706        }
[f06894d]707}
Note: See TracBrowser for help on using the repository browser.