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_QDBM)
23 #elif defined(HAVE_GDBM)
26 #error no supported DB library found ?
34 #if defined(HAVE_TOKYOCABINET)
36 #elif defined(HAVE_QDBM)
38 #elif defined(HAVE_GDBM)
45 static unsigned int crc32(unsigned int crc, const void *src, ssize_t len)
48 const unsigned char *p = src;
52 for (i = 0; i < 8; i++)
53 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
58 static int generate_crc32(void)
62 crc = crc32(crc, "madmutt.2007.05.13", m_strlen("madmutt.2007.05.13"));
63 #ifdef HAVE_LANGINFO_H
64 crc = crc32(crc, mod_cset.charset, m_strlen(mod_cset.charset));
66 crc = crc32(crc, "USE_POP", m_strlen("USE_POP"));
67 crc = crc32(crc, "USE_IMAP", m_strlen("USE_IMAP"));
72 mutt_hcache_per_folder(const char *path, const char *folder)
74 static char buf[_POSIX_PATH_MAX];
78 if (stat(path, &st) < 0 || !S_ISDIR(st.st_mode)) {
82 pos = m_strcpy(buf, sizeof(buf), path);
83 pos += m_strputc(buf + pos, sizeof(buf) - pos, '/');
86 if (isalnum((unsigned char)*folder) || *folder == '.') {
87 pos += m_strputc(buf + pos, sizeof(buf) - pos, *folder);
89 pos += m_strputc(buf + pos, sizeof(buf) - pos, '_');
93 pos += m_strcpy(buf + pos, sizeof(buf) - pos, ".hdb");
97 /* store and restore things {{{ */
99 static void dump_int(buffer_t *buf, int i)
101 buffer_add(buf, &i, sizeof(i));
104 static const void *restore_int(const char *d, int *i)
106 memcpy(i, d, sizeof(*i));
107 return d + sizeof(*i);
110 static void dump_cstr(buffer_t *buf, const char *s)
114 if (m_strisempty(s)) {
119 size = strlen(s) + 1;
121 buffer_add(buf, s, size);
124 static const void *restore_cstr(const char *d, char **c)
128 d = restore_int(d, &size);
133 static void dump_address(buffer_t *buf, address_t *a)
135 int counter = 0, pos = buf->len;
137 dump_int(buf, counter);
139 for (; a; a = a->next, counter++) {
140 dump_cstr(buf, a->personal);
141 dump_cstr(buf, a->mailbox);
142 dump_int(buf, a->group);
145 memcpy(buf->data + pos, &counter, sizeof(counter));
148 static const void *restore_address(const char *d, address_t **a)
152 d = restore_int(d, &counter);
154 for (; counter > 0; counter--) {
156 d = restore_cstr(d, &(*a)->personal);
157 d = restore_cstr(d, &(*a)->mailbox);
158 d = restore_int(d, &(*a)->group);
166 static void dump_list(buffer_t *buf, string_list_t *l)
171 dump_int(buf, counter);
173 for (; l; l = l->next, counter++) {
174 dump_cstr(buf, l->data);
177 memcpy(buf->data + pos, &counter, sizeof(counter));
180 static const void *restore_list(const char *d, string_list_t **l)
184 d = restore_int(d, &counter);
186 for (; counter > 0; counter--) {
187 *l = string_item_new();
188 d = restore_cstr(d, &(*l)->data);
196 static void dump_parameter(buffer_t *buf, parameter_t *p)
198 int pos = buf->len, counter = 0;
200 dump_int(buf, counter);
202 for (; p; p = p->next, counter++) {
203 dump_cstr(buf, p->attribute);
204 dump_cstr(buf, p->value);
207 memcpy(buf->data + pos, &counter, sizeof(counter));
210 static const void *restore_parameter(const char *d, parameter_t ** p)
214 d = restore_int(d, &counter);
216 for (; counter > 0; counter--) {
217 *p = parameter_new();
218 d = restore_cstr(d, &(*p)->attribute);
219 d = restore_cstr(d, &(*p)->value);
227 static void dump_body(buffer_t *buf, BODY *b)
229 buffer_add(buf, b, sizeof(*b));
230 dump_cstr(buf, b->xtype);
231 dump_cstr(buf, b->subtype);
233 dump_parameter(buf, b->parameter);
235 dump_cstr(buf, b->description);
236 dump_cstr(buf, b->form_name);
237 dump_cstr(buf, b->filename);
238 dump_cstr(buf, b->d_filename);
241 static const void *restore_body(const char *d, BODY *c)
243 memcpy(c, d, sizeof(*c));
246 d = restore_cstr(d, &c->xtype);
247 d = restore_cstr(d, &c->subtype);
249 d = restore_parameter(d, &c->parameter);
251 d = restore_cstr(d, &c->description);
252 d = restore_cstr(d, &c->form_name);
253 d = restore_cstr(d, &c->filename);
254 d = restore_cstr(d, &c->d_filename);
258 static void dump_envelope(buffer_t *buf, ENVELOPE * e)
262 dump_address(buf, e->return_path);
263 dump_address(buf, e->from);
264 dump_address(buf, e->to);
265 dump_address(buf, e->cc);
266 dump_address(buf, e->bcc);
267 dump_address(buf, e->sender);
268 dump_address(buf, e->reply_to);
269 dump_address(buf, e->mail_followup_to);
271 dump_cstr(buf, e->subject);
272 n = e->real_subj ? e->real_subj - e->subject : -1;
275 dump_cstr(buf, e->message_id);
276 dump_cstr(buf, e->supersedes);
277 dump_cstr(buf, e->date);
278 dump_cstr(buf, e->x_label);
279 dump_cstr(buf, e->list_post);
281 dump_list(buf, e->references);
282 dump_list(buf, e->in_reply_to);
283 dump_list(buf, e->userhdrs);
286 static const void *restore_envelope(const char *d, ENVELOPE *e)
290 d = restore_address(d, &e->return_path);
291 d = restore_address(d, &e->from);
292 d = restore_address(d, &e->to);
293 d = restore_address(d, &e->cc);
294 d = restore_address(d, &e->bcc);
295 d = restore_address(d, &e->sender);
296 d = restore_address(d, &e->reply_to);
297 d = restore_address(d, &e->mail_followup_to);
299 d = restore_cstr(d, &e->subject);
300 d = restore_int(d, &real_subj_off);
301 if (real_subj_off >= 0) {
302 e->real_subj = e->subject + real_subj_off;
306 d = restore_cstr(d, &e->message_id);
307 d = restore_cstr(d, &e->supersedes);
308 d = restore_cstr(d, &e->date);
309 d = restore_cstr(d, &e->x_label);
310 d = restore_cstr(d, &e->list_post);
312 d = restore_list(d, &e->references);
313 d = restore_list(d, &e->in_reply_to);
314 d = restore_list(d, &e->userhdrs);
318 static buffer_t *mutt_hcache_dump(hcache_t *db, HEADER *h, long uid_validity)
320 buffer_t *res = buffer_new();
323 uid_validity = time(NULL);
325 buffer_add(res, &uid_validity, sizeof(uid_validity));
326 dump_int(res, db->crc);
327 buffer_add(res, h, sizeof(*h));
329 dump_envelope(res, h->env);
330 dump_body(res, h->content);
331 dump_cstr(res, h->maildir_flags);
335 HEADER *mutt_hcache_restore(const void *_d, HEADER **oh)
338 HEADER *h = header_new();
340 /* skip uid_validity + crc */
341 d += sizeof(long) + sizeof(int);
343 memcpy(h, d, sizeof(*h));
346 h->env = envelope_new();
347 d = restore_envelope(d, h->env);
349 h->content = body_new();
350 d = restore_body(d, h->content);
352 d = restore_cstr(d, &h->maildir_flags);
354 /* this is needed for maildir style mailboxes */
357 h->path = m_strdup((*oh)->path);
366 hcache_t *mutt_hcache_open(const char *folder)
371 if (m_strisempty(mod_core.cachedir)) {
375 h = p_new(hcache_t, 1);
376 h->folder = m_strdup(folder);
377 h->crc = generate_crc32();
379 path = mutt_hcache_per_folder(mod_core.cachedir, folder);
382 #if defined(HAVE_TOKYOCABINET)
384 if (!tchdbopen(h->db, path, HDBOWRITER | HDBOCREAT)) {
388 #elif defined(HAVE_QDBM)
389 h->db = vlopen(path, VL_OWRITER | VL_OCREAT, VL_CMPLEX);
390 #elif defined(HAVE_GDBM)
391 h->db = gdbm_open((char *) path, 16384, GDBM_WRCREAT, 00600, NULL);
396 p_delete(&h->folder);
402 void mutt_hcache_close(hcache_t **db)
407 #if defined(HAVE_TOKYOCABINET)
410 #elif defined(HAVE_QDBM)
412 #elif defined(HAVE_GDBM)
413 gdbm_close((*db)->db);
416 p_delete(&(*db)->folder);
420 void *mutt_hcache_fetch(hcache_t *db, const char *filename,
421 ssize_t (*keylen)(const char *fn))
423 char path[_POSIX_PATH_MAX];
429 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
432 int ksize = keylen(path);
433 #if defined(HAVE_TOKYOCABINET)
435 data = tchdbget(db->db, path, ksize, &unused);
436 #elif defined(HAVE_QDBM)
437 data = vlget(db->db, path, ksize, NULL);
438 #elif defined(HAVE_GDBM)
439 datum k = { .dptr = path, .dsize = ksize };
441 data = gdbm_fetch(db->db, k).dtpr;
448 restore_int(data + sizeof(long), (int *)&crc);
456 int mutt_hcache_store(hcache_t *db, const char *filename, HEADER *header,
457 long uid_validity, ssize_t (*keylen)(const char *fn))
459 char path[_POSIX_PATH_MAX];
466 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
467 data = mutt_hcache_dump(db, header, uid_validity);
470 int ksize = keylen(path);
471 #if defined(HAVE_TOKYOCABINET)
472 ret = !!tchdbput(db->db, path, ksize, data->data, data->len) - 1;
473 #elif defined(HAVE_QDBM)
474 ret = !!vlput(db->db, path, ksize, data->data, data->len, VL_DOVER) - 1;
475 #elif defined(HAVE_GDBM)
476 datum k = { .dptr = path, .dsize = ksize };
477 datum v = { .dptr = data->data, .dsize = data->len };
479 ret = gdbm_store(db->db, k, v, GDBM_REPLACE);
483 buffer_delete(&data);
487 void mutt_hcache_delete(hcache_t *db, const char *filename,
488 ssize_t (*keylen)(const char *fn))
490 char path[_POSIX_PATH_MAX];
495 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
498 int ksize = keylen(path);
499 #if defined(HAVE_TOKYOCABINET)
500 tchdbout(db->db, path, ksize);
501 #elif defined(HAVE_QDBM)
502 vlout(db->db, path, ksize);
503 #elif defined(HAVE_GDBM)
504 datum k = { .dptr = path, .dsize = ksize };
505 gdbm_delete(db->db, k);
510 #endif /* USE_HCACHE */