Commit 957c3a0a authored by David Champion's avatar David Champion

Invalidate header caches when spam rules change. closes #3473

User 'exg' on #mutt reported a problem when using header cache with IMAP
and spam rules.  Spam tags are retrieved from cache and not updated when
spam rules change.

1. Enable header caching in muttrc
2. Set spam rules in muttrc
3. Use IMAP (or POP or Maildir).  Your spam rules store spam tags into
   your message headers, and these get cached.
4. Quit mutt
5. Change your spam rules in muttrc
6. Relaunch mutt
7. Because of header caching, the rfc822 headers are not reparsed, and
   spam rules are not reapplied.  User has outdated spam tags.

Spam rules are applied during rfc822 header parsing because it's the
only time we get a full and unadulterated view of all headers.  Once
header parse is done we have only processed headers and 'user headers'
(X-headers).  We might do spam rule matching against the struct HEADER
copy of headers, except that:

a. the header might not be in HEADER if it's non-standard and not an
b. spam rules are regular expressions matching any arbitrary header
   line, and we would need to map these regexps to the header used to
   create the HEADER fields.  This would change the syntax and behavior
   of spam rules dramatically.

One solution, perhaps stopgap, is to invalidate the header cache when
spam rules change.  That's what this patch does.  It alters the hcache
versioning algorithm so that in addition to using the compiled-in
checksum of mutt's structures definitions, we also mix in the user's
runtime spam rules.  Any time spam rules change the hcachever hash value
changes, causing the hcache to be rewritten.
parent cc56d921
......@@ -47,6 +47,8 @@
#include "md5.h"
#include "rfc822.h"
unsigned int hcachever = 0x0;
static struct header_cache
......@@ -1119,9 +1121,41 @@ mutt_hcache_open(const char *path, const char *folder, hcache_namer_t namer)
hcache_open = hcache_open_db4;
/* Calculate the current hcache version from dynamic configuration */
if (hcachever == 0x0) {
unsigned char digest[16];
struct md5_ctx ctx;
SPAM_LIST *spam;
RX_LIST *nospam;
hcachever = HCACHEVER;
/* Seed with the compiled-in header structure hash */
md5_process_bytes(&hcachever, sizeof(hcachever), &ctx);
/* Mix in user's spam list */
for (spam = SpamList; spam; spam = spam->next)
md5_process_bytes(spam->rx->pattern, strlen(spam->rx->pattern), &ctx);
md5_process_bytes(spam->template, strlen(spam->template), &ctx);
/* Mix in user's nospam list */
for (nospam = NoSpamList; nospam; nospam = nospam->next)
md5_process_bytes(nospam->rx->pattern, strlen(nospam->rx->pattern), &ctx);
/* Get a hash and take its bytes as an (unsigned int) hash version */
md5_finish_ctx(&ctx, digest);
hcachever = *((unsigned int *)digest);
h->db = NULL;
h->folder = get_foldername(folder);
h->crc = HCACHEVER;
h->crc = hcachever;
if (!path || path[0] == '\0')
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment