Scan media entities as well, not just url entities. This should expand more
[bitlbee.git] / set.c
bloba1eb9f0342fad99d343c1b743dd95fdb43496e35
1 /********************************************************************\
2 * BitlBee -- An IRC to other IM-networks gateway *
3 * *
4 * Copyright 2002-2005 Wilmer van der Gaast and others *
5 \********************************************************************/
7 /* Some stuff to register, handle and save user preferences */
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.
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.
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
25 #define BITLBEE_CORE
26 #include "bitlbee.h"
28 /* Used to use NULL for this, but NULL is actually a "valid" value. */
29 char *SET_INVALID = "nee";
31 set_t *set_add( set_t **head, const char *key, const char *def, set_eval eval, void *data )
33 set_t *s = set_find( head, key );
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. */
37 if( !s )
39 if( ( s = *head ) )
41 while( s->next ) s = s->next;
42 s->next = g_new0( set_t, 1 );
43 s = s->next;
45 else
47 s = *head = g_new0( set_t, 1 );
49 s->key = g_strdup( key );
52 if( s->def )
54 g_free( s->def );
55 s->def = NULL;
57 if( def ) s->def = g_strdup( def );
59 s->eval = eval;
60 s->data = data;
62 return s;
65 set_t *set_find( set_t **head, const char *key )
67 set_t *s = *head;
69 while( s )
71 if( g_strcasecmp( s->key, key ) == 0 ||
72 ( s->old_key && g_strcasecmp( s->old_key, key ) == 0 ) )
73 break;
74 s = s->next;
77 return s;
80 char *set_getstr( set_t **head, const char *key )
82 set_t *s = set_find( head, key );
84 if( !s || ( !s->value && !s->def ) )
85 return NULL;
87 return set_value( s );
90 int set_getint( set_t **head, const char *key )
92 char *s = set_getstr( head, key );
93 int i = 0;
95 if( !s )
96 return 0;
98 if( sscanf( s, "%d", &i ) != 1 )
99 return 0;
101 return i;
104 int set_getbool( set_t **head, const char *key )
106 char *s = set_getstr( head, key );
108 if( !s )
109 return 0;
111 return bool2int( s );
114 int set_isvisible( set_t *set )
116 /* the default value is not stored in value, only in def */
117 return !( ( set->flags & SET_HIDDEN ) ||
118 ( ( set->flags & SET_HIDDEN_DEFAULT ) &&
119 ( set->value == NULL ) ) );
122 int set_setstr( set_t **head, const char *key, char *value )
124 set_t *s = set_find( head, key );
125 char *nv = value;
127 if( !s )
129 Used to do this, but it never really made sense.
130 s = set_add( head, key, NULL, NULL, NULL );
132 return 0;
134 if( value == NULL && ( s->flags & SET_NULL_OK ) == 0 )
135 return 0;
137 /* Call the evaluator. For invalid values, evaluators should now
138 return SET_INVALID, but previously this was NULL. Try to handle
139 that too if NULL is not an allowed value for this setting. */
140 if( s->eval && ( ( nv = s->eval( s, value ) ) == SET_INVALID ||
141 ( ( s->flags & SET_NULL_OK ) == 0 && nv == NULL ) ) )
142 return 0;
144 if( s->value )
146 g_free( s->value );
147 s->value = NULL;
150 /* If there's a default setting and it's equal to what we're trying to
151 set, stick with s->value = NULL. Otherwise, remember the setting. */
152 if( !s->def || ( strcmp( nv, s->def ) != 0 ) )
153 s->value = g_strdup( nv );
155 if( nv != value )
156 g_free( nv );
158 return 1;
161 int set_setint( set_t **head, const char *key, int value )
163 char s[24]; /* Not quite 128-bit clean eh? ;-) */
165 g_snprintf( s, sizeof( s ), "%d", value );
166 return set_setstr( head, key, s );
169 void set_del( set_t **head, const char *key )
171 set_t *s = *head, *t = NULL;
173 while( s )
175 if( g_strcasecmp( s->key, key ) == 0 )
176 break;
177 s = (t=s)->next;
179 if( s )
181 if( t )
182 t->next = s->next;
183 else
184 *head = s->next;
186 g_free( s->key );
187 g_free( s->old_key );
188 g_free( s->value );
189 g_free( s->def );
190 g_free( s );
194 int set_reset( set_t **head, const char *key )
196 set_t *s;
198 s = set_find( head, key );
199 if( s )
200 return set_setstr( head, key, s->def );
202 return 0;
205 char *set_eval_int( set_t *set, char *value )
207 char *s = value;
209 /* Allow a minus at the first position. */
210 if( *s == '-' )
211 s ++;
213 for( ; *s; s ++ )
214 if( !isdigit( *s ) )
215 return SET_INVALID;
217 return value;
220 char *set_eval_bool( set_t *set, char *value )
222 return is_bool( value ) ? value : SET_INVALID;
225 char *set_eval_list( set_t *set, char *value )
227 GSList *options = set->eval_data, *opt;
229 for( opt = options; opt; opt = opt->next )
230 if( strcmp( value, opt->data ) == 0 )
231 return value;
233 /* TODO: It'd be nice to show the user a list of allowed values,
234 but we don't have enough context here to do that. May
235 want to fix that. */
237 return NULL;
240 char *set_eval_to_char( set_t *set, char *value )
242 char *s = g_new( char, 3 );
244 if( *value == ' ' )
245 strcpy( s, " " );
246 else
247 sprintf( s, "%c ", *value );
249 return s;
252 char *set_eval_oauth( set_t *set, char *value )
254 account_t *acc = set->data;
256 if( bool2int( value ) && strcmp( acc->pass, PASSWORD_PENDING ) == 0 )
257 *acc->pass = '\0';
259 return set_eval_bool( set, value );