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"));
67 mutt_hcache_per_folder(const char *path, const char *folder)
69 static char buf[_POSIX_PATH_MAX];
73 if (stat(path, &st) < 0 || !S_ISDIR(st.st_mode)) {
77 pos = m_strcpy(buf, sizeof(buf), path);
78 pos += m_strputc(buf + pos, sizeof(buf) - pos, '/');
81 if (isalnum((unsigned char)*folder) || *folder == '.') {
82 pos += m_strputc(buf + pos, sizeof(buf) - pos, *folder);
84 pos += m_strputc(buf + pos, sizeof(buf) - pos, '_');
88 pos += m_strcpy(buf + pos, sizeof(buf) - pos, ".hdb");
92 /* store and restore things {{{ */
94 static void dump_int(buffer_t *buf, int i)
96 buffer_add(buf, &i, sizeof(i));
99 static const void *restore_int(const char *d, int *i)
101 memcpy(i, d, sizeof(*i));
102 return d + sizeof(*i);
105 static void dump_cstr(buffer_t *buf, const char *s)
109 if (m_strisempty(s)) {
114 size = strlen(s) + 1;
116 buffer_add(buf, s, size);
119 static const void *restore_cstr(const char *d, char **c)
123 d = restore_int(d, &size);
128 static void dump_address(buffer_t *buf, address_t *a)
130 int counter = 0, pos = buf->len;
132 dump_int(buf, counter);
134 for (; a; a = a->next, counter++) {
135 dump_cstr(buf, a->personal);
136 dump_cstr(buf, a->mailbox);
137 dump_int(buf, a->group);
140 memcpy(buf->data + pos, &counter, sizeof(counter));
143 static const void *restore_address(const char *d, address_t **a)
147 d = restore_int(d, &counter);
149 for (; counter > 0; counter--) {
151 d = restore_cstr(d, &(*a)->personal);
152 d = restore_cstr(d, &(*a)->mailbox);
153 d = restore_int(d, &(*a)->group);
161 static void dump_list(buffer_t *buf, string_list_t *l)
166 dump_int(buf, counter);
168 for (; l; l = l->next, counter++) {
169 dump_cstr(buf, l->data);
172 memcpy(buf->data + pos, &counter, sizeof(counter));
175 static const void *restore_list(const char *d, string_list_t **l)
179 d = restore_int(d, &counter);
181 for (; counter > 0; counter--) {
182 *l = string_item_new();
183 d = restore_cstr(d, &(*l)->data);
191 static void dump_parameter(buffer_t *buf, parameter_t *p)
193 int pos = buf->len, counter = 0;
195 dump_int(buf, counter);
197 for (; p; p = p->next, counter++) {
198 dump_cstr(buf, p->attribute);
199 dump_cstr(buf, p->value);
202 memcpy(buf->data + pos, &counter, sizeof(counter));
205 static const void *restore_parameter(const char *d, parameter_t ** p)
209 d = restore_int(d, &counter);
211 for (; counter > 0; counter--) {
212 *p = parameter_new();
213 d = restore_cstr(d, &(*p)->attribute);
214 d = restore_cstr(d, &(*p)->value);
222 static void dump_body(buffer_t *buf, BODY *b)
224 buffer_add(buf, b, sizeof(*b));
225 dump_cstr(buf, b->xtype);
226 dump_cstr(buf, b->subtype);
228 dump_parameter(buf, b->parameter);
230 dump_cstr(buf, b->description);
231 dump_cstr(buf, b->form_name);
232 dump_cstr(buf, b->filename);
233 dump_cstr(buf, b->d_filename);
236 static const void *restore_body(const char *d, BODY *c)
238 memcpy(c, d, sizeof(*c));
241 d = restore_cstr(d, &c->xtype);
242 d = restore_cstr(d, &c->subtype);
244 d = restore_parameter(d, &c->parameter);
246 d = restore_cstr(d, &c->description);
247 d = restore_cstr(d, &c->form_name);
248 d = restore_cstr(d, &c->filename);
249 d = restore_cstr(d, &c->d_filename);
253 static void dump_envelope(buffer_t *buf, ENVELOPE * e)
257 dump_address(buf, e->return_path);
258 dump_address(buf, e->from);
259 dump_address(buf, e->to);
260 dump_address(buf, e->cc);
261 dump_address(buf, e->bcc);
262 dump_address(buf, e->sender);
263 dump_address(buf, e->reply_to);
264 dump_address(buf, e->mail_followup_to);
266 dump_cstr(buf, e->subject);
267 n = e->real_subj ? e->real_subj - e->subject : -1;
270 dump_cstr(buf, e->message_id);
271 dump_cstr(buf, e->supersedes);
272 dump_cstr(buf, e->date);
273 dump_cstr(buf, e->x_label);
274 dump_cstr(buf, e->list_post);
276 dump_list(buf, e->references);
277 dump_list(buf, e->in_reply_to);
278 dump_list(buf, e->userhdrs);
281 static const void *restore_envelope(const char *d, ENVELOPE *e)
285 d = restore_address(d, &e->return_path);
286 d = restore_address(d, &e->from);
287 d = restore_address(d, &e->to);
288 d = restore_address(d, &e->cc);
289 d = restore_address(d, &e->bcc);
290 d = restore_address(d, &e->sender);
291 d = restore_address(d, &e->reply_to);
292 d = restore_address(d, &e->mail_followup_to);
294 d = restore_cstr(d, &e->subject);
295 d = restore_int(d, &real_subj_off);
296 if (real_subj_off >= 0) {
297 e->real_subj = e->subject + real_subj_off;
301 d = restore_cstr(d, &e->message_id);
302 d = restore_cstr(d, &e->supersedes);
303 d = restore_cstr(d, &e->date);
304 d = restore_cstr(d, &e->x_label);
305 d = restore_cstr(d, &e->list_post);
307 d = restore_list(d, &e->references);
308 d = restore_list(d, &e->in_reply_to);
309 d = restore_list(d, &e->userhdrs);
313 static buffer_t *mutt_hcache_dump(hcache_t *db, HEADER *h, long uid_validity)
315 buffer_t *res = buffer_new();
318 uid_validity = time(NULL);
320 buffer_add(res, &uid_validity, sizeof(uid_validity));
321 dump_int(res, db->crc);
322 buffer_add(res, h, sizeof(*h));
324 dump_envelope(res, h->env);
325 dump_body(res, h->content);
326 dump_cstr(res, h->maildir_flags);
330 HEADER *mutt_hcache_restore(const void *_d, HEADER **oh)
333 HEADER *h = header_new();
335 /* skip uid_validity + crc */
336 d += sizeof(long) + sizeof(int);
338 memcpy(h, d, sizeof(*h));
341 h->env = envelope_new();
342 d = restore_envelope(d, h->env);
344 h->content = body_new();
345 d = restore_body(d, h->content);
347 d = restore_cstr(d, &h->maildir_flags);
349 /* this is needed for maildir style mailboxes */
352 h->path = m_strdup((*oh)->path);
361 hcache_t *mutt_hcache_open(const char *folder)
366 if (m_strisempty(mod_core.cachedir)) {
370 h = p_new(hcache_t, 1);
371 h->folder = m_strdup(folder);
372 h->crc = generate_crc32();
374 path = mutt_hcache_per_folder(mod_core.cachedir, folder);
377 #if defined(HAVE_QDBM)
378 h->db = vlopen(path, VL_OWRITER | VL_OCREAT, VL_CMPLEX);
379 #elif defined(HAVE_GDBM)
380 h->db = gdbm_open((char *) path, 16384, GDBM_WRCREAT, 00600, NULL);
385 p_delete(&h->folder);
391 void mutt_hcache_close(hcache_t **db)
396 #if defined(HAVE_QDBM)
398 #elif defined(HAVE_GDBM)
399 gdbm_close((*db)->db);
402 p_delete(&(*db)->folder);
406 void *mutt_hcache_fetch(hcache_t *db, const char *filename,
407 ssize_t (*keylen)(const char *fn))
409 char path[_POSIX_PATH_MAX];
415 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
418 #if defined(HAVE_QDBM)
419 int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
420 data = vlget(db->db, path, ksize, NULL);
421 #elif defined(HAVE_GDBM)
422 datum k = { .dptr = path, .dsize = keylen(path) };
424 data = gdbm_fetch(db->db, k).dtpr;
431 restore_int(data + sizeof(long), (int *)&crc);
439 int mutt_hcache_store(hcache_t *db, const char *filename, HEADER *header,
440 long uid_validity, ssize_t (*keylen)(const char *fn))
442 char path[_POSIX_PATH_MAX];
449 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
450 data = mutt_hcache_dump(db, header, uid_validity);
453 #if defined(HAVE_QDBM)
454 int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
456 ret = vlput(db->db, path, ksize, data->data, data->len, VL_DOVER);
457 #elif defined(HAVE_GDBM)
458 datum k = { .dptr = path, .dsize = keylen(path) };
459 datum v = { .dptr = data->data, .dsize = data->len };
461 ret = gdbm_store(db->db, k, v, GDBM_REPLACE);
465 buffer_delete(&data);
469 int mutt_hcache_delete(hcache_t *db, const char *filename,
470 ssize_t (*keylen)(const char *fn))
472 char path[_POSIX_PATH_MAX];
477 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
480 #if defined(HAVE_QDBM)
481 int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
482 return vlout(db->db, path, ksize);
483 #elif defined(HAVE_GDBM)
484 datum k = { .dptr = path, .dsize = keylen(path) };
485 return gdbm_delete(db->db, k);
490 #endif /* USE_HCACHE */