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