8748adf198c0f859064fb3c382f18b8ea905701f
[apps/madmutt.git] / lib-mx / hcache.c
1 /*
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>
6  *
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.
10  */
11
12 #include <lib-lib/lib-lib.h>
13
14 #ifdef USE_HCACHE
15
16 #if defined(HAVE_QDBM)
17 #include <depot.h>
18 #include <cabin.h>
19 #include <villa.h>
20 #elif defined(HAVE_GDBM)
21 #include <gdbm.h>
22 #else
23 #error neither HAVE_QDBM nor HAVE_GDBM are set ?!
24 #endif
25
26 #include "charset.h"
27 #include "mutt.h"
28 #include "hcache.h"
29
30 struct hcache_t {
31 #if defined(HAVE_QDBM)
32     VILLA *db;
33 #elif defined(HAVE_GDBM)
34     GDBM_FILE db;
35 #endif
36     char *folder;
37     unsigned int crc;
38 };
39
40 static unsigned int crc32(unsigned int crc, const void *src, ssize_t len)
41 {
42     int i;
43     const unsigned char *p = src;
44
45     while (len--) {
46         crc ^= *p++;
47         for (i = 0; i < 8; i++)
48             crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
49     }
50     return crc;
51 }
52
53 static int generate_crc32(void)
54 {
55     int crc = 0;
56
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));
60 #endif
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"));
64 #ifdef USE_NNTP
65     crc = crc32(crc, "USE_NNTP",  m_strlen("USE_NNTP"));
66 #endif
67     return crc;
68 }
69
70 static const char *
71 mutt_hcache_per_folder(const char *path, const char *folder)
72 {
73     static char buf[_POSIX_PATH_MAX];
74     struct stat st;
75     int pos;
76
77     if (stat(path, &st) < 0 || !S_ISDIR(st.st_mode)) {
78         return path;
79     }
80
81     pos  = m_strcpy(buf, sizeof(buf), path);
82     pos += m_strputc(buf + pos, sizeof(buf) - pos, '/');
83
84     while (*folder) {
85         if (isalnum((unsigned char)*folder) || *folder == '.') {
86             pos += m_strputc(buf + pos, sizeof(buf) - pos, *folder);
87         } else {
88             pos += m_strputc(buf + pos, sizeof(buf) - pos, '_');
89         }
90         folder++;
91     }
92     pos += m_strcpy(buf + pos, sizeof(buf) - pos, ".hdb");
93     return buf;
94 }
95
96 /* store and restore things {{{ */
97
98 static void dump_int(buffer_t *buf, int i)
99 {
100     buffer_add(buf, &i, sizeof(i));
101 }
102
103 static const void *restore_int(const char *d, int *i)
104 {
105     memcpy(i, d, sizeof(*i));
106     return d + sizeof(*i);
107 }
108
109 static void dump_cstr(buffer_t *buf, const char *s)
110 {
111     int size = 0;
112
113     if (m_strisempty(s)) {
114         dump_int(buf, size);
115         return;
116     }
117
118     size = strlen(s) + 1;
119     dump_int(buf, size);
120     buffer_add(buf, s, size);
121 }
122
123 static const void *restore_cstr(const char *d, char **c)
124 {
125     int size;
126
127     d  = restore_int(d, &size);
128     *c = p_dup(d, size);
129     return d + size;
130 }
131
132 static void dump_address(buffer_t *buf, address_t *a)
133 {
134     int counter = 0, pos = buf->len;
135
136     dump_int(buf, counter);
137
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);
142     }
143
144     memcpy(buf->data + pos, &counter, sizeof(counter));
145 }
146
147 static const void *restore_address(const char *d, address_t **a)
148 {
149     int counter;
150
151     d = restore_int(d, &counter);
152
153     for (; counter > 0; counter--) {
154         *a = address_new();
155         d = restore_cstr(d, &(*a)->personal);
156         d = restore_cstr(d, &(*a)->mailbox);
157         d = restore_int(d, &(*a)->group);
158         a = &(*a)->next;
159     }
160
161     *a = NULL;
162     return d;
163 }
164
165 static void dump_list(buffer_t *buf, string_list_t *l)
166 {
167     int pos = buf->len;
168     int counter = 0;
169
170     dump_int(buf, counter);
171
172     for (; l; l = l->next, counter++) {
173         dump_cstr(buf, l->data);
174     }
175
176     memcpy(buf->data + pos, &counter, sizeof(counter));
177 }
178
179 static const void *restore_list(const char *d, string_list_t **l)
180 {
181     int counter;
182
183     d = restore_int(d, &counter);
184
185     for (; counter > 0; counter--) {
186         *l = string_item_new();
187         d = restore_cstr(d, &(*l)->data);
188         l = &(*l)->next;
189     }
190
191     *l = NULL;
192     return d;
193 }
194
195 static void dump_parameter(buffer_t *buf, parameter_t *p)
196 {
197     int pos = buf->len, counter = 0;
198
199     dump_int(buf, counter);
200
201     for (; p; p = p->next, counter++) {
202         dump_cstr(buf, p->attribute);
203         dump_cstr(buf, p->value);
204     }
205
206     memcpy(buf->data + pos, &counter, sizeof(counter));
207 }
208
209 static const void *restore_parameter(const char *d, parameter_t ** p)
210 {
211     int counter;
212
213     d = restore_int(d, &counter);
214
215     for (; counter > 0; counter--) {
216         *p = parameter_new();
217         d  = restore_cstr(d, &(*p)->attribute);
218         d  = restore_cstr(d, &(*p)->value);
219         p  = &(*p)->next;
220     }
221
222     *p = NULL;
223     return d;
224 }
225
226 static void dump_body(buffer_t *buf, BODY *b)
227 {
228     buffer_add(buf, b, sizeof(*b));
229     dump_cstr(buf, b->xtype);
230     dump_cstr(buf, b->subtype);
231
232     dump_parameter(buf, b->parameter);
233
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);
238 }
239
240 static const void *restore_body(const char *d, BODY *c)
241 {
242     memcpy(c, d, sizeof(*c));
243     d += sizeof(*c);
244
245     d = restore_cstr(d, &c->xtype);
246     d = restore_cstr(d, &c->subtype);
247
248     d = restore_parameter(d, &c->parameter);
249
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);
254     return d;
255 }
256
257 static void dump_envelope(buffer_t *buf, ENVELOPE * e)
258 {
259     int n;
260
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);
269
270     dump_cstr(buf, e->subject);
271     n = e->real_subj ? e->real_subj - e->subject : -1;
272     dump_int(buf, n);
273
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);
279
280 #ifdef USE_NNTP
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);
285 #endif
286
287     dump_list(buf, e->references);
288     dump_list(buf, e->in_reply_to);
289     dump_list(buf, e->userhdrs);
290 }
291
292 static const void *restore_envelope(const char *d, ENVELOPE *e)
293 {
294     int real_subj_off;
295
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);
304
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;
309     } else {
310         e->real_subj = NULL;
311     }
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);
317
318 #ifdef USE_NNTP
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);
323 #endif
324
325     d = restore_list(d, &e->references);
326     d = restore_list(d, &e->in_reply_to);
327     d = restore_list(d, &e->userhdrs);
328
329     return d;
330 }
331
332 static buffer_t *mutt_hcache_dump(hcache_t *db, HEADER *h, long uid_validity)
333 {
334     buffer_t *res = buffer_new();
335
336     if (!uid_validity) {
337         uid_validity = time(NULL);
338     }
339     buffer_add(res, &uid_validity, sizeof(uid_validity));
340     dump_int(res, db->crc);
341     buffer_add(res, h, sizeof(*h));
342
343     dump_envelope(res, h->env);
344     dump_body(res, h->content);
345     dump_cstr(res, h->maildir_flags);
346     return res;
347 }
348
349 HEADER *mutt_hcache_restore(const void *_d, HEADER **oh)
350 {
351     const char *d = _d;
352     HEADER *h = header_new();
353
354     /* skip uid_validity + crc */
355     d += sizeof(long) + sizeof(int);
356
357     memcpy(h, d, sizeof(*h));
358     d += sizeof(*h);
359
360     h->env = envelope_new();
361     d = restore_envelope(d, h->env);
362
363     h->content = body_new();
364     d = restore_body(d, h->content);
365
366     d = restore_cstr(d, &h->maildir_flags);
367
368     /* this is needed for maildir style mailboxes */
369     if (oh) {
370         h->old = (*oh)->old;
371         h->path = m_strdup((*oh)->path);
372         header_delete(oh);
373     }
374
375     return h;
376 }
377
378 /* }}} */
379
380 hcache_t *mutt_hcache_open(const char *path, const char *folder)
381 {
382     hcache_t *h = p_new(hcache_t, 1);
383
384     h->folder = m_strdup(folder);
385     h->crc = generate_crc32();
386
387     if (m_strisempty(path)) {
388         p_delete(&h->folder);
389         p_delete(&h);
390         return NULL;
391     }
392
393     path = mutt_hcache_per_folder(path, folder);
394
395     {
396 #if defined(HAVE_QDBM)
397         int flags = VL_OWRITER | VL_OCREAT;
398         if (option(OPTHCACHECOMPRESS))
399             flags |= VL_OZCOMP;
400
401         h->db = vlopen(path, flags, VL_CMPLEX);
402 #elif defined(HAVE_GDBM)
403         int pagesize = atoi(HeaderCachePageSize) ?: 16384;
404
405         h->db = gdbm_open((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
406 #endif
407     }
408
409     if (!h->db) {
410         p_delete(&h->folder);
411         p_delete(&h);
412     }
413     return h;
414 }
415
416 void mutt_hcache_close(hcache_t **db)
417 {
418     if (!*db)
419         return;
420
421 #if defined(HAVE_QDBM)
422     vlclose((*db)->db);
423 #elif defined(HAVE_GDBM)
424     gdbm_close((*db)->db);
425 #endif
426
427     p_delete(&(*db)->folder);
428     p_delete(db);
429 }
430
431 void *mutt_hcache_fetch(hcache_t *db, const char *filename,
432                         ssize_t (*keylen)(const char *fn))
433 {
434     char path[_POSIX_PATH_MAX];
435     char *data = NULL;
436
437     if (!db)
438         return NULL;
439
440     snprintf(path, sizeof(path), "%s%s", db->folder, filename);
441
442     {
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) };
448
449         data = gdbm_fetch(db->db, k).dtpr;
450 #endif
451     }
452
453     if (data) {
454         unsigned crc = 0;
455
456         restore_int(data + sizeof(long), (int *)&crc);
457         if (crc != db->crc)
458             p_delete(&data);
459     }
460
461     return data;
462 }
463
464 int mutt_hcache_store(hcache_t *db, const char *filename, HEADER *header,
465                       long uid_validity, ssize_t (*keylen)(const char *fn))
466 {
467     char path[_POSIX_PATH_MAX];
468     buffer_t *data;
469     int ret;
470
471     if (!db)
472         return -1;
473
474     snprintf(path, sizeof(path), "%s%s", db->folder, filename);
475     data = mutt_hcache_dump(db, header, uid_validity);
476
477     {
478 #if defined(HAVE_QDBM)
479         int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
480
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 };
485
486         ret = gdbm_store(db->db, k, v, GDBM_REPLACE);
487 #endif
488     }
489
490     buffer_delete(&data);
491     return ret;
492 }
493
494 int mutt_hcache_delete(hcache_t *db, const char *filename,
495                        ssize_t (*keylen)(const char *fn))
496 {
497     char path[_POSIX_PATH_MAX];
498
499     if (!db)
500         return -1;
501
502     snprintf(path, sizeof(path), "%s%s", db->folder, filename);
503
504     {
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);
511 #endif
512     }
513 }
514
515 #endif /* USE_HCACHE */