source: account.c @ 96863f6

Last change on this file since 96863f6 was 96863f6, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-07-02T09:49:31Z

Added protocol-specific settings, made the server setting specific to only
OSCAR and Jabber.

  • Property mode set to 100644
File size: 5.1 KB
RevLine 
[b7d3cc34]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
[7b23afd]30account_t *account_add( irc_t *irc, struct prpl *prpl, char *user, char *pass )
[b7d3cc34]31{
32        account_t *a;
[5100caa]33        set_t *s;
[b7d3cc34]34       
35        if( irc->accounts )
36        {
37                for( a = irc->accounts; a->next; a = a->next );
[a312b6b]38                a = a->next = g_new0( account_t, 1 );
[b7d3cc34]39        }
40        else
41        {
42                irc->accounts = a = g_new0 ( account_t, 1 );
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;
[b7d3cc34]49        a->irc = irc;
50       
[96863f6]51        s = set_add( &a->set, "auto_connect", "true", set_eval_account, a );
[5100caa]52        s->flags |= ACC_SET_NOSAVE;
53       
54        s = set_add( &a->set, "password", NULL, set_eval_account, a );
55        s->flags |= ACC_SET_NOSAVE;
56       
57        s = set_add( &a->set, "username", NULL, set_eval_account, a );
58        s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY;
59        set_setstr( &a->set, "username", user );
60       
[96863f6]61        /* This function adds some more settings (and might want to do more
62           things that have to be done now, although I can't think of anything. */
63        if( prpl->acc_init )
64                prpl->acc_init( a );
65       
[b7d3cc34]66        return( a );
67}
68
[5100caa]69char *set_eval_account( set_t *set, char *value )
70{
71        account_t *acc = set->data;
72       
73        /* Double-check: We refuse to edit on-line accounts. */
[fef6116]74        if( set->flags & ACC_SET_OFFLINE_ONLY && acc->gc )
[5100caa]75                return NULL;
76       
77        if( strcmp( set->key, "username" ) == 0 )
78        {
79                g_free( acc->user );
80                acc->user = g_strdup( value );
81                return value;
82        }
83        else if( strcmp( set->key, "password" ) == 0 )
84        {
85                g_free( acc->pass );
86                acc->pass = g_strdup( value );
87                return NULL;    /* password shouldn't be visible in plaintext! */
88        }
89        else if( strcmp( set->key, "server" ) == 0 )
90        {
91                g_free( acc->server );
92                if( *value )
93                        acc->server = g_strdup( value );
94                else
95                        acc->server = NULL;
96                return value;
97        }
98        else if( strcmp( set->key, "auto_connect" ) == 0 )
99        {
100                if( !is_bool( value ) )
101                        return NULL;
102               
103                acc->auto_connect = bool2int( value );
104                return value;
105        }
106       
107        return NULL;
108}
109
[b7d3cc34]110account_t *account_get( irc_t *irc, char *id )
111{
112        account_t *a, *ret = NULL;
[85616c3]113        char *handle, *s;
[b7d3cc34]114        int nr;
115       
[85616c3]116        /* This checks if the id string ends with (...) */
117        if( ( handle = strchr( id, '(' ) ) && ( s = strchr( handle, ')' ) ) && s[1] == 0 )
118        {
119                struct prpl *proto;
120               
121                *s = *handle = 0;
122                handle ++;
123               
124                if( ( proto = find_protocol( id ) ) )
125                {
126                        for( a = irc->accounts; a; a = a->next )
127                                if( a->prpl == proto &&
128                                    a->prpl->cmp_buddynames( handle, a->user ) == 0 )
129                                        ret = a;
130                }
131               
132                /* Restore the string. */
133                handle --;
134                *handle = '(';
135                *s = ')';
136               
137                if( ret )
138                        return ret;
139        }
140       
[b7d3cc34]141        if( sscanf( id, "%d", &nr ) == 1 && nr < 1000 )
142        {
143                for( a = irc->accounts; a; a = a->next )
144                        if( ( nr-- ) == 0 )
145                                return( a );
146               
147                return( NULL );
148        }
149       
150        for( a = irc->accounts; a; a = a->next )
151        {
[7b23afd]152                if( g_strcasecmp( id, a->prpl->name ) == 0 )
[b7d3cc34]153                {
154                        if( !ret )
155                                ret = a;
156                        else
157                                return( NULL ); /* We don't want to match more than one... */
158                }
159                else if( strstr( a->user, id ) )
160                {
161                        if( !ret )
162                                ret = a;
163                        else
164                                return( NULL );
165                }
166        }
167       
168        return( ret );
169}
170
171void account_del( irc_t *irc, account_t *acc )
172{
173        account_t *a, *l = NULL;
174       
175        for( a = irc->accounts; a; a = (l=a)->next )
176                if( a == acc )
177                {
178                        if( a->gc ) return; /* Caller should have checked, accounts still in use can't be deleted. */
179                       
180                        if( l )
181                        {
182                                l->next = a->next;
183                        }
184                        else
185                        {
186                                irc->accounts = a->next;
187                        }
188                       
[5100caa]189                        while( a->set )
190                                set_del( &a->set, a->set->key );
191                       
[b7d3cc34]192                        g_free( a->user );
193                        g_free( a->pass );
194                        if( a->server ) g_free( a->server );
195                        if( a->reconnect )      /* This prevents any reconnect still queued to happen */
196                                cancel_auto_reconnect( a );
197                        g_free( a );
198                       
199                        break;
200                }
201}
202
203void account_on( irc_t *irc, account_t *a )
204{
205        if( a->gc )
206        {
207                /* Trying to enable an already-enabled account */
208                return;
209        }
210       
211        cancel_auto_reconnect( a );
212       
213        a->reconnect = 0;
[0a3c243]214        a->prpl->login( a );
[b7d3cc34]215}
216
217void account_off( irc_t *irc, account_t *a )
218{
[c99af3a]219        a->gc->wants_to_die = TRUE;
220        signoff( a->gc );
[b7d3cc34]221        a->gc = NULL;
222        if( a->reconnect )
223        {
224                /* Shouldn't happen */
225                cancel_auto_reconnect( a );
226        }
227}
Note: See TracBrowser for help on using the repository browser.