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