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