source: storage.c @ 823de9d

Last change on this file since 823de9d was 823de9d, checked in by Sven Moritz Hallberg <pesco@…>, at 2009-03-12T19:10:06Z

commit updates by ashish shukla <wahjava@…>

  • Property mode set to 100644
File size: 5.3 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/* Support for multiple storage backends */
8
[d9d36fc]9/* Copyright (C) 2005 Jelmer Vernooij <jelmer@samba.org> */
10
[1ee6c18]11/*
12  This program is free software; you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 2 of the License, or
15  (at your option) any later version.
16
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  GNU General Public License for more details.
21
22  You should have received a copy of the GNU General Public License with
23  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
24  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
25  Suite 330, Boston, MA  02111-1307  USA
26*/
27
28#define BITLBEE_CORE
29#include "bitlbee.h"
30#include "crypting.h"
[764c7d1]31#include "otr.h"
[1ee6c18]32
33extern storage_t storage_text;
[c121f89]34extern storage_t storage_xml;
[1ee6c18]35
[c121f89]36static GList *storage_backends = NULL;
[1ee6c18]37
38void register_storage_backend(storage_t *backend)
39{
40        storage_backends = g_list_append(storage_backends, backend);
41}
42
[b73ac9c]43static storage_t *storage_init_single(const char *name)
[1ee6c18]44{
45        GList *gl;
[b3c467b]46        storage_t *st = NULL;
[1ee6c18]47
48        for (gl = storage_backends; gl; gl = gl->next) {
49                st = gl->data;
50                if (strcmp(st->name, name) == 0)
51                        break;
52        }
53
54        if (gl == NULL) 
55                return NULL;
56
57        if (st->init)
58                st->init();
59
60        return st;
61}
[ab49fdc]62
[b73ac9c]63GList *storage_init(const char *primary, char **migrate)
64{
65        GList *ret = NULL;
66        int i;
67        storage_t *storage;
[c121f89]68       
69        register_storage_backend(&storage_text);
70        register_storage_backend(&storage_xml);
71       
[b73ac9c]72        storage = storage_init_single(primary);
[7e3592e]73        if (storage == NULL && storage->save == NULL)
[b73ac9c]74                return NULL;
75
76        ret = g_list_append(ret, storage);
77
78        for (i = 0; migrate && migrate[i]; i++) {
79                storage = storage_init_single(migrate[i]);
80       
81                if (storage)
82                        ret = g_list_append(ret, storage);
83        }
84
85        return ret;
86}
87
[ab49fdc]88storage_status_t storage_check_pass (const char *nick, const char *password)
89{
[b73ac9c]90        GList *gl;
91       
92        /* Loop until we don't get NO_SUCH_USER */
93
94        for (gl = global.storage; gl; gl = gl->next) {
95                storage_t *st = gl->data;
96                storage_status_t status;
97
98                status = st->check_pass(nick, password);
99                if (status != STORAGE_NO_SUCH_USER)
100                        return status;
101        }
102       
103        return STORAGE_NO_SUCH_USER;
[ab49fdc]104}
105
[823de9d]106storage_status_t storage_load (irc_t * irc, const char *password)
[ab49fdc]107{
[b73ac9c]108        GList *gl;
109       
[823de9d]110        if (irc && irc->status & USTATUS_IDENTIFIED)
111                return STORAGE_OTHER_ERROR;
112       
[b73ac9c]113        /* Loop until we don't get NO_SUCH_USER */
114        for (gl = global.storage; gl; gl = gl->next) {
115                storage_t *st = gl->data;
116                storage_status_t status;
117
[823de9d]118                status = st->load(irc, password);
[b73ac9c]119                if (status == STORAGE_OK) {
[823de9d]120                        otr_load(irc);
[b73ac9c]121                        return status;
122                }
123               
[823de9d]124                if (status != STORAGE_NO_SUCH_USER) 
[b73ac9c]125                        return status;
126        }
127       
128        return STORAGE_NO_SUCH_USER;
[ab49fdc]129}
130
[823de9d]131storage_status_t storage_save (irc_t *irc, char *password, int overwrite)
[ab49fdc]132{
[764c7d1]133        storage_status_t st;
134       
[823de9d]135        if (password != NULL) {
136                /* Should only use this in the "register" command. */
137                if (irc->password || overwrite)
138                        return STORAGE_OTHER_ERROR;
139               
140                irc_setpass(irc, password);
141        } else if ((irc->status & USTATUS_IDENTIFIED) == 0) {
142                return STORAGE_NO_SUCH_USER;
143        }
144
[764c7d1]145        otr_save(irc);
146        st = ((storage_t *)global.storage->data)->save(irc, overwrite);
[823de9d]147       
148        if (password != NULL) {
149                irc_setpass(irc, NULL);
150        }
151       
[764c7d1]152        return st;
[ab49fdc]153}
154
155storage_status_t storage_remove (const char *nick, const char *password)
156{
[b73ac9c]157        GList *gl;
158        storage_status_t ret = STORAGE_OK;
159       
160        /* Remove this account from all storage backends. If this isn't
161         * done, the account will still be usable, it'd just be
162         * loaded from a different backend. */
163        for (gl = global.storage; gl; gl = gl->next) {
164                storage_t *st = gl->data;
165                storage_status_t status;
166
167                status = st->remove(nick, password);
[823de9d]168                if (status != STORAGE_NO_SUCH_USER && status != STORAGE_OK)
[b73ac9c]169                        ret = status;
170        }
[764c7d1]171        if (ret == STORAGE_OK) {
172                otr_remove(nick);
173        }
[b73ac9c]174       
175        return ret;
[ab49fdc]176}
177
[823de9d]178#if 0
179Not using this yet. Test thoroughly before adding UI hooks to this function.
180
[ab49fdc]181storage_status_t storage_rename (const char *onick, const char *nnick, const char *password)
182{
[b73ac9c]183        storage_status_t status;
184        GList *gl = global.storage;
185        storage_t *primary_storage = gl->data;
186        irc_t *irc;
[764c7d1]187       
[b73ac9c]188        /* First, try to rename in the current write backend, assuming onick
189         * is stored there */
190        status = primary_storage->rename(onick, nnick, password);
[764c7d1]191        if (status != STORAGE_NO_SUCH_USER) {
192                otr_rename(onick, nnick);
[b73ac9c]193                return status;
[764c7d1]194        }
[b73ac9c]195
196        /* Try to load from a migration backend and save to the current backend.
197         * Explicitly remove the account from the migration backend as otherwise
198         * it'd still be usable under the old name */
199       
200        irc = g_new0(irc_t, 1);
201        status = storage_load(onick, password, irc);
202        if (status != STORAGE_OK) {
203                irc_free(irc);
204                return status;
205        }
206
207        g_free(irc->nick);
208        irc->nick = g_strdup(nnick);
209
210        status = storage_save(irc, FALSE);
211        if (status != STORAGE_OK) {
212                irc_free(irc);
213                return status;
214        }
215        irc_free(irc);
216
217        storage_remove(onick, password);
[764c7d1]218        otr_rename(onick, nnick);
[b73ac9c]219
220        return STORAGE_OK;
[ab49fdc]221}
[823de9d]222#endif
Note: See TracBrowser for help on using the repository browser.