source: protocols/account.c @ 0e4c3dd

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