2 * Copyright notice from original mutt:
3 * Copyright (C) 2004 Thomas Glanzmann <sithglan@stud.uni-erlangen.de>
4 * Copyright (C) 2004 Tobias Werth <sitowert@stud.uni-erlangen.de>
5 * Copyright (C) 2004 Brian Fundakowski Feldman <green@FreeBSD.org>
7 * This file is part of mutt-ng, see http://www.muttng.org/.
8 * It's licensed under the GNU General Public License,
9 * please see the file GPL in the top level source directory.
12 #include <lib-lib/lib-lib.h>
16 #if defined(HAVE_TOKYOCABINET)
19 #elif defined(HAVE_GDBM)
22 #error no supported DB library found ?
30 #if defined(HAVE_TOKYOCABINET)
32 #elif defined(HAVE_GDBM)
39 static unsigned int crc32(unsigned int crc, const void *src, ssize_t len)
42 const unsigned char *p = src;
46 for (i = 0; i < 8; i++)
47 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
52 static int generate_crc32(void)
56 crc = crc32(crc, "madmutt.2007.05.13", m_strlen("madmutt.2007.05.13"));
57 #ifdef HAVE_LANGINFO_H
58 crc = crc32(crc, mod_cset.charset, m_strlen(mod_cset.charset));
60 crc = crc32(crc, "USE_POP", m_strlen("USE_POP"));
61 crc = crc32(crc, "USE_IMAP", m_strlen("USE_IMAP"));
66 mutt_hcache_per_folder(const char *path, const char *folder)
68 static char buf[_POSIX_PATH_MAX];
72 if (stat(path, &st) < 0 || !S_ISDIR(st.st_mode)) {
76 pos = m_strcpy(buf, sizeof(buf), path);
77 pos += m_strputc(buf + pos, sizeof(buf) - pos, '/');
80 if (isalnum((unsigned char)*folder) || *folder == '.') {
81 pos += m_strputc(buf + pos, sizeof(buf) - pos, *folder);
83 pos += m_strputc(buf + pos, sizeof(buf) - pos, '_');
87 pos += m_strcpy(buf + pos, sizeof(buf) - pos, ".hdb");
91 /* store and restore things {{{ */
93 static void dump_int(buffer_t *buf, int i)
95 buffer_add(buf, &i, sizeof(i));
98 static const void *restore_int(const char *d, int *i)
100 memcpy(i, d, sizeof(*i));
101 return d + sizeof(*i);
104 static void dump_cstr(buffer_t *buf, const char *s)
108 if (m_strisempty(s)) {
113 size = strlen(s) + 1;
115 buffer_add(buf, s, size);
118 static const void *restore_cstr(const char *d, char **c)
122 d = restore_int(d, &size);
127 static void dump_address(buffer_t *buf, address_t *a)
129 int counter = 0, pos = buf->len;
131 dump_int(buf, counter);
133 for (; a; a = a->next, counter++) {
134 dump_cstr(buf, a->personal);
135 dump_cstr(buf, a->mailbox);
136 dump_int(buf, a->group);
139 memcpy(buf->data + pos, &counter, sizeof(counter));
142 static const void *restore_address(const char *d, address_t **a)
146 d = restore_int(d, &counter);
148 for (; counter > 0; counter--) {
150 d = restore_cstr(d, &(*a)->personal);
151 d = restore_cstr(d, &(*a)->mailbox);
152 d = restore_int(d, &(*a)->group);
160 static void dump_list(buffer_t *buf, string_list_t *l)
165 dump_int(buf, counter);
167 for (; l; l = l->next, counter++) {
168 dump_cstr(buf, l->data);
171 memcpy(buf->data + pos, &counter, sizeof(counter));
174 static const void *restore_list(const char *d, string_list_t **l)
178 d = restore_int(d, &counter);
180 for (; counter > 0; counter--) {
181 *l = string_item_new();
182 d = restore_cstr(d, &(*l)->data);
190 static void dump_parameter(buffer_t *buf, parameter_t *p)
192 int pos = buf->len, counter = 0;
194 dump_int(buf, counter);
196 for (; p; p = p->next, counter++) {
197 dump_cstr(buf, p->attribute);
198 dump_cstr(buf, p->value);
201 memcpy(buf->data + pos, &counter, sizeof(counter));
204 static const void *restore_parameter(const char *d, parameter_t ** p)
208 d = restore_int(d, &counter);
210 for (; counter > 0; counter--) {
211 *p = parameter_new();
212 d = restore_cstr(d, &(*p)->attribute);
213 d = restore_cstr(d, &(*p)->value);
221 static void dump_body(buffer_t *buf, BODY *b)
223 buffer_add(buf, b, sizeof(*b));
224 dump_cstr(buf, b->xtype);
225 dump_cstr(buf, b->subtype);
227 dump_parameter(buf, b->parameter);
229 dump_cstr(buf, b->description);
230 dump_cstr(buf, b->form_name);
231 dump_cstr(buf, b->filename);
232 dump_cstr(buf, b->d_filename);
235 static const void *restore_body(const char *d, BODY *c)
237 memcpy(c, d, sizeof(*c));
240 d = restore_cstr(d, &c->xtype);
241 d = restore_cstr(d, &c->subtype);
243 d = restore_parameter(d, &c->parameter);
245 d = restore_cstr(d, &c->description);
246 d = restore_cstr(d, &c->form_name);
247 d = restore_cstr(d, &c->filename);
248 d = restore_cstr(d, &c->d_filename);
252 static void dump_envelope(buffer_t *buf, ENVELOPE * e)
256 dump_address(buf, e->return_path);
257 dump_address(buf, e->from);
258 dump_address(buf, e->to);
259 dump_address(buf, e->cc);
260 dump_address(buf, e->bcc);
261 dump_address(buf, e->sender);
262 dump_address(buf, e->reply_to);
263 dump_address(buf, e->mail_followup_to);
265 dump_cstr(buf, e->subject);
266 n = e->real_subj ? e->real_subj - e->subject : -1;
269 dump_cstr(buf, e->message_id);
270 dump_cstr(buf, e->supersedes);
271 dump_cstr(buf, e->date);
272 dump_cstr(buf, e->x_label);
273 dump_cstr(buf, e->list_post);
275 dump_list(buf, e->references);
276 dump_list(buf, e->in_reply_to);
277 dump_list(buf, e->userhdrs);
280 static const void *restore_envelope(const char *d, ENVELOPE *e)
284 d = restore_address(d, &e->return_path);
285 d = restore_address(d, &e->from);
286 d = restore_address(d, &e->to);
287 d = restore_address(d, &e->cc);
288 d = restore_address(d, &e->bcc);
289 d = restore_address(d, &e->sender);
290 d = restore_address(d, &e->reply_to);
291 d = restore_address(d, &e->mail_followup_to);
293 d = restore_cstr(d, &e->subject);
294 d = restore_int(d, &real_subj_off);
295 if (real_subj_off >= 0) {
296 e->real_subj = e->subject + real_subj_off;
300 d = restore_cstr(d, &e->message_id);
301 d = restore_cstr(d, &e->supersedes);
302 d = restore_cstr(d, &e->date);
303 d = restore_cstr(d, &e->x_label);
304 d = restore_cstr(d, &e->list_post);
306 d = restore_list(d, &e->references);
307 d = restore_list(d, &e->in_reply_to);
308 d = restore_list(d, &e->userhdrs);
312 static buffer_t *mutt_hcache_dump(hcache_t *db, HEADER *h, long uid_validity)
314 buffer_t *res = buffer_new();
317 uid_validity = time(NULL);
319 buffer_add(res, &uid_validity, sizeof(uid_validity));
320 dump_int(res, db->crc);
321 buffer_add(res, h, sizeof(*h));
323 dump_envelope(res, h->env);
324 dump_body(res, h->content);
325 dump_cstr(res, h->maildir_flags);
329 HEADER *mutt_hcache_restore(const void *_d, HEADER **oh)
332 HEADER *h = header_new();
334 /* skip uid_validity + crc */
335 d += sizeof(long) + sizeof(int);
337 memcpy(h, d, sizeof(*h));
340 h->env = envelope_new();
341 d = restore_envelope(d, h->env);
343 h->content = body_new();
344 d = restore_body(d, h->content);
346 d = restore_cstr(d, &h->maildir_flags);
348 /* this is needed for maildir style mailboxes */
351 h->path = m_strdup((*oh)->path);
360 hcache_t *mutt_hcache_open(const char *folder)
365 if (m_strisempty(mod_core.cachedir)) {
369 h = p_new(hcache_t, 1);
370 h->folder = m_strdup(folder);
371 h->crc = generate_crc32();
373 path = mutt_hcache_per_folder(mod_core.cachedir, folder);
376 #if defined(HAVE_TOKYOCABINET)
378 if (!tchdbopen(h->db, path, HDBOWRITER | HDBOCREAT)) {
382 #elif defined(HAVE_GDBM)
383 h->db = gdbm_open((char *) path, 16384, GDBM_WRCREAT, 00600, NULL);
388 p_delete(&h->folder);
394 void mutt_hcache_close(hcache_t **db)
399 #if defined(HAVE_TOKYOCABINET)
402 #elif defined(HAVE_GDBM)
403 gdbm_close((*db)->db);
406 p_delete(&(*db)->folder);
410 void *mutt_hcache_fetch(hcache_t *db, const char *filename,
411 ssize_t (*keylen)(const char *fn))
413 char path[_POSIX_PATH_MAX];
419 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
422 int ksize = keylen(path);
423 #if defined(HAVE_TOKYOCABINET)
425 data = tchdbget(db->db, path, ksize, &unused);
426 #elif defined(HAVE_GDBM)
427 datum k = { .dptr = path, .dsize = ksize };
429 data = gdbm_fetch(db->db, k).dtpr;
436 restore_int(data + sizeof(long), (int *)&crc);
444 int mutt_hcache_store(hcache_t *db, const char *filename, HEADER *header,
445 long uid_validity, ssize_t (*keylen)(const char *fn))
447 char path[_POSIX_PATH_MAX];
454 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
455 data = mutt_hcache_dump(db, header, uid_validity);
458 int ksize = keylen(path);
459 #if defined(HAVE_TOKYOCABINET)
460 ret = !!tchdbput(db->db, path, ksize, data->data, data->len) - 1;
461 #elif defined(HAVE_GDBM)
462 datum k = { .dptr = path, .dsize = ksize };
463 datum v = { .dptr = data->data, .dsize = data->len };
465 ret = gdbm_store(db->db, k, v, GDBM_REPLACE);
469 buffer_delete(&data);
473 void mutt_hcache_delete(hcache_t *db, const char *filename,
474 ssize_t (*keylen)(const char *fn))
476 char path[_POSIX_PATH_MAX];
481 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
484 int ksize = keylen(path);
485 #if defined(HAVE_TOKYOCABINET)
486 tchdbout(db->db, path, ksize);
487 #elif defined(HAVE_GDBM)
488 datum k = { .dptr = path, .dsize = ksize };
489 gdbm_delete(db->db, k);
494 #endif /* USE_HCACHE */