[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 | |
---|
| 28 | set_t *set_add( irc_t *irc, char *key, char *def, void *eval ) |
---|
| 29 | { |
---|
| 30 | set_t *s = set_find( irc, key ); |
---|
| 31 | |
---|
| 32 | if( !s ) |
---|
| 33 | { |
---|
| 34 | if( ( s = irc->set ) ) |
---|
| 35 | { |
---|
| 36 | while( s->next ) s = s->next; |
---|
| 37 | s->next = g_new ( set_t, 1 ); |
---|
| 38 | s = s->next; |
---|
| 39 | } |
---|
| 40 | else |
---|
| 41 | { |
---|
| 42 | s = irc->set = g_new( set_t, 1 ); |
---|
| 43 | } |
---|
| 44 | memset( s, 0, sizeof( set_t ) ); |
---|
| 45 | s->key = g_strdup( key ); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | if( s->def ) |
---|
| 49 | { |
---|
| 50 | g_free( s->def ); |
---|
| 51 | s->def = NULL; |
---|
| 52 | } |
---|
| 53 | if( def ) s->def = g_strdup( def ); |
---|
| 54 | |
---|
| 55 | if( s->eval ) |
---|
| 56 | { |
---|
| 57 | g_free( s->eval ); |
---|
| 58 | s->eval = NULL; |
---|
| 59 | } |
---|
| 60 | if( eval ) s->eval = eval; |
---|
| 61 | |
---|
| 62 | return( s ); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | set_t *set_find( irc_t *irc, char *key ) |
---|
| 66 | { |
---|
| 67 | set_t *s = irc->set; |
---|
| 68 | |
---|
| 69 | while( s ) |
---|
| 70 | { |
---|
| 71 | if( g_strcasecmp( s->key, key ) == 0 ) |
---|
| 72 | break; |
---|
| 73 | s = s->next; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | return( s ); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | char *set_getstr( irc_t *irc, char *key ) |
---|
| 80 | { |
---|
| 81 | set_t *s = set_find( irc, key ); |
---|
| 82 | |
---|
| 83 | if( !s || ( !s->value && !s->def ) ) |
---|
| 84 | return( NULL ); |
---|
| 85 | |
---|
| 86 | return( s->value?s->value:s->def ); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | int set_getint( irc_t *irc, char *key ) |
---|
| 90 | { |
---|
| 91 | char *s = set_getstr( irc, key ); |
---|
| 92 | int i = 0; |
---|
| 93 | |
---|
| 94 | if( !s ) |
---|
| 95 | return( 0 ); |
---|
| 96 | |
---|
| 97 | if( ( g_strcasecmp( s, "true" ) == 0 ) || ( g_strcasecmp( s, "yes" ) == 0 ) || ( g_strcasecmp( s, "on" ) == 0 ) ) |
---|
| 98 | return( 1 ); |
---|
| 99 | |
---|
| 100 | if( sscanf( s, "%d", &i ) != 1 ) |
---|
| 101 | return( 0 ); |
---|
| 102 | |
---|
| 103 | return( i ); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | int set_setstr( irc_t *irc, char *key, char *value ) |
---|
| 107 | { |
---|
| 108 | set_t *s = set_find( irc, key ); |
---|
| 109 | char *nv = value; |
---|
| 110 | |
---|
| 111 | if( !s ) |
---|
| 112 | s = set_add( irc, key, NULL, NULL ); |
---|
| 113 | |
---|
| 114 | if( s->eval && !( nv = s->eval( irc, s, value ) ) ) |
---|
| 115 | return( 0 ); |
---|
| 116 | |
---|
| 117 | if( s->value ) |
---|
| 118 | { |
---|
| 119 | g_free( s->value ); |
---|
| 120 | s->value = NULL; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | if( !s->def || ( strcmp( nv, s->def ) != 0 ) ) |
---|
| 124 | s->value = g_strdup( nv ); |
---|
| 125 | |
---|
| 126 | if( nv != value ) |
---|
| 127 | g_free( nv ); |
---|
| 128 | |
---|
| 129 | return( 1 ); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | int set_setint( irc_t *irc, char *key, int value ) |
---|
| 133 | { |
---|
| 134 | char s[24]; /* Not quite 128-bit clean eh? ;-) */ |
---|
| 135 | |
---|
| 136 | sprintf( s, "%d", value ); |
---|
| 137 | return( set_setstr( irc, key, s ) ); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | void set_del( irc_t *irc, char *key ) |
---|
| 141 | { |
---|
| 142 | set_t *s = irc->set, *t = NULL; |
---|
| 143 | |
---|
| 144 | while( s ) |
---|
| 145 | { |
---|
| 146 | if( g_strcasecmp( s->key, key ) == 0 ) |
---|
| 147 | break; |
---|
| 148 | s = (t=s)->next; |
---|
| 149 | } |
---|
| 150 | if( s ) |
---|
| 151 | { |
---|
[dd89a55] | 152 | if( t ) |
---|
| 153 | t->next = s->next; |
---|
| 154 | else |
---|
| 155 | irc->set = s->next; |
---|
| 156 | |
---|
[b7d3cc34] | 157 | g_free( s->key ); |
---|
| 158 | if( s->value ) g_free( s->value ); |
---|
| 159 | if( s->def ) g_free( s->def ); |
---|
| 160 | g_free( s ); |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | char *set_eval_int( irc_t *irc, set_t *set, char *value ) |
---|
| 165 | { |
---|
| 166 | char *s = value; |
---|
| 167 | |
---|
| 168 | for( ; *s; s ++ ) |
---|
| 169 | if( *s < '0' || *s > '9' ) |
---|
| 170 | return( NULL ); |
---|
| 171 | |
---|
| 172 | return( value ); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | char *set_eval_bool( irc_t *irc, set_t *set, char *value ) |
---|
| 176 | { |
---|
| 177 | if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) ) |
---|
| 178 | return( value ); |
---|
| 179 | if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) ) |
---|
| 180 | return( value ); |
---|
| 181 | return( set_eval_int( irc, set, value ) ); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | char *set_eval_to_char( irc_t *irc, set_t *set, char *value ) |
---|
| 185 | { |
---|
| 186 | char *s = g_new( char, 3 ); |
---|
| 187 | |
---|
| 188 | if( *value == ' ' ) |
---|
| 189 | strcpy( s, " " ); |
---|
| 190 | else |
---|
| 191 | sprintf( s, "%c ", *value ); |
---|
| 192 | |
---|
| 193 | return( s ); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | char *set_eval_ops( irc_t *irc, set_t *set, char *value ) |
---|
| 197 | { |
---|
| 198 | if( g_strcasecmp( value, "user" ) == 0 ) |
---|
| 199 | { |
---|
| 200 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
| 201 | irc->channel, "+o-o", irc->nick, irc->mynick ); |
---|
| 202 | return( value ); |
---|
| 203 | } |
---|
| 204 | else if( g_strcasecmp( value, "root" ) == 0 ) |
---|
| 205 | { |
---|
| 206 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
| 207 | irc->channel, "-o+o", irc->nick, irc->mynick ); |
---|
| 208 | return( value ); |
---|
| 209 | } |
---|
| 210 | else if( g_strcasecmp( value, "both" ) == 0 ) |
---|
| 211 | { |
---|
| 212 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
| 213 | irc->channel, "+oo", irc->nick, irc->mynick ); |
---|
| 214 | return( value ); |
---|
| 215 | } |
---|
| 216 | else if( g_strcasecmp( value, "none" ) == 0 ) |
---|
| 217 | { |
---|
| 218 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
| 219 | irc->channel, "-oo", irc->nick, irc->mynick ); |
---|
| 220 | return( value ); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | return( NULL ); |
---|
| 224 | } |
---|
| 225 | |
---|