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