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_CODESET
59 crc = crc32(crc, MCharset.charset, m_strlen(MCharset.charset));
60 crc = crc32(crc, "HAVE_LANGINFO_CODESET",
61 m_strlen("HAVE_LANGINFO_CODESET"));
63 crc = crc32(crc, "USE_POP", m_strlen("USE_POP"));
64 crc = crc32(crc, "MIXMASTER", m_strlen("MIXMASTER"));
65 crc = crc32(crc, "USE_IMAP", m_strlen("USE_IMAP"));
67 crc = crc32(crc, "USE_NNTP", m_strlen("USE_NNTP"));
73 mutt_hcache_per_folder(const char *path, const char *folder)
75 static char buf[_POSIX_PATH_MAX];
79 if (stat(path, &st) < 0 || !S_ISDIR(st.st_mode)) {
83 pos = m_strcpy(buf, sizeof(buf), path);
84 pos += m_strputc(buf + pos, sizeof(buf) - pos, '/');
87 if (isalnum((unsigned char)*folder) || *folder == '.') {
88 pos += m_strputc(buf + pos, sizeof(buf) - pos, *folder);
90 pos += m_strputc(buf + pos, sizeof(buf) - pos, '_');
94 pos += m_strcpy(buf + pos, sizeof(buf) - pos, ".hdb");
98 /* store and restore things {{{ */
100 static const void *restore_int(const char *d, int *i)
102 memcpy(i, d, sizeof(*i));
103 return d + sizeof(*i);
106 static void dump_cstr(buffer_t *buf, const char *s)
110 if (m_strisempty(s)) {
111 buffer_add(buf, &size, sizeof(size));
115 size = strlen(s) + 1;
116 buffer_add(buf, &size, sizeof(size));
117 buffer_add(buf, s, size);
120 static const void *restore_cstr(const char *d, char **c)
124 d = restore_int(d, &size);
129 static void dump_address(buffer_t *buf, address_t *a)
131 int counter = 0, pos = buf->len;
133 buffer_add(buf, &counter, sizeof(counter));
135 for (; a; a = a->next, counter++) {
136 dump_cstr(buf, a->personal);
137 dump_cstr(buf, a->mailbox);
138 buffer_add(buf, &a->group, sizeof(a->group));
141 memcpy(buf->data + pos, &counter, sizeof(counter));
144 static const void *restore_address(const char *d, address_t **a)
148 d = restore_int(d, &counter);
150 for (; counter > 0; counter--) {
152 d = restore_cstr(d, &(*a)->personal);
153 d = restore_cstr(d, &(*a)->mailbox);
154 d = restore_int(d, &(*a)->group);
162 static void dump_list(buffer_t *buf, string_list_t *l)
167 buffer_add(buf, &counter, sizeof(counter));
169 for (; l; l = l->next, counter++) {
170 dump_cstr(buf, l->data);
173 memcpy(buf->data + pos, &counter, sizeof(counter));
176 static const void *restore_list(const char *d, string_list_t **l)
180 d = restore_int(d, &counter);
182 for (; counter > 0; counter--) {
183 *l = string_item_new();
184 d = restore_cstr(d, &(*l)->data);
192 static void dump_parameter(buffer_t *buf, parameter_t *p)
194 int pos = buf->len, counter = 0;
196 buffer_add(buf, &counter, sizeof(counter));
198 for (; p; p = p->next, counter++) {
199 dump_cstr(buf, p->attribute);
200 dump_cstr(buf, p->value);
203 memcpy(buf->data + pos, &counter, sizeof(counter));
206 static const void *restore_parameter(const char *d, parameter_t ** p)
210 d = restore_int(d, &counter);
212 for (; counter > 0; counter--) {
213 *p = parameter_new();
214 d = restore_cstr(d, &(*p)->attribute);
215 d = restore_cstr(d, &(*p)->value);
223 static void dump_body(buffer_t *buf, BODY *b)
225 buffer_add(buf, b, sizeof(*b));
226 dump_cstr(buf, b->xtype);
227 dump_cstr(buf, b->subtype);
229 dump_parameter(buf, b->parameter);
231 dump_cstr(buf, b->description);
232 dump_cstr(buf, b->form_name);
233 dump_cstr(buf, b->filename);
234 dump_cstr(buf, b->d_filename);
237 static const void *restore_body(const char *d, BODY *c)
239 memcpy(c, d, sizeof(*c));
242 d = restore_cstr(d, &c->xtype);
243 d = restore_cstr(d, &c->subtype);
245 d = restore_parameter(d, &c->parameter);
247 d = restore_cstr(d, &c->description);
248 d = restore_cstr(d, &c->form_name);
249 d = restore_cstr(d, &c->filename);
250 d = restore_cstr(d, &c->d_filename);
254 static void dump_envelope(buffer_t *buf, ENVELOPE * e)
258 dump_address(buf, e->return_path);
259 dump_address(buf, e->from);
260 dump_address(buf, e->to);
261 dump_address(buf, e->cc);
262 dump_address(buf, e->bcc);
263 dump_address(buf, e->sender);
264 dump_address(buf, e->reply_to);
265 dump_address(buf, e->mail_followup_to);
267 dump_cstr(buf, e->subject);
268 n = e->real_subj ? e->real_subj - e->subject : -1;
269 buffer_add(buf, &n, sizeof(n));
271 dump_cstr(buf, e->message_id);
272 dump_cstr(buf, e->supersedes);
273 dump_cstr(buf, e->date);
274 dump_cstr(buf, e->x_label);
275 dump_cstr(buf, e->list_post);
278 dump_cstr(buf, e->newsgroups);
279 dump_cstr(buf, e->xref);
280 dump_cstr(buf, e->followup_to);
281 dump_cstr(buf, e->x_comment_to);
284 dump_list(buf, e->references);
285 dump_list(buf, e->in_reply_to);
286 dump_list(buf, e->userhdrs);
289 static const void *restore_envelope(const char *d, ENVELOPE *e)
293 d = restore_address(d, &e->return_path);
294 d = restore_address(d, &e->from);
295 d = restore_address(d, &e->to);
296 d = restore_address(d, &e->cc);
297 d = restore_address(d, &e->bcc);
298 d = restore_address(d, &e->sender);
299 d = restore_address(d, &e->reply_to);
300 d = restore_address(d, &e->mail_followup_to);
302 d = restore_cstr(d, &e->subject);
303 d = restore_int(d, &real_subj_off);
304 if (real_subj_off >= 0) {
305 e->real_subj = e->subject + real_subj_off;
309 d = restore_cstr(d, &e->message_id);
310 d = restore_cstr(d, &e->supersedes);
311 d = restore_cstr(d, &e->date);
312 d = restore_cstr(d, &e->x_label);
313 d = restore_cstr(d, &e->list_post);
316 d = restore_cstr(d, &e->newsgroups);
317 d = restore_cstr(d, &e->xref);
318 d = restore_cstr(d, &e->followup_to);
319 d = restore_cstr(d, &e->x_comment_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 buffer_add(res, &db->crc, sizeof(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 *path, const char *folder)
379 hcache_t *h = p_new(hcache_t, 1);
381 h->folder = m_strdup(folder);
382 h->crc = generate_crc32();
384 if (m_strisempty(path)) {
385 p_delete(&h->folder);
390 path = mutt_hcache_per_folder(path, 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 */