source: protocols/account.c @ 84c3a72

Last change on this file since 84c3a72 was 84c3a72, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-27T12:39:07Z

Import chatrooms configured in older BitlBee versions. Settings are currently
ignored though. Also removing the old chat.[ch] files since they're really not
important anymore.

  • Property mode set to 100644
File size: 8.1 KB
RevLine 
[b7d3cc34]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[58adb7e]4  * Copyright 2002-2010 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;
22  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23  Suite 330, Boston, MA  02111-1307  USA
24*/
25
26#define BITLBEE_CORE
27#include "bitlbee.h"
28#include "account.h"
29
[81e04e1]30account_t *account_add( bee_t *bee, struct prpl *prpl, char *user, char *pass )
[b7d3cc34]31{
32        account_t *a;
[5100caa]33        set_t *s;
[b7d3cc34]34       
[81e04e1]35        if( bee->accounts )
[b7d3cc34]36        {
[81e04e1]37                for( a = bee->accounts; a->next; a = a->next );
[a312b6b]38                a = a->next = g_new0( account_t, 1 );
[b7d3cc34]39        }
40        else
41        {
[81e04e1]42                bee->accounts = a = g_new0 ( account_t, 1 );
[b7d3cc34]43        }
44       
[7b23afd]45        a->prpl = prpl;
[b7d3cc34]46        a->user = g_strdup( user );
47        a->pass = g_strdup( pass );
[2b14eef]48        a->auto_connect = 1;
[81e04e1]49        a->bee = bee;
[b7d3cc34]50       
[96863f6]51        s = set_add( &a->set, "auto_connect", "true", set_eval_account, a );
[5100caa]52        s->flags |= ACC_SET_NOSAVE;
53       
[04026d4]54        s = set_add( &a->set, "auto_reconnect", "true", set_eval_bool, a );
[00a5270]55       
[286b28e]56        s = set_add( &a->set, "nick_source", "handle", NULL, a );
57       
[5100caa]58        s = set_add( &a->set, "password", NULL, set_eval_account, a );
[f3579fd]59        s->flags |= ACC_SET_NOSAVE | SET_NULL_OK;
[5100caa]60       
61        s = set_add( &a->set, "username", NULL, set_eval_account, a );
62        s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY;
63        set_setstr( &a->set, "username", user );
64       
[5b52a48]65        a->nicks = g_hash_table_new_full( g_str_hash, g_str_equal, g_free, g_free );
66       
[96863f6]67        /* This function adds some more settings (and might want to do more
68           things that have to be done now, although I can't think of anything. */
[0da65d5]69        if( prpl->init )
70                prpl->init( a );
[96863f6]71       
[58adb7e]72        s = set_add( &a->set, "away", NULL, set_eval_account, a );
73        s->flags |= SET_NULL_OK;
74       
75        if( a->flags & ACC_FLAG_STATUS_MESSAGE )
76        {
77                s = set_add( &a->set, "status", NULL, set_eval_account, a );
78                s->flags |= SET_NULL_OK;
79        }
80       
81        return a;
[b7d3cc34]82}
83
[5100caa]84char *set_eval_account( set_t *set, char *value )
85{
86        account_t *acc = set->data;
87       
88        /* Double-check: We refuse to edit on-line accounts. */
[0da65d5]89        if( set->flags & ACC_SET_OFFLINE_ONLY && acc->ic )
[7125cb3]90                return SET_INVALID;
[5100caa]91       
[3b32017]92        if( strcmp( set->key, "server" ) == 0 )
[5100caa]93        {
94                g_free( acc->server );
[3b32017]95                if( value && *value )
[30ce1ce]96                {
[5100caa]97                        acc->server = g_strdup( value );
[30ce1ce]98                        return value;
99                }
[5100caa]100                else
[30ce1ce]101                {
[7125cb3]102                        acc->server = g_strdup( set->def );
[30ce1ce]103                        return g_strdup( set->def );
104                }
[5100caa]105        }
[3b32017]106        else if( strcmp( set->key, "username" ) == 0 )
107        {
108                g_free( acc->user );
109                acc->user = g_strdup( value );
110                return value;
111        }
112        else if( strcmp( set->key, "password" ) == 0 )
113        {
[7125cb3]114                if( value )
115                {
116                        g_free( acc->pass );
117                        acc->pass = g_strdup( value );
118                        return NULL;    /* password shouldn't be visible in plaintext! */
119                }
120                else
121                {
122                        /* NULL can (should) be stored in the set_t
123                           variable, but is otherwise not correct. */
124                        return SET_INVALID;
125                }
[3b32017]126        }
[5100caa]127        else if( strcmp( set->key, "auto_connect" ) == 0 )
128        {
129                if( !is_bool( value ) )
[7125cb3]130                        return SET_INVALID;
[5100caa]131               
132                acc->auto_connect = bool2int( value );
133                return value;
134        }
[58adb7e]135        else if( strcmp( set->key, "away" ) == 0 ||
136                 strcmp( set->key, "status" ) == 0 )
137        {
138                if( acc->ic && acc->ic->flags & OPT_LOGGED_IN )
139                {
140                        /* If we're currently on-line, set the var now already
141                           (bit of a hack) and send an update. */
142                        g_free( set->value );
143                        set->value = g_strdup( value );
144                       
145                        imc_away_send_update( acc->ic );
146                }
147               
148                return value;
149        }
[5100caa]150       
[7125cb3]151        return SET_INVALID;
[5100caa]152}
153
[81e04e1]154account_t *account_get( bee_t *bee, char *id )
[b7d3cc34]155{
156        account_t *a, *ret = NULL;
[85616c3]157        char *handle, *s;
[b7d3cc34]158        int nr;
159       
[85616c3]160        /* This checks if the id string ends with (...) */
161        if( ( handle = strchr( id, '(' ) ) && ( s = strchr( handle, ')' ) ) && s[1] == 0 )
162        {
163                struct prpl *proto;
164               
165                *s = *handle = 0;
166                handle ++;
167               
168                if( ( proto = find_protocol( id ) ) )
169                {
[81e04e1]170                        for( a = bee->accounts; a; a = a->next )
[85616c3]171                                if( a->prpl == proto &&
[5b52a48]172                                    a->prpl->handle_cmp( handle, a->user ) == 0 )
[85616c3]173                                        ret = a;
174                }
175               
176                /* Restore the string. */
177                handle --;
178                *handle = '(';
179                *s = ')';
180               
181                if( ret )
182                        return ret;
183        }
184       
[b7d3cc34]185        if( sscanf( id, "%d", &nr ) == 1 && nr < 1000 )
186        {
[81e04e1]187                for( a = bee->accounts; a; a = a->next )
[b7d3cc34]188                        if( ( nr-- ) == 0 )
189                                return( a );
190               
191                return( NULL );
192        }
193       
[81e04e1]194        for( a = bee->accounts; a; a = a->next )
[b7d3cc34]195        {
[7b23afd]196                if( g_strcasecmp( id, a->prpl->name ) == 0 )
[b7d3cc34]197                {
198                        if( !ret )
199                                ret = a;
200                        else
201                                return( NULL ); /* We don't want to match more than one... */
202                }
203                else if( strstr( a->user, id ) )
204                {
205                        if( !ret )
206                                ret = a;
207                        else
208                                return( NULL );
209                }
210        }
211       
212        return( ret );
213}
214
[81e04e1]215void account_del( bee_t *bee, account_t *acc )
[b7d3cc34]216{
217        account_t *a, *l = NULL;
218       
[fa75134]219        if( acc->ic )
220                /* Caller should have checked, accounts still in use can't be deleted. */
221                return;
222       
[81e04e1]223        for( a = bee->accounts; a; a = (l=a)->next )
[b7d3cc34]224                if( a == acc )
225                {
226                        if( l )
227                                l->next = a->next;
228                        else
[81e04e1]229                                bee->accounts = a->next;
[b7d3cc34]230                       
[81e04e1]231                        /** FIXME
232                        for( c = bee->chatrooms; c; c = nc )
[f86a3d5]233                        {
234                                nc = c->next;
[d995c9b]235                                if( acc == c->acc )
[81e04e1]236                                        chat_del( bee, c );
[f86a3d5]237                        }
[81e04e1]238                        */
[d995c9b]239                       
[5100caa]240                        while( a->set )
241                                set_del( &a->set, a->set->key );
242                       
[5b52a48]243                        g_hash_table_destroy( a->nicks );
244                       
[b7d3cc34]245                        g_free( a->user );
246                        g_free( a->pass );
[fa75134]247                        g_free( a->server );
[b7d3cc34]248                        if( a->reconnect )      /* This prevents any reconnect still queued to happen */
249                                cancel_auto_reconnect( a );
250                        g_free( a );
251                       
252                        break;
253                }
254}
255
[81e04e1]256void account_on( bee_t *bee, account_t *a )
[b7d3cc34]257{
[0da65d5]258        if( a->ic )
[b7d3cc34]259        {
260                /* Trying to enable an already-enabled account */
261                return;
262        }
263       
264        cancel_auto_reconnect( a );
265       
266        a->reconnect = 0;
[0a3c243]267        a->prpl->login( a );
[b7d3cc34]268}
269
[81e04e1]270void account_off( bee_t *bee, account_t *a )
[b7d3cc34]271{
[b0eaa5b]272        imc_logout( a->ic, FALSE );
[0da65d5]273        a->ic = NULL;
[b7d3cc34]274        if( a->reconnect )
275        {
276                /* Shouldn't happen */
277                cancel_auto_reconnect( a );
278        }
279}
[280e655]280
[4230221]281struct account_reconnect_delay
[280e655]282{
283        int start;
284        char op;
285        int step;
[4230221]286        int max;
287};
288
289int account_reconnect_delay_parse( char *value, struct account_reconnect_delay *p )
290{
291        memset( p, 0, sizeof( *p ) );
292        /* A whole day seems like a sane "maximum maximum". */
293        p->max = 86400;
[280e655]294       
[58adb7e]295        /* Format: /[0-9]+([*+][0-9]+(<[0-9+])?)?/ */
[4230221]296        while( *value && isdigit( *value ) )
297                p->start = p->start * 10 + *value++ - '0';
298       
299        /* Sure, call me evil for implementing my own fscanf here, but it's
[7125cb3]300           dead simple and I immediately know where to continue parsing. */
[4230221]301       
302        if( *value == 0 )
303                /* If the string ends now, the delay is constant. */
304                return 1;
305        else if( *value != '+' && *value != '*' )
306                /* Otherwise allow either a + or a * */
307                return 0;
308       
309        p->op = *value++;
310       
311        /* + or * the delay by this number every time. */
312        while( *value && isdigit( *value ) )
313                p->step = p->step * 10 + *value++ - '0';
314       
315        if( *value == 0 )
316                /* Use the default maximum (one day). */
317                return 1;
318        else if( *value != '<' )
319                return 0;
320       
321        p->max = 0;
322        value ++;
323        while( *value && isdigit( *value ) )
324                p->max = p->max * 10 + *value++ - '0';
325       
326        return p->max > 0;
327}
328
329char *set_eval_account_reconnect_delay( set_t *set, char *value )
330{
331        struct account_reconnect_delay p;
332       
[7125cb3]333        return account_reconnect_delay_parse( value, &p ) ? value : SET_INVALID;
[280e655]334}
335
336int account_reconnect_delay( account_t *a )
337{
[81e04e1]338        char *setting = set_getstr( &a->bee->set, "auto_reconnect_delay" );
[4230221]339        struct account_reconnect_delay p;
[280e655]340       
[4230221]341        if( account_reconnect_delay_parse( setting, &p ) )
[280e655]342        {
343                if( a->auto_reconnect_delay == 0 )
[4230221]344                        a->auto_reconnect_delay = p.start;
345                else if( p.op == '+' )
346                        a->auto_reconnect_delay += p.step;
347                else if( p.op == '*' )
348                        a->auto_reconnect_delay *= p.step;
349               
350                if( a->auto_reconnect_delay > p.max )
351                        a->auto_reconnect_delay = p.max;
[280e655]352        }
[4230221]353        else
[280e655]354        {
[4230221]355                a->auto_reconnect_delay = 0;
[280e655]356        }
357       
[4230221]358        return a->auto_reconnect_delay;
[280e655]359}
Note: See TracBrowser for help on using the repository browser.