1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2012 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* Storage backend that uses an XMLish format for all data. */ |
---|
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 | |
---|
26 | #define BITLBEE_CORE |
---|
27 | #include "bitlbee.h" |
---|
28 | #include "base64.h" |
---|
29 | #include "arc.h" |
---|
30 | #include "md5.h" |
---|
31 | #include "xmltree.h" |
---|
32 | |
---|
33 | #include <glib/gstdio.h> |
---|
34 | |
---|
35 | typedef enum |
---|
36 | { |
---|
37 | XML_PASS_CHECK_ONLY = -1, |
---|
38 | XML_PASS_UNKNOWN = 0, |
---|
39 | XML_PASS_WRONG, |
---|
40 | XML_PASS_OK |
---|
41 | } xml_pass_st; |
---|
42 | |
---|
43 | /* To make it easier later when extending the format: */ |
---|
44 | #define XML_FORMAT_VERSION "1" |
---|
45 | |
---|
46 | struct xml_parsedata |
---|
47 | { |
---|
48 | irc_t *irc; |
---|
49 | char *current_setting; |
---|
50 | account_t *current_account; |
---|
51 | irc_channel_t *current_channel; |
---|
52 | set_t **current_set_head; |
---|
53 | char *given_nick; |
---|
54 | char *given_pass; |
---|
55 | xml_pass_st pass_st; |
---|
56 | int unknown_tag; |
---|
57 | }; |
---|
58 | |
---|
59 | static char *xml_attr( const gchar **attr_names, const gchar **attr_values, const gchar *key ) |
---|
60 | { |
---|
61 | int i; |
---|
62 | |
---|
63 | for( i = 0; attr_names[i]; i ++ ) |
---|
64 | if( g_strcasecmp( attr_names[i], key ) == 0 ) |
---|
65 | return (char*) attr_values[i]; |
---|
66 | |
---|
67 | return NULL; |
---|
68 | } |
---|
69 | |
---|
70 | static void xml_destroy_xd( gpointer data ) |
---|
71 | { |
---|
72 | struct xml_parsedata *xd = data; |
---|
73 | |
---|
74 | g_free( xd->given_nick ); |
---|
75 | g_free( xd->given_pass ); |
---|
76 | g_free( xd ); |
---|
77 | } |
---|
78 | |
---|
79 | static void xml_start_element( GMarkupParseContext *ctx, const gchar *element_name, const gchar **attr_names, const gchar **attr_values, gpointer data, GError **error ) |
---|
80 | { |
---|
81 | struct xml_parsedata *xd = data; |
---|
82 | irc_t *irc = xd->irc; |
---|
83 | |
---|
84 | if( xd->unknown_tag > 0 ) |
---|
85 | { |
---|
86 | xd->unknown_tag ++; |
---|
87 | } |
---|
88 | else if( g_strcasecmp( element_name, "user" ) == 0 ) |
---|
89 | { |
---|
90 | char *nick = xml_attr( attr_names, attr_values, "nick" ); |
---|
91 | char *pass = xml_attr( attr_names, attr_values, "password" ); |
---|
92 | int st; |
---|
93 | |
---|
94 | if( !nick || !pass ) |
---|
95 | { |
---|
96 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
97 | "Missing attributes for %s element", element_name ); |
---|
98 | } |
---|
99 | else if( ( st = md5_verify_password( xd->given_pass, pass ) ) == -1 ) |
---|
100 | { |
---|
101 | xd->pass_st = XML_PASS_WRONG; |
---|
102 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
103 | "Error while decoding password attribute" ); |
---|
104 | } |
---|
105 | else if( st == 0 ) |
---|
106 | { |
---|
107 | if( xd->pass_st != XML_PASS_CHECK_ONLY ) |
---|
108 | xd->pass_st = XML_PASS_OK; |
---|
109 | } |
---|
110 | else |
---|
111 | { |
---|
112 | xd->pass_st = XML_PASS_WRONG; |
---|
113 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
114 | "Password mismatch" ); |
---|
115 | } |
---|
116 | } |
---|
117 | else if( xd->pass_st < XML_PASS_OK ) |
---|
118 | { |
---|
119 | /* Let's not parse anything else if we only have to check |
---|
120 | the password. */ |
---|
121 | } |
---|
122 | else if( g_strcasecmp( element_name, "account" ) == 0 ) |
---|
123 | { |
---|
124 | char *protocol, *handle, *server, *password = NULL, *autoconnect, *tag; |
---|
125 | char *pass_b64 = NULL; |
---|
126 | unsigned char *pass_cr = NULL; |
---|
127 | int pass_len; |
---|
128 | struct prpl *prpl = NULL; |
---|
129 | |
---|
130 | handle = xml_attr( attr_names, attr_values, "handle" ); |
---|
131 | pass_b64 = xml_attr( attr_names, attr_values, "password" ); |
---|
132 | server = xml_attr( attr_names, attr_values, "server" ); |
---|
133 | autoconnect = xml_attr( attr_names, attr_values, "autoconnect" ); |
---|
134 | tag = xml_attr( attr_names, attr_values, "tag" ); |
---|
135 | |
---|
136 | protocol = xml_attr( attr_names, attr_values, "protocol" ); |
---|
137 | if( protocol ) |
---|
138 | prpl = find_protocol( protocol ); |
---|
139 | |
---|
140 | if( !handle || !pass_b64 || !protocol ) |
---|
141 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
142 | "Missing attributes for %s element", element_name ); |
---|
143 | else if( !prpl ) |
---|
144 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
145 | "Unknown protocol: %s", protocol ); |
---|
146 | else if( ( pass_len = base64_decode( pass_b64, (unsigned char**) &pass_cr ) ) && |
---|
147 | arc_decode( pass_cr, pass_len, &password, xd->given_pass ) >= 0 ) |
---|
148 | { |
---|
149 | xd->current_account = account_add( irc->b, prpl, handle, password ); |
---|
150 | if( server ) |
---|
151 | set_setstr( &xd->current_account->set, "server", server ); |
---|
152 | if( autoconnect ) |
---|
153 | set_setstr( &xd->current_account->set, "auto_connect", autoconnect ); |
---|
154 | if( tag ) |
---|
155 | set_setstr( &xd->current_account->set, "tag", tag ); |
---|
156 | } |
---|
157 | else |
---|
158 | { |
---|
159 | /* Actually the _decode functions don't even return error codes, |
---|
160 | but maybe they will later... */ |
---|
161 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
162 | "Error while decrypting account password" ); |
---|
163 | } |
---|
164 | |
---|
165 | g_free( pass_cr ); |
---|
166 | g_free( password ); |
---|
167 | } |
---|
168 | else if( g_strcasecmp( element_name, "setting" ) == 0 ) |
---|
169 | { |
---|
170 | char *setting; |
---|
171 | |
---|
172 | if( xd->current_setting ) |
---|
173 | { |
---|
174 | g_free( xd->current_setting ); |
---|
175 | xd->current_setting = NULL; |
---|
176 | } |
---|
177 | |
---|
178 | if( ( setting = xml_attr( attr_names, attr_values, "name" ) ) ) |
---|
179 | { |
---|
180 | if( xd->current_channel != NULL ) |
---|
181 | xd->current_set_head = &xd->current_channel->set; |
---|
182 | else if( xd->current_account != NULL ) |
---|
183 | xd->current_set_head = &xd->current_account->set; |
---|
184 | else |
---|
185 | xd->current_set_head = &xd->irc->b->set; |
---|
186 | |
---|
187 | xd->current_setting = g_strdup( setting ); |
---|
188 | } |
---|
189 | else |
---|
190 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
191 | "Missing attributes for %s element", element_name ); |
---|
192 | } |
---|
193 | else if( g_strcasecmp( element_name, "buddy" ) == 0 ) |
---|
194 | { |
---|
195 | char *handle, *nick; |
---|
196 | |
---|
197 | handle = xml_attr( attr_names, attr_values, "handle" ); |
---|
198 | nick = xml_attr( attr_names, attr_values, "nick" ); |
---|
199 | |
---|
200 | if( xd->current_account && handle && nick ) |
---|
201 | { |
---|
202 | nick_set_raw( xd->current_account, handle, nick ); |
---|
203 | } |
---|
204 | else |
---|
205 | { |
---|
206 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
207 | "Missing attributes for %s element", element_name ); |
---|
208 | } |
---|
209 | } |
---|
210 | else if( g_strcasecmp( element_name, "channel" ) == 0 ) |
---|
211 | { |
---|
212 | char *name, *type; |
---|
213 | |
---|
214 | name = xml_attr( attr_names, attr_values, "name" ); |
---|
215 | type = xml_attr( attr_names, attr_values, "type" ); |
---|
216 | |
---|
217 | if( !name || !type ) |
---|
218 | { |
---|
219 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
220 | "Missing attributes for %s element", element_name ); |
---|
221 | return; |
---|
222 | } |
---|
223 | |
---|
224 | /* The channel may exist already, for example if it's &bitlbee. |
---|
225 | Also, it's possible that the user just reconnected and the |
---|
226 | IRC client already rejoined all channels it was in. They |
---|
227 | should still get the right settings. */ |
---|
228 | if( ( xd->current_channel = irc_channel_by_name( irc, name ) ) || |
---|
229 | ( xd->current_channel = irc_channel_new( irc, name ) ) ) |
---|
230 | set_setstr( &xd->current_channel->set, "type", type ); |
---|
231 | } |
---|
232 | /* Backward compatibility: Keep this around for a while for people |
---|
233 | switching from BitlBee 1.2.4+. */ |
---|
234 | else if( g_strcasecmp( element_name, "chat" ) == 0 ) |
---|
235 | { |
---|
236 | char *handle, *channel; |
---|
237 | |
---|
238 | handle = xml_attr( attr_names, attr_values, "handle" ); |
---|
239 | channel = xml_attr( attr_names, attr_values, "channel" ); |
---|
240 | |
---|
241 | if( xd->current_account && handle && channel ) |
---|
242 | { |
---|
243 | irc_channel_t *ic; |
---|
244 | |
---|
245 | if( ( ic = irc_channel_new( irc, channel ) ) && |
---|
246 | set_setstr( &ic->set, "type", "chat" ) && |
---|
247 | set_setstr( &ic->set, "chat_type", "room" ) && |
---|
248 | set_setstr( &ic->set, "account", xd->current_account->tag ) && |
---|
249 | set_setstr( &ic->set, "room", handle ) ) |
---|
250 | { |
---|
251 | /* Try to pick up some settings where possible. */ |
---|
252 | xd->current_channel = ic; |
---|
253 | } |
---|
254 | else if( ic ) |
---|
255 | irc_channel_free( ic ); |
---|
256 | } |
---|
257 | else |
---|
258 | { |
---|
259 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
260 | "Missing attributes for %s element", element_name ); |
---|
261 | } |
---|
262 | } |
---|
263 | else |
---|
264 | { |
---|
265 | xd->unknown_tag ++; |
---|
266 | irc_rootmsg( irc, "Warning: Unknown XML tag found in configuration file (%s). " |
---|
267 | "This may happen when downgrading BitlBee versions. " |
---|
268 | "This tag will be skipped and the information will be lost " |
---|
269 | "once you save your settings.", element_name ); |
---|
270 | /* |
---|
271 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT, |
---|
272 | "Unkown element: %s", element_name ); |
---|
273 | */ |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | static void xml_end_element( GMarkupParseContext *ctx, const gchar *element_name, gpointer data, GError **error ) |
---|
278 | { |
---|
279 | struct xml_parsedata *xd = data; |
---|
280 | |
---|
281 | if( xd->unknown_tag > 0 ) |
---|
282 | { |
---|
283 | xd->unknown_tag --; |
---|
284 | } |
---|
285 | else if( g_strcasecmp( element_name, "setting" ) == 0 && xd->current_setting ) |
---|
286 | { |
---|
287 | g_free( xd->current_setting ); |
---|
288 | xd->current_setting = NULL; |
---|
289 | } |
---|
290 | else if( g_strcasecmp( element_name, "account" ) == 0 ) |
---|
291 | { |
---|
292 | xd->current_account = NULL; |
---|
293 | } |
---|
294 | else if( g_strcasecmp( element_name, "channel" ) == 0 || |
---|
295 | g_strcasecmp( element_name, "chat" ) == 0 ) |
---|
296 | { |
---|
297 | xd->current_channel = NULL; |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | static void xml_text( GMarkupParseContext *ctx, const gchar *text_orig, gsize text_len, gpointer data, GError **error ) |
---|
302 | { |
---|
303 | char text[text_len+1]; |
---|
304 | struct xml_parsedata *xd = data; |
---|
305 | |
---|
306 | strncpy( text, text_orig, text_len ); |
---|
307 | text[text_len] = 0; |
---|
308 | |
---|
309 | if( xd->pass_st < XML_PASS_OK ) |
---|
310 | { |
---|
311 | /* Let's not parse anything else if we only have to check |
---|
312 | the password, or if we didn't get the chance to check it |
---|
313 | yet. */ |
---|
314 | } |
---|
315 | else if( g_strcasecmp( g_markup_parse_context_get_element( ctx ), "setting" ) == 0 && xd->current_setting ) |
---|
316 | { |
---|
317 | if( xd->current_account ) |
---|
318 | { |
---|
319 | set_t *s = set_find( xd->current_set_head, xd->current_setting ); |
---|
320 | if( s && ( s->flags & ACC_SET_ONLINE_ONLY ) ) |
---|
321 | { |
---|
322 | g_free( xd->current_setting ); |
---|
323 | xd->current_setting = NULL; |
---|
324 | return; |
---|
325 | } |
---|
326 | } |
---|
327 | set_setstr( xd->current_set_head, xd->current_setting, (char*) text ); |
---|
328 | g_free( xd->current_setting ); |
---|
329 | xd->current_setting = NULL; |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | GMarkupParser xml_parser = |
---|
334 | { |
---|
335 | xml_start_element, |
---|
336 | xml_end_element, |
---|
337 | xml_text, |
---|
338 | NULL, |
---|
339 | NULL |
---|
340 | }; |
---|
341 | |
---|
342 | static void xml_init( void ) |
---|
343 | { |
---|
344 | if( g_access( global.conf->configdir, F_OK ) != 0 ) |
---|
345 | log_message( LOGLVL_WARNING, "The configuration directory `%s' does not exist. Configuration won't be saved.", global.conf->configdir ); |
---|
346 | else if( g_access( global.conf->configdir, F_OK ) != 0 || |
---|
347 | g_access( global.conf->configdir, W_OK ) != 0 ) |
---|
348 | log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to `%s'.", global.conf->configdir ); |
---|
349 | } |
---|
350 | |
---|
351 | static storage_status_t xml_load_real( irc_t *irc, const char *my_nick, const char *password, xml_pass_st action ) |
---|
352 | { |
---|
353 | GMarkupParseContext *ctx; |
---|
354 | struct xml_parsedata *xd; |
---|
355 | char *fn, buf[512]; |
---|
356 | GError *gerr = NULL; |
---|
357 | int fd, st; |
---|
358 | |
---|
359 | xd = g_new0( struct xml_parsedata, 1 ); |
---|
360 | xd->irc = irc; |
---|
361 | xd->given_nick = g_strdup( my_nick ); |
---|
362 | xd->given_pass = g_strdup( password ); |
---|
363 | xd->pass_st = action; |
---|
364 | nick_lc( xd->given_nick ); |
---|
365 | |
---|
366 | fn = g_strdup_printf( "%s%s%s", global.conf->configdir, xd->given_nick, ".xml" ); |
---|
367 | if( ( fd = open( fn, O_RDONLY ) ) < 0 ) |
---|
368 | { |
---|
369 | xml_destroy_xd( xd ); |
---|
370 | g_free( fn ); |
---|
371 | return STORAGE_NO_SUCH_USER; |
---|
372 | } |
---|
373 | g_free( fn ); |
---|
374 | |
---|
375 | ctx = g_markup_parse_context_new( &xml_parser, 0, xd, xml_destroy_xd ); |
---|
376 | |
---|
377 | while( ( st = read( fd, buf, sizeof( buf ) ) ) > 0 ) |
---|
378 | { |
---|
379 | if( !g_markup_parse_context_parse( ctx, buf, st, &gerr ) || gerr ) |
---|
380 | { |
---|
381 | xml_pass_st pass_st = xd->pass_st; |
---|
382 | |
---|
383 | g_markup_parse_context_free( ctx ); |
---|
384 | close( fd ); |
---|
385 | |
---|
386 | if( pass_st == XML_PASS_WRONG ) |
---|
387 | { |
---|
388 | g_clear_error( &gerr ); |
---|
389 | return STORAGE_INVALID_PASSWORD; |
---|
390 | } |
---|
391 | else |
---|
392 | { |
---|
393 | if( gerr && irc ) |
---|
394 | irc_rootmsg( irc, "Error from XML-parser: %s", gerr->message ); |
---|
395 | |
---|
396 | g_clear_error( &gerr ); |
---|
397 | return STORAGE_OTHER_ERROR; |
---|
398 | } |
---|
399 | } |
---|
400 | } |
---|
401 | /* Just to be sure... */ |
---|
402 | g_clear_error( &gerr ); |
---|
403 | |
---|
404 | g_markup_parse_context_free( ctx ); |
---|
405 | close( fd ); |
---|
406 | |
---|
407 | if( action == XML_PASS_CHECK_ONLY ) |
---|
408 | return STORAGE_OK; |
---|
409 | |
---|
410 | return STORAGE_OK; |
---|
411 | } |
---|
412 | |
---|
413 | static storage_status_t xml_load( irc_t *irc, const char *password ) |
---|
414 | { |
---|
415 | return xml_load_real( irc, irc->user->nick, password, XML_PASS_UNKNOWN ); |
---|
416 | } |
---|
417 | |
---|
418 | static storage_status_t xml_check_pass( const char *my_nick, const char *password ) |
---|
419 | { |
---|
420 | /* This is a little bit risky because we have to pass NULL for the |
---|
421 | irc_t argument. This *should* be fine, if I didn't miss anything... */ |
---|
422 | return xml_load_real( NULL, my_nick, password, XML_PASS_CHECK_ONLY ); |
---|
423 | } |
---|
424 | |
---|
425 | static gboolean xml_save_nick( gpointer key, gpointer value, gpointer data ); |
---|
426 | |
---|
427 | struct xt_node *xml_generate( irc_t *irc ) |
---|
428 | { |
---|
429 | char *pass_buf = NULL; |
---|
430 | set_t *set; |
---|
431 | account_t *acc; |
---|
432 | md5_byte_t pass_md5[21]; |
---|
433 | md5_state_t md5_state; |
---|
434 | GSList *l; |
---|
435 | struct xt_node *root, *cur; |
---|
436 | |
---|
437 | /* Generate a salted md5sum of the password. Use 5 bytes for the salt |
---|
438 | (to prevent dictionary lookups of passwords) to end up with a 21- |
---|
439 | byte password hash, more convenient for base64 encoding. */ |
---|
440 | random_bytes( pass_md5 + 16, 5 ); |
---|
441 | md5_init( &md5_state ); |
---|
442 | md5_append( &md5_state, (md5_byte_t*) irc->password, strlen( irc->password ) ); |
---|
443 | md5_append( &md5_state, pass_md5 + 16, 5 ); /* Add the salt. */ |
---|
444 | md5_finish( &md5_state, pass_md5 ); |
---|
445 | /* Save the hash in base64-encoded form. */ |
---|
446 | pass_buf = base64_encode( pass_md5, 21 ); |
---|
447 | |
---|
448 | root = cur = xt_new_node( "user", NULL, NULL ); |
---|
449 | xt_add_attr( cur, "nick", irc->user->nick ); |
---|
450 | xt_add_attr( cur, "password", pass_buf ); |
---|
451 | xt_add_attr( cur, "version", XML_FORMAT_VERSION ); |
---|
452 | |
---|
453 | g_free( pass_buf ); |
---|
454 | |
---|
455 | for( set = irc->b->set; set; set = set->next ) |
---|
456 | if( set->value && !( set->flags & SET_NOSAVE ) ) |
---|
457 | { |
---|
458 | struct xt_node *xset; |
---|
459 | xt_add_child( cur, xset = xt_new_node( "setting", set->value, NULL ) ); |
---|
460 | xt_add_attr( xset, "name", set->key ); |
---|
461 | } |
---|
462 | |
---|
463 | for( acc = irc->b->accounts; acc; acc = acc->next ) |
---|
464 | { |
---|
465 | unsigned char *pass_cr; |
---|
466 | char *pass_b64; |
---|
467 | int pass_len; |
---|
468 | |
---|
469 | pass_len = arc_encode( acc->pass, strlen( acc->pass ), (unsigned char**) &pass_cr, irc->password, 12 ); |
---|
470 | pass_b64 = base64_encode( pass_cr, pass_len ); |
---|
471 | g_free( pass_cr ); |
---|
472 | |
---|
473 | cur = xt_new_node( "account", NULL, NULL ); |
---|
474 | xt_add_attr( cur, "protocol", acc->prpl->name ); |
---|
475 | xt_add_attr( cur, "handle", acc->user ); |
---|
476 | xt_add_attr( cur, "password", pass_b64 ); |
---|
477 | xt_add_attr( cur, "autoconnect", acc->auto_connect ? "true" : "false" ); |
---|
478 | xt_add_attr( cur, "tag", acc->tag ); |
---|
479 | if( acc->server && acc->server[0] ) |
---|
480 | xt_add_attr( cur, "server", acc->server ); |
---|
481 | |
---|
482 | g_free( pass_b64 ); |
---|
483 | |
---|
484 | for( set = acc->set; set; set = set->next ) |
---|
485 | if( set->value && !( set->flags & ACC_SET_NOSAVE ) ) |
---|
486 | { |
---|
487 | struct xt_node *xset; |
---|
488 | xt_add_child( cur, xset = xt_new_node( "setting", set->value, NULL ) ); |
---|
489 | xt_add_attr( xset, "name", set->key ); |
---|
490 | } |
---|
491 | |
---|
492 | /* This probably looks pretty strange. g_hash_table_foreach |
---|
493 | is quite a PITA already (but it can't get much better in |
---|
494 | C without using #define, I'm afraid), and since it |
---|
495 | doesn't seem to be possible to abort the foreach on write |
---|
496 | errors, so instead let's use the _find function and |
---|
497 | return TRUE on write errors. Which means, if we found |
---|
498 | something, there was an error. :-) */ |
---|
499 | g_hash_table_find( acc->nicks, xml_save_nick, cur ); |
---|
500 | |
---|
501 | xt_add_child( root, cur ); |
---|
502 | } |
---|
503 | |
---|
504 | for( l = irc->channels; l; l = l->next ) |
---|
505 | { |
---|
506 | irc_channel_t *ic = l->data; |
---|
507 | |
---|
508 | if( ic->flags & IRC_CHANNEL_TEMP ) |
---|
509 | continue; |
---|
510 | |
---|
511 | cur = xt_new_node( "channel", NULL, NULL ); |
---|
512 | xt_add_attr( cur, "name", ic->name ); |
---|
513 | xt_add_attr( cur, "type", set_getstr( &ic->set, "type" ) ); |
---|
514 | |
---|
515 | for( set = ic->set; set; set = set->next ) |
---|
516 | if( set->value && strcmp( set->key, "type" ) != 0 ) |
---|
517 | { |
---|
518 | struct xt_node *xset; |
---|
519 | xt_add_child( cur, xset = xt_new_node( "setting", set->value, NULL ) ); |
---|
520 | xt_add_attr( xset, "name", set->key ); |
---|
521 | } |
---|
522 | |
---|
523 | xt_add_child( root, cur ); |
---|
524 | } |
---|
525 | |
---|
526 | return root; |
---|
527 | } |
---|
528 | |
---|
529 | static storage_status_t xml_save( irc_t *irc, int overwrite ) |
---|
530 | { |
---|
531 | char path[512], *path2, *xml; |
---|
532 | struct xt_node *tree; |
---|
533 | int fd; |
---|
534 | |
---|
535 | path2 = g_strdup( irc->user->nick ); |
---|
536 | nick_lc( path2 ); |
---|
537 | g_snprintf( path, sizeof( path ) - 2, "%s%s%s", global.conf->configdir, path2, ".xml" ); |
---|
538 | g_free( path2 ); |
---|
539 | |
---|
540 | if( !overwrite && g_access( path, F_OK ) == 0 ) |
---|
541 | return STORAGE_ALREADY_EXISTS; |
---|
542 | |
---|
543 | strcat( path, ".XXXXXX" ); |
---|
544 | if( ( fd = mkstemp( path ) ) < 0 ) |
---|
545 | { |
---|
546 | irc_rootmsg( irc, "Error while opening configuration file." ); |
---|
547 | return STORAGE_OTHER_ERROR; |
---|
548 | } |
---|
549 | |
---|
550 | tree = xml_generate( irc ); |
---|
551 | xml = xt_to_string( tree ); |
---|
552 | write( fd, xml, strlen( xml ) ); |
---|
553 | |
---|
554 | fsync( fd ); |
---|
555 | close( fd ); |
---|
556 | |
---|
557 | path2 = g_strndup( path, strlen( path ) - 7 ); |
---|
558 | if( rename( path, path2 ) != 0 ) |
---|
559 | { |
---|
560 | irc_rootmsg( irc, "Error while renaming temporary configuration file." ); |
---|
561 | |
---|
562 | g_free( path2 ); |
---|
563 | unlink( path ); |
---|
564 | |
---|
565 | return STORAGE_OTHER_ERROR; |
---|
566 | } |
---|
567 | |
---|
568 | g_free( path2 ); |
---|
569 | |
---|
570 | return STORAGE_OK; |
---|
571 | |
---|
572 | write_error: |
---|
573 | |
---|
574 | irc_rootmsg( irc, "Write error. Disk full?" ); |
---|
575 | close( fd ); |
---|
576 | |
---|
577 | return STORAGE_OTHER_ERROR; |
---|
578 | } |
---|
579 | |
---|
580 | static gboolean xml_save_nick( gpointer key, gpointer value, gpointer data ) |
---|
581 | { |
---|
582 | struct xt_node *node = xt_new_node( "buddy", NULL, NULL ); |
---|
583 | xt_add_attr( node, "handle", key ); |
---|
584 | xt_add_attr( node, "nick", value ); |
---|
585 | xt_add_child( (struct xt_node *) data, node ); |
---|
586 | |
---|
587 | return FALSE; |
---|
588 | } |
---|
589 | |
---|
590 | static storage_status_t xml_remove( const char *nick, const char *password ) |
---|
591 | { |
---|
592 | char s[512], *lc; |
---|
593 | storage_status_t status; |
---|
594 | |
---|
595 | status = xml_check_pass( nick, password ); |
---|
596 | if( status != STORAGE_OK ) |
---|
597 | return status; |
---|
598 | |
---|
599 | lc = g_strdup( nick ); |
---|
600 | nick_lc( lc ); |
---|
601 | g_snprintf( s, 511, "%s%s%s", global.conf->configdir, lc, ".xml" ); |
---|
602 | g_free( lc ); |
---|
603 | |
---|
604 | if( unlink( s ) == -1 ) |
---|
605 | return STORAGE_OTHER_ERROR; |
---|
606 | |
---|
607 | return STORAGE_OK; |
---|
608 | } |
---|
609 | |
---|
610 | storage_t storage_xml = { |
---|
611 | .name = "xml", |
---|
612 | .init = xml_init, |
---|
613 | .check_pass = xml_check_pass, |
---|
614 | .remove = xml_remove, |
---|
615 | .load = xml_load, |
---|
616 | .save = xml_save |
---|
617 | }; |
---|