source: storage_text.c @ a9a7287

Last change on this file since a9a7287 was 85d7b85, checked in by Jelmer Vernooij <jelmer@…>, at 2008-04-02T14:22:57Z

Merge trunk.

  • Property mode set to 100644
File size: 4.7 KB
Line 
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#ifdef _WIN32
30# define umask _umask
31# define mode_t int
32#endif
33
34#ifndef F_OK
35#define F_OK 0
36#endif
37
38static void text_init (void)
39{
40        /* Don't complain about the configuration directory anymore, leave it
41           up to the XML storage module, which uses the same directory for it
42           anyway. Nobody should be using just the text plugin anymore since
43           it's read only! */
44}
45
46static storage_status_t text_load ( const char *my_nick, const char* password, irc_t *irc )
47{
48        char s[512];
49        char *line;
50        int proto;
51        char nick[MAX_NICK_LENGTH+1];
52        FILE *fp;
53        user_t *ru = user_find( irc, ROOT_NICK );
54        account_t *acc, *acc_lookup[9];
55       
56        if( irc->status & USTATUS_IDENTIFIED )
57                return( 1 );
58       
59        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, my_nick, ".accounts" );
60        fp = fopen( s, "r" );
61        if( !fp ) return STORAGE_NO_SUCH_USER;
62       
63        fscanf( fp, "%32[^\n]s", s );
64
65        if( checkpass( password, s ) != 0 )
66        {
67                fclose( fp );
68                return STORAGE_INVALID_PASSWORD;
69        }
70       
71        /* Do this now. If the user runs with AuthMode = Registered, the
72           account command will not work otherwise. */
73        irc->status |= USTATUS_IDENTIFIED;
74       
75        while( fscanf( fp, "%511[^\n]s", s ) > 0 )
76        {
77                fgetc( fp );
78                line = deobfucrypt( s, password );
79                if (line == NULL) return STORAGE_OTHER_ERROR;
80                root_command_string( irc, ru, line, 0 );
81                g_free( line );
82        }
83        fclose( fp );
84       
85        /* Build a list with the first listed account of every protocol
86           number. So if the user had nicks defined for a second account on
87           the same IM network, those nicks will be added to the wrong
88           account, and the user should rename those buddies again. But at
89           least from now on things will be saved properly. */
90        memset( acc_lookup, 0, sizeof( acc_lookup ) );
91        for( acc = irc->accounts; acc; acc = acc->next )
92        {
93                if( acc_lookup[0] == NULL && strcmp( acc->prpl->name, "oscar" ) == 0 )
94                        acc_lookup[0] = acc_lookup[1] = acc_lookup[3] = acc;
95                else if( acc_lookup[2] == NULL && strcmp( acc->prpl->name, "yahoo" ) == 0 )
96                        acc_lookup[2] = acc;
97                else if( acc_lookup[4] == NULL && strcmp( acc->prpl->name, "msn" ) == 0 )
98                        acc_lookup[4] = acc;
99                else if( acc_lookup[8] == NULL && strcmp( acc->prpl->name, "jabber" ) == 0 )
100                        acc_lookup[8] = acc;
101        }
102       
103        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, my_nick, ".nicks" );
104        fp = fopen( s, "r" );
105        if( !fp ) return STORAGE_NO_SUCH_USER;
106        while( fscanf( fp, "%s %d %s", s, &proto, nick ) > 0 )
107        {
108                if( proto < 0 || proto > 8 || ( acc = acc_lookup[proto] ) == NULL )
109                        continue;
110               
111                http_decode( s );
112                nick_set( acc, s, nick );
113        }
114        fclose( fp );
115       
116        return STORAGE_OK;
117}
118
119static storage_status_t text_check_pass( const char *nick, const char *password )
120{
121        char s[512];
122        FILE *fp;
123       
124        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".accounts" );
125        fp = fopen( s, "r" );
126        if (!fp)
127                return STORAGE_NO_SUCH_USER;
128
129        fscanf( fp, "%32[^\n]s", s );
130        fclose( fp );
131
132        if (checkpass( password, s) == -1)
133                return STORAGE_INVALID_PASSWORD;
134
135        return STORAGE_OK;
136}
137
138static storage_status_t text_remove( const char *nick, const char *password )
139{
140        char s[512];
141        storage_status_t status;
142
143        status = text_check_pass( nick, password );
144        if (status != STORAGE_OK)
145                return status;
146
147        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".accounts" );
148        if (unlink( s ) == -1)
149                return STORAGE_OTHER_ERROR;
150       
151        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".nicks" );
152        if (unlink( s ) == -1)
153                return STORAGE_OTHER_ERROR;
154
155        return STORAGE_OK;
156}
157
158storage_t storage_text = {
159        .name = "text",
160        .init = text_init,
161        .check_pass = text_check_pass,
162        .remove = text_remove,
163        .load = text_load
164};
Note: See TracBrowser for help on using the repository browser.