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_QDBM)
20 #elif defined(HAVE_GDBM)
23 #error neither HAVE_QDBM nor HAVE_GDBM are set ?!
31 #if defined(HAVE_QDBM)
33 #elif defined(HAVE_GDBM)
40 static unsigned int crc32(unsigned int crc, const void *src, ssize_t len)
43 const unsigned char *p = src;
47 for (i = 0; i < 8; i++)
48 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
53 static int generate_crc32(void)
57 crc = crc32(crc, "madmutt.2007.05.13", m_strlen("madmutt.2007.05.13"));
58 #ifdef HAVE_LANGINFO_H
59 crc = crc32(crc, mod_cset.charset, m_strlen(mod_cset.charset));
61 crc = crc32(crc, "USE_POP", m_strlen("USE_POP"));
62 crc = crc32(crc, "USE_IMAP", m_strlen("USE_IMAP"));
64 crc = crc32(crc, "USE_NNTP", m_strlen("USE_NNTP"));
70 mutt_hcache_per_folder(const char *path, const char *folder)
72 static char buf[_POSIX_PATH_MAX];
76 if (stat(path, &st) < 0 || !S_ISDIR(st.st_mode)) {
80 pos = m_strcpy(buf, sizeof(buf), path);
81 pos += m_strputc(buf + pos, sizeof(buf) - pos, '/');
84 if (isalnum((unsigned char)*folder) || *folder == '.') {
85 pos += m_strputc(buf + pos, sizeof(buf) - pos, *folder);
87 pos += m_strputc(buf + pos, sizeof(buf) - pos, '_');
91 pos += m_strcpy(buf + pos, sizeof(buf) - pos, ".hdb");
95 /* store and restore things {{{ */
97 static void dump_int(buffer_t *buf, int i)
99 buffer_add(buf, &i, sizeof(i));
102 static const void *restore_int(const char *d, int *i)
104 memcpy(i, d, sizeof(*i));
105 return d + sizeof(*i);
108 static void dump_cstr(buffer_t *buf, const char *s)
112 if (m_strisempty(s)) {
117 size = strlen(s) + 1;
119 buffer_add(buf, s, size);
122 static const void *restore_cstr(const char *d, char **c)
126 d = restore_int(d, &size);
131 static void dump_address(buffer_t *buf, address_t *a)
133 int counter = 0, pos = buf->len;
135 dump_int(buf, counter);
137 for (; a; a = a->next, counter++) {
138 dump_cstr(buf, a->personal);
139 dump_cstr(buf, a->mailbox);
140 dump_int(buf, a->group);
143 memcpy(buf->data + pos, &counter, sizeof(counter));
146 static const void *restore_address(const char *d, address_t **a)
150 d = restore_int(d, &counter);
152 for (; counter > 0; counter--) {
154 d = restore_cstr(d, &(*a)->personal);
155 d = restore_cstr(d, &(*a)->mailbox);
156 d = restore_int(d, &(*a)->group);
164 static void dump_list(buffer_t *buf, string_list_t *l)
169 dump_int(buf, counter);
171 for (; l; l = l->next, counter++) {
172 dump_cstr(buf, l->data);
175 memcpy(buf->data + pos, &counter, sizeof(counter));
178 static const void *restore_list(const char *d, string_list_t **l)
182 d = restore_int(d, &counter);
184 for (; counter > 0; counter--) {
185 *l = string_item_new();
186 d = restore_cstr(d, &(*l)->data);
194 static void dump_parameter(buffer_t *buf, parameter_t *p)
196 int pos = buf->len, counter = 0;
198 dump_int(buf, counter);
200 for (; p; p = p->next, counter++) {
201 dump_cstr(buf, p->attribute);
202 dump_cstr(buf, p->value);
205 memcpy(buf->data + pos, &counter, sizeof(counter));
208 static const void *restore_parameter(const char *d, parameter_t ** p)
212 d = restore_int(d, &counter);
214 for (; counter > 0; counter--) {
215 *p = parameter_new();
216 d = restore_cstr(d, &(*p)->attribute);
217 d = restore_cstr(d, &(*p)->value);
225 static void dump_body(buffer_t *buf, BODY *b)
227 buffer_add(buf, b, sizeof(*b));
228 dump_cstr(buf, b->xtype);
229 dump_cstr(buf, b->subtype);
231 dump_parameter(buf, b->parameter);
233 dump_cstr(buf, b->description);
234 dump_cstr(buf, b->form_name);
235 dump_cstr(buf, b->filename);
236 dump_cstr(buf, b->d_filename);
239 static const void *restore_body(const char *d, BODY *c)
241 memcpy(c, d, sizeof(*c));
244 d = restore_cstr(d, &c->xtype);
245 d = restore_cstr(d, &c->subtype);
247 d = restore_parameter(d, &c->parameter);
249 d = restore_cstr(d, &c->description);
250 d = restore_cstr(d, &c->form_name);
251 d = restore_cstr(d, &c->filename);
252 d = restore_cstr(d, &c->d_filename);
256 static void dump_envelope(buffer_t *buf, ENVELOPE * e)
260 dump_address(buf, e->return_path);
261 dump_address(buf, e->from);
262 dump_address(buf, e->to);
263 dump_address(buf, e->cc);
264 dump_address(buf, e->bcc);
265 dump_address(buf, e->sender);
266 dump_address(buf, e->reply_to);
267 dump_address(buf, e->mail_followup_to);
269 dump_cstr(buf, e->subject);
270 n = e->real_subj ? e->real_subj - e->subject : -1;
273 dump_cstr(buf, e->message_id);
274 dump_cstr(buf, e->supersedes);
275 dump_cstr(buf, e->date);
276 dump_cstr(buf, e->x_label);
277 dump_cstr(buf, e->list_post);
280 dump_cstr(buf, e->newsgroups);
281 dump_cstr(buf, e->xref);
282 dump_cstr(buf, e->followup_to);
285 dump_list(buf, e->references);
286 dump_list(buf, e->in_reply_to);
287 dump_list(buf, e->userhdrs);
290 static const void *restore_envelope(const char *d, ENVELOPE *e)
294 d = restore_address(d, &e->return_path);
295 d = restore_address(d, &e->from);
296 d = restore_address(d, &e->to);
297 d = restore_address(d, &e->cc);
298 d = restore_address(d, &e->bcc);
299 d = restore_address(d, &e->sender);
300 d = restore_address(d, &e->reply_to);
301 d = restore_address(d, &e->mail_followup_to);
303 d = restore_cstr(d, &e->subject);
304 d = restore_int(d, &real_subj_off);
305 if (real_subj_off >= 0) {
306 e->real_subj = e->subject + real_subj_off;
310 d = restore_cstr(d, &e->message_id);
311 d = restore_cstr(d, &e->supersedes);
312 d = restore_cstr(d, &e->date);
313 d = restore_cstr(d, &e->x_label);
314 d = restore_cstr(d, &e->list_post);
317 d = restore_cstr(d, &e->newsgroups);
318 d = restore_cstr(d, &e->xref);
319 d = restore_cstr(d, &e->followup_to);
322 d = restore_list(d, &e->references);
323 d = restore_list(d, &e->in_reply_to);
324 d = restore_list(d, &e->userhdrs);
329 static buffer_t *mutt_hcache_dump(hcache_t *db, HEADER *h, long uid_validity)
331 buffer_t *res = buffer_new();
334 uid_validity = time(NULL);
336 buffer_add(res, &uid_validity, sizeof(uid_validity));
337 dump_int(res, db->crc);
338 buffer_add(res, h, sizeof(*h));
340 dump_envelope(res, h->env);
341 dump_body(res, h->content);
342 dump_cstr(res, h->maildir_flags);
346 HEADER *mutt_hcache_restore(const void *_d, HEADER **oh)
349 HEADER *h = header_new();
351 /* skip uid_validity + crc */
352 d += sizeof(long) + sizeof(int);
354 memcpy(h, d, sizeof(*h));
357 h->env = envelope_new();
358 d = restore_envelope(d, h->env);
360 h->content = body_new();
361 d = restore_body(d, h->content);
363 d = restore_cstr(d, &h->maildir_flags);
365 /* this is needed for maildir style mailboxes */
368 h->path = m_strdup((*oh)->path);
377 hcache_t *mutt_hcache_open(const char *folder)
382 if (m_strisempty(mod_core.cachedir)) {
386 h = p_new(hcache_t, 1);
387 h->folder = m_strdup(folder);
388 h->crc = generate_crc32();
390 path = mutt_hcache_per_folder(mod_core.cachedir, folder);
393 #if defined(HAVE_QDBM)
394 int flags = VL_OWRITER | VL_OCREAT;
395 if (option(OPTHCACHECOMPRESS))
398 h->db = vlopen(path, flags, VL_CMPLEX);
399 #elif defined(HAVE_GDBM)
400 int pagesize = atoi(HeaderCachePageSize) ?: 16384;
402 h->db = gdbm_open((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
407 p_delete(&h->folder);
413 void mutt_hcache_close(hcache_t **db)
418 #if defined(HAVE_QDBM)
420 #elif defined(HAVE_GDBM)
421 gdbm_close((*db)->db);
424 p_delete(&(*db)->folder);
428 void *mutt_hcache_fetch(hcache_t *db, const char *filename,
429 ssize_t (*keylen)(const char *fn))
431 char path[_POSIX_PATH_MAX];
437 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
440 #if defined(HAVE_QDBM)
441 int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
442 data = vlget(db->db, path, ksize, NULL);
443 #elif defined(HAVE_GDBM)
444 datum k = { .dptr = path, .dsize = keylen(path) };
446 data = gdbm_fetch(db->db, k).dtpr;
453 restore_int(data + sizeof(long), (int *)&crc);
461 int mutt_hcache_store(hcache_t *db, const char *filename, HEADER *header,
462 long uid_validity, ssize_t (*keylen)(const char *fn))
464 char path[_POSIX_PATH_MAX];
471 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
472 data = mutt_hcache_dump(db, header, uid_validity);
475 #if defined(HAVE_QDBM)
476 int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
478 ret = vlput(db->db, path, ksize, data->data, data->len, VL_DOVER);
479 #elif defined(HAVE_GDBM)
480 datum k = { .dptr = path, .dsize = keylen(path) };
481 datum v = { .dptr = data->data, .dsize = data->len };
483 ret = gdbm_store(db->db, k, v, GDBM_REPLACE);
487 buffer_delete(&data);
491 int mutt_hcache_delete(hcache_t *db, const char *filename,
492 ssize_t (*keylen)(const char *fn))
494 char path[_POSIX_PATH_MAX];
499 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
502 #if defined(HAVE_QDBM)
503 int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
504 return vlout(db->db, path, ksize);
505 #elif defined(HAVE_GDBM)
506 datum k = { .dptr = path, .dsize = keylen(path) };
507 return gdbm_delete(db->db, k);
512 #endif /* USE_HCACHE */