source: protocols/jabber/jabber.c @ 1675c0b

Last change on this file since 1675c0b was 1675c0b, checked in by / <>, at 2021-03-26T13:53:42Z

double free mayb?

  • Property mode set to 100644
File size: 22.8 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
[5ebff60]56static void jabber_init(account_t *acc)
[f06894d]57{
58        set_t *s;
[7f69740]59        char str[16];
[5ebff60]60
61        s = set_add(&acc->set, "activity_timeout", "600", set_eval_int, acc);
62
63        s = set_add(&acc->set, "display_name", NULL, NULL, acc);
64
65        g_snprintf(str, sizeof(str), "%d", jabber_port_list[0]);
66        s = set_add(&acc->set, "port", str, set_eval_int, acc);
[f06894d]67        s->flags |= ACC_SET_OFFLINE_ONLY;
[1c3008a]68
[5ebff60]69        s = set_add(&acc->set, "priority", "0", set_eval_priority, acc);
70
71        s = set_add(&acc->set, "proxy", "<local>;<auto>", NULL, acc);
72
73        s = set_add(&acc->set, "resource", "BitlBee", NULL, acc);
[ebe7b36]74        s->flags |= ACC_SET_OFFLINE_ONLY;
[5ebff60]75
76        s = set_add(&acc->set, "resource_select", "activity", NULL, acc);
77
78        s = set_add(&acc->set, "sasl", "true", set_eval_bool, acc);
[ce199b7]79        s->flags |= ACC_SET_OFFLINE_ONLY | SET_HIDDEN_DEFAULT;
[5ebff60]80
81        s = set_add(&acc->set, "server", NULL, set_eval_account, acc);
[bb5ce568]82        s->flags |= SET_NOSAVE | ACC_SET_OFFLINE_ONLY | SET_NULL_OK;
[5ebff60]83
[67ea361]84        set_add(&acc->set, "oauth", "false", set_eval_oauth, acc);
85
[71074ac]86        if (strcmp(acc->prpl->name, "hipchat") == 0) {
87                set_setstr(&acc->set, "server", "chat.hipchat.com");
88        } else {
[73dd021]89                /* this reuses set_eval_oauth, which clears the password */
90                set_add(&acc->set, "anonymous", "false", set_eval_oauth, acc);
[71074ac]91        }
92
[5ebff60]93        s = set_add(&acc->set, "ssl", "false", set_eval_bool, acc);
[f06894d]94        s->flags |= ACC_SET_OFFLINE_ONLY;
[5ebff60]95
96        s = set_add(&acc->set, "tls", "true", set_eval_tls, acc);
[f06894d]97        s->flags |= ACC_SET_OFFLINE_ONLY;
[5ebff60]98
99        s = set_add(&acc->set, "tls_verify", "true", set_eval_bool, acc);
[486ddb5]100        s->flags |= ACC_SET_OFFLINE_ONLY;
[34ded90]101
[5ebff60]102        s = set_add(&acc->set, "user_agent", "BitlBee", NULL, acc);
103
104        s = set_add(&acc->set, "xmlconsole", "false", set_eval_bool, acc);
[dd43c62]105
[b38f655]106        s = set_add(&acc->set, "mail_notifications", "false", set_eval_bool, acc);
[a6df0b5]107        s->flags |= ACC_SET_OFFLINE_ONLY;
[5ebff60]108
[faeb521]109        /* changing this is rarely needed so keeping it secret */
[b38f655]110        s = set_add(&acc->set, "mail_notifications_limit", "5", set_eval_int, acc);
[faeb521]111        s->flags |= SET_HIDDEN_DEFAULT;
112
[b38f655]113        s = set_add(&acc->set, "mail_notifications_handle", NULL, NULL, acc);
[dd43c62]114        s->flags |= ACC_SET_OFFLINE_ONLY | SET_NULL_OK;
115
[fa8f57b]116        s = set_add(&acc->set, "carbons", "true", set_eval_bool, acc);
117        s->flags |= ACC_SET_OFFLINE_ONLY;
118
[06eef80]119        acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE |
120                      ACC_FLAG_HANDLE_DOMAINS;
[f06894d]121}
122
[5ebff60]123static void jabber_generate_id_hash(struct jabber_data *jd);
[608f8cf]124
[5ebff60]125static void jabber_login(account_t *acc)
[f06894d]126{
[5ebff60]127        struct im_connection *ic = imcb_new(acc);
128        struct jabber_data *jd = g_new0(struct jabber_data, 1);
[4a5d885]129        char *s;
[5ebff60]130
[b5c8a34]131        /* For now this is needed in the _connected() handlers if using
132           GLib event handling, to make sure we're not handling events
133           on dead connections. */
[5ebff60]134        jabber_connections = g_slist_prepend(jabber_connections, ic);
135
[0da65d5]136        jd->ic = ic;
137        ic->proto_data = jd;
[5ebff60]138
139        jabber_set_me(ic, acc->user);
140
[de03374]141        jd->fd = jd->r_inpa = jd->w_inpa = -1;
[5ebff60]142
[71074ac]143        if (strcmp(acc->prpl->name, "hipchat") == 0) {
144                jd->flags |= JFLAG_HIPCHAT;
145        }
146
[5ebff60]147        if (jd->server == NULL) {
148                imcb_error(ic, "Incomplete account name (format it like <username@jabberserver.name>)");
149                imc_logout(ic, FALSE);
[21167d2]150                return;
151        }
[5ebff60]152
[9b02bab]153        if (strstr(jd->server, ".facebook.com")) {
154                imcb_error(ic, "Facebook's XMPP service is gone. Try this instead: https://wiki.bitlbee.org/HowtoFacebookMQTT");
155                imc_logout(ic, FALSE);
156                return;
157        }
158
[5ebff60]159        if ((s = strchr(jd->server, '/'))) {
[3b3cd693]160                *s = 0;
[5ebff60]161                set_setstr(&acc->set, "resource", s + 1);
162
[3b3cd693]163                /* Also remove the /resource from the original variable so we
164                   won't have to do this again every time. */
[5ebff60]165                s = strchr(acc->user, '/');
[3b3cd693]166                *s = 0;
167        }
[5ebff60]168
169        jd->node_cache = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, jabber_cache_entry_free);
170        jd->buddies = g_hash_table_new(g_str_hash, g_str_equal);
171
172        if (set_getbool(&acc->set, "oauth")) {
[e14b47b8]173                GSList *p_in = NULL;
174                const char *tok;
[5ebff60]175
[f138bd2]176                jd->fd = jd->r_inpa = jd->w_inpa = -1;
[5ebff60]177
[9b02bab]178                /* There are no other options atm, so assume google for everything
179                   Facebook and MSN XMPP used to be here. RIP. */
180                jd->oauth2_service = &oauth2_service_google;
[5ebff60]181
182                oauth_params_parse(&p_in, ic->acc->pass);
183
[64b6635]184                /* First see if we have a refresh token, in which case any
185                   access token we *might* have has probably expired already
186                   anyway. */
[5ebff60]187                if ((tok = oauth_params_get(&p_in, "refresh_token"))) {
188                        sasl_oauth2_refresh(ic, tok);
[64b6635]189                }
190                /* If we don't have a refresh token, let's hope the access
191                   token is still usable. */
[5ebff60]192                else if ((tok = oauth_params_get(&p_in, "access_token"))) {
193                        jd->oauth2_access_token = g_strdup(tok);
194                        jabber_connect(ic);
[64b6635]195                }
196                /* If we don't have any, start the OAuth process now. Don't
197                   even open an XMPP connection yet. */
[5ebff60]198                else {
199                        sasl_oauth2_init(ic);
[f988ad3]200                        ic->flags |= OPT_SLOW_LOGIN;
201                }
[5ebff60]202
203                oauth_params_free(&p_in);
204        } else {
205                jabber_connect(ic);
[4a5d885]206        }
207}
208
[3314ced]209static void jabber_xmlconsole_enable(struct im_connection *ic)
210{
211        struct jabber_data *jd = ic->proto_data;
212        const char *handle = JABBER_XMLCONSOLE_HANDLE;
213        bee_user_t *bu;
214       
215        jd->flags |= JFLAG_XMLCONSOLE;
216
217        if (!(bu = bee_user_by_handle(ic->bee, ic, handle))) {
218                bu = bee_user_new(ic->bee, ic, handle, 0);
219                bu->flags |= BEE_USER_NOOTR;
220        }
221}
222
[4a5d885]223/* Separate this from jabber_login() so we can do OAuth first if necessary.
224   Putting this in io.c would probably be more correct. */
[5ebff60]225void jabber_connect(struct im_connection *ic)
[4a5d885]226{
227        account_t *acc = ic->acc;
228        struct jabber_data *jd = ic->proto_data;
229        int i;
230        char *connect_to;
231        struct ns_srv_reply **srvl = NULL, *srv = NULL;
[5ebff60]232
[36e9f62]233        /* Figure out the hostname to connect to. */
[5ebff60]234        if (acc->server && *acc->server) {
[36e9f62]235                connect_to = acc->server;
[5ebff60]236        } else if ((srvl = srv_lookup("xmpp-client", "tcp", jd->server)) ||
237                   (srvl = srv_lookup("jabber-client", "tcp", jd->server))) {
[ffdf2e7]238                /* Find the lowest-priority one. These usually come
239                   back in random/shuffled order. Not looking at
240                   weights etc for now. */
241                srv = *srvl;
[5ebff60]242                for (i = 1; srvl[i]; i++) {
243                        if (srvl[i]->prio < srv->prio) {
[ffdf2e7]244                                srv = srvl[i];
[5ebff60]245                        }
246                }
247
[36e9f62]248                connect_to = srv->name;
[5ebff60]249        } else {
[36e9f62]250                connect_to = jd->server;
[5ebff60]251        }
252
253        imcb_log(ic, "Connecting");
254
255        for (i = 0; jabber_port_list[i] > 0; i++) {
256                if (set_getint(&acc->set, "port") == jabber_port_list[i]) {
[7f69740]257                        break;
[5ebff60]258                }
259        }
[7f69740]260
[5ebff60]261        if (jabber_port_list[i] == 0) {
262                imcb_log(ic, "Illegal port number");
263                imc_logout(ic, FALSE);
[0f4c1bb5]264                return;
265        }
[5ebff60]266
[36e9f62]267        /* For non-SSL connections we can try to use the port # from the SRV
268           reply, but let's not do that when using SSL, SSL usually runs on
269           non-standard ports... */
[5ebff60]270        if (set_getbool(&acc->set, "ssl")) {
271                jd->ssl = ssl_connect(connect_to, set_getint(&acc->set, "port"), set_getbool(&acc->set,
272                                                                                             "tls_verify"), jabber_connected_ssl,
273                                      ic);
274                jd->fd = jd->ssl ? ssl_getfd(jd->ssl) : -1;
275        } else {
276                jd->fd = proxy_connect(connect_to, srv ? srv->port : set_getint(&acc->set,
277                                                                                "port"), jabber_connected_plain, ic);
278        }
279        srv_free(srvl);
280
281        if (jd->fd == -1) {
282                imcb_error(ic, "Could not connect to server");
283                imc_logout(ic, TRUE);
284
[fb4ebcc5]285                return;
[35f6677]286        }
[5ebff60]287
288        if (set_getbool(&acc->set, "xmlconsole")) {
[3314ced]289                jabber_xmlconsole_enable(ic);
[a6df0b5]290        }
[b38f655]291
292        if (set_getbool(&acc->set, "mail_notifications")) {
293                /* It's gmail specific, but it checks for server support before enabling it */
[dd43c62]294                jd->flags |= JFLAG_GMAILNOTIFY;
[b38f655]295                if (set_getstr(&acc->set, "mail_notifications_handle")) {
296                        imcb_add_buddy(ic, set_getstr(&acc->set, "mail_notifications_handle"), NULL);
[dd43c62]297                }
298        }
[5ebff60]299
300        jabber_generate_id_hash(jd);
[608f8cf]301}
302
[89d736a]303/* This generates an unfinished md5_state_t variable. Every time we generate
304   an ID, we finish the state by adding a sequence number and take the hash. */
[5ebff60]305static void jabber_generate_id_hash(struct jabber_data *jd)
[608f8cf]306{
[89d736a]307        md5_byte_t binbuf[4];
[608f8cf]308        char *s;
[5ebff60]309
310        md5_init(&jd->cached_id_prefix);
311        md5_append(&jd->cached_id_prefix, (unsigned char *) jd->username, strlen(jd->username));
312        md5_append(&jd->cached_id_prefix, (unsigned char *) jd->server, strlen(jd->server));
313        s = set_getstr(&jd->ic->acc->set, "resource");
314        md5_append(&jd->cached_id_prefix, (unsigned char *) s, strlen(s));
315        random_bytes(binbuf, 4);
316        md5_append(&jd->cached_id_prefix, binbuf, 4);
[f06894d]317}
318
[5ebff60]319static void jabber_logout(struct im_connection *ic)
[f06894d]320{
[0da65d5]321        struct jabber_data *jd = ic->proto_data;
[4ac647d]322
[399d65a]323        imcb_chat_list_free(ic);
324
[5ebff60]325        while (jd->filetransfers) {
326                imcb_file_canceled(ic, (( struct jabber_transfer *) jd->filetransfers->data)->ft, "Logging out");
327        }
328
329        while (jd->streamhosts) {
[4ac647d]330                jabber_streamhost_t *sh = jd->streamhosts->data;
[5ebff60]331                jd->streamhosts = g_slist_remove(jd->streamhosts, sh);
[4c075d1]332//              g_free(sh->jid);
333//              g_free(sh->host);
[5ebff60]334                g_free(sh);
335        }
336
337        if (jd->fd >= 0) {
338                jabber_end_stream(ic);
339        }
340
341        while (ic->groupchats) {
342                jabber_chat_free(ic->groupchats->data);
343        }
344
345        if (jd->r_inpa >= 0) {
346                b_event_remove(jd->r_inpa);
347        }
348        if (jd->w_inpa >= 0) {
349                b_event_remove(jd->w_inpa);
350        }
351
352        if (jd->ssl) {
353                ssl_disconnect(jd->ssl);
354        }
355        if (jd->fd >= 0) {
[0db6618]356                proxy_disconnect(jd->fd);
[5ebff60]357        }
358
359        if (jd->tx_len) {
360                g_free(jd->txq);
361        }
362
363        if (jd->node_cache) {
364                g_hash_table_destroy(jd->node_cache);
365        }
366
[666722e]367        if (jd->buddies) {
368                jabber_buddy_remove_all(ic);
369        }
[5ebff60]370
371        xt_free(jd->xt);
372
373        md5_free(&jd->cached_id_prefix);
374
[262c3df]375//      g_free(jd->oauth2_access_token);
376//      g_free(jd->away_message);
377//      g_free(jd->internal_jid);
378//      g_free(jd->gmail_tid);
379//      g_free(jd->muc_host);
380//      g_free(jd->username);
381//      g_free(jd->me);
[4c075d1]382        g_free(jd);
[1675c0b]383        g_free(jd);
[5ebff60]384
385        jabber_connections = g_slist_remove(jabber_connections, ic);
[f06894d]386}
387
[5ebff60]388static int jabber_buddy_msg(struct im_connection *ic, char *who, char *message, int flags)
[f06894d]389{
[0da65d5]390        struct jabber_data *jd = ic->proto_data;
[a21a8ac]391        struct jabber_buddy *bud;
[cc2cb2d]392        struct xt_node *node;
[b9f8b87]393        char *s;
[4a0614e]394        int st;
[5ebff60]395
396        if (g_strcasecmp(who, JABBER_XMLCONSOLE_HANDLE) == 0) {
397                return jabber_write(ic, message, strlen(message));
398        }
399
400        if (g_strcasecmp(who, JABBER_OAUTH_HANDLE) == 0 &&
401            !(jd->flags & OPT_LOGGED_IN) && jd->fd == -1) {
[67ea361]402
403                if (jd->flags & JFLAG_HIPCHAT) {
404                        sasl_oauth2_got_token(ic, message, NULL, NULL);
405                        return 1;
406                } else if (sasl_oauth2_get_refresh_token(ic, message)) {
[4a5d885]407                        return 1;
[5ebff60]408                } else {
409                        imcb_error(ic, "OAuth failure");
410                        imc_logout(ic, TRUE);
[911d97a]411                        return 0;
[4a5d885]412                }
413        }
[5ebff60]414
415        if ((s = strchr(who, '=')) && jabber_chat_by_jid(ic, s + 1)) {
416                bud = jabber_buddy_by_ext_jid(ic, who, 0);
417        } else {
418                bud = jabber_buddy_by_jid(ic, who, GET_BUDDY_BARE_OK);
419        }
420
421        node = xt_new_node("body", message, NULL);
422        node = jabber_make_packet("message", "chat", bud ? bud->full_jid : who, node);
423
424        if (bud && (jd->flags & JFLAG_WANT_TYPING) &&
425            ((bud->flags & JBFLAG_DOES_XEP85) ||
426             !(bud->flags & JBFLAG_PROBED_XEP85))) {
[a21a8ac]427                struct xt_node *act;
[5ebff60]428
[a21a8ac]429                /* If the user likes typing notification and if we don't know
[788a1af]430                   (and didn't probe before) if this resource supports XEP85,
[abbd8ed]431                   include a probe in this packet now. Also, if we know this
432                   buddy does support XEP85, we have to send this <active/>
433                   tag to tell that the user stopped typing (well, that's what
434                   we guess when s/he pressed Enter...). */
[5ebff60]435                act = xt_new_node("active", NULL, NULL);
436                xt_add_attr(act, "xmlns", XMLNS_CHATSTATES);
437                xt_add_child(node, act);
438
[a21a8ac]439                /* Just make sure we do this only once. */
[788a1af]440                bud->flags |= JBFLAG_PROBED_XEP85;
[a21a8ac]441        }
[5ebff60]442
[3a620ff]443        /* XEP-0364 suggests we add message processing hints (XEP-0334) to OTR messages,
444           mostly to avoid carbons (XEP-0280) and server-side message archiving.
445           OTR messages are roughly like this: /^\?OTR(.*\?| Error:|:)/
446           But I'm going to simplify it to messages starting with "?OTR". */
447        if (g_str_has_prefix(message, "?OTR")) {
448                int i;
449                char *hints[] = {
450                        "no-copy", XMLNS_HINTS,
451                        "no-permanent-store", XMLNS_HINTS,
452                        "private", XMLNS_CARBONS,
453                        NULL
454                };
455                       
456                for (i = 0; hints[i]; i += 2) {
457                        struct xt_node *hint;
458                        hint = xt_new_node(hints[i], NULL, NULL);
459                        xt_add_attr(hint, "xmlns", hints[i + 1]);
460                        xt_add_child(node, hint);
461                }
462        }
463
[5ebff60]464        st = jabber_write_packet(ic, node);
465        xt_free_node(node);
466
[4a0614e]467        return st;
[f06894d]468}
469
[5ebff60]470static GList *jabber_away_states(struct im_connection *ic)
[dd788bb]471{
[5e202b0]472        static GList *l = NULL;
473        int i;
[5ebff60]474
475        if (l == NULL) {
476                for (i = 0; jabber_away_state_list[i].full_name; i++) {
477                        l = g_list_append(l, (void *) jabber_away_state_list[i].full_name);
478                }
479        }
480
[5e202b0]481        return l;
[dd788bb]482}
483
[5ebff60]484static void jabber_get_info(struct im_connection *ic, char *who)
[038d17f]485{
[6a1128d]486        struct jabber_buddy *bud;
[5ebff60]487
488        bud = jabber_buddy_by_jid(ic, who, GET_BUDDY_FIRST);
489
490        while (bud) {
491                imcb_log(ic, "Buddy %s (%d) information:", bud->full_jid, bud->priority);
492                if (bud->away_state) {
493                        imcb_log(ic, "Away state: %s", bud->away_state->full_name);
494                }
495                imcb_log(ic, "Status message: %s", bud->away_message ? bud->away_message : "(none)");
496
[6a1128d]497                bud = bud->next;
498        }
[5ebff60]499
[5535a47]500        jabber_get_vcard(ic, who);
[038d17f]501}
502
[5ebff60]503static void jabber_set_away(struct im_connection *ic, char *state_txt, char *message)
[dd788bb]504{
[0da65d5]505        struct jabber_data *jd = ic->proto_data;
[5ebff60]506
[840bba8]507        /* state_txt == NULL -> Not away.
508           Unknown state -> fall back to the first defined away state. */
[5ebff60]509        if (state_txt == NULL) {
[daae10f]510                jd->away_state = NULL;
[5ebff60]511        } else if ((jd->away_state = jabber_away_state_by_name(state_txt)) == NULL) {
[daae10f]512                jd->away_state = jabber_away_state_list;
[5ebff60]513        }
514
515        g_free(jd->away_message);
516        jd->away_message = (message && *message) ? g_strdup(message) : NULL;
517
518        presence_send_update(ic);
[deff040]519}
520
[5ebff60]521static void jabber_add_buddy(struct im_connection *ic, char *who, char *group)
[cfbb3a6]522{
[5ebff60]523        if (g_strcasecmp(who, JABBER_XMLCONSOLE_HANDLE) == 0) {
[3314ced]524                jabber_xmlconsole_enable(ic);
[bb95d43]525                return;
526        }
[5ebff60]527
528        if (jabber_add_to_roster(ic, who, NULL, group)) {
529                presence_send_request(ic, who, "subscribe");
530        }
[cfbb3a6]531}
532
[5ebff60]533static void jabber_remove_buddy(struct im_connection *ic, char *who, char *group)
[cfbb3a6]534{
[bb95d43]535        struct jabber_data *jd = ic->proto_data;
[5ebff60]536
537        if (g_strcasecmp(who, JABBER_XMLCONSOLE_HANDLE) == 0) {
[bb95d43]538                jd->flags &= ~JFLAG_XMLCONSOLE;
[a3d5766]539                /* Not necessary for now. And for now the code isn't too
540                   happy if the buddy is completely gone right after calling
541                   this function already.
[998b103]542                imcb_remove_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL );
[a3d5766]543                */
[bb95d43]544                return;
545        }
[5ebff60]546
[788a1af]547        /* We should always do this part. Clean up our administration a little bit. */
[5ebff60]548        jabber_buddy_remove_bare(ic, who);
549
550        if (jabber_remove_from_roster(ic, who)) {
551                presence_send_request(ic, who, "unsubscribe");
552        }
[cfbb3a6]553}
554
[5ebff60]555static struct groupchat *jabber_chat_join_(struct im_connection *ic, const char *room, const char *nick,
556                                           const char *password, set_t **sets)
[e35d1a1]557{
[441a67e]558        struct jabber_data *jd = ic->proto_data;
[fcb2c2e]559        char *final_nick;
[5ebff60]560
[fcb2c2e]561        /* Ignore the passed nick parameter if we have our own default */
[5ebff60]562        if (!(final_nick = set_getstr(sets, "nick")) &&
563            !(final_nick = set_getstr(&ic->acc->set, "display_name"))) {
[fcb2c2e]564                /* Well, whatever, actually use the provided default, then */
565                final_nick = (char *) nick;
566        }
567
[9c8dbc7]568        if (jd->flags & JFLAG_HIPCHAT && jd->muc_host && !g_str_has_suffix(room, jd->muc_host)) {
569                char *guessed_name = hipchat_guess_channel_name(ic, room);
570                if (guessed_name) {
571                        set_setstr(sets, "room", guessed_name);
572                        g_free(guessed_name);
573
574                        /* call this same function again with the fixed name */
575                        return jabber_chat_join_(ic, set_getstr(sets, "room"), nick, password, sets);
576                }
577        }
578
[5ebff60]579        if (strchr(room, '@') == NULL) {
580                imcb_error(ic, "%s is not a valid Jabber room name. Maybe you mean %s@conference.%s?",
581                           room, room, jd->server);
582        } else {
[246b98b]583                struct groupchat *old;
584
585                if ((old = jabber_chat_by_jid(ic, room))) {
586                        imcb_log(ic, "Warning: Already present in chat `%s' - trying to join anyway", room);
587                        jabber_chat_free(old);
588                }
589
[9c8dbc7]590                /* jabber_chat_join without the underscore is the conference.c one */
[3320d6d]591                return jabber_chat_join(ic, room, final_nick, set_getstr(sets, "password"),
592                                        set_getbool(sets, "always_use_nicks"));
[5ebff60]593        }
594
[e35d1a1]595        return NULL;
596}
597
[5ebff60]598static struct groupchat *jabber_chat_with_(struct im_connection *ic, char *who)
[fc0640e]599{
[5ebff60]600        return jabber_chat_with(ic, who);
[fc0640e]601}
602
[399d65a]603static void jabber_chat_list_(struct im_connection *ic, const char *server)
604{
605        struct jabber_data *jd = ic->proto_data;
606
607        if (server && *server) {
608                jabber_iq_disco_muc(ic, server);
609        } else if (jd->muc_host && *jd->muc_host) {
610                jabber_iq_disco_muc(ic, jd->muc_host);
611        } else {
612                /* throw an error here, don't query conference.[server] directly.
613                 * for things like jabber.org it gets you 18000 results of garbage */
614                imcb_error(ic, "Please specify a server name such as `conference.%s'", jd->server);
615        }
616}
617
[5ebff60]618static void jabber_chat_msg_(struct groupchat *c, char *message, int flags)
[43671b9]619{
[5ebff60]620        if (c && message) {
621                jabber_chat_msg(c, message, flags);
622        }
[43671b9]623}
624
[5ebff60]625static void jabber_chat_topic_(struct groupchat *c, char *topic)
[ef5c185]626{
[5ebff60]627        if (c && topic) {
628                jabber_chat_topic(c, topic);
629        }
[ef5c185]630}
631
[5ebff60]632static void jabber_chat_leave_(struct groupchat *c)
[e35d1a1]633{
[5ebff60]634        if (c) {
635                jabber_chat_leave(c, NULL);
636        }
[e35d1a1]637}
638
[5ebff60]639static void jabber_chat_invite_(struct groupchat *c, char *who, char *msg)
[c058ff9]640{
[68286eb]641        struct jabber_data *jd = c->ic->proto_data;
[c058ff9]642        struct jabber_chat *jc = c->data;
643        gchar *msg_alt = NULL;
644
[5ebff60]645        if (msg == NULL) {
646                msg_alt = g_strdup_printf("%s invited you to %s", jd->me, jc->name);
647        }
648
649        if (c && who) {
650                jabber_chat_invite(c, who, msg ? msg : msg_alt);
651        }
652
653        g_free(msg_alt);
[c058ff9]654}
655
[5ebff60]656static void jabber_keepalive(struct im_connection *ic)
[deff040]657{
658        /* Just any whitespace character is enough as a keepalive for XMPP sessions. */
[5ebff60]659        if (!jabber_write(ic, "\n", 1)) {
[38ff846]660                return;
[5ebff60]661        }
662
[038d17f]663        /* This runs the garbage collection every minute, which means every packet
664           is in the cache for about a minute (which should be enough AFAIK). */
[5ebff60]665        jabber_cache_clean(ic);
[dd788bb]666}
667
[5ebff60]668static int jabber_send_typing(struct im_connection *ic, char *who, int typing)
[a21a8ac]669{
[0da65d5]670        struct jabber_data *jd = ic->proto_data;
[0c78bb7]671        struct jabber_buddy *bud, *bare;
[5ebff60]672
[a21a8ac]673        /* Enable typing notification related code from now. */
674        jd->flags |= JFLAG_WANT_TYPING;
[5ebff60]675
[0c78bb7]676        if ((bud = jabber_buddy_by_jid(ic, who, 0)) == NULL ||
677            (bare = jabber_buddy_by_jid(ic, who, GET_BUDDY_BARE)) == NULL) {
[788a1af]678                /* Sending typing notifications to unknown buddies is
679                   unsupported for now. Shouldn't be a problem, I think. */
680                return 0;
681        }
[5ebff60]682
[0c78bb7]683
684        if (bud->flags & JBFLAG_DOES_XEP85 || bare->flags & JBFLAG_DOES_XEP85) {
[a21a8ac]685                /* We're only allowed to send this stuff if we know the other
[0c78bb7]686                   side supports it. If the bare JID has the flag, all other
687                   resources get it, too (That is the case in gtalk) */
[5ebff60]688
[a21a8ac]689                struct xt_node *node;
690                char *type;
691                int st;
[5ebff60]692
693                if (typing & OPT_TYPING) {
[a21a8ac]694                        type = "composing";
[5ebff60]695                } else if (typing & OPT_THINKING) {
[df1fb67]696                        type = "paused";
[5ebff60]697                } else {
[df1fb67]698                        type = "active";
[5ebff60]699                }
700
701                node = xt_new_node(type, NULL, NULL);
702                xt_add_attr(node, "xmlns", XMLNS_CHATSTATES);
703                node = jabber_make_packet("message", "chat", bud->full_jid, node);
704
705                st = jabber_write_packet(ic, node);
706                xt_free_node(node);
707
[a21a8ac]708                return st;
709        }
[5ebff60]710
[a21a8ac]711        return 1;
712}
713
[5ebff60]714void jabber_chat_add_settings(account_t *acc, set_t **head)
[6d544a1]715{
[3320d6d]716        set_add(head, "always_use_nicks", "false", set_eval_bool, NULL);
717
[6d544a1]718        /* Meh. Stupid room passwords. Not trying to obfuscate/hide
719           them from the user for now. */
[5ebff60]720        set_add(head, "password", NULL, NULL, NULL);
[6d544a1]721}
722
[5ebff60]723void jabber_chat_free_settings(account_t *acc, set_t **head)
[6d544a1]724{
[3320d6d]725        set_del(head, "always_use_nicks");
726
[5ebff60]727        set_del(head, "password");
[6d544a1]728}
729
[5ebff60]730GList *jabber_buddy_action_list(bee_user_t *bu)
[d88c92a]731{
732        static GList *ret = NULL;
[5ebff60]733
734        if (ret == NULL) {
[a97a336]735                static const struct buddy_action ba[2] = {
[d88c92a]736                        { "VERSION", "Get client (version) information" },
737                };
[5ebff60]738
739                ret = g_list_prepend(ret, (void *) ba + 0);
[d88c92a]740        }
[5ebff60]741
[d88c92a]742        return ret;
743}
744
[5ebff60]745void *jabber_buddy_action(struct bee_user *bu, const char *action, char * const args[], void *data)
[d88c92a]746{
[5ebff60]747        if (g_strcasecmp(action, "VERSION") == 0) {
[d88c92a]748                struct jabber_buddy *bud;
[5ebff60]749
750                if ((bud = jabber_buddy_by_ext_jid(bu->ic, bu->handle, 0)) == NULL) {
751                        bud = jabber_buddy_by_jid(bu->ic, bu->handle, GET_BUDDY_FIRST);
752                }
753                for (; bud; bud = bud->next) {
754                        jabber_iq_version_send(bu->ic, bud, data);
755                }
[d88c92a]756        }
[5ebff60]757
[d88c92a]758        return NULL;
759}
760
[5ebff60]761gboolean jabber_handle_is_self(struct im_connection *ic, const char *who)
762{
[be1efa3]763        struct jabber_data *jd = ic->proto_data;
[5ebff60]764
765        return ((g_strcasecmp(who, ic->acc->user) == 0) ||
766                (jd->internal_jid &&
767                 g_strcasecmp(who, jd->internal_jid) == 0));
[be1efa3]768}
769
[0da65d5]770void jabber_initmodule()
[f06894d]771{
[5ebff60]772        struct prpl *ret = g_new0(struct prpl, 1);
[71074ac]773        struct prpl *hipchat = NULL;
[5ebff60]774
[f06894d]775        ret->name = "jabber";
[6d544a1]776        ret->mms = 0;                        /* no limit */
[f06894d]777        ret->login = jabber_login;
[0da65d5]778        ret->init = jabber_init;
779        ret->logout = jabber_logout;
[f6c963b]780        ret->buddy_msg = jabber_buddy_msg;
[dd788bb]781        ret->away_states = jabber_away_states;
782        ret->set_away = jabber_set_away;
[f06894d]783//      ret->set_info = jabber_set_info;
[038d17f]784        ret->get_info = jabber_get_info;
[cfbb3a6]785        ret->add_buddy = jabber_add_buddy;
786        ret->remove_buddy = jabber_remove_buddy;
[43671b9]787        ret->chat_msg = jabber_chat_msg_;
[ef5c185]788        ret->chat_topic = jabber_chat_topic_;
[c058ff9]789        ret->chat_invite = jabber_chat_invite_;
[e35d1a1]790        ret->chat_leave = jabber_chat_leave_;
791        ret->chat_join = jabber_chat_join_;
[fc0640e]792        ret->chat_with = jabber_chat_with_;
[399d65a]793        ret->chat_list = jabber_chat_list_;
[6d544a1]794        ret->chat_add_settings = jabber_chat_add_settings;
795        ret->chat_free_settings = jabber_chat_free_settings;
[deff040]796        ret->keepalive = jabber_keepalive;
[a21a8ac]797        ret->send_typing = jabber_send_typing;
[f06894d]798        ret->handle_cmp = g_strcasecmp;
[be1efa3]799        ret->handle_is_self = jabber_handle_is_self;
[2ff2076]800        ret->transfer_request = jabber_si_transfer_request;
[d88c92a]801        ret->buddy_action_list = jabber_buddy_action_list;
802        ret->buddy_action = jabber_buddy_action;
[f06894d]803
[5ebff60]804        register_protocol(ret);
[71074ac]805
806        /* Another one for hipchat, which has completely different logins */
807        hipchat = g_memdup(ret, sizeof(struct prpl));
808        hipchat->name = "hipchat";
809        register_protocol(hipchat);
[f06894d]810}
Note: See TracBrowser for help on using the repository browser.