Debian Codegraph @

protocols/account.c

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