source: protocols/jabber/jabber.c

Last change on this file was 552da22, checked in by GitHub <noreply@…>, at 2023-04-01T20:29:42Z

Use g_memdup2() with glib versions >= 2.68.0 (#168)

  • Use g_memdup2() with glib versions >= 2.68.0

g_memdup() was deprecated in glib 2.68.0. The new function is
g_memdup2(). Still support building with older versions of glib via
macros.

Signed-off-by: David Cantrell <dcantrell@…>

  • Fall back to g_memdup when g_memdup2 is not available, rather than defining custom macro

Signed-off-by: David Cantrell <dcantrell@…>
Co-authored-by: Jelmer Vernooij <jelmer@…>

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