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, MCharset.charset, m_strlen(MCharset.charset));
61 crc = crc32(crc, "USE_POP", m_strlen("USE_POP"));
62 crc = crc32(crc, "MIXMASTER", m_strlen("MIXMASTER"));
63 crc = crc32(crc, "USE_IMAP", m_strlen("USE_IMAP"));
65 crc = crc32(crc, "USE_NNTP", m_strlen("USE_NNTP"));
71 mutt_hcache_per_folder(const char *path, const char *folder)
73 static char buf[_POSIX_PATH_MAX];
77 if (stat(path, &st) < 0 || !S_ISDIR(st.st_mode)) {
81 pos = m_strcpy(buf, sizeof(buf), path);
82 pos += m_strputc(buf + pos, sizeof(buf) - pos, '/');
85 if (isalnum((unsigned char)*folder) || *folder == '.') {
86 pos += m_strputc(buf + pos, sizeof(buf) - pos, *folder);
88 pos += m_strputc(buf + pos, sizeof(buf) - pos, '_');
92 pos += m_strcpy(buf + pos, sizeof(buf) - pos, ".hdb");
96 /* store and restore things {{{ */
98 static void dump_int(buffer_t *buf, int i)
100 buffer_add(buf, &i, sizeof(i));
103 static const void *restore_int(const char *d, int *i)
105 memcpy(i, d, sizeof(*i));
106 return d + sizeof(*i);
109 static void dump_cstr(buffer_t *buf, const char *s)
113 if (m_strisempty(s)) {
118 size = strlen(s) + 1;
120 buffer_add(buf, s, size);
123 static const void *restore_cstr(const char *d, char **c)
127 d = restore_int(d, &size);
132 static void dump_address(buffer_t *buf, address_t *a)
134 int counter = 0, pos = buf->len;
136 dump_int(buf, counter);
138 for (; a; a = a->next, counter++) {
139 dump_cstr(buf, a->personal);
140 dump_cstr(buf, a->mailbox);
141 dump_int(buf, a->group);
144 memcpy(buf->data + pos, &counter, sizeof(counter));
147 static const void *restore_address(const char *d, address_t **a)
151 d = restore_int(d, &counter);
153 for (; counter > 0; counter--) {
155 d = restore_cstr(d, &(*a)->personal);
156 d = restore_cstr(d, &(*a)->mailbox);
157 d = restore_int(d, &(*a)->group);
165 static void dump_list(buffer_t *buf, string_list_t *l)
170 dump_int(buf, counter);
172 for (; l; l = l->next, counter++) {
173 dump_cstr(buf, l->data);
176 memcpy(buf->data + pos, &counter, sizeof(counter));
179 static const void *restore_list(const char *d, string_list_t **l)
183 d = restore_int(d, &counter);
185 for (; counter > 0; counter--) {
186 *l = string_item_new();
187 d = restore_cstr(d, &(*l)->data);
195 static void dump_parameter(buffer_t *buf, parameter_t *p)
197 int pos = buf->len, counter = 0;
199 dump_int(buf, counter);
201 for (; p; p = p->next, counter++) {
202 dump_cstr(buf, p->attribute);
203 dump_cstr(buf, p->value);
206 memcpy(buf->data + pos, &counter, sizeof(counter));
209 static const void *restore_parameter(const char *d, parameter_t ** p)
213 d = restore_int(d, &counter);
215 for (; counter > 0; counter--) {
216 *p = parameter_new();
217 d = restore_cstr(d, &(*p)->attribute);
218 d = restore_cstr(d, &(*p)->value);
226 static void dump_body(buffer_t *buf, BODY *b)
228 buffer_add(buf, b, sizeof(*b));
229 dump_cstr(buf, b->xtype);
230 dump_cstr(buf, b->subtype);
232 dump_parameter(buf, b->parameter);
234 dump_cstr(buf, b->description);
235 dump_cstr(buf, b->form_name);
236 dump_cstr(buf, b->filename);
237 dump_cstr(buf, b->d_filename);
240 static const void *restore_body(const char *d, BODY *c)
242 memcpy(c, d, sizeof(*c));
245 d = restore_cstr(d, &c->xtype);
246 d = restore_cstr(d, &c->subtype);
248 d = restore_parameter(d, &c->parameter);
250 d = restore_cstr(d, &c->description);
251 d = restore_cstr(d, &c->form_name);
252 d = restore_cstr(d, &c->filename);
253 d = restore_cstr(d, &c->d_filename);
257 static void dump_envelope(buffer_t *buf, ENVELOPE * e)
261 dump_address(buf, e->return_path);
262 dump_address(buf, e->from);
263 dump_address(buf, e->to);
264 dump_address(buf, e->cc);
265 dump_address(buf, e->bcc);
266 dump_address(buf, e->sender);
267 dump_address(buf, e->reply_to);
268 dump_address(buf, e->mail_followup_to);
270 dump_cstr(buf, e->subject);
271 n = e->real_subj ? e->real_subj - e->subject : -1;
274 dump_cstr(buf, e->message_id);
275 dump_cstr(buf, e->supersedes);
276 dump_cstr(buf, e->date);
277 dump_cstr(buf, e->x_label);
278 dump_cstr(buf, e->list_post);
281 dump_cstr(buf, e->newsgroups);
282 dump_cstr(buf, e->xref);
283 dump_cstr(buf, e->followup_to);
284 dump_cstr(buf, e->x_comment_to);
287 dump_list(buf, e->references);
288 dump_list(buf, e->in_reply_to);
289 dump_list(buf, e->userhdrs);
292 static const void *restore_envelope(const char *d, ENVELOPE *e)
296 d = restore_address(d, &e->return_path);
297 d = restore_address(d, &e->from);
298 d = restore_address(d, &e->to);
299 d = restore_address(d, &e->cc);
300 d = restore_address(d, &e->bcc);
301 d = restore_address(d, &e->sender);
302 d = restore_address(d, &e->reply_to);
303 d = restore_address(d, &e->mail_followup_to);
305 d = restore_cstr(d, &e->subject);
306 d = restore_int(d, &real_subj_off);
307 if (real_subj_off >= 0) {
308 e->real_subj = e->subject + real_subj_off;
312 d = restore_cstr(d, &e->message_id);
313 d = restore_cstr(d, &e->supersedes);
314 d = restore_cstr(d, &e->date);
315 d = restore_cstr(d, &e->x_label);
316 d = restore_cstr(d, &e->list_post);
319 d = restore_cstr(d, &e->newsgroups);
320 d = restore_cstr(d, &e->xref);
321 d = restore_cstr(d, &e->followup_to);
322 d = restore_cstr(d, &e->x_comment_to);
325 d = restore_list(d, &e->references);
326 d = restore_list(d, &e->in_reply_to);
327 d = restore_list(d, &e->userhdrs);
332 static buffer_t *mutt_hcache_dump(hcache_t *db, HEADER *h, long uid_validity)
334 buffer_t *res = buffer_new();
337 uid_validity = time(NULL);
339 buffer_add(res, &uid_validity, sizeof(uid_validity));
340 dump_int(res, db->crc);
341 buffer_add(res, h, sizeof(*h));
343 dump_envelope(res, h->env);
344 dump_body(res, h->content);
345 dump_cstr(res, h->maildir_flags);
349 HEADER *mutt_hcache_restore(const void *_d, HEADER **oh)
352 HEADER *h = header_new();
354 /* skip uid_validity + crc */
355 d += sizeof(long) + sizeof(int);
357 memcpy(h, d, sizeof(*h));
360 h->env = envelope_new();
361 d = restore_envelope(d, h->env);
363 h->content = body_new();
364 d = restore_body(d, h->content);
366 d = restore_cstr(d, &h->maildir_flags);
368 /* this is needed for maildir style mailboxes */
371 h->path = m_strdup((*oh)->path);
380 hcache_t *mutt_hcache_open(const char *path, const char *folder)
382 hcache_t *h = p_new(hcache_t, 1);
384 h->folder = m_strdup(folder);
385 h->crc = generate_crc32();
387 if (m_strisempty(path)) {
388 p_delete(&h->folder);
393 path = mutt_hcache_per_folder(path, folder);
396 #if defined(HAVE_QDBM)
397 int flags = VL_OWRITER | VL_OCREAT;
398 if (option(OPTHCACHECOMPRESS))
401 h->db = vlopen(path, flags, VL_CMPLEX);
402 #elif defined(HAVE_GDBM)
403 int pagesize = atoi(HeaderCachePageSize) ?: 16384;
405 h->db = gdbm_open((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
410 p_delete(&h->folder);
416 void mutt_hcache_close(hcache_t **db)
421 #if defined(HAVE_QDBM)
423 #elif defined(HAVE_GDBM)
424 gdbm_close((*db)->db);
427 p_delete(&(*db)->folder);
431 void *mutt_hcache_fetch(hcache_t *db, const char *filename,
432 ssize_t (*keylen)(const char *fn))
434 char path[_POSIX_PATH_MAX];
440 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
443 #if defined(HAVE_QDBM)
444 int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
445 data = vlget(db->db, path, ksize, NULL);
446 #elif defined(HAVE_GDBM)
447 datum k = { .dptr = path, .dsize = keylen(path) };
449 data = gdbm_fetch(db->db, k).dtpr;
456 restore_int(data + sizeof(long), (int *)&crc);
464 int mutt_hcache_store(hcache_t *db, const char *filename, HEADER *header,
465 long uid_validity, ssize_t (*keylen)(const char *fn))
467 char path[_POSIX_PATH_MAX];
474 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
475 data = mutt_hcache_dump(db, header, uid_validity);
478 #if defined(HAVE_QDBM)
479 int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
481 ret = vlput(db->db, path, ksize, data->data, data->len, VL_DOVER);
482 #elif defined(HAVE_GDBM)
483 datum k = { .dptr = path, .dsize = keylen(path) };
484 datum v = { .dptr = data->data, .dsize = data->len };
486 ret = gdbm_store(db->db, k, v, GDBM_REPLACE);
490 buffer_delete(&data);
494 int mutt_hcache_delete(hcache_t *db, const char *filename,
495 ssize_t (*keylen)(const char *fn))
497 char path[_POSIX_PATH_MAX];
502 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
505 #if defined(HAVE_QDBM)
506 int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
507 return vlout(db->db, path, ksize);
508 #elif defined(HAVE_GDBM)
509 datum k = { .dptr = path, .dsize = keylen(path) };
510 return gdbm_delete(db->db, k);
515 #endif /* USE_HCACHE */