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