[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( ( g_strcasecmp( s, "true" ) == 0 ) || ( g_strcasecmp( s, "yes" ) == 0 ) || ( g_strcasecmp( s, "on" ) == 0 ) ) |
---|
[5c9512f] | 95 | return 1; |
---|
[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 ) |
---|
[5c9512f] | 119 | s = set_add( head, key, NULL, NULL, NULL ); |
---|
[b7d3cc34] | 120 | |
---|
[5c9512f] | 121 | if( s->eval && !( nv = s->eval( s, value ) ) ) |
---|
| 122 | return 0; |
---|
[b7d3cc34] | 123 | |
---|
| 124 | if( s->value ) |
---|
| 125 | { |
---|
| 126 | g_free( s->value ); |
---|
| 127 | s->value = NULL; |
---|
| 128 | } |
---|
| 129 | |
---|
[5c9512f] | 130 | /* If there's a default setting and it's equal to what we're trying to |
---|
| 131 | set, stick with s->value = NULL. Otherwise, remember the setting. */ |
---|
[b7d3cc34] | 132 | if( !s->def || ( strcmp( nv, s->def ) != 0 ) ) |
---|
| 133 | s->value = g_strdup( nv ); |
---|
| 134 | |
---|
| 135 | if( nv != value ) |
---|
| 136 | g_free( nv ); |
---|
| 137 | |
---|
[5c9512f] | 138 | return 1; |
---|
[b7d3cc34] | 139 | } |
---|
| 140 | |
---|
[5c9512f] | 141 | int set_setint( set_t **head, char *key, int value ) |
---|
[b7d3cc34] | 142 | { |
---|
| 143 | char s[24]; /* Not quite 128-bit clean eh? ;-) */ |
---|
| 144 | |
---|
[5c9512f] | 145 | g_snprintf( s, sizeof( s ), "%d", value ); |
---|
| 146 | return set_setstr( head, key, s ); |
---|
[b7d3cc34] | 147 | } |
---|
| 148 | |
---|
[5c9512f] | 149 | void set_del( set_t **head, char *key ) |
---|
[b7d3cc34] | 150 | { |
---|
[5c9512f] | 151 | set_t *s = *head, *t = NULL; |
---|
[b7d3cc34] | 152 | |
---|
| 153 | while( s ) |
---|
| 154 | { |
---|
| 155 | if( g_strcasecmp( s->key, key ) == 0 ) |
---|
| 156 | break; |
---|
| 157 | s = (t=s)->next; |
---|
| 158 | } |
---|
| 159 | if( s ) |
---|
| 160 | { |
---|
[dd89a55] | 161 | if( t ) |
---|
| 162 | t->next = s->next; |
---|
| 163 | else |
---|
[5c9512f] | 164 | *head = s->next; |
---|
[dd89a55] | 165 | |
---|
[b7d3cc34] | 166 | g_free( s->key ); |
---|
| 167 | if( s->value ) g_free( s->value ); |
---|
| 168 | if( s->def ) g_free( s->def ); |
---|
| 169 | g_free( s ); |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | |
---|
[5c9512f] | 173 | char *set_eval_int( set_t *set, char *value ) |
---|
[b7d3cc34] | 174 | { |
---|
[6237ded] | 175 | char *s = value; |
---|
[b7d3cc34] | 176 | |
---|
[5eec897] | 177 | /* Allow a minus at the first position. */ |
---|
| 178 | if( *s == '-' ) |
---|
| 179 | s ++; |
---|
| 180 | |
---|
[6237ded] | 181 | for( ; *s; s ++ ) |
---|
[5100caa] | 182 | if( !isdigit( *s ) ) |
---|
[5c9512f] | 183 | return NULL; |
---|
[b7d3cc34] | 184 | |
---|
[5c9512f] | 185 | return value; |
---|
[b7d3cc34] | 186 | } |
---|
| 187 | |
---|
[5c9512f] | 188 | char *set_eval_bool( set_t *set, char *value ) |
---|
[b7d3cc34] | 189 | { |
---|
[5100caa] | 190 | return is_bool( value ) ? value : NULL; |
---|
[b7d3cc34] | 191 | } |
---|
| 192 | |
---|
[5c9512f] | 193 | char *set_eval_to_char( set_t *set, char *value ) |
---|
[b7d3cc34] | 194 | { |
---|
| 195 | char *s = g_new( char, 3 ); |
---|
| 196 | |
---|
| 197 | if( *value == ' ' ) |
---|
| 198 | strcpy( s, " " ); |
---|
| 199 | else |
---|
| 200 | sprintf( s, "%c ", *value ); |
---|
| 201 | |
---|
[5c9512f] | 202 | return s; |
---|
[b7d3cc34] | 203 | } |
---|
| 204 | |
---|
[5c9512f] | 205 | char *set_eval_ops( set_t *set, char *value ) |
---|
[b7d3cc34] | 206 | { |
---|
[5c9512f] | 207 | irc_t *irc = set->data; |
---|
| 208 | |
---|
[b7d3cc34] | 209 | if( g_strcasecmp( value, "user" ) == 0 ) |
---|
| 210 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
| 211 | irc->channel, "+o-o", irc->nick, irc->mynick ); |
---|
| 212 | else if( g_strcasecmp( value, "root" ) == 0 ) |
---|
| 213 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
| 214 | irc->channel, "-o+o", irc->nick, irc->mynick ); |
---|
| 215 | else if( g_strcasecmp( value, "both" ) == 0 ) |
---|
| 216 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
| 217 | irc->channel, "+oo", irc->nick, irc->mynick ); |
---|
| 218 | else if( g_strcasecmp( value, "none" ) == 0 ) |
---|
| 219 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
| 220 | irc->channel, "-oo", irc->nick, irc->mynick ); |
---|
[5c9512f] | 221 | else |
---|
| 222 | return NULL; |
---|
[b7d3cc34] | 223 | |
---|
[5c9512f] | 224 | return value; |
---|
[b7d3cc34] | 225 | } |
---|
| 226 | |
---|
[5c9512f] | 227 | char *set_eval_charset( set_t *set, char *value ) |
---|
[1719464] | 228 | { |
---|
| 229 | GIConv cd; |
---|
| 230 | |
---|
| 231 | if ( g_strncasecmp( value, "none", 4 ) == 0 ) |
---|
[5c9512f] | 232 | return value; |
---|
[1719464] | 233 | |
---|
| 234 | cd = g_iconv_open( "UTF-8", value ); |
---|
| 235 | if( cd == (GIConv) -1 ) |
---|
[5c9512f] | 236 | return NULL; |
---|
[1719464] | 237 | |
---|
| 238 | g_iconv_close( cd ); |
---|
[5c9512f] | 239 | return value; |
---|
[1719464] | 240 | } |
---|