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));
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 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)) {
109 buffer_add(buf, &size, sizeof(size));
113 size = strlen(s) + 1;
114 buffer_add(buf, &size, sizeof(size));
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 buffer_add(buf, &counter, sizeof(counter));
133 for (; a; a = a->next, counter++) {
134 dump_cstr(buf, a->personal);
135 dump_cstr(buf, a->mailbox);
136 buffer_add(buf, &a->group, sizeof(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 buffer_add(buf, &counter, sizeof(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 buffer_add(buf, &counter, sizeof(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;
267 buffer_add(buf, &n, sizeof(n));
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);
276 dump_cstr(buf, e->newsgroups);
277 dump_cstr(buf, e->xref);
278 dump_cstr(buf, e->followup_to);
279 dump_cstr(buf, e->x_comment_to);
282 dump_list(buf, e->references);
283 dump_list(buf, e->in_reply_to);
284 dump_list(buf, e->userhdrs);
287 static const void *restore_envelope(const char *d, ENVELOPE *e)
291 d = restore_address(d, &e->return_path);
292 d = restore_address(d, &e->from);
293 d = restore_address(d, &e->to);
294 d = restore_address(d, &e->cc);
295 d = restore_address(d, &e->bcc);
296 d = restore_address(d, &e->sender);
297 d = restore_address(d, &e->reply_to);
298 d = restore_address(d, &e->mail_followup_to);
300 d = restore_cstr(d, &e->subject);
301 d = restore_int(d, &real_subj_off);
302 if (real_subj_off >= 0) {
303 e->real_subj = e->subject + real_subj_off;
307 d = restore_cstr(d, &e->message_id);
308 d = restore_cstr(d, &e->supersedes);
309 d = restore_cstr(d, &e->date);
310 d = restore_cstr(d, &e->x_label);
311 d = restore_cstr(d, &e->list_post);
314 d = restore_cstr(d, &e->newsgroups);
315 d = restore_cstr(d, &e->xref);
316 d = restore_cstr(d, &e->followup_to);
317 d = restore_cstr(d, &e->x_comment_to);
320 d = restore_list(d, &e->references);
321 d = restore_list(d, &e->in_reply_to);
322 d = restore_list(d, &e->userhdrs);
327 static buffer_t *mutt_hcache_dump(hcache_t *db, HEADER *h, long uid_validity)
329 buffer_t *res = buffer_new();
332 uid_validity = time(NULL);
334 buffer_add(res, &uid_validity, sizeof(uid_validity));
335 buffer_add(res, &db->crc, sizeof(db->crc));
336 buffer_add(res, h, sizeof(*h));
338 dump_envelope(res, h->env);
339 dump_body(res, h->content);
340 dump_cstr(res, h->maildir_flags);
344 HEADER *mutt_hcache_restore(const void *_d, HEADER **oh)
347 HEADER *h = header_new();
349 /* skip uid_validity + crc */
350 d += sizeof(long) + sizeof(int);
352 memcpy(h, d, sizeof(*h));
355 h->env = envelope_new();
356 d = restore_envelope(d, h->env);
358 h->content = body_new();
359 d = restore_body(d, h->content);
361 d = restore_cstr(d, &h->maildir_flags);
363 /* this is needed for maildir style mailboxes */
366 h->path = m_strdup((*oh)->path);
375 hcache_t *mutt_hcache_open(const char *path, const char *folder)
377 hcache_t *h = p_new(hcache_t, 1);
379 h->folder = m_strdup(folder);
380 h->crc = generate_crc32();
382 if (m_strisempty(path)) {
383 p_delete(&h->folder);
388 path = mutt_hcache_per_folder(path, folder);
391 #if defined(HAVE_QDBM)
392 int flags = VL_OWRITER | VL_OCREAT;
393 if (option(OPTHCACHECOMPRESS))
396 h->db = vlopen(path, flags, VL_CMPLEX);
397 #elif defined(HAVE_GDBM)
398 int pagesize = atoi(HeaderCachePageSize) ?: 16384;
400 h->db = gdbm_open((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
405 p_delete(&h->folder);
411 void mutt_hcache_close(hcache_t **db)
416 #if defined(HAVE_QDBM)
418 #elif defined(HAVE_GDBM)
419 gdbm_close((*db)->db);
422 p_delete(&(*db)->folder);
426 void *mutt_hcache_fetch(hcache_t *db, const char *filename,
427 ssize_t (*keylen)(const char *fn))
429 char path[_POSIX_PATH_MAX];
435 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
438 #if defined(HAVE_QDBM)
439 int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
440 data = vlget(db->db, path, ksize, NULL);
441 #elif defined(HAVE_GDBM)
442 datum k = { .dptr = path, .dsize = keylen(path) };
444 data = gdbm_fetch(db->db, k).dtpr;
451 restore_int(data + sizeof(long), (int *)&crc);
459 int mutt_hcache_store(hcache_t *db, const char *filename, HEADER *header,
460 long uid_validity, ssize_t (*keylen)(const char *fn))
462 char path[_POSIX_PATH_MAX];
469 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
470 data = mutt_hcache_dump(db, header, uid_validity);
473 #if defined(HAVE_QDBM)
474 int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
476 ret = vlput(db->db, path, ksize, data->data, data->len, VL_DOVER);
477 #elif defined(HAVE_GDBM)
478 datum k = { .dptr = path, .dsize = keylen(path) };
479 datum v = { .dptr = data->data, .dsize = data->len };
481 ret = gdbm_store(db->db, k, v, GDBM_REPLACE);
485 buffer_delete(&data);
489 int mutt_hcache_delete(hcache_t *db, const char *filename,
490 ssize_t (*keylen)(const char *fn))
492 char path[_POSIX_PATH_MAX];
497 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
500 #if defined(HAVE_QDBM)
501 int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
502 return vlout(db->db, path, ksize);
503 #elif defined(HAVE_GDBM)
504 datum k = { .dptr = path, .dsize = keylen(path) };
505 return gdbm_delete(db->db, k);
510 #endif /* USE_HCACHE */