source: storage_text.c @ 501b4e0

Last change on this file since 501b4e0 was 7e3592e, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-07-05T18:34:31Z

Fixed text_load(), added detection of primary storage backends without
save support (which shouldn't be allowed) and added a call to nick_lc()
to xml_save() so at least nicks should now be case-insensitive.

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[1ee6c18]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2004 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* Storage backend that uses the same file format as <=1.0 */
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 "crypting.h"
29
30static void text_init (void)
31{
32        if( access( global.conf->configdir, F_OK ) != 0 )
33                log_message( LOGLVL_WARNING, "The configuration directory %s does not exist. Configuration won't be saved.", CONFIG );
34        else if( access( global.conf->configdir, R_OK ) != 0 || access( global.conf->configdir, W_OK ) != 0 )
35                log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to %s.", global.conf->configdir );
36}
37
[a1f17d4]38static storage_status_t text_load ( const char *my_nick, const char* password, irc_t *irc )
[1ee6c18]39{
40        char s[512];
41        char *line;
42        int proto;
43        char nick[MAX_NICK_LENGTH+1];
44        FILE *fp;
45        user_t *ru = user_find( irc, ROOT_NICK );
[7e3592e]46        account_t *acc, *acc_lookup[9];
[1ee6c18]47       
[79e826a]48        if( irc->status & USTATUS_IDENTIFIED )
[1ee6c18]49                return( 1 );
50       
51        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, my_nick, ".accounts" );
52        fp = fopen( s, "r" );
[a1f17d4]53        if( !fp ) return STORAGE_NO_SUCH_USER;
[1ee6c18]54       
55        fscanf( fp, "%32[^\n]s", s );
[7cad7b4]56
[7e3592e]57        if( checkpass( password, s ) != 0 )
[1ee6c18]58        {
59                fclose( fp );
[a1f17d4]60                return STORAGE_INVALID_PASSWORD;
[1ee6c18]61        }
62       
63        /* Do this now. If the user runs with AuthMode = Registered, the
64           account command will not work otherwise. */
[79e826a]65        irc->status |= USTATUS_IDENTIFIED;
[1ee6c18]66       
67        while( fscanf( fp, "%511[^\n]s", s ) > 0 )
68        {
69                fgetc( fp );
[b73ac9c]70                line = deobfucrypt( s, password );
71                if (line == NULL) return STORAGE_OTHER_ERROR;
[1ee6c18]72                root_command_string( irc, ru, line, 0 );
73                g_free( line );
74        }
75        fclose( fp );
76       
[7e3592e]77        /* Build a list with the first listed account of every protocol
78           number. So if the user had nicks defined for a second account on
79           the same IM network, those nicks will be added to the wrong
80           account, and the user should rename those buddies again. But at
81           least from now on things will be saved properly. */
82        memset( acc_lookup, 0, sizeof( acc_lookup ) );
83        for( acc = irc->accounts; acc; acc = acc->next )
84        {
85                if( acc_lookup[0] == NULL && strcmp( acc->prpl->name, "oscar" ) == 0 )
86                        acc_lookup[0] = acc_lookup[1] = acc_lookup[3] = acc;
87                else if( acc_lookup[2] == NULL && strcmp( acc->prpl->name, "yahoo" ) == 0 )
88                        acc_lookup[2] = acc;
89                else if( acc_lookup[4] == NULL && strcmp( acc->prpl->name, "msn" ) == 0 )
90                        acc_lookup[4] = acc;
91                else if( acc_lookup[8] == NULL && strcmp( acc->prpl->name, "jabber" ) == 0 )
92                        acc_lookup[8] = acc;
93        }
94       
[1ee6c18]95        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, my_nick, ".nicks" );
96        fp = fopen( s, "r" );
[a1f17d4]97        if( !fp ) return STORAGE_NO_SUCH_USER;
[1ee6c18]98        while( fscanf( fp, "%s %d %s", s, &proto, nick ) > 0 )
99        {
[7e3592e]100                if( ( acc = acc_lookup[proto] ) == NULL )
[703f0f7]101                        continue;
[7e3592e]102               
[1ee6c18]103                http_decode( s );
[7e3592e]104                nick_set( acc, s, nick );
[1ee6c18]105        }
106        fclose( fp );
107       
[a1f17d4]108        return STORAGE_OK;
[1ee6c18]109}
110
[7989fcf3]111static storage_status_t text_check_pass( const char *nick, const char *password )
[1ee6c18]112{
113        char s[512];
114        FILE *fp;
115       
[b73ac9c]116        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".accounts" );
[1ee6c18]117        fp = fopen( s, "r" );
[a1f17d4]118        if (!fp)
119                return STORAGE_NO_SUCH_USER;
[1ee6c18]120
121        fscanf( fp, "%32[^\n]s", s );
122        fclose( fp );
123
[7cad7b4]124        if (checkpass( password, s) == -1)
125                return STORAGE_INVALID_PASSWORD;
[a1f17d4]126
[7989fcf3]127        return STORAGE_OK;
128}
129
130static storage_status_t text_remove( const char *nick, const char *password )
131{
132        char s[512];
133        storage_status_t status;
134
135        status = text_check_pass( nick, password );
136        if (status != STORAGE_OK)
137                return status;
138
[a1f17d4]139        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".accounts" );
140        if (unlink( s ) == -1)
141                return STORAGE_OTHER_ERROR;
142       
143        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".nicks" );
144        if (unlink( s ) == -1)
145                return STORAGE_OTHER_ERROR;
146
147        return STORAGE_OK;
[1ee6c18]148}
149
150storage_t storage_text = {
151        .name = "text",
152        .init = text_init,
[7989fcf3]153        .check_pass = text_check_pass,
[1ee6c18]154        .remove = text_remove,
[5b52a48]155        .load = text_load
[1ee6c18]156};
Note: See TracBrowser for help on using the repository browser.