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