source: protocols/account.c @ 921ea8b

Last change on this file since 921ea8b was 7801298, checked in by dequis <dx@…>, at 2016-12-27T17:24:50Z

Per-account handle_unknown

Credit for the idea goes to russian XMPP spammers. Thanks!

  • Property mode set to 100644
File size: 11.2 KB
RevLine 
[5ebff60]1/********************************************************************\
[b7d3cc34]2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[0e788f5]4  * Copyright 2002-2013 Wilmer van der Gaast and others                *
[b7d3cc34]5  \********************************************************************/
6
7/* Account management functions                                         */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
[6f10697]22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
[b7d3cc34]24*/
25
26#define BITLBEE_CORE
27#include "bitlbee.h"
28#include "account.h"
29
[1783ab6]30static const char* account_protocols_local[] = {
[11e7828]31        "gg", "whatsapp", NULL
[1783ab6]32};
33
[5ebff60]34static char *set_eval_nick_source(set_t *set, char *value);
[06b39f2]35
[5ebff60]36account_t *account_add(bee_t *bee, struct prpl *prpl, char *user, char *pass)
[b7d3cc34]37{
38        account_t *a;
[5100caa]39        set_t *s;
[5ebff60]40        char tag[strlen(prpl->name) + 10];
41
42        if (bee->accounts) {
43                for (a = bee->accounts; a->next; a = a->next) {
44                        ;
45                }
46                a = a->next = g_new0(account_t, 1);
47        } else {
48                bee->accounts = a = g_new0(account_t, 1);
[b7d3cc34]49        }
[5ebff60]50
[7b23afd]51        a->prpl = prpl;
[5ebff60]52        a->user = g_strdup(user);
53        a->pass = g_strdup(pass);
[2b14eef]54        a->auto_connect = 1;
[81e04e1]55        a->bee = bee;
[5ebff60]56
57        s = set_add(&a->set, "auto_connect", "true", set_eval_account, a);
[bb5ce568]58        s->flags |= SET_NOSAVE;
[5ebff60]59
60        s = set_add(&a->set, "auto_reconnect", "true", set_eval_bool, a);
61
[7801298]62        s = set_add(&a->set, "handle_unknown", NULL, NULL, a);
63        s->flags |= SET_NULL_OK;
64
[5ebff60]65        s = set_add(&a->set, "nick_format", NULL, NULL, a);
[badd148]66        s->flags |= SET_NULL_OK;
[5ebff60]67
68        s = set_add(&a->set, "nick_source", "handle", set_eval_nick_source, a);
[bb5ce568]69        s->flags |= SET_NOSAVE; /* Just for bw compatibility! */
[5ebff60]70
71        s = set_add(&a->set, "password", NULL, set_eval_account, a);
[3ac6d9f]72        s->flags |= SET_NOSAVE | SET_NULL_OK | SET_PASSWORD | ACC_SET_LOCKABLE;
[5ebff60]73
74        s = set_add(&a->set, "tag", NULL, set_eval_account, a);
[bb5ce568]75        s->flags |= SET_NOSAVE;
[5ebff60]76
77        s = set_add(&a->set, "username", NULL, set_eval_account, a);
[3ac6d9f]78        s->flags |= SET_NOSAVE | ACC_SET_OFFLINE_ONLY | ACC_SET_LOCKABLE;
[5ebff60]79        set_setstr(&a->set, "username", user);
80
[5a8afc3]81        if (prpl == &protocol_missing) {
82                s = set_add(&a->set, "server", NULL, set_eval_account, a);
83                s->flags |= SET_NOSAVE | SET_HIDDEN | ACC_SET_OFFLINE_ONLY | ACC_SET_ONLINE_ONLY;
84        }
85
[3b3c50d9]86        /* Hardcode some more clever tag guesses. */
[5ebff60]87        strcpy(tag, prpl->name);
88        if (strcmp(prpl->name, "oscar") == 0) {
89                if (g_ascii_isdigit(a->user[0])) {
90                        strcpy(tag, "icq");
91                } else {
92                        strcpy(tag, "aim");
93                }
94        } else if (strcmp(prpl->name, "jabber") == 0) {
95                if (strstr(a->user, "@gmail.com") ||
96                    strstr(a->user, "@googlemail.com")) {
97                        strcpy(tag, "gtalk");
98                }
[3b3c50d9]99        }
[5ebff60]100
101        if (account_by_tag(bee, tag)) {
102                char *numpos = tag + strlen(tag);
[40e6dac]103                int i;
104
[5ebff60]105                for (i = 2; i < 10000; i++) {
106                        sprintf(numpos, "%d", i);
107                        if (!account_by_tag(bee, tag)) {
[40e6dac]108                                break;
[5ebff60]109                        }
[40e6dac]110                }
111        }
[5ebff60]112        set_setstr(&a->set, "tag", tag);
113
114        a->nicks = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
115
[96863f6]116        /* This function adds some more settings (and might want to do more
117           things that have to be done now, although I can't think of anything. */
[5ebff60]118        if (prpl->init) {
119                prpl->init(a);
120        }
121
122        s = set_add(&a->set, "away", NULL, set_eval_account, a);
[58adb7e]123        s->flags |= SET_NULL_OK;
[5ebff60]124
125        if (a->flags & ACC_FLAG_STATUS_MESSAGE) {
126                s = set_add(&a->set, "status", NULL, set_eval_account, a);
[58adb7e]127                s->flags |= SET_NULL_OK;
128        }
[5ebff60]129
[58adb7e]130        return a;
[b7d3cc34]131}
132
[5ebff60]133char *set_eval_account(set_t *set, char *value)
[5100caa]134{
135        account_t *acc = set->data;
[5ebff60]136
[5100caa]137        /* Double-check: We refuse to edit on-line accounts. */
[5ebff60]138        if (set->flags & ACC_SET_OFFLINE_ONLY && acc->ic) {
[7125cb3]139                return SET_INVALID;
[5ebff60]140        }
141
142        if (strcmp(set->key, "server") == 0) {
143                g_free(acc->server);
144                if (value && *value) {
145                        acc->server = g_strdup(value);
[30ce1ce]146                        return value;
[5ebff60]147                } else {
148                        acc->server = g_strdup(set->def);
149                        return g_strdup(set->def);
[30ce1ce]150                }
[5ebff60]151        } else if (strcmp(set->key, "username") == 0) {
152                g_free(acc->user);
153                acc->user = g_strdup(value);
[3b32017]154                return value;
[5ebff60]155        } else if (strcmp(set->key, "password") == 0) {
[35987a1]156                /* set -del allows /oper to be used to change the password or,
157                   iff oauth is enabled, reset the oauth credential magic.
158                */
[5ebff60]159                if (!value) {
160                        if (set_getbool(&(acc->set), "oauth")) {
[35987a1]161                                value = "";
162                        } else {
163                                value = PASSWORD_PENDING;
[5ebff60]164                                ((irc_t *) acc->bee->ui_data)->status |= OPER_HACK_ACCOUNT_PASSWORD;
165                                irc_rootmsg((irc_t *) acc->bee->ui_data, "You may now use /OPER to set the password");
[35987a1]166                        }
167                }
[5ebff60]168
169                g_free(acc->pass);
170                acc->pass = g_strdup(value);
171                return NULL;    /* password shouldn't be visible in plaintext! */
172        } else if (strcmp(set->key, "tag") == 0) {
[40e6dac]173                account_t *oa;
[5ebff60]174
[40e6dac]175                /* Enforce uniqueness. */
[5ebff60]176                if ((oa = account_by_tag(acc->bee, value)) && oa != acc) {
[40e6dac]177                        return SET_INVALID;
[5ebff60]178                }
179
180                g_free(acc->tag);
181                acc->tag = g_strdup(value);
[40e6dac]182                return value;
[5ebff60]183        } else if (strcmp(set->key, "auto_connect") == 0) {
184                if (!is_bool(value)) {
[7125cb3]185                        return SET_INVALID;
[5ebff60]186                }
187
188                acc->auto_connect = bool2int(value);
[5100caa]189                return value;
[5ebff60]190        } else if (strcmp(set->key, "away") == 0 ||
191                   strcmp(set->key, "status") == 0) {
192                if (acc->ic && acc->ic->flags & OPT_LOGGED_IN) {
[58adb7e]193                        /* If we're currently on-line, set the var now already
194                           (bit of a hack) and send an update. */
[5ebff60]195                        g_free(set->value);
196                        set->value = g_strdup(value);
197
198                        imc_away_send_update(acc->ic);
[58adb7e]199                }
[5ebff60]200
[58adb7e]201                return value;
202        }
[5ebff60]203
[7125cb3]204        return SET_INVALID;
[5100caa]205}
206
[06b39f2]207/* For bw compatibility, have this write-only setting. */
[5ebff60]208static char *set_eval_nick_source(set_t *set, char *value)
[06b39f2]209{
210        account_t *a = set->data;
[5ebff60]211
212        if (strcmp(value, "full_name") == 0) {
213                set_setstr(&a->set, "nick_format", "%full_name");
214        } else if (strcmp(value, "first_name") == 0) {
215                set_setstr(&a->set, "nick_format", "%first_name");
216        } else {
217                set_setstr(&a->set, "nick_format", "%-@nick");
218        }
219
[06b39f2]220        return value;
221}
222
[5ebff60]223account_t *account_get(bee_t *bee, const char *id)
[b7d3cc34]224{
225        account_t *a, *ret = NULL;
[85616c3]226        char *handle, *s;
[b7d3cc34]227        int nr;
[5ebff60]228
[40e6dac]229        /* Tags get priority above anything else. */
[5ebff60]230        if ((a = account_by_tag(bee, id))) {
[40e6dac]231                return a;
[5ebff60]232        }
233
[85616c3]234        /* This checks if the id string ends with (...) */
[5ebff60]235        if ((handle = strchr(id, '(')) && (s = strchr(handle, ')')) && s[1] == 0) {
[85616c3]236                struct prpl *proto;
[5ebff60]237
[85616c3]238                *s = *handle = 0;
[5ebff60]239                handle++;
240
241                if ((proto = find_protocol(id))) {
242                        for (a = bee->accounts; a; a = a->next) {
243                                if (a->prpl == proto &&
244                                    a->prpl->handle_cmp(handle, a->user) == 0) {
[85616c3]245                                        ret = a;
[5ebff60]246                                }
247                        }
[85616c3]248                }
[5ebff60]249
[85616c3]250                /* Restore the string. */
[5ebff60]251                handle--;
[85616c3]252                *handle = '(';
253                *s = ')';
[5ebff60]254
255                if (ret) {
[85616c3]256                        return ret;
[5ebff60]257                }
[85616c3]258        }
[5ebff60]259
260        if (sscanf(id, "%d", &nr) == 1 && nr < 1000) {
261                for (a = bee->accounts; a; a = a->next) {
262                        if ((nr--) == 0) {
263                                return(a);
264                        }
265                }
266
267                return(NULL);
[b7d3cc34]268        }
[5ebff60]269
270        for (a = bee->accounts; a; a = a->next) {
271                if (g_strcasecmp(id, a->prpl->name) == 0) {
272                        if (!ret) {
[b7d3cc34]273                                ret = a;
[5ebff60]274                        } else {
275                                return(NULL);   /* We don't want to match more than one... */
276                        }
277                } else if (strstr(a->user, id)) {
278                        if (!ret) {
[b7d3cc34]279                                ret = a;
[5ebff60]280                        } else {
281                                return(NULL);
282                        }
[b7d3cc34]283                }
284        }
[5ebff60]285
286        return(ret);
[b7d3cc34]287}
288
[5ebff60]289account_t *account_by_tag(bee_t *bee, const char *tag)
[40e6dac]290{
291        account_t *a;
[5ebff60]292
293        for (a = bee->accounts; a; a = a->next) {
294                if (a->tag && g_strcasecmp(tag, a->tag) == 0) {
[40e6dac]295                        return a;
[5ebff60]296                }
297        }
298
[40e6dac]299        return NULL;
300}
301
[5ebff60]302void account_del(bee_t *bee, account_t *acc)
[b7d3cc34]303{
304        account_t *a, *l = NULL;
[5ebff60]305
306        if (acc->ic) {
[fa75134]307                /* Caller should have checked, accounts still in use can't be deleted. */
308                return;
[5ebff60]309        }
310
311        for (a = bee->accounts; a; a = (l = a)->next) {
312                if (a == acc) {
313                        if (l) {
[b7d3cc34]314                                l->next = a->next;
[5ebff60]315                        } else {
[81e04e1]316                                bee->accounts = a->next;
[5ebff60]317                        }
318
[81e04e1]319                        /** FIXME
320                        for( c = bee->chatrooms; c; c = nc )
[f86a3d5]321                        {
[5ebff60]322                                nc = c->next;
323                                if( acc == c->acc )
324                                        chat_del( bee, c );
[f86a3d5]325                        }
[81e04e1]326                        */
[5ebff60]327
328                        while (a->set) {
329                                set_del(&a->set, a->set->key);
330                        }
331
332                        g_hash_table_destroy(a->nicks);
333
334                        g_free(a->tag);
335                        g_free(a->user);
336                        g_free(a->pass);
337                        g_free(a->server);
338                        if (a->reconnect) {     /* This prevents any reconnect still queued to happen */
339                                cancel_auto_reconnect(a);
340                        }
341                        g_free(a);
342
[b7d3cc34]343                        break;
344                }
[5ebff60]345        }
[b7d3cc34]346}
347
[5ebff60]348static gboolean account_on_timeout(gpointer d, gint fd, b_input_condition cond);
[748bcdd]349
[5ebff60]350void account_on(bee_t *bee, account_t *a)
[b7d3cc34]351{
[5ebff60]352        if (a->ic) {
[b7d3cc34]353                /* Trying to enable an already-enabled account */
354                return;
355        }
[5ebff60]356
357        cancel_auto_reconnect(a);
358
[b7d3cc34]359        a->reconnect = 0;
[5ebff60]360        a->prpl->login(a);
361
362        if (a->ic && !(a->ic->flags & (OPT_SLOW_LOGIN | OPT_LOGGED_IN))) {
363                a->ic->keepalive = b_timeout_add(120000, account_on_timeout, a->ic);
364        }
[b7d3cc34]365}
366
[5ebff60]367void account_off(bee_t *bee, account_t *a)
[b7d3cc34]368{
[5ebff60]369        imc_logout(a->ic, FALSE);
[0da65d5]370        a->ic = NULL;
[5ebff60]371        if (a->reconnect) {
[b7d3cc34]372                /* Shouldn't happen */
[5ebff60]373                cancel_auto_reconnect(a);
[b7d3cc34]374        }
375}
[280e655]376
[5ebff60]377static gboolean account_on_timeout(gpointer d, gint fd, b_input_condition cond)
[748bcdd]378{
379        struct im_connection *ic = d;
[5ebff60]380
381        if (!(ic->flags & (OPT_SLOW_LOGIN | OPT_LOGGED_IN))) {
382                imcb_error(ic, "Connection timeout");
383                imc_logout(ic, TRUE);
[4cb21b7]384        }
[5ebff60]385
[748bcdd]386        return FALSE;
387}
388
[5ebff60]389struct account_reconnect_delay {
[280e655]390        int start;
391        char op;
392        int step;
[4230221]393        int max;
394};
395
[5ebff60]396int account_reconnect_delay_parse(char *value, struct account_reconnect_delay *p)
[4230221]397{
[5ebff60]398        memset(p, 0, sizeof(*p));
[4230221]399        /* A whole day seems like a sane "maximum maximum". */
400        p->max = 86400;
[5ebff60]401
[58adb7e]402        /* Format: /[0-9]+([*+][0-9]+(<[0-9+])?)?/ */
[5ebff60]403        while (*value && g_ascii_isdigit(*value)) {
[4230221]404                p->start = p->start * 10 + *value++ - '0';
[5ebff60]405        }
406
[4230221]407        /* Sure, call me evil for implementing my own fscanf here, but it's
[7125cb3]408           dead simple and I immediately know where to continue parsing. */
[5ebff60]409
410        if (*value == 0) {
[4230221]411                /* If the string ends now, the delay is constant. */
412                return 1;
[5ebff60]413        } else if (*value != '+' && *value != '*') {
[4230221]414                /* Otherwise allow either a + or a * */
415                return 0;
[5ebff60]416        }
417
[4230221]418        p->op = *value++;
[5ebff60]419
[4230221]420        /* + or * the delay by this number every time. */
[5ebff60]421        while (*value && g_ascii_isdigit(*value)) {
[4230221]422                p->step = p->step * 10 + *value++ - '0';
[5ebff60]423        }
424
425        if (*value == 0) {
[4230221]426                /* Use the default maximum (one day). */
427                return 1;
[5ebff60]428        } else if (*value != '<') {
[4230221]429                return 0;
[5ebff60]430        }
431
[4230221]432        p->max = 0;
[5ebff60]433        value++;
434        while (*value && g_ascii_isdigit(*value)) {
[4230221]435                p->max = p->max * 10 + *value++ - '0';
[5ebff60]436        }
437
[4230221]438        return p->max > 0;
439}
440
[5ebff60]441char *set_eval_account_reconnect_delay(set_t *set, char *value)
[4230221]442{
443        struct account_reconnect_delay p;
[5ebff60]444
445        return account_reconnect_delay_parse(value, &p) ? value : SET_INVALID;
[280e655]446}
447
[5ebff60]448int account_reconnect_delay(account_t *a)
[280e655]449{
[5ebff60]450        char *setting = set_getstr(&a->bee->set, "auto_reconnect_delay");
[4230221]451        struct account_reconnect_delay p;
[5ebff60]452
453        if (account_reconnect_delay_parse(setting, &p)) {
454                if (a->auto_reconnect_delay == 0) {
[4230221]455                        a->auto_reconnect_delay = p.start;
[5ebff60]456                } else if (p.op == '+') {
[4230221]457                        a->auto_reconnect_delay += p.step;
[5ebff60]458                } else if (p.op == '*') {
[4230221]459                        a->auto_reconnect_delay *= p.step;
[5ebff60]460                }
461
462                if (a->auto_reconnect_delay > p.max) {
[4230221]463                        a->auto_reconnect_delay = p.max;
[5ebff60]464                }
465        } else {
[4230221]466                a->auto_reconnect_delay = 0;
[280e655]467        }
[5ebff60]468
[4230221]469        return a->auto_reconnect_delay;
[280e655]470}
[1783ab6]471
[5ebff60]472int protocol_account_islocal(const char* protocol)
[1783ab6]473{
474        const char** p = account_protocols_local;
[5ebff60]475
[1783ab6]476        do {
[5ebff60]477                if (strcmp(*p, protocol) == 0) {
[1783ab6]478                        return 1;
[5ebff60]479                }
480        } while (*(++p));
[1783ab6]481        return 0;
482}
Note: See TracBrowser for help on using the repository browser.