[5ebff60] | 1 | /********************************************************************\ |
---|
[1ee6c18] | 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; |
---|
[6f10697] | 24 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
| 25 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
[1ee6c18] | 26 | */ |
---|
| 27 | |
---|
| 28 | #define BITLBEE_CORE |
---|
| 29 | #include "bitlbee.h" |
---|
| 30 | |
---|
| 31 | extern storage_t storage_text; |
---|
[c121f89] | 32 | extern storage_t storage_xml; |
---|
[1ee6c18] | 33 | |
---|
[c121f89] | 34 | static GList *storage_backends = NULL; |
---|
[1ee6c18] | 35 | |
---|
| 36 | void register_storage_backend(storage_t *backend) |
---|
| 37 | { |
---|
| 38 | storage_backends = g_list_append(storage_backends, backend); |
---|
| 39 | } |
---|
| 40 | |
---|
[b73ac9c] | 41 | static storage_t *storage_init_single(const char *name) |
---|
[1ee6c18] | 42 | { |
---|
| 43 | GList *gl; |
---|
[b3c467b] | 44 | storage_t *st = NULL; |
---|
[1ee6c18] | 45 | |
---|
| 46 | for (gl = storage_backends; gl; gl = gl->next) { |
---|
| 47 | st = gl->data; |
---|
[5ebff60] | 48 | if (strcmp(st->name, name) == 0) { |
---|
[1ee6c18] | 49 | break; |
---|
[5ebff60] | 50 | } |
---|
[1ee6c18] | 51 | } |
---|
| 52 | |
---|
[5ebff60] | 53 | if (gl == NULL) { |
---|
[1ee6c18] | 54 | return NULL; |
---|
[5ebff60] | 55 | } |
---|
[1ee6c18] | 56 | |
---|
[5ebff60] | 57 | if (st->init) { |
---|
[1ee6c18] | 58 | st->init(); |
---|
[5ebff60] | 59 | } |
---|
[1ee6c18] | 60 | |
---|
| 61 | return st; |
---|
| 62 | } |
---|
[ab49fdc] | 63 | |
---|
[b73ac9c] | 64 | GList *storage_init(const char *primary, char **migrate) |
---|
| 65 | { |
---|
| 66 | GList *ret = NULL; |
---|
| 67 | int i; |
---|
| 68 | storage_t *storage; |
---|
[5ebff60] | 69 | |
---|
[c121f89] | 70 | register_storage_backend(&storage_xml); |
---|
[5ebff60] | 71 | |
---|
[b73ac9c] | 72 | storage = storage_init_single(primary); |
---|
[5ebff60] | 73 | if (storage == NULL && storage->save == NULL) { |
---|
[b73ac9c] | 74 | return NULL; |
---|
[5ebff60] | 75 | } |
---|
[b73ac9c] | 76 | |
---|
| 77 | ret = g_list_append(ret, storage); |
---|
| 78 | |
---|
| 79 | for (i = 0; migrate && migrate[i]; i++) { |
---|
| 80 | storage = storage_init_single(migrate[i]); |
---|
[5ebff60] | 81 | |
---|
| 82 | if (storage) { |
---|
[b73ac9c] | 83 | ret = g_list_append(ret, storage); |
---|
[5ebff60] | 84 | } |
---|
[b73ac9c] | 85 | } |
---|
| 86 | |
---|
| 87 | return ret; |
---|
| 88 | } |
---|
| 89 | |
---|
[5ebff60] | 90 | storage_status_t storage_check_pass(const char *nick, const char *password) |
---|
[ab49fdc] | 91 | { |
---|
[b73ac9c] | 92 | GList *gl; |
---|
[5ebff60] | 93 | |
---|
[b73ac9c] | 94 | /* Loop until we don't get NO_SUCH_USER */ |
---|
| 95 | |
---|
| 96 | for (gl = global.storage; gl; gl = gl->next) { |
---|
| 97 | storage_t *st = gl->data; |
---|
| 98 | storage_status_t status; |
---|
| 99 | |
---|
| 100 | status = st->check_pass(nick, password); |
---|
[5ebff60] | 101 | if (status != STORAGE_NO_SUCH_USER) { |
---|
[b73ac9c] | 102 | return status; |
---|
[5ebff60] | 103 | } |
---|
[b73ac9c] | 104 | } |
---|
[5ebff60] | 105 | |
---|
[b73ac9c] | 106 | return STORAGE_NO_SUCH_USER; |
---|
[ab49fdc] | 107 | } |
---|
| 108 | |
---|
[5ebff60] | 109 | storage_status_t storage_load(irc_t * irc, const char *password) |
---|
[ab49fdc] | 110 | { |
---|
[b73ac9c] | 111 | GList *gl; |
---|
[5ebff60] | 112 | |
---|
| 113 | if (irc && irc->status & USTATUS_IDENTIFIED) { |
---|
[3183c21] | 114 | return STORAGE_OTHER_ERROR; |
---|
[5ebff60] | 115 | } |
---|
| 116 | |
---|
[b73ac9c] | 117 | /* Loop until we don't get NO_SUCH_USER */ |
---|
| 118 | for (gl = global.storage; gl; gl = gl->next) { |
---|
| 119 | storage_t *st = gl->data; |
---|
| 120 | storage_status_t status; |
---|
| 121 | |
---|
[3183c21] | 122 | status = st->load(irc, password); |
---|
[5ebff60] | 123 | if (status == STORAGE_OK) { |
---|
[2dcaf9a] | 124 | GSList *l; |
---|
[5ebff60] | 125 | for (l = irc_plugins; l; l = l->next) { |
---|
[2dcaf9a] | 126 | irc_plugin_t *p = l->data; |
---|
[5ebff60] | 127 | if (p->storage_load) { |
---|
| 128 | p->storage_load(irc); |
---|
| 129 | } |
---|
[2dcaf9a] | 130 | } |
---|
[b73ac9c] | 131 | return status; |
---|
[2dcaf9a] | 132 | } |
---|
[5ebff60] | 133 | |
---|
| 134 | if (status != STORAGE_NO_SUCH_USER) { |
---|
[b73ac9c] | 135 | return status; |
---|
[5ebff60] | 136 | } |
---|
[b73ac9c] | 137 | } |
---|
[5ebff60] | 138 | |
---|
[b73ac9c] | 139 | return STORAGE_NO_SUCH_USER; |
---|
[ab49fdc] | 140 | } |
---|
| 141 | |
---|
[5ebff60] | 142 | storage_status_t storage_save(irc_t *irc, char *password, int overwrite) |
---|
[ab49fdc] | 143 | { |
---|
[3183c21] | 144 | storage_status_t st; |
---|
[2dcaf9a] | 145 | GSList *l; |
---|
[5ebff60] | 146 | |
---|
[3183c21] | 147 | if (password != NULL) { |
---|
| 148 | /* Should only use this in the "register" command. */ |
---|
[5ebff60] | 149 | if (irc->password || overwrite) { |
---|
[3183c21] | 150 | return STORAGE_OTHER_ERROR; |
---|
[5ebff60] | 151 | } |
---|
| 152 | |
---|
[3183c21] | 153 | irc_setpass(irc, password); |
---|
| 154 | } else if ((irc->status & USTATUS_IDENTIFIED) == 0) { |
---|
| 155 | return STORAGE_NO_SUCH_USER; |
---|
| 156 | } |
---|
[5ebff60] | 157 | |
---|
| 158 | st = ((storage_t *) global.storage->data)->save(irc, overwrite); |
---|
| 159 | |
---|
| 160 | for (l = irc_plugins; l; l = l->next) { |
---|
[2dcaf9a] | 161 | irc_plugin_t *p = l->data; |
---|
[5ebff60] | 162 | if (p->storage_save) { |
---|
| 163 | p->storage_save(irc); |
---|
| 164 | } |
---|
[2dcaf9a] | 165 | } |
---|
[5ebff60] | 166 | |
---|
[3183c21] | 167 | if (password != NULL) { |
---|
| 168 | irc_setpass(irc, NULL); |
---|
| 169 | } |
---|
[5ebff60] | 170 | |
---|
[3183c21] | 171 | return st; |
---|
[ab49fdc] | 172 | } |
---|
| 173 | |
---|
[5ebff60] | 174 | storage_status_t storage_remove(const char *nick, const char *password) |
---|
[ab49fdc] | 175 | { |
---|
[b73ac9c] | 176 | GList *gl; |
---|
| 177 | storage_status_t ret = STORAGE_OK; |
---|
[2dcaf9a] | 178 | gboolean ok = FALSE; |
---|
| 179 | GSList *l; |
---|
[5ebff60] | 180 | |
---|
| 181 | /* Remove this account from all storage backends. If this isn't |
---|
| 182 | * done, the account will still be usable, it'd just be |
---|
[b73ac9c] | 183 | * loaded from a different backend. */ |
---|
| 184 | for (gl = global.storage; gl; gl = gl->next) { |
---|
| 185 | storage_t *st = gl->data; |
---|
| 186 | storage_status_t status; |
---|
| 187 | |
---|
| 188 | status = st->remove(nick, password); |
---|
[2dcaf9a] | 189 | ok |= status == STORAGE_OK; |
---|
[5ebff60] | 190 | if (status != STORAGE_NO_SUCH_USER && status != STORAGE_OK) { |
---|
[b73ac9c] | 191 | ret = status; |
---|
[5ebff60] | 192 | } |
---|
[b73ac9c] | 193 | } |
---|
[5ebff60] | 194 | |
---|
[2dcaf9a] | 195 | /* If at least one succeeded, remove plugin data. */ |
---|
[5ebff60] | 196 | if (ok) { |
---|
| 197 | for (l = irc_plugins; l; l = l->next) { |
---|
[2dcaf9a] | 198 | irc_plugin_t *p = l->data; |
---|
[5ebff60] | 199 | if (p->storage_remove) { |
---|
| 200 | p->storage_remove(nick); |
---|
| 201 | } |
---|
[2dcaf9a] | 202 | } |
---|
[5ebff60] | 203 | } |
---|
| 204 | |
---|
[b73ac9c] | 205 | return ret; |
---|
[ab49fdc] | 206 | } |
---|