source: storage_xml.c @ c43f488

Last change on this file since c43f488 was c43f488, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-06-04T10:58:51Z

Remove two hacks for some glib<2.14 versions that are no longer supported.

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