c3ab7019acd13c4465820b499dbd8e92ac7fa337
[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, "USE_IMAP",  m_strlen("USE_IMAP"));
63 #ifdef USE_NNTP
64     crc = crc32(crc, "USE_NNTP",  m_strlen("USE_NNTP"));
65 #endif
66     return crc;
67 }
68
69 static const char *
70 mutt_hcache_per_folder(const char *path, const char *folder)
71 {
72     static char buf[_POSIX_PATH_MAX];
73     struct stat st;
74     int pos;
75
76     if (stat(path, &st) < 0 || !S_ISDIR(st.st_mode)) {
77         return path;
78     }
79
80     pos  = m_strcpy(buf, sizeof(buf), path);
81     pos += m_strputc(buf + pos, sizeof(buf) - pos, '/');
82
83     while (*folder) {
84         if (isalnum((unsigned char)*folder) || *folder == '.') {
85             pos += m_strputc(buf + pos, sizeof(buf) - pos, *folder);
86         } else {
87             pos += m_strputc(buf + pos, sizeof(buf) - pos, '_');
88         }
89         folder++;
90     }
91     pos += m_strcpy(buf + pos, sizeof(buf) - pos, ".hdb");
92     return buf;
93 }
94
95 /* store and restore things {{{ */
96
97 static void dump_int(buffer_t *buf, int i)
98 {
99     buffer_add(buf, &i, sizeof(i));
100 }
101
102 static const void *restore_int(const char *d, int *i)
103 {
104     memcpy(i, d, sizeof(*i));
105     return d + sizeof(*i);
106 }
107
108 static void dump_cstr(buffer_t *buf, const char *s)
109 {
110     int size = 0;
111
112     if (m_strisempty(s)) {
113         dump_int(buf, size);
114         return;
115     }
116
117     size = strlen(s) + 1;
118     dump_int(buf, size);
119     buffer_add(buf, s, size);
120 }
121
122 static const void *restore_cstr(const char *d, char **c)
123 {
124     int size;
125
126     d  = restore_int(d, &size);
127     *c = p_dup(d, size);
128     return d + size;
129 }
130
131 static void dump_address(buffer_t *buf, address_t *a)
132 {
133     int counter = 0, pos = buf->len;
134
135     dump_int(buf, counter);
136
137     for (; a; a = a->next, counter++) {
138         dump_cstr(buf, a->personal);
139         dump_cstr(buf, a->mailbox);
140         dump_int(buf, a->group);
141     }
142
143     memcpy(buf->data + pos, &counter, sizeof(counter));
144 }
145
146 static const void *restore_address(const char *d, address_t **a)
147 {
148     int counter;
149
150     d = restore_int(d, &counter);
151
152     for (; counter > 0; counter--) {
153         *a = address_new();
154         d = restore_cstr(d, &(*a)->personal);
155         d = restore_cstr(d, &(*a)->mailbox);
156         d = restore_int(d, &(*a)->group);
157         a = &(*a)->next;
158     }
159
160     *a = NULL;
161     return d;
162 }
163
164 static void dump_list(buffer_t *buf, string_list_t *l)
165 {
166     int pos = buf->len;
167     int counter = 0;
168
169     dump_int(buf, counter);
170
171     for (; l; l = l->next, counter++) {
172         dump_cstr(buf, l->data);
173     }
174
175     memcpy(buf->data + pos, &counter, sizeof(counter));
176 }
177
178 static const void *restore_list(const char *d, string_list_t **l)
179 {
180     int counter;
181
182     d = restore_int(d, &counter);
183
184     for (; counter > 0; counter--) {
185         *l = string_item_new();
186         d = restore_cstr(d, &(*l)->data);
187         l = &(*l)->next;
188     }
189
190     *l = NULL;
191     return d;
192 }
193
194 static void dump_parameter(buffer_t *buf, parameter_t *p)
195 {
196     int pos = buf->len, counter = 0;
197
198     dump_int(buf, counter);
199
200     for (; p; p = p->next, counter++) {
201         dump_cstr(buf, p->attribute);
202         dump_cstr(buf, p->value);
203     }
204
205     memcpy(buf->data + pos, &counter, sizeof(counter));
206 }
207
208 static const void *restore_parameter(const char *d, parameter_t ** p)
209 {
210     int counter;
211
212     d = restore_int(d, &counter);
213
214     for (; counter > 0; counter--) {
215         *p = parameter_new();
216         d  = restore_cstr(d, &(*p)->attribute);
217         d  = restore_cstr(d, &(*p)->value);
218         p  = &(*p)->next;
219     }
220
221     *p = NULL;
222     return d;
223 }
224
225 static void dump_body(buffer_t *buf, BODY *b)
226 {
227     buffer_add(buf, b, sizeof(*b));
228     dump_cstr(buf, b->xtype);
229     dump_cstr(buf, b->subtype);
230
231     dump_parameter(buf, b->parameter);
232
233     dump_cstr(buf, b->description);
234     dump_cstr(buf, b->form_name);
235     dump_cstr(buf, b->filename);
236     dump_cstr(buf, b->d_filename);
237 }
238
239 static const void *restore_body(const char *d, BODY *c)
240 {
241     memcpy(c, d, sizeof(*c));
242     d += sizeof(*c);
243
244     d = restore_cstr(d, &c->xtype);
245     d = restore_cstr(d, &c->subtype);
246
247     d = restore_parameter(d, &c->parameter);
248
249     d = restore_cstr(d, &c->description);
250     d = restore_cstr(d, &c->form_name);
251     d = restore_cstr(d, &c->filename);
252     d = restore_cstr(d, &c->d_filename);
253     return d;
254 }
255
256 static void dump_envelope(buffer_t *buf, ENVELOPE * e)
257 {
258     int n;
259
260     dump_address(buf, e->return_path);
261     dump_address(buf, e->from);
262     dump_address(buf, e->to);
263     dump_address(buf, e->cc);
264     dump_address(buf, e->bcc);
265     dump_address(buf, e->sender);
266     dump_address(buf, e->reply_to);
267     dump_address(buf, e->mail_followup_to);
268
269     dump_cstr(buf, e->subject);
270     n = e->real_subj ? e->real_subj - e->subject : -1;
271     dump_int(buf, n);
272
273     dump_cstr(buf, e->message_id);
274     dump_cstr(buf, e->supersedes);
275     dump_cstr(buf, e->date);
276     dump_cstr(buf, e->x_label);
277     dump_cstr(buf, e->list_post);
278
279 #ifdef USE_NNTP
280     dump_cstr(buf, e->newsgroups);
281     dump_cstr(buf, e->xref);
282     dump_cstr(buf, e->followup_to);
283 #endif
284
285     dump_list(buf, e->references);
286     dump_list(buf, e->in_reply_to);
287     dump_list(buf, e->userhdrs);
288 }
289
290 static const void *restore_envelope(const char *d, ENVELOPE *e)
291 {
292     int real_subj_off;
293
294     d = restore_address(d, &e->return_path);
295     d = restore_address(d, &e->from);
296     d = restore_address(d, &e->to);
297     d = restore_address(d, &e->cc);
298     d = restore_address(d, &e->bcc);
299     d = restore_address(d, &e->sender);
300     d = restore_address(d, &e->reply_to);
301     d = restore_address(d, &e->mail_followup_to);
302
303     d = restore_cstr(d, &e->subject);
304     d = restore_int(d, &real_subj_off);
305     if (real_subj_off >= 0) {
306         e->real_subj = e->subject + real_subj_off;
307     } else {
308         e->real_subj = NULL;
309     }
310     d = restore_cstr(d, &e->message_id);
311     d = restore_cstr(d, &e->supersedes);
312     d = restore_cstr(d, &e->date);
313     d = restore_cstr(d, &e->x_label);
314     d = restore_cstr(d, &e->list_post);
315
316 #ifdef USE_NNTP
317     d = restore_cstr(d, &e->newsgroups);
318     d = restore_cstr(d, &e->xref);
319     d = restore_cstr(d, &e->followup_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     dump_int(res, 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 *folder)
378 {
379     const char *path;
380     hcache_t *h;
381
382     if (m_strisempty(mod_core.cachedir)) {
383         return NULL;
384     }
385
386     h = p_new(hcache_t, 1);
387     h->folder = m_strdup(folder);
388     h->crc    = generate_crc32();
389
390     path = mutt_hcache_per_folder(mod_core.cachedir, 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     char *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 */