[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 | |
---|
[7125cb3] | 28 | /* Used to use NULL for this, but NULL is actually a "valid" value. */ |
---|
| 29 | char *SET_INVALID = "nee"; |
---|
| 30 | |
---|
[0f7ee7e5] | 31 | set_t *set_add( set_t **head, const char *key, const 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 | |
---|
[b74b287] | 65 | set_t *set_find( set_t **head, const char *key ) |
---|
[b7d3cc34] | 66 | { |
---|
[5c9512f] | 67 | set_t *s = *head; |
---|
[b7d3cc34] | 68 | |
---|
| 69 | while( s ) |
---|
| 70 | { |
---|
[88eaf4b] | 71 | if( g_strcasecmp( s->key, key ) == 0 || |
---|
| 72 | ( s->old_key && g_strcasecmp( s->old_key, key ) == 0 ) ) |
---|
[b7d3cc34] | 73 | break; |
---|
| 74 | s = s->next; |
---|
| 75 | } |
---|
| 76 | |
---|
[5c9512f] | 77 | return s; |
---|
[b7d3cc34] | 78 | } |
---|
| 79 | |
---|
[b74b287] | 80 | char *set_getstr( set_t **head, const char *key ) |
---|
[b7d3cc34] | 81 | { |
---|
[5c9512f] | 82 | set_t *s = set_find( head, key ); |
---|
[b7d3cc34] | 83 | |
---|
| 84 | if( !s || ( !s->value && !s->def ) ) |
---|
[5c9512f] | 85 | return NULL; |
---|
[b7d3cc34] | 86 | |
---|
[5c9512f] | 87 | return s->value ? s->value : s->def; |
---|
[b7d3cc34] | 88 | } |
---|
| 89 | |
---|
[b74b287] | 90 | int set_getint( set_t **head, const char *key ) |
---|
[b7d3cc34] | 91 | { |
---|
[5c9512f] | 92 | char *s = set_getstr( head, key ); |
---|
[b7d3cc34] | 93 | int i = 0; |
---|
| 94 | |
---|
| 95 | if( !s ) |
---|
[5c9512f] | 96 | return 0; |
---|
[b7d3cc34] | 97 | |
---|
| 98 | if( sscanf( s, "%d", &i ) != 1 ) |
---|
[5c9512f] | 99 | return 0; |
---|
[b7d3cc34] | 100 | |
---|
[5c9512f] | 101 | return i; |
---|
[b7d3cc34] | 102 | } |
---|
| 103 | |
---|
[b74b287] | 104 | int set_getbool( set_t **head, const char *key ) |
---|
[5100caa] | 105 | { |
---|
| 106 | char *s = set_getstr( head, key ); |
---|
| 107 | |
---|
| 108 | if( !s ) |
---|
| 109 | return 0; |
---|
| 110 | |
---|
| 111 | return bool2int( s ); |
---|
| 112 | } |
---|
| 113 | |
---|
[b74b287] | 114 | int set_setstr( set_t **head, const char *key, char *value ) |
---|
[b7d3cc34] | 115 | { |
---|
[5c9512f] | 116 | set_t *s = set_find( head, key ); |
---|
[b7d3cc34] | 117 | char *nv = value; |
---|
| 118 | |
---|
| 119 | if( !s ) |
---|
[7125cb3] | 120 | /* |
---|
| 121 | Used to do this, but it never really made sense. |
---|
[5c9512f] | 122 | s = set_add( head, key, NULL, NULL, NULL ); |
---|
[7125cb3] | 123 | */ |
---|
| 124 | return 0; |
---|
[b7d3cc34] | 125 | |
---|
[7125cb3] | 126 | if( value == NULL && ( s->flags & SET_NULL_OK ) == 0 ) |
---|
| 127 | return 0; |
---|
| 128 | |
---|
| 129 | /* Call the evaluator. For invalid values, evaluators should now |
---|
| 130 | return SET_INVALID, but previously this was NULL. Try to handle |
---|
| 131 | that too if NULL is not an allowed value for this setting. */ |
---|
| 132 | if( s->eval && ( ( nv = s->eval( s, value ) ) == SET_INVALID || |
---|
| 133 | ( ( s->flags & SET_NULL_OK ) == 0 && nv == NULL ) ) ) |
---|
[5c9512f] | 134 | return 0; |
---|
[b7d3cc34] | 135 | |
---|
| 136 | if( s->value ) |
---|
| 137 | { |
---|
| 138 | g_free( s->value ); |
---|
| 139 | s->value = NULL; |
---|
| 140 | } |
---|
| 141 | |
---|
[5c9512f] | 142 | /* If there's a default setting and it's equal to what we're trying to |
---|
| 143 | set, stick with s->value = NULL. Otherwise, remember the setting. */ |
---|
[b7d3cc34] | 144 | if( !s->def || ( strcmp( nv, s->def ) != 0 ) ) |
---|
| 145 | s->value = g_strdup( nv ); |
---|
| 146 | |
---|
| 147 | if( nv != value ) |
---|
| 148 | g_free( nv ); |
---|
| 149 | |
---|
[5c9512f] | 150 | return 1; |
---|
[b7d3cc34] | 151 | } |
---|
| 152 | |
---|
[b74b287] | 153 | int set_setint( set_t **head, const char *key, int value ) |
---|
[b7d3cc34] | 154 | { |
---|
| 155 | char s[24]; /* Not quite 128-bit clean eh? ;-) */ |
---|
| 156 | |
---|
[5c9512f] | 157 | g_snprintf( s, sizeof( s ), "%d", value ); |
---|
| 158 | return set_setstr( head, key, s ); |
---|
[b7d3cc34] | 159 | } |
---|
| 160 | |
---|
[b74b287] | 161 | void set_del( set_t **head, const char *key ) |
---|
[b7d3cc34] | 162 | { |
---|
[5c9512f] | 163 | set_t *s = *head, *t = NULL; |
---|
[b7d3cc34] | 164 | |
---|
| 165 | while( s ) |
---|
| 166 | { |
---|
| 167 | if( g_strcasecmp( s->key, key ) == 0 ) |
---|
| 168 | break; |
---|
| 169 | s = (t=s)->next; |
---|
| 170 | } |
---|
| 171 | if( s ) |
---|
| 172 | { |
---|
[dd89a55] | 173 | if( t ) |
---|
| 174 | t->next = s->next; |
---|
| 175 | else |
---|
[5c9512f] | 176 | *head = s->next; |
---|
[dd89a55] | 177 | |
---|
[b7d3cc34] | 178 | g_free( s->key ); |
---|
[88eaf4b] | 179 | g_free( s->old_key ); |
---|
| 180 | g_free( s->value ); |
---|
| 181 | g_free( s->def ); |
---|
[b7d3cc34] | 182 | g_free( s ); |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
[b74b287] | 186 | int set_reset( set_t **head, const char *key ) |
---|
[cd428e4] | 187 | { |
---|
| 188 | set_t *s; |
---|
| 189 | |
---|
| 190 | s = set_find( head, key ); |
---|
| 191 | if( s ) |
---|
[f3579fd] | 192 | return set_setstr( head, key, s->def ); |
---|
| 193 | |
---|
| 194 | return 0; |
---|
[cd428e4] | 195 | } |
---|
| 196 | |
---|
[5c9512f] | 197 | char *set_eval_int( set_t *set, char *value ) |
---|
[b7d3cc34] | 198 | { |
---|
[6237ded] | 199 | char *s = value; |
---|
[b7d3cc34] | 200 | |
---|
[5eec897] | 201 | /* Allow a minus at the first position. */ |
---|
| 202 | if( *s == '-' ) |
---|
| 203 | s ++; |
---|
| 204 | |
---|
[6237ded] | 205 | for( ; *s; s ++ ) |
---|
[5100caa] | 206 | if( !isdigit( *s ) ) |
---|
[7125cb3] | 207 | return SET_INVALID; |
---|
[b7d3cc34] | 208 | |
---|
[5c9512f] | 209 | return value; |
---|
[b7d3cc34] | 210 | } |
---|
| 211 | |
---|
[5c9512f] | 212 | char *set_eval_bool( set_t *set, char *value ) |
---|
[b7d3cc34] | 213 | { |
---|
[7125cb3] | 214 | return is_bool( value ) ? value : SET_INVALID; |
---|
[b7d3cc34] | 215 | } |
---|
| 216 | |
---|
[56244c0] | 217 | char *set_eval_list( set_t *set, char *value ) |
---|
| 218 | { |
---|
| 219 | GSList *options = set->eval_data, *opt; |
---|
| 220 | |
---|
| 221 | for( opt = options; opt; opt = opt->next ) |
---|
| 222 | if( strcmp( value, opt->data ) == 0 ) |
---|
| 223 | return value; |
---|
| 224 | |
---|
| 225 | /* TODO: It'd be nice to show the user a list of allowed values, |
---|
| 226 | but we don't have enough context here to do that. May |
---|
| 227 | want to fix that. */ |
---|
| 228 | |
---|
| 229 | return NULL; |
---|
| 230 | } |
---|
| 231 | |
---|
[5c9512f] | 232 | char *set_eval_to_char( set_t *set, char *value ) |
---|
[b7d3cc34] | 233 | { |
---|
| 234 | char *s = g_new( char, 3 ); |
---|
| 235 | |
---|
| 236 | if( *value == ' ' ) |
---|
| 237 | strcpy( s, " " ); |
---|
| 238 | else |
---|
| 239 | sprintf( s, "%c ", *value ); |
---|
| 240 | |
---|
[5c9512f] | 241 | return s; |
---|
[b7d3cc34] | 242 | } |
---|
| 243 | |
---|
[3ddb7477] | 244 | /* |
---|
[5c9512f] | 245 | char *set_eval_ops( set_t *set, char *value ) |
---|
[b7d3cc34] | 246 | { |
---|
[5c9512f] | 247 | irc_t *irc = set->data; |
---|
| 248 | |
---|
[b7d3cc34] | 249 | if( g_strcasecmp( value, "user" ) == 0 ) |
---|
| 250 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
| 251 | irc->channel, "+o-o", irc->nick, irc->mynick ); |
---|
| 252 | else if( g_strcasecmp( value, "root" ) == 0 ) |
---|
| 253 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
| 254 | irc->channel, "-o+o", irc->nick, irc->mynick ); |
---|
| 255 | else if( g_strcasecmp( value, "both" ) == 0 ) |
---|
| 256 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
| 257 | irc->channel, "+oo", irc->nick, irc->mynick ); |
---|
| 258 | else if( g_strcasecmp( value, "none" ) == 0 ) |
---|
| 259 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
| 260 | irc->channel, "-oo", irc->nick, irc->mynick ); |
---|
[5c9512f] | 261 | else |
---|
[7125cb3] | 262 | return SET_INVALID; |
---|
[b7d3cc34] | 263 | |
---|
[5c9512f] | 264 | return value; |
---|
[b7d3cc34] | 265 | } |
---|
[3ddb7477] | 266 | */ |
---|