source: set.c @ 92cb8c4

Last change on this file since 92cb8c4 was 88eaf4b, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-06T01:24:13Z

buddy_sendbuffer should be renamed, since it has to work for users and
chats. For smooth transitions, add an old_key attribute to settings
which can be used as a (temporary) alias.

  • Property mode set to 100644
File size: 5.8 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2005 Wilmer van der Gaast and others                *
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/* Used to use NULL for this, but NULL is actually a "valid" value. */
29char *SET_INVALID = "nee";
30
31set_t *set_add( set_t **head, char *key, char *def, set_eval eval, void *data )
32{
33        set_t *s = set_find( head, key );
34       
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 )
38        {
39                if( ( s = *head ) )
40                {
41                        while( s->next ) s = s->next;
42                        s->next = g_new0( set_t, 1 );
43                        s = s->next;
44                }
45                else
46                {
47                        s = *head = g_new0( set_t, 1 );
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       
59        s->eval = eval;
60        s->data = data;
61       
62        return s;
63}
64
65set_t *set_find( set_t **head, char *key )
66{
67        set_t *s = *head;
68       
69        while( s )
70        {
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;
75        }
76       
77        return s;
78}
79
80char *set_getstr( set_t **head, char *key )
81{
82        set_t *s = set_find( head, key );
83       
84        if( !s || ( !s->value && !s->def ) )
85                return NULL;
86       
87        return s->value ? s->value : s->def;
88}
89
90int set_getint( set_t **head, char *key )
91{
92        char *s = set_getstr( head, key );
93        int i = 0;
94       
95        if( !s )
96                return 0;
97       
98        if( sscanf( s, "%d", &i ) != 1 )
99                return 0;
100       
101        return i;
102}
103
104int set_getbool( set_t **head, char *key )
105{
106        char *s = set_getstr( head, key );
107       
108        if( !s )
109                return 0;
110       
111        return bool2int( s );
112}
113
114int set_setstr( set_t **head, char *key, char *value )
115{
116        set_t *s = set_find( head, key );
117        char *nv = value;
118       
119        if( !s )
120                /*
121                Used to do this, but it never really made sense.
122                s = set_add( head, key, NULL, NULL, NULL );
123                */
124                return 0;
125       
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 ) ) )
134                return 0;
135       
136        if( s->value )
137        {
138                g_free( s->value );
139                s->value = NULL;
140        }
141       
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. */
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       
150        return 1;
151}
152
153int set_setint( set_t **head, char *key, int value )
154{
155        char s[24];     /* Not quite 128-bit clean eh? ;-) */
156       
157        g_snprintf( s, sizeof( s ), "%d", value );
158        return set_setstr( head, key, s );
159}
160
161void set_del( set_t **head, char *key )
162{
163        set_t *s = *head, *t = NULL;
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        {
173                if( t )
174                        t->next = s->next;
175                else
176                        *head = s->next;
177               
178                g_free( s->key );
179                g_free( s->old_key );
180                g_free( s->value );
181                g_free( s->def );
182                g_free( s );
183        }
184}
185
186int set_reset( set_t **head, char *key )
187{
188        set_t *s;
189       
190        s = set_find( head, key );
191        if( s )
192                return set_setstr( head, key, s->def );
193       
194        return 0;
195}
196
197char *set_eval_int( set_t *set, char *value )
198{
199        char *s = value;
200       
201        /* Allow a minus at the first position. */
202        if( *s == '-' )
203                s ++;
204       
205        for( ; *s; s ++ )
206                if( !isdigit( *s ) )
207                        return SET_INVALID;
208       
209        return value;
210}
211
212char *set_eval_bool( set_t *set, char *value )
213{
214        return is_bool( value ) ? value : SET_INVALID;
215}
216
217char *set_eval_to_char( set_t *set, char *value )
218{
219        char *s = g_new( char, 3 );
220       
221        if( *value == ' ' )
222                strcpy( s, " " );
223        else
224                sprintf( s, "%c ", *value );
225       
226        return s;
227}
228
229/*
230char *set_eval_ops( set_t *set, char *value )
231{
232        irc_t *irc = set->data;
233       
234        if( g_strcasecmp( value, "user" ) == 0 )
235                irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost,
236                                                              irc->channel, "+o-o", irc->nick, irc->mynick );
237        else if( g_strcasecmp( value, "root" ) == 0 )
238                irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost,
239                                                              irc->channel, "-o+o", irc->nick, irc->mynick );
240        else if( g_strcasecmp( value, "both" ) == 0 )
241                irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost,
242                                                              irc->channel, "+oo", irc->nick, irc->mynick );
243        else if( g_strcasecmp( value, "none" ) == 0 )
244                irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost,
245                                                              irc->channel, "-oo", irc->nick, irc->mynick );
246        else
247                return SET_INVALID;
248       
249        return value;
250}
251*/
Note: See TracBrowser for help on using the repository browser.