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