source: account.c @ 2b14eef

Last change on this file since 2b14eef was 2b14eef, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-06-20T22:14:46Z

Implemented handling of autoconnect attribute.

  • Property mode set to 100644
File size: 4.0 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       
34        if( irc->accounts )
35        {
36                for( a = irc->accounts; a->next; a = a->next );
37                a = a->next = g_new0( account_t, 1 );
38        }
39        else
40        {
41                irc->accounts = a = g_new0 ( account_t, 1 );
42        }
43       
44        a->prpl = prpl;
45        a->user = g_strdup( user );
46        a->pass = g_strdup( pass );
47        a->auto_connect = 1;
48        a->irc = irc;
49       
50        return( a );
51}
52
53account_t *account_get( irc_t *irc, char *id )
54{
55        account_t *a, *ret = NULL;
56        char *handle, *s;
57        int nr;
58       
59        /* This checks if the id string ends with (...) */
60        if( ( handle = strchr( id, '(' ) ) && ( s = strchr( handle, ')' ) ) && s[1] == 0 )
61        {
62                struct prpl *proto;
63               
64                *s = *handle = 0;
65                handle ++;
66               
67                if( ( proto = find_protocol( id ) ) )
68                {
69                        for( a = irc->accounts; a; a = a->next )
70                                if( a->prpl == proto &&
71                                    a->prpl->cmp_buddynames( handle, a->user ) == 0 )
72                                        ret = a;
73                }
74               
75                /* Restore the string. */
76                handle --;
77                *handle = '(';
78                *s = ')';
79               
80                if( ret )
81                        return ret;
82        }
83       
84        if( sscanf( id, "%d", &nr ) == 1 && nr < 1000 )
85        {
86                for( a = irc->accounts; a; a = a->next )
87                        if( ( nr-- ) == 0 )
88                                return( a );
89               
90                return( NULL );
91        }
92       
93        for( a = irc->accounts; a; a = a->next )
94        {
95                if( g_strcasecmp( id, a->prpl->name ) == 0 )
96                {
97                        if( !ret )
98                                ret = a;
99                        else
100                                return( NULL ); /* We don't want to match more than one... */
101                }
102                else if( strstr( a->user, id ) )
103                {
104                        if( !ret )
105                                ret = a;
106                        else
107                                return( NULL );
108                }
109        }
110       
111        return( ret );
112}
113
114void account_del( irc_t *irc, account_t *acc )
115{
116        account_t *a, *l = NULL;
117       
118        for( a = irc->accounts; a; a = (l=a)->next )
119                if( a == acc )
120                {
121                        if( a->gc ) return; /* Caller should have checked, accounts still in use can't be deleted. */
122                       
123                        if( l )
124                        {
125                                l->next = a->next;
126                        }
127                        else
128                        {
129                                irc->accounts = a->next;
130                        }
131                       
132                        g_free( a->user );
133                        g_free( a->pass );
134                        if( a->server ) g_free( a->server );
135                        if( a->reconnect )      /* This prevents any reconnect still queued to happen */
136                                cancel_auto_reconnect( a );
137                        g_free( a );
138                       
139                        break;
140                }
141}
142
143void account_on( irc_t *irc, account_t *a )
144{
145        struct aim_user *u;
146       
147        if( a->gc )
148        {
149                /* Trying to enable an already-enabled account */
150                return;
151        }
152       
153        cancel_auto_reconnect( a );
154       
155        u = g_new0 ( struct aim_user, 1 );
156        u->irc = irc;
157        u->prpl = a->prpl;
158        strncpy( u->username, a->user, sizeof( u->username ) - 1 );
159        strncpy( u->password, a->pass, sizeof( u->password ) - 1 );
160        if( a->server) strncpy( u->proto_opt[0], a->server, sizeof( u->proto_opt[0] ) - 1 );
161       
162        a->gc = (struct gaim_connection *) u; /* Bit hackish :-/ */
163        a->reconnect = 0;
164       
165        a->prpl->login( u );
166}
167
168void account_off( irc_t *irc, account_t *a )
169{
170        a->gc->wants_to_die = TRUE;
171        signoff( a->gc );
172        a->gc = NULL;
173        if( a->reconnect )
174        {
175                /* Shouldn't happen */
176                cancel_auto_reconnect( a );
177        }
178}
Note: See TracBrowser for help on using the repository browser.