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 | set_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 | |
---|
62 | set_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 | |
---|
76 | char *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 | |
---|
86 | int 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 | |
---|
103 | int set_getbool( set_t **head, char *key ) |
---|
104 | { |
---|
105 | char *s = set_getstr( head, key ); |
---|
106 | |
---|
107 | if( !s ) |
---|
108 | return 0; |
---|
109 | |
---|
110 | return bool2int( s ); |
---|
111 | } |
---|
112 | |
---|
113 | int set_setstr( set_t **head, char *key, char *value ) |
---|
114 | { |
---|
115 | set_t *s = set_find( head, key ); |
---|
116 | char *nv = value; |
---|
117 | |
---|
118 | if( !s ) |
---|
119 | s = set_add( head, key, NULL, NULL, NULL ); |
---|
120 | |
---|
121 | if( s->eval && !( nv = s->eval( s, value ) ) ) |
---|
122 | return 0; |
---|
123 | |
---|
124 | if( s->value ) |
---|
125 | { |
---|
126 | g_free( s->value ); |
---|
127 | s->value = NULL; |
---|
128 | } |
---|
129 | |
---|
130 | /* If there's a default setting and it's equal to what we're trying to |
---|
131 | set, stick with s->value = NULL. Otherwise, remember the setting. */ |
---|
132 | if( !s->def || ( strcmp( nv, s->def ) != 0 ) ) |
---|
133 | s->value = g_strdup( nv ); |
---|
134 | |
---|
135 | if( nv != value ) |
---|
136 | g_free( nv ); |
---|
137 | |
---|
138 | return 1; |
---|
139 | } |
---|
140 | |
---|
141 | int set_setint( set_t **head, char *key, int value ) |
---|
142 | { |
---|
143 | char s[24]; /* Not quite 128-bit clean eh? ;-) */ |
---|
144 | |
---|
145 | g_snprintf( s, sizeof( s ), "%d", value ); |
---|
146 | return set_setstr( head, key, s ); |
---|
147 | } |
---|
148 | |
---|
149 | void set_del( set_t **head, char *key ) |
---|
150 | { |
---|
151 | set_t *s = *head, *t = NULL; |
---|
152 | |
---|
153 | while( s ) |
---|
154 | { |
---|
155 | if( g_strcasecmp( s->key, key ) == 0 ) |
---|
156 | break; |
---|
157 | s = (t=s)->next; |
---|
158 | } |
---|
159 | if( s ) |
---|
160 | { |
---|
161 | if( t ) |
---|
162 | t->next = s->next; |
---|
163 | else |
---|
164 | *head = s->next; |
---|
165 | |
---|
166 | g_free( s->key ); |
---|
167 | if( s->value ) g_free( s->value ); |
---|
168 | if( s->def ) g_free( s->def ); |
---|
169 | g_free( s ); |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | char *set_eval_int( set_t *set, char *value ) |
---|
174 | { |
---|
175 | char *s; |
---|
176 | |
---|
177 | for( s = value; *s; s ++ ) |
---|
178 | if( !isdigit( *s ) ) |
---|
179 | return NULL; |
---|
180 | |
---|
181 | return value; |
---|
182 | } |
---|
183 | |
---|
184 | char *set_eval_bool( set_t *set, char *value ) |
---|
185 | { |
---|
186 | return is_bool( value ) ? value : NULL; |
---|
187 | } |
---|
188 | |
---|
189 | char *set_eval_to_char( set_t *set, char *value ) |
---|
190 | { |
---|
191 | char *s = g_new( char, 3 ); |
---|
192 | |
---|
193 | if( *value == ' ' ) |
---|
194 | strcpy( s, " " ); |
---|
195 | else |
---|
196 | sprintf( s, "%c ", *value ); |
---|
197 | |
---|
198 | return s; |
---|
199 | } |
---|
200 | |
---|
201 | char *set_eval_ops( set_t *set, char *value ) |
---|
202 | { |
---|
203 | irc_t *irc = set->data; |
---|
204 | |
---|
205 | if( g_strcasecmp( value, "user" ) == 0 ) |
---|
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 | else if( g_strcasecmp( value, "root" ) == 0 ) |
---|
209 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
210 | irc->channel, "-o+o", irc->nick, irc->mynick ); |
---|
211 | else if( g_strcasecmp( value, "both" ) == 0 ) |
---|
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 | else if( g_strcasecmp( value, "none" ) == 0 ) |
---|
215 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
216 | irc->channel, "-oo", irc->nick, irc->mynick ); |
---|
217 | else |
---|
218 | return NULL; |
---|
219 | |
---|
220 | return value; |
---|
221 | } |
---|
222 | |
---|
223 | char *set_eval_charset( set_t *set, char *value ) |
---|
224 | { |
---|
225 | GIConv cd; |
---|
226 | |
---|
227 | if ( g_strncasecmp( value, "none", 4 ) == 0 ) |
---|
228 | return value; |
---|
229 | |
---|
230 | cd = g_iconv_open( "UTF-8", value ); |
---|
231 | if( cd == (GIConv) -1 ) |
---|
232 | return NULL; |
---|
233 | |
---|
234 | g_iconv_close( cd ); |
---|
235 | return value; |
---|
236 | } |
---|