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