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 | |
---|
30 | account_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, "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 | |
---|
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 | |
---|
66 | return( a ); |
---|
67 | } |
---|
68 | |
---|
69 | char *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. */ |
---|
74 | if( set->flags & ACC_SET_OFFLINE_ONLY && acc->gc ) |
---|
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 | |
---|
110 | account_t *account_get( irc_t *irc, char *id ) |
---|
111 | { |
---|
112 | account_t *a, *ret = NULL; |
---|
113 | char *handle, *s; |
---|
114 | int nr; |
---|
115 | |
---|
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 | |
---|
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 | { |
---|
152 | if( g_strcasecmp( id, a->prpl->name ) == 0 ) |
---|
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 | |
---|
171 | void 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 | |
---|
189 | while( a->set ) |
---|
190 | set_del( &a->set, a->set->key ); |
---|
191 | |
---|
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 | |
---|
203 | void 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; |
---|
214 | a->prpl->login( a ); |
---|
215 | } |
---|
216 | |
---|
217 | void account_off( irc_t *irc, account_t *a ) |
---|
218 | { |
---|
219 | a->gc->wants_to_die = TRUE; |
---|
220 | signoff( a->gc ); |
---|
221 | a->gc = NULL; |
---|
222 | if( a->reconnect ) |
---|
223 | { |
---|
224 | /* Shouldn't happen */ |
---|
225 | cancel_auto_reconnect( a ); |
---|
226 | } |
---|
227 | } |
---|