source: account.c @ a830512

Last change on this file since a830512 was 4230221, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-08-09T23:00:38Z

Added ceiling to auto-reconnect delay, changed the default to 5*3<900 and
added documentation.

  • Property mode set to 100644
File size: 7.1 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2004 Wilmer van der Gaast and others                *
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
30account_t *account_add( irc_t *irc, struct prpl *prpl, char *user, char *pass )
31{
32        account_t *a;
33        set_t *s;
34       
35        if( irc->accounts )
36        {
37                for( a = irc->accounts; a->next; a = a->next );
38                a = a->next = g_new0( account_t, 1 );
39        }
40        else
41        {
42                irc->accounts = a = g_new0 ( account_t, 1 );
43        }
44       
45        a->prpl = prpl;
46        a->user = g_strdup( user );
47        a->pass = g_strdup( pass );
48        a->auto_connect = 1;
49        a->irc = irc;
50       
51        s = set_add( &a->set, "auto_connect", "true", set_eval_account, a );
52        s->flags |= ACC_SET_NOSAVE;
53       
54        s = set_add( &a->set, "auto_reconnect", "true", set_eval_bool, a );
55       
56        s = set_add( &a->set, "password", NULL, set_eval_account, a );
57        s->flags |= ACC_SET_NOSAVE;
58       
59        s = set_add( &a->set, "username", NULL, set_eval_account, a );
60        s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY;
61        set_setstr( &a->set, "username", user );
62       
63        a->nicks = g_hash_table_new_full( g_str_hash, g_str_equal, g_free, g_free );
64       
65        /* This function adds some more settings (and might want to do more
66           things that have to be done now, although I can't think of anything. */
67        if( prpl->init )
68                prpl->init( a );
69       
70        return( a );
71}
72
73char *set_eval_account( set_t *set, char *value )
74{
75        account_t *acc = set->data;
76       
77        /* Double-check: We refuse to edit on-line accounts. */
78        if( set->flags & ACC_SET_OFFLINE_ONLY && acc->ic )
79                return NULL;
80       
81        if( strcmp( set->key, "username" ) == 0 )
82        {
83                g_free( acc->user );
84                acc->user = g_strdup( value );
85                return value;
86        }
87        else if( strcmp( set->key, "password" ) == 0 )
88        {
89                g_free( acc->pass );
90                acc->pass = g_strdup( value );
91                return NULL;    /* password shouldn't be visible in plaintext! */
92        }
93        else if( strcmp( set->key, "server" ) == 0 )
94        {
95                g_free( acc->server );
96                if( *value )
97                {
98                        acc->server = g_strdup( value );
99                        return value;
100                }
101                else
102                {
103                        acc->server = NULL;
104                        return g_strdup( set->def );
105                }
106        }
107        else if( strcmp( set->key, "auto_connect" ) == 0 )
108        {
109                if( !is_bool( value ) )
110                        return NULL;
111               
112                acc->auto_connect = bool2int( value );
113                return value;
114        }
115       
116        return NULL;
117}
118
119account_t *account_get( irc_t *irc, char *id )
120{
121        account_t *a, *ret = NULL;
122        char *handle, *s;
123        int nr;
124       
125        /* This checks if the id string ends with (...) */
126        if( ( handle = strchr( id, '(' ) ) && ( s = strchr( handle, ')' ) ) && s[1] == 0 )
127        {
128                struct prpl *proto;
129               
130                *s = *handle = 0;
131                handle ++;
132               
133                if( ( proto = find_protocol( id ) ) )
134                {
135                        for( a = irc->accounts; a; a = a->next )
136                                if( a->prpl == proto &&
137                                    a->prpl->handle_cmp( handle, a->user ) == 0 )
138                                        ret = a;
139                }
140               
141                /* Restore the string. */
142                handle --;
143                *handle = '(';
144                *s = ')';
145               
146                if( ret )
147                        return ret;
148        }
149       
150        if( sscanf( id, "%d", &nr ) == 1 && nr < 1000 )
151        {
152                for( a = irc->accounts; a; a = a->next )
153                        if( ( nr-- ) == 0 )
154                                return( a );
155               
156                return( NULL );
157        }
158       
159        for( a = irc->accounts; a; a = a->next )
160        {
161                if( g_strcasecmp( id, a->prpl->name ) == 0 )
162                {
163                        if( !ret )
164                                ret = a;
165                        else
166                                return( NULL ); /* We don't want to match more than one... */
167                }
168                else if( strstr( a->user, id ) )
169                {
170                        if( !ret )
171                                ret = a;
172                        else
173                                return( NULL );
174                }
175        }
176       
177        return( ret );
178}
179
180void account_del( irc_t *irc, account_t *acc )
181{
182        account_t *a, *l = NULL;
183       
184        if( acc->ic )
185                /* Caller should have checked, accounts still in use can't be deleted. */
186                return;
187       
188        for( a = irc->accounts; a; a = (l=a)->next )
189                if( a == acc )
190                {
191                        if( l )
192                                l->next = a->next;
193                        else
194                                irc->accounts = a->next;
195                       
196                        while( a->set )
197                                set_del( &a->set, a->set->key );
198                       
199                        g_hash_table_destroy( a->nicks );
200                       
201                        g_free( a->user );
202                        g_free( a->pass );
203                        g_free( a->server );
204                        if( a->reconnect )      /* This prevents any reconnect still queued to happen */
205                                cancel_auto_reconnect( a );
206                        g_free( a );
207                       
208                        break;
209                }
210}
211
212void account_on( irc_t *irc, account_t *a )
213{
214        if( a->ic )
215        {
216                /* Trying to enable an already-enabled account */
217                return;
218        }
219       
220        cancel_auto_reconnect( a );
221       
222        a->reconnect = 0;
223        a->prpl->login( a );
224}
225
226void account_off( irc_t *irc, account_t *a )
227{
228        imc_logout( a->ic, FALSE );
229        a->ic = NULL;
230        if( a->reconnect )
231        {
232                /* Shouldn't happen */
233                cancel_auto_reconnect( a );
234        }
235}
236
237struct account_reconnect_delay
238{
239        int start;
240        char op;
241        int step;
242        int max;
243};
244
245int account_reconnect_delay_parse( char *value, struct account_reconnect_delay *p )
246{
247        memset( p, 0, sizeof( *p ) );
248        /* A whole day seems like a sane "maximum maximum". */
249        p->max = 86400;
250       
251        /* Format: /[0-9]+([*+][0-9]+(<[0-9+]))/ */
252        while( *value && isdigit( *value ) )
253                p->start = p->start * 10 + *value++ - '0';
254       
255        /* Sure, call me evil for implementing my own fscanf here, but it's
256           dead simple and I'm immediately at the next part to parse. */
257       
258        if( *value == 0 )
259                /* If the string ends now, the delay is constant. */
260                return 1;
261        else if( *value != '+' && *value != '*' )
262                /* Otherwise allow either a + or a * */
263                return 0;
264       
265        p->op = *value++;
266       
267        /* + or * the delay by this number every time. */
268        while( *value && isdigit( *value ) )
269                p->step = p->step * 10 + *value++ - '0';
270       
271        if( *value == 0 )
272                /* Use the default maximum (one day). */
273                return 1;
274        else if( *value != '<' )
275                return 0;
276       
277        p->max = 0;
278        value ++;
279        while( *value && isdigit( *value ) )
280                p->max = p->max * 10 + *value++ - '0';
281       
282        return p->max > 0;
283}
284
285char *set_eval_account_reconnect_delay( set_t *set, char *value )
286{
287        struct account_reconnect_delay p;
288       
289        return account_reconnect_delay_parse( value, &p ) ? value : NULL;
290}
291
292int account_reconnect_delay( account_t *a )
293{
294        char *setting = set_getstr( &a->irc->set, "auto_reconnect_delay" );
295        struct account_reconnect_delay p;
296       
297        if( account_reconnect_delay_parse( setting, &p ) )
298        {
299                if( a->auto_reconnect_delay == 0 )
300                        a->auto_reconnect_delay = p.start;
301                else if( p.op == '+' )
302                        a->auto_reconnect_delay += p.step;
303                else if( p.op == '*' )
304                        a->auto_reconnect_delay *= p.step;
305               
306                if( a->auto_reconnect_delay > p.max )
307                        a->auto_reconnect_delay = p.max;
308        }
309        else
310        {
311                a->auto_reconnect_delay = 0;
312        }
313       
314        return a->auto_reconnect_delay;
315}
Note: See TracBrowser for help on using the repository browser.