source: storage_xml.c @ 1aa74f55

Last change on this file since 1aa74f55 was e135cd09, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-24T15:58:27Z

Use the account tag in a few places and store it in the XML file as an
attribute, not as a setting (since all accounts have it anyway).

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