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( irc_t *irc, char *key, char *def, void *eval ) |
---|
29 | { |
---|
30 | set_t *s = set_find( irc, key ); |
---|
31 | |
---|
32 | if( !s ) |
---|
33 | { |
---|
34 | if( ( s = irc->set ) ) |
---|
35 | { |
---|
36 | while( s->next ) s = s->next; |
---|
37 | s->next = g_new ( set_t, 1 ); |
---|
38 | s = s->next; |
---|
39 | } |
---|
40 | else |
---|
41 | { |
---|
42 | s = irc->set = g_new( set_t, 1 ); |
---|
43 | } |
---|
44 | memset( s, 0, sizeof( set_t ) ); |
---|
45 | s->key = g_strdup( key ); |
---|
46 | } |
---|
47 | |
---|
48 | if( s->def ) |
---|
49 | { |
---|
50 | g_free( s->def ); |
---|
51 | s->def = NULL; |
---|
52 | } |
---|
53 | if( def ) s->def = g_strdup( def ); |
---|
54 | |
---|
55 | if( s->eval ) |
---|
56 | { |
---|
57 | g_free( s->eval ); |
---|
58 | s->eval = NULL; |
---|
59 | } |
---|
60 | if( eval ) s->eval = eval; |
---|
61 | |
---|
62 | return( s ); |
---|
63 | } |
---|
64 | |
---|
65 | set_t *set_find( irc_t *irc, char *key ) |
---|
66 | { |
---|
67 | set_t *s = irc->set; |
---|
68 | |
---|
69 | while( s ) |
---|
70 | { |
---|
71 | if( g_strcasecmp( s->key, key ) == 0 ) |
---|
72 | break; |
---|
73 | s = s->next; |
---|
74 | } |
---|
75 | |
---|
76 | return( s ); |
---|
77 | } |
---|
78 | |
---|
79 | char *set_getstr( irc_t *irc, char *key ) |
---|
80 | { |
---|
81 | set_t *s = set_find( irc, key ); |
---|
82 | |
---|
83 | if( !s || ( !s->value && !s->def ) ) |
---|
84 | return( NULL ); |
---|
85 | |
---|
86 | return( s->value?s->value:s->def ); |
---|
87 | } |
---|
88 | |
---|
89 | int set_getint( irc_t *irc, char *key ) |
---|
90 | { |
---|
91 | char *s = set_getstr( irc, key ); |
---|
92 | int i = 0; |
---|
93 | |
---|
94 | if( !s ) |
---|
95 | return( 0 ); |
---|
96 | |
---|
97 | if( ( g_strcasecmp( s, "true" ) == 0 ) || ( g_strcasecmp( s, "yes" ) == 0 ) || ( g_strcasecmp( s, "on" ) == 0 ) ) |
---|
98 | return( 1 ); |
---|
99 | |
---|
100 | if( sscanf( s, "%d", &i ) != 1 ) |
---|
101 | return( 0 ); |
---|
102 | |
---|
103 | return( i ); |
---|
104 | } |
---|
105 | |
---|
106 | int set_setstr( irc_t *irc, char *key, char *value ) |
---|
107 | { |
---|
108 | set_t *s = set_find( irc, key ); |
---|
109 | char *nv = value; |
---|
110 | |
---|
111 | if( !s ) |
---|
112 | s = set_add( irc, key, NULL, NULL ); |
---|
113 | |
---|
114 | if( s->eval && !( nv = s->eval( irc, s, value ) ) ) |
---|
115 | return( 0 ); |
---|
116 | |
---|
117 | if( s->value ) |
---|
118 | { |
---|
119 | g_free( s->value ); |
---|
120 | s->value = NULL; |
---|
121 | } |
---|
122 | |
---|
123 | if( !s->def || ( strcmp( nv, s->def ) != 0 ) ) |
---|
124 | s->value = g_strdup( nv ); |
---|
125 | |
---|
126 | if( nv != value ) |
---|
127 | g_free( nv ); |
---|
128 | |
---|
129 | return( 1 ); |
---|
130 | } |
---|
131 | |
---|
132 | int set_setint( irc_t *irc, char *key, int value ) |
---|
133 | { |
---|
134 | char s[24]; /* Not quite 128-bit clean eh? ;-) */ |
---|
135 | |
---|
136 | sprintf( s, "%d", value ); |
---|
137 | return( set_setstr( irc, key, s ) ); |
---|
138 | } |
---|
139 | |
---|
140 | void set_del( irc_t *irc, char *key ) |
---|
141 | { |
---|
142 | set_t *s = irc->set, *t = NULL; |
---|
143 | |
---|
144 | while( s ) |
---|
145 | { |
---|
146 | if( g_strcasecmp( s->key, key ) == 0 ) |
---|
147 | break; |
---|
148 | s = (t=s)->next; |
---|
149 | } |
---|
150 | if( s ) |
---|
151 | { |
---|
152 | t->next = s->next; |
---|
153 | g_free( s->key ); |
---|
154 | if( s->value ) g_free( s->value ); |
---|
155 | if( s->def ) g_free( s->def ); |
---|
156 | g_free( s ); |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | char *set_eval_int( irc_t *irc, set_t *set, char *value ) |
---|
161 | { |
---|
162 | char *s = value; |
---|
163 | |
---|
164 | for( ; *s; s ++ ) |
---|
165 | if( *s < '0' || *s > '9' ) |
---|
166 | return( NULL ); |
---|
167 | |
---|
168 | return( value ); |
---|
169 | } |
---|
170 | |
---|
171 | char *set_eval_bool( irc_t *irc, set_t *set, char *value ) |
---|
172 | { |
---|
173 | if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) ) |
---|
174 | return( value ); |
---|
175 | if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) ) |
---|
176 | return( value ); |
---|
177 | return( set_eval_int( irc, set, value ) ); |
---|
178 | } |
---|
179 | |
---|
180 | char *set_eval_to_char( irc_t *irc, set_t *set, char *value ) |
---|
181 | { |
---|
182 | char *s = g_new( char, 3 ); |
---|
183 | |
---|
184 | if( *value == ' ' ) |
---|
185 | strcpy( s, " " ); |
---|
186 | else |
---|
187 | sprintf( s, "%c ", *value ); |
---|
188 | |
---|
189 | return( s ); |
---|
190 | } |
---|
191 | |
---|
192 | char *set_eval_ops( irc_t *irc, set_t *set, char *value ) |
---|
193 | { |
---|
194 | if( g_strcasecmp( value, "user" ) == 0 ) |
---|
195 | { |
---|
196 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
197 | irc->channel, "+o-o", irc->nick, irc->mynick ); |
---|
198 | return( value ); |
---|
199 | } |
---|
200 | else if( g_strcasecmp( value, "root" ) == 0 ) |
---|
201 | { |
---|
202 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
203 | irc->channel, "-o+o", irc->nick, irc->mynick ); |
---|
204 | return( value ); |
---|
205 | } |
---|
206 | else if( g_strcasecmp( value, "both" ) == 0 ) |
---|
207 | { |
---|
208 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
209 | irc->channel, "+oo", irc->nick, irc->mynick ); |
---|
210 | return( value ); |
---|
211 | } |
---|
212 | else if( g_strcasecmp( value, "none" ) == 0 ) |
---|
213 | { |
---|
214 | irc_write( irc, ":%s!%s@%s MODE %s %s %s %s", irc->mynick, irc->mynick, irc->myhost, |
---|
215 | irc->channel, "-oo", irc->nick, irc->mynick ); |
---|
216 | return( value ); |
---|
217 | } |
---|
218 | |
---|
219 | return( NULL ); |
---|
220 | } |
---|
221 | |
---|