74ccb5d3282c496caf2d55a8634234a609904db6
[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     static int crc = 0;
56
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"));
62 #endif
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"));
66 #ifdef USE_NNTP
67     crc = crc32(crc, "USE_NNTP",  m_strlen("USE_NNTP"));
68 #endif
69     return crc;
70 }
71
72 static const char *
73 mutt_hcache_per_folder(const char *path, const char *folder)
74 {
75     static char buf[_POSIX_PATH_MAX];
76     struct stat st;
77     int pos;
78
79     if (stat(path, &st) < 0 || !S_ISDIR(st.st_mode)) {
80         return path;
81     }
82
83     pos  = m_strcpy(buf, sizeof(buf), path);
84     pos += m_strputc(buf + pos, sizeof(buf) - pos, '/');
85
86     while (*folder) {
87         if (isalnum((unsigned char)*folder) || *folder == '.') {
88             pos += m_strputc(buf + pos, sizeof(buf) - pos, *folder);
89         } else {
90             pos += m_strputc(buf + pos, sizeof(buf) - pos, '_');
91         }
92         folder++;
93     }
94     pos += m_strcpy(buf + pos, sizeof(buf) - pos, ".hdb");
95     return buf;
96 }
97
98 /* store and restore things {{{ */
99
100 static const void *restore_int(const char *d, int *i)
101 {
102     memcpy(i, d, sizeof(*i));
103     return d + sizeof(*i);
104 }
105
106 static void dump_cstr(buffer_t *buf, const char *s)
107 {
108     int size = 0;
109
110     if (m_strisempty(s)) {
111         buffer_add(buf, &size, sizeof(size));
112         return;
113     }
114
115     size = strlen(s) + 1;
116     buffer_add(buf, &size, sizeof(size));
117     buffer_add(buf, s, size);
118 }
119
120 static const void *restore_cstr(const char *d, char **c)
121 {
122     int size;
123
124     d  = restore_int(d, &size);
125     *c = p_dup(d, size);
126     return d + size;
127 }
128
129 static void dump_address(buffer_t *buf, address_t *a)
130 {
131     int counter = 0, pos = buf->len;
132
133     buffer_add(buf, &counter, sizeof(counter));
134
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));
139     }
140
141     memcpy(buf->data + pos, &counter, sizeof(counter));
142 }
143
144 static const void *restore_address(const char *d, address_t **a)
145 {
146     int counter;
147
148     d = restore_int(d, &counter);
149
150     for (; counter > 0; counter--) {
151         *a = address_new();
152         d = restore_cstr(d, &(*a)->personal);
153         d = restore_cstr(d, &(*a)->mailbox);
154         d = restore_int(d, &(*a)->group);
155         a = &(*a)->next;
156     }
157
158     *a = NULL;
159     return d;
160 }
161
162 static void dump_list(buffer_t *buf, string_list_t *l)
163 {
164     int pos = buf->len;
165     int counter = 0;
166
167     buffer_add(buf, &counter, sizeof(counter));
168
169     for (; l; l = l->next, counter++) {
170         dump_cstr(buf, l->data);
171     }
172
173     memcpy(buf->data + pos, &counter, sizeof(counter));
174 }
175
176 static const void *restore_list(const char *d, string_list_t **l)
177 {
178     int counter;
179
180     d = restore_int(d, &counter);
181
182     for (; counter > 0; counter--) {
183         *l = string_item_new();
184         d = restore_cstr(d, &(*l)->data);
185         l = &(*l)->next;
186     }
187
188     *l = NULL;
189     return d;
190 }
191
192 static void dump_parameter(buffer_t *buf, parameter_t *p)
193 {
194     int pos = buf->len, counter = 0;
195
196     buffer_add(buf, &counter, sizeof(counter));
197
198     for (; p; p = p->next, counter++) {
199         dump_cstr(buf, p->attribute);
200         dump_cstr(buf, p->value);
201     }
202
203     memcpy(buf->data + pos, &counter, sizeof(counter));
204 }
205
206 static const void *restore_parameter(const char *d, parameter_t ** p)
207 {
208     int counter;
209
210     d = restore_int(d, &counter);
211
212     for (; counter > 0; counter--) {
213         *p = parameter_new();
214         d  = restore_cstr(d, &(*p)->attribute);
215         d  = restore_cstr(d, &(*p)->value);
216         p  = &(*p)->next;
217     }
218
219     *p = NULL;
220     return d;
221 }
222
223 static void dump_body(buffer_t *buf, BODY *b)
224 {
225     buffer_add(buf, b, sizeof(*b));
226     dump_cstr(buf, b->xtype);
227     dump_cstr(buf, b->subtype);
228
229     dump_parameter(buf, b->parameter);
230
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);
235 }
236
237 static const void *restore_body(const char *d, BODY *c)
238 {
239     memcpy(c, d, sizeof(*c));
240     d += sizeof(*c);
241
242     d = restore_cstr(d, &c->xtype);
243     d = restore_cstr(d, &c->subtype);
244
245     d = restore_parameter(d, &c->parameter);
246
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);
251     return d;
252 }
253
254 static void dump_envelope(buffer_t *buf, ENVELOPE * e)
255 {
256     int n;
257
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);
266
267     dump_cstr(buf, e->subject);
268     n = e->real_subj ? e->real_subj - e->subject : -1;
269     buffer_add(buf, &n, sizeof(n));
270
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);
276
277 #ifdef USE_NNTP
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);
282 #endif
283
284     dump_list(buf, e->references);
285     dump_list(buf, e->in_reply_to);
286     dump_list(buf, e->userhdrs);
287 }
288
289 static const void *restore_envelope(const char *d, ENVELOPE *e)
290 {
291     int real_subj_off;
292
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);
301
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;
306     } else {
307         e->real_subj = NULL;
308     }
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);
314
315 #ifdef USE_NNTP
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);
320 #endif
321
322     d = restore_list(d, &e->references);
323     d = restore_list(d, &e->in_reply_to);
324     d = restore_list(d, &e->userhdrs);
325
326     return d;
327 }
328
329 static buffer_t *mutt_hcache_dump(hcache_t *db, HEADER *h, long uid_validity)
330 {
331     buffer_t *res = buffer_new();
332
333     if (!uid_validity) {
334         uid_validity = time(NULL);
335     }
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));
339
340     dump_envelope(res, h->env);
341     dump_body(res, h->content);
342     dump_cstr(res, h->maildir_flags);
343     return res;
344 }
345
346 HEADER *mutt_hcache_restore(const void *_d, HEADER **oh)
347 {
348     const char *d = _d;
349     HEADER *h = header_new();
350
351     /* skip uid_validity + crc */
352     d += sizeof(long) + sizeof(int);
353
354     memcpy(h, d, sizeof(*h));
355     d += sizeof(*h);
356
357     h->env = envelope_new();
358     d = restore_envelope(d, h->env);
359
360     h->content = body_new();
361     d = restore_body(d, h->content);
362
363     d = restore_cstr(d, &h->maildir_flags);
364
365     /* this is needed for maildir style mailboxes */
366     if (oh) {
367         h->old = (*oh)->old;
368         h->path = m_strdup((*oh)->path);
369         header_delete(oh);
370     }
371
372     return h;
373 }
374
375 /* }}} */
376
377 hcache_t *mutt_hcache_open(const char *path, const char *folder)
378 {
379     hcache_t *h = p_new(hcache_t, 1);
380
381     h->folder = m_strdup(folder);
382     h->crc = generate_crc32();
383
384     if (m_strisempty(path)) {
385         p_delete(&h->folder);
386         p_delete(&h);
387         return NULL;
388     }
389
390     path = mutt_hcache_per_folder(path, folder);
391
392     {
393 #if defined(HAVE_QDBM)
394         int flags = VL_OWRITER | VL_OCREAT;
395         if (option(OPTHCACHECOMPRESS))
396             flags |= VL_OZCOMP;
397
398         h->db = vlopen(path, flags, VL_CMPLEX);
399 #elif defined(HAVE_GDBM)
400         int pagesize = atoi(HeaderCachePageSize) ?: 16384;
401
402         h->db = gdbm_open((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
403 #endif
404     }
405
406     if (!h->db) {
407         p_delete(&h->folder);
408         p_delete(&h);
409     }
410     return h;
411 }
412
413 void mutt_hcache_close(hcache_t **db)
414 {
415     if (!*db)
416         return;
417
418 #if defined(HAVE_QDBM)
419     vlclose((*db)->db);
420 #elif defined(HAVE_GDBM)
421     gdbm_close((*db)->db);
422 #endif
423
424     p_delete(&(*db)->folder);
425     p_delete(db);
426 }
427
428 void *mutt_hcache_fetch(hcache_t *db, const char *filename,
429                         ssize_t (*keylen)(const char *fn))
430 {
431     char path[_POSIX_PATH_MAX];
432     void *data = NULL;
433
434     if (!db)
435         return NULL;
436
437     snprintf(path, sizeof(path), "%s%s", db->folder, filename);
438
439     {
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) };
445
446         data = gdbm_fetch(db->db, k).dtpr;
447 #endif
448     }
449
450     if (data) {
451         unsigned crc = 0;
452
453         restore_int(data + sizeof(long), (int *)&crc);
454         if (crc != db->crc)
455             p_delete(&data);
456     }
457
458     return data;
459 }
460
461 int mutt_hcache_store(hcache_t *db, const char *filename, HEADER *header,
462                       long uid_validity, ssize_t (*keylen)(const char *fn))
463 {
464     char path[_POSIX_PATH_MAX];
465     buffer_t *data;
466     int ret;
467
468     if (!db)
469         return -1;
470
471     snprintf(path, sizeof(path), "%s%s", db->folder, filename);
472     data = mutt_hcache_dump(db, header, uid_validity);
473
474     {
475 #if defined(HAVE_QDBM)
476         int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
477
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 };
482
483         ret = gdbm_store(db->db, k, v, GDBM_REPLACE);
484 #endif
485     }
486
487     buffer_delete(&data);
488     return ret;
489 }
490
491 int mutt_hcache_delete(hcache_t *db, const char *filename,
492                        ssize_t (*keylen)(const char *fn))
493 {
494     char path[_POSIX_PATH_MAX];
495
496     if (!db)
497         return -1;
498
499     snprintf(path, sizeof(path), "%s%s", db->folder, filename);
500
501     {
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);
508 #endif
509     }
510 }
511
512 #endif /* USE_HCACHE */