[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[c92e6801] | 4 | * Copyright 2002-2005 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* Some stuff to register, handle and save user preferences */ |
---|
| 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 | #define BITLBEE_CORE |
---|
| 26 | #include "bitlbee.h" |
---|
| 27 | |
---|
[823de9d] | 28 | /* Used to use NULL for this, but NULL is actually a "valid" value. */ |
---|
| 29 | char *SET_INVALID = "nee"; |
---|
| 30 | |
---|
[0383943] | 31 | set_t *set_add( set_t **head, char *key, char *def, set_eval eval, void *data ) |
---|
[b7d3cc34] | 32 | { |
---|
[5c9512f] | 33 | set_t *s = set_find( head, key ); |
---|
[b7d3cc34] | 34 | |
---|
[5c9512f] | 35 | /* Possibly the setting already exists. If it doesn't exist yet, |
---|
| 36 | we create it. If it does, we'll just change the default. */ |
---|
[b7d3cc34] | 37 | if( !s ) |
---|
| 38 | { |
---|
[5c9512f] | 39 | if( ( s = *head ) ) |
---|
[b7d3cc34] | 40 | { |
---|
| 41 | while( s->next ) s = s->next; |
---|
[5c9512f] | 42 | s->next = g_new0( set_t, 1 ); |
---|
[b7d3cc34] | 43 | s = s->next; |
---|
| 44 | } |
---|
| 45 | else |
---|
| 46 | { |
---|
[5c9512f] | 47 | s = *head = g_new0( set_t, 1 ); |
---|
[b7d3cc34] | 48 | } |
---|
| 49 | s->key = g_strdup( key ); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | if( s->def ) |
---|
| 53 | { |
---|
| 54 | g_free( s->def ); |
---|
| 55 | s->def = NULL; |
---|
| 56 | } |
---|
| 57 | if( def ) s->def = g_strdup( def ); |
---|
| 58 | |
---|
[5c9512f] | 59 | s->eval = eval; |
---|
| 60 | s->data = data; |
---|
[b7d3cc34] | 61 | |
---|
[5c9512f] | 62 | return s; |
---|
[b7d3cc34] | 63 | } |
---|
| 64 | |
---|
[5c9512f] | 65 | set_t *set_find( set_t **head, char *key ) |
---|
[b7d3cc34] | 66 | { |
---|
[5c9512f] | 67 | set_t *s = *head; |
---|
[b7d3cc34] | 68 | |
---|
| 69 | while( s ) |
---|
| 70 | { |
---|
| 71 | if( g_strcasecmp( s->key, key ) == 0 ) |
---|
| 72 | break; |
---|
| 73 | s = s->next; |
---|
| 74 | } |
---|
| 75 | |
---|
[5c9512f] | 76 | return s; |
---|
[b7d3cc34] | 77 | } |
---|
| 78 | |
---|
[5c9512f] | 79 | char *set_getstr( set_t **head, char *key ) |
---|
[b7d3cc34] | 80 | { |
---|
[5c9512f] | 81 | set_t *s = set_find( head, key ); |
---|
[b7d3cc34] | 82 | |
---|
| 83 | if( !s || ( !s->value && !s->def ) ) |
---|
[5c9512f] | 84 | return NULL; |
---|
[b7d3cc34] | 85 | |
---|
[5c9512f] | 86 | return s->value ? s->value : s->def; |
---|
[b7d3cc34] | 87 | } |
---|
| 88 | |
---|
[5c9512f] | 89 | int set_getint( set_t **head, char *key ) |
---|
[b7d3cc34] | 90 | { |
---|
[5c9512f] | 91 | char *s = set_getstr( head, key ); |
---|
[b7d3cc34] | 92 | int i = 0; |
---|
| 93 | |
---|
| 94 | if( !s ) |
---|
[5c9512f] | 95 | return 0; |
---|
[b7d3cc34] | 96 | |
---|
| 97 | if( sscanf( s, "%d", &i ) != 1 ) |
---|
[5c9512f] | 98 | return 0; |
---|
[b7d3cc34] | 99 | |
---|
[5c9512f] | 100 | return i; |
---|
[b7d3cc34] | 101 | } |
---|
| 102 | |
---|
[5100caa] | 103 | int set_getbool( set_t **head, char *key ) |
---|
| 104 | { |
---|
| 105 | char *s = set_getstr( head, key ); |
---|
| 106 | |
---|
| 107 | if( !s ) |
---|
| 108 | return 0; |
---|
| 109 | |
---|
| 110 | return bool2int( s ); |
---|
| 111 | } |
---|
| 112 | |
---|
[5c9512f] | 113 | int set_setstr( set_t **head, char *key, char *value ) |
---|
[b7d3cc34] | 114 | { |
---|
[5c9512f] | 115 | set_t *s = set_find( head, key ); |
---|
[b7d3cc34] | 116 | char *nv = value; |
---|
| 117 | |
---|
| 118 | if( !s ) |
---|
[823de9d] | 119 | /* |
---|
| 120 | Used to do this, but it never really made sense. |
---|
[5c9512f] | 121 | s = set_add( head, key, NULL, NULL, NULL ); |
---|
[823de9d] | 122 | */ |
---|
| 123 | return 0; |
---|
[b7d3cc34] | 124 | |
---|
[823de9d] | 125 | if( value == NULL && ( s->flags & SET_NULL_OK ) == 0 ) |
---|
| 126 | return 0; |
---|
| 127 | |
---|
| 128 | /* Call the evaluator. For invalid values, evaluators should now |
---|
| 129 | return SET_INVALID, but previously this was NULL. Try to handle |
---|
| 130 | that too if NULL is not an allowed value for this setting. */ |
---|
| 131 | if( s->eval && ( ( nv = s->eval( s, value ) ) == SET_INVALID || |
---|
| 132 | ( ( s->flags & SET_NULL_OK ) == 0 && nv == NULL ) ) ) |
---|
[5c9512f] | 133 | return 0; |
---|
[b7d3cc34] | 134 | |
---|
| 135 | if( s->value ) |
---|
| 136 | { |
---|
| 137 | g_free( s->value ); |
---|
| 138 | s->value = NULL; |
---|
| 139 | } |
---|
| 140 | |
---|
[5c9512f] | 141 | /* If there's a default setting and it's equal to what we're trying to |
---|
| 142 | set, stick with s->value = NULL. Otherwise, remember the setting. */ |
---|
[b7d3cc34] | 143 | if( !s->def || ( strcmp( nv, s->def ) != 0 ) ) |
---|
| 144 | s->value = g_strdup( nv ); |
---|
| 145 | |
---|
| 146 | if( nv != value ) |
---|
| 147 | g_free( nv ); |
---|
| 148 | |
---|
[5c9512f] | 149 | return 1; |
---|
[b7d3cc34] | 150 | } |
---|
| 151 | |
---|
[5c9512f] | 152 | int set_setint( set_t **head, char *key, int value ) |
---|
[b7d3cc34] | 153 | { |
---|
| 154 | char s[24]; /* Not quite 128-bit clean eh? ;-) */ |
---|
| 155 | |
---|
[5c9512f] | 156 | g_snprintf( s, sizeof( s ), "%d", value ); |
---|
| 157 | return set_setstr( head, key, s ); |
---|
[b7d3cc34] | 158 | } |
---|
| 159 | |
---|
[5c9512f] | 160 | void set_del( set_t **head, char *key ) |
---|
[b7d3cc34] | 161 | { |
---|
[5c9512f] | 162 | set_t *s = *head, *t = NULL; |
---|
[b7d3cc34] | 163 | |
---|
| 164 | while( s ) |
---|
| 165 | { |
---|
| 166 | if( g_strcasecmp( s->key, key ) == 0 ) |
---|
| 167 | break; |
---|
| 168 | s = (t=s)->next; |
---|
| 169 | } |
---|
| 170 | if( s ) |
---|
| 171 | { |
---|
[dd89a55] | 172 | if( t ) |
---|
| 173 | t->next = s->next; |
---|
| 174 | else |
---|
[5c9512f] | 175 | *head = s->next; |
---|
[dd89a55] | 176 | |
---|
[b7d3cc34] | 177 | g_free( s->key ); |
---|
| 178 | if( s->value ) g_free( s->value ); |
---|
| 179 | if( s->def ) g_free( s->def ); |
---|
| 180 | g_free( s ); |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
[823de9d] | 184 | int set_reset( set_t **head, char *key ) |
---|
[cd428e4] | 185 | { |
---|
| 186 | set_t *s; |
---|
| 187 | |
---|
| 188 | s = set_find( head, key ); |
---|
| 189 | if( s ) |
---|
[823de9d] | 190 | return set_setstr( head, key, s->def ); |
---|
| 191 | |
---|
| 192 | return 0; |
---|
[cd428e4] | 193 | } |
---|
| 194 | |
---|
[5c9512f] | 195 | char *set_eval_int( set_t *set, char *value ) |
---|
[b7d3cc34] | 196 | { |
---|
[6237ded] | 197 | char *s = value; |
---|
[b7d3cc34] | 198 | |
---|
[5eec897] | 199 | /* Allow a minus at the first position. */ |
---|
| 200 | if( *s == '-' ) |
---|
| 201 | s ++; |
---|
| 202 | |
---|
[6237ded] | 203 | for( ; *s; s ++ ) |
---|
[5100caa] | 204 | if( !isdigit( *s ) ) |
---|
[823de9d] | 205 | return SET_INVALID; |
---|
[b7d3cc34] | 206 | |
---|
[5c9512f] | 207 | return value; |
---|
[b7d3cc34] | 208 | } |
---|
| 209 | |
---|
[5c9512f] | 210 | char *set_eval_bool( set_t *set, char *value ) |
---|
[b7d3cc34] | 211 | { |
---|
[823de9d] | 212 | return is_bool( value ) ? value : SET_INVALID; |
---|
[b7d3cc34] | 213 | } |
---|
| 214 | |
---|
[5c9512f] | 215 | char *set_eval_to_char( set_t *set, char *value ) |
---|
[b7d3cc34] | 216 | { |
---|
| 217 | char *s = g_new( char, 3 ); |
---|
| 218 | |
---|
| 219 | if( *value == ' ' ) |
---|
| 220 | strcpy( s, " " ); |
---|
| 221 | else |
---|
| 222 | sprintf( s, "%c ", *value ); |
---|
| 223 | |
---|
[5c9512f] | 224 | return s; |
---|
[b7d3cc34] | 225 | } |
---|
| 226 | |
---|
[823de9d] | 227 | char* set_eval_op_root( set_t *set, char* value ) |
---|
[b7d3cc34] | 228 | { |
---|
[5c9512f] | 229 | irc_t *irc = set->data; |
---|
[823de9d] | 230 | char* ret = set_eval_bool(set, value); |
---|
[5a71d9c] | 231 | int b = bool2int(ret); |
---|
[823de9d] | 232 | |
---|
[5a71d9c] | 233 | irc_write( irc, ":%s!%s@%s MODE %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
[823de9d] | 234 | irc->channel, b?"+o":"-o", irc->mynick); |
---|
| 235 | |
---|
[5a71d9c] | 236 | return ret; |
---|
| 237 | } |
---|
| 238 | |
---|
[823de9d] | 239 | char* set_eval_op_user( set_t *set, char* value ) |
---|
[5a71d9c] | 240 | { |
---|
| 241 | irc_t *irc = set->data; |
---|
[823de9d] | 242 | char* ret = set_eval_bool(set, value); |
---|
[5a71d9c] | 243 | int b = bool2int(ret); |
---|
[823de9d] | 244 | |
---|
[5a71d9c] | 245 | irc_write( irc, ":%s!%s@%s MODE %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
[823de9d] | 246 | irc->channel, b?"+o":"-o", irc->nick); |
---|
| 247 | |
---|
[5a71d9c] | 248 | return ret; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | /* generalized version of set_eval_op/voice_buddies */ |
---|
| 252 | char *set_eval_mode_buddies( set_t *set, char *value, char modeflag ) |
---|
| 253 | { |
---|
| 254 | irc_t *irc = set->data; |
---|
| 255 | char op[64], deop[64]; |
---|
| 256 | int nop=0, ndeop=0; |
---|
| 257 | user_t *u; |
---|
| 258 | int mode; |
---|
| 259 | |
---|
| 260 | if(!strcmp(value, "false")) |
---|
| 261 | mode=0; |
---|
| 262 | else if(!strcmp(value, "encrypted")) |
---|
| 263 | mode=1; |
---|
| 264 | else if(!strcmp(value, "trusted")) |
---|
| 265 | mode=2; |
---|
| 266 | else if(!strcmp(value, "notaway")) |
---|
| 267 | mode=3; |
---|
[5c9512f] | 268 | else |
---|
[7125cb3] | 269 | return SET_INVALID; |
---|
[823de9d] | 270 | |
---|
[5a71d9c] | 271 | /* sorry for calling them op/deop - too lazy for search+replace :P */ |
---|
| 272 | op[0]='\0'; |
---|
| 273 | deop[0]='\0'; |
---|
| 274 | for(u=irc->users; u; u=u->next) { |
---|
| 275 | /* we're only concerned with online buddies */ |
---|
| 276 | if(!u->ic || !u->online) |
---|
| 277 | continue; |
---|
| 278 | |
---|
| 279 | /* just in case... */ |
---|
| 280 | if(strlen(u->nick) >= 64) |
---|
| 281 | continue; |
---|
| 282 | |
---|
| 283 | /* dump out ops/deops when the corresponding name list fills up */ |
---|
| 284 | if(strlen(op)+strlen(u->nick)+2 > 64) { |
---|
| 285 | char *flags = g_strnfill(nop, modeflag); |
---|
| 286 | irc_write( irc, ":%s!%s@%s MODE %s +%s%s", irc->mynick, irc->mynick, irc->myhost, |
---|
| 287 | irc->channel, flags, op ); |
---|
| 288 | op[0]='\0'; |
---|
[1a1faa0] | 289 | nop=0; |
---|
[5a71d9c] | 290 | g_free(flags); |
---|
| 291 | } |
---|
| 292 | if(strlen(deop)+strlen(u->nick)+2 > 64) { |
---|
| 293 | char *flags = g_strnfill(ndeop, modeflag); |
---|
| 294 | irc_write( irc, ":%s!%s@%s MODE %s -%s%s", irc->mynick, irc->mynick, irc->myhost, |
---|
| 295 | irc->channel, flags, deop ); |
---|
| 296 | deop[0]='\0'; |
---|
[1a1faa0] | 297 | ndeop=0; |
---|
[5a71d9c] | 298 | g_free(flags); |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | switch(mode) { |
---|
| 302 | /* "false" */ |
---|
| 303 | case 0: |
---|
| 304 | g_strlcat(deop, " ", 64); |
---|
| 305 | g_strlcat(deop, u->nick, 64); |
---|
| 306 | ndeop++; |
---|
| 307 | break; |
---|
| 308 | /* "encrypted" */ |
---|
| 309 | case 1: |
---|
| 310 | if(u->encrypted) { |
---|
| 311 | g_strlcat(op, " ", 64); |
---|
| 312 | g_strlcat(op, u->nick, 64); |
---|
| 313 | nop++; |
---|
| 314 | } else { |
---|
| 315 | g_strlcat(deop, " ", 64); |
---|
| 316 | g_strlcat(deop, u->nick, 64); |
---|
| 317 | ndeop++; |
---|
| 318 | } |
---|
| 319 | break; |
---|
| 320 | /* "trusted" */ |
---|
| 321 | case 2: |
---|
| 322 | if(u->encrypted > 1) { |
---|
| 323 | g_strlcat(op, " ", 64); |
---|
| 324 | g_strlcat(op, u->nick, 64); |
---|
| 325 | nop++; |
---|
| 326 | } else { |
---|
| 327 | g_strlcat(deop, " ", 64); |
---|
| 328 | g_strlcat(deop, u->nick, 64); |
---|
| 329 | ndeop++; |
---|
| 330 | } |
---|
| 331 | break; |
---|
| 332 | /* "notaway" */ |
---|
| 333 | case 3: |
---|
| 334 | if(u->away) { |
---|
| 335 | g_strlcat(deop, " ", 64); |
---|
| 336 | g_strlcat(deop, u->nick, 64); |
---|
| 337 | ndeop++; |
---|
| 338 | } else { |
---|
| 339 | g_strlcat(op, " ", 64); |
---|
| 340 | g_strlcat(op, u->nick, 64); |
---|
| 341 | nop++; |
---|
| 342 | } |
---|
| 343 | } |
---|
| 344 | } |
---|
| 345 | /* dump anything left in op/deop lists */ |
---|
| 346 | if(*op) { |
---|
| 347 | char *flags = g_strnfill(nop, modeflag); |
---|
| 348 | irc_write( irc, ":%s!%s@%s MODE %s +%s%s", irc->mynick, irc->mynick, irc->myhost, |
---|
| 349 | irc->channel, flags, op ); |
---|
| 350 | g_free(flags); |
---|
| 351 | } |
---|
| 352 | if(*deop) { |
---|
| 353 | char *flags = g_strnfill(ndeop, modeflag); |
---|
| 354 | irc_write( irc, ":%s!%s@%s MODE %s -%s%s", irc->mynick, irc->mynick, irc->myhost, |
---|
[823de9d] | 355 | irc->channel, flags, deop ); |
---|
| 356 | g_free(flags); |
---|
| 357 | } |
---|
| 358 | |
---|
[5c9512f] | 359 | return value; |
---|
[b7d3cc34] | 360 | } |
---|
| 361 | |
---|
[5a71d9c] | 362 | char *set_eval_op_buddies( set_t *set, char *value ) |
---|
| 363 | { |
---|
| 364 | return set_eval_mode_buddies(set, value, 'o'); |
---|
| 365 | } |
---|
| 366 | |
---|
[52e6e17] | 367 | char *set_eval_halfop_buddies( set_t *set, char *value ) |
---|
| 368 | { |
---|
| 369 | return set_eval_mode_buddies(set, value, 'h'); |
---|
| 370 | } |
---|
| 371 | |
---|
[5a71d9c] | 372 | char *set_eval_voice_buddies( set_t *set, char *value ) |
---|
| 373 | { |
---|
| 374 | return set_eval_mode_buddies(set, value, 'v'); |
---|
| 375 | } |
---|
| 376 | |
---|
[e2b15bb] | 377 | /* possible values: never, opportunistic, manual, always */ |
---|
| 378 | char *set_eval_otr_policy( set_t *set, char *value ) |
---|
| 379 | { |
---|
| 380 | if ( !strcmp(value, "never") ) |
---|
| 381 | return value; |
---|
| 382 | if ( !strcmp(value, "opportunistic") ) |
---|
| 383 | return value; |
---|
| 384 | if ( !strcmp(value, "manual") ) |
---|
| 385 | return value; |
---|
| 386 | if ( !strcmp(value, "always") ) |
---|
| 387 | return value; |
---|
| 388 | return NULL; |
---|
| 389 | } |
---|
[823de9d] | 390 | |
---|