source: set.c @ 5c9512f

Last change on this file since 5c9512f 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
RevLine 
[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
[5c9512f]28set_t *set_add( set_t **head, char *key, char *def, void *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]62set_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]76char *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]86int 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
[5c9512f]103int set_setstr( set_t **head, char *key, char *value )
[b7d3cc34]104{
[5c9512f]105        set_t *s = set_find( head, key );
[b7d3cc34]106        char *nv = value;
107       
108        if( !s )
[5c9512f]109                s = set_add( head, key, NULL, NULL, NULL );
[b7d3cc34]110       
[5c9512f]111        if( s->eval && !( nv = s->eval( s, value ) ) )
112                return 0;
[b7d3cc34]113       
114        if( s->value )
115        {
116                g_free( s->value );
117                s->value = NULL;
118        }
119       
[5c9512f]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. */
[b7d3cc34]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       
[5c9512f]128        return 1;
[b7d3cc34]129}
130
[5c9512f]131int set_setint( set_t **head, char *key, int value )
[b7d3cc34]132{
133        char s[24];     /* Not quite 128-bit clean eh? ;-) */
134       
[5c9512f]135        g_snprintf( s, sizeof( s ), "%d", value );
136        return set_setstr( head, key, s );
[b7d3cc34]137}
138
[5c9512f]139void set_del( set_t **head, char *key )
[b7d3cc34]140{
[5c9512f]141        set_t *s = *head, *t = NULL;
[b7d3cc34]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        {
[dd89a55]151                if( t )
152                        t->next = s->next;
153                else
[5c9512f]154                        *head = s->next;
[dd89a55]155               
[b7d3cc34]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
[5c9512f]163char *set_eval_int( set_t *set, char *value )
[b7d3cc34]164{
[5c9512f]165        char *s;
[b7d3cc34]166       
[5c9512f]167        for( s = value; *s; s ++ )
[b7d3cc34]168                if( *s < '0' || *s > '9' )
[5c9512f]169                        return NULL;
[b7d3cc34]170       
[5c9512f]171        return value;
[b7d3cc34]172}
173
[5c9512f]174char *set_eval_bool( set_t *set, char *value )
[b7d3cc34]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 );
[5c9512f]180        return( set_eval_int( set, value ) );
[b7d3cc34]181}
182
[5c9512f]183char *set_eval_to_char( set_t *set, char *value )
[b7d3cc34]184{
185        char *s = g_new( char, 3 );
186       
187        if( *value == ' ' )
188                strcpy( s, " " );
189        else
190                sprintf( s, "%c ", *value );
191       
[5c9512f]192        return s;
[b7d3cc34]193}
194
[5c9512f]195char *set_eval_ops( set_t *set, char *value )
[b7d3cc34]196{
[5c9512f]197        irc_t *irc = set->data;
198       
[b7d3cc34]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 );
[5c9512f]211        else
212                return NULL;
[b7d3cc34]213       
[5c9512f]214        return value;
[b7d3cc34]215}
216
[5c9512f]217char *set_eval_charset( set_t *set, char *value )
[1719464]218{
219        GIConv cd;
220
221        if ( g_strncasecmp( value, "none", 4 ) == 0 )
[5c9512f]222                return value;
[1719464]223
224        cd = g_iconv_open( "UTF-8", value );
225        if( cd == (GIConv) -1 )
[5c9512f]226                return NULL;
[1719464]227
228        g_iconv_close( cd );
[5c9512f]229        return value;
[1719464]230}
Note: See TracBrowser for help on using the repository browser.