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