source: set.c @ 0a3c243

Last change on this file since 0a3c243 was 5c9512f, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-06-30T09:17:18Z

Made set.c API more generic so it's not specific to irc_t structures anymore,
but can be used for account_t structures too, for example.

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