e59e43ac3a3381bf18581f0fb342c2a39b600819
[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, mod_cset.charset, m_strlen(mod_cset.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 #endif
285
286     dump_list(buf, e->references);
287     dump_list(buf, e->in_reply_to);
288     dump_list(buf, e->userhdrs);
289 }
290
291 static const void *restore_envelope(const char *d, ENVELOPE *e)
292 {
293     int real_subj_off;
294
295     d = restore_address(d, &e->return_path);
296     d = restore_address(d, &e->from);
297     d = restore_address(d, &e->to);
298     d = restore_address(d, &e->cc);
299     d = restore_address(d, &e->bcc);
300     d = restore_address(d, &e->sender);
301     d = restore_address(d, &e->reply_to);
302     d = restore_address(d, &e->mail_followup_to);
303
304     d = restore_cstr(d, &e->subject);
305     d = restore_int(d, &real_subj_off);
306     if (real_subj_off >= 0) {
307         e->real_subj = e->subject + real_subj_off;
308     } else {
309         e->real_subj = NULL;
310     }
311     d = restore_cstr(d, &e->message_id);
312     d = restore_cstr(d, &e->supersedes);
313     d = restore_cstr(d, &e->date);
314     d = restore_cstr(d, &e->x_label);
315     d = restore_cstr(d, &e->list_post);
316
317 #ifdef USE_NNTP
318     d = restore_cstr(d, &e->newsgroups);
319     d = restore_cstr(d, &e->xref);
320     d = restore_cstr(d, &e->followup_to);
321 #endif
322
323     d = restore_list(d, &e->references);
324     d = restore_list(d, &e->in_reply_to);
325     d = restore_list(d, &e->userhdrs);
326
327     return d;
328 }
329
330 static buffer_t *mutt_hcache_dump(hcache_t *db, HEADER *h, long uid_validity)
331 {
332     buffer_t *res = buffer_new();
333
334     if (!uid_validity) {
335         uid_validity = time(NULL);
336     }
337     buffer_add(res, &uid_validity, sizeof(uid_validity));
338     dump_int(res, db->crc);
339     buffer_add(res, h, sizeof(*h));
340
341     dump_envelope(res, h->env);
342     dump_body(res, h->content);
343     dump_cstr(res, h->maildir_flags);
344     return res;
345 }
346
347 HEADER *mutt_hcache_restore(const void *_d, HEADER **oh)
348 {
349     const char *d = _d;
350     HEADER *h = header_new();
351
352     /* skip uid_validity + crc */
353     d += sizeof(long) + sizeof(int);
354
355     memcpy(h, d, sizeof(*h));
356     d += sizeof(*h);
357
358     h->env = envelope_new();
359     d = restore_envelope(d, h->env);
360
361     h->content = body_new();
362     d = restore_body(d, h->content);
363
364     d = restore_cstr(d, &h->maildir_flags);
365
366     /* this is needed for maildir style mailboxes */
367     if (oh) {
368         h->old = (*oh)->old;
369         h->path = m_strdup((*oh)->path);
370         header_delete(oh);
371     }
372
373     return h;
374 }
375
376 /* }}} */
377
378 hcache_t *mutt_hcache_open(const char *path, const char *folder)
379 {
380     hcache_t *h = p_new(hcache_t, 1);
381
382     h->folder = m_strdup(folder);
383     h->crc = generate_crc32();
384
385     if (m_strisempty(path)) {
386         p_delete(&h->folder);
387         p_delete(&h);
388         return NULL;
389     }
390
391     path = mutt_hcache_per_folder(path, folder);
392
393     {
394 #if defined(HAVE_QDBM)
395         int flags = VL_OWRITER | VL_OCREAT;
396         if (option(OPTHCACHECOMPRESS))
397             flags |= VL_OZCOMP;
398
399         h->db = vlopen(path, flags, VL_CMPLEX);
400 #elif defined(HAVE_GDBM)
401         int pagesize = atoi(HeaderCachePageSize) ?: 16384;
402
403         h->db = gdbm_open((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
404 #endif
405     }
406
407     if (!h->db) {
408         p_delete(&h->folder);
409         p_delete(&h);
410     }
411     return h;
412 }
413
414 void mutt_hcache_close(hcache_t **db)
415 {
416     if (!*db)
417         return;
418
419 #if defined(HAVE_QDBM)
420     vlclose((*db)->db);
421 #elif defined(HAVE_GDBM)
422     gdbm_close((*db)->db);
423 #endif
424
425     p_delete(&(*db)->folder);
426     p_delete(db);
427 }
428
429 void *mutt_hcache_fetch(hcache_t *db, const char *filename,
430                         ssize_t (*keylen)(const char *fn))
431 {
432     char path[_POSIX_PATH_MAX];
433     char *data = NULL;
434
435     if (!db)
436         return NULL;
437
438     snprintf(path, sizeof(path), "%s%s", db->folder, filename);
439
440     {
441 #if defined(HAVE_QDBM)
442         int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
443         data  = vlget(db->db, path, ksize, NULL);
444 #elif defined(HAVE_GDBM)
445         datum k = { .dptr = path, .dsize = keylen(path) };
446
447         data = gdbm_fetch(db->db, k).dtpr;
448 #endif
449     }
450
451     if (data) {
452         unsigned crc = 0;
453
454         restore_int(data + sizeof(long), (int *)&crc);
455         if (crc != db->crc)
456             p_delete(&data);
457     }
458
459     return data;
460 }
461
462 int mutt_hcache_store(hcache_t *db, const char *filename, HEADER *header,
463                       long uid_validity, ssize_t (*keylen)(const char *fn))
464 {
465     char path[_POSIX_PATH_MAX];
466     buffer_t *data;
467     int ret;
468
469     if (!db)
470         return -1;
471
472     snprintf(path, sizeof(path), "%s%s", db->folder, filename);
473     data = mutt_hcache_dump(db, header, uid_validity);
474
475     {
476 #if defined(HAVE_QDBM)
477         int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
478
479         ret = vlput(db->db, path, ksize, data->data, data->len, VL_DOVER);
480 #elif defined(HAVE_GDBM)
481         datum k = { .dptr = path, .dsize = keylen(path) };
482         datum v = { .dptr = data->data, .dsize = data->len };
483
484         ret = gdbm_store(db->db, k, v, GDBM_REPLACE);
485 #endif
486     }
487
488     buffer_delete(&data);
489     return ret;
490 }
491
492 int mutt_hcache_delete(hcache_t *db, const char *filename,
493                        ssize_t (*keylen)(const char *fn))
494 {
495     char path[_POSIX_PATH_MAX];
496
497     if (!db)
498         return -1;
499
500     snprintf(path, sizeof(path), "%s%s", db->folder, filename);
501
502     {
503 #if defined(HAVE_QDBM)
504         int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
505         return vlout(db->db, path, ksize);
506 #elif defined(HAVE_GDBM)
507         datum k = { .dptr = path, .dsize = keylen(path) };
508         return gdbm_delete(db->db, k);
509 #endif
510     }
511 }
512
513 #endif /* USE_HCACHE */