NeoMutt  2025-12-11-694-ga89709
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
hcache.c File Reference

Header cache multiplexor. More...

#include "config.h"
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "lib.h"
#include "compress/lib.h"
#include "store/lib.h"
#include "hcache/hcversion.h"
#include "muttlib.h"
#include "serialize.h"
+ Include dependency graph for hcache.c:

Go to the source code of this file.

Data Structures

struct  RealKey
 Hcache key name (including compression method) More...
 

Functions

static struct RealKeyrealkey (struct HeaderCache *hc, const char *key, size_t keylen, bool compress)
 Compute the real key used in the backend, taking into account the compression method.
 
static void hcache_free (struct HeaderCache **ptr)
 Free a header cache.
 
static struct HeaderCachehcache_new (void)
 Create a new header cache.
 
static size_t header_size (void)
 Compute the size of the header with uuid validity and crc.
 
static uint32_t email_pack_flags (const struct Email *e)
 Pack the Email flags into a uint32_t.
 
static void email_unpack_flags (struct Email *e, uint32_t packed)
 Unpack the Email flags from a uint32_t.
 
static uint32_t email_pack_timezone (const struct Email *e)
 Pack the Email timezone into a uint32_t.
 
static void email_unpack_timezone (struct Email *e, uint32_t packed)
 Unpack the Email timezone from a uint32_t.
 
static void * dump_email (struct HeaderCache *hc, const struct Email *e, int *off, uint32_t uidvalidity)
 Serialise an Email object.
 
static struct Emailrestore_email (const unsigned char *d)
 Restore an Email from data retrieved from the cache.
 
static bool create_hcache_dir (const char *path)
 Create parent dirs for the hcache database.
 
static void hcache_per_folder (struct HeaderCache *hc, struct Buffer *hcpath, const char *path, hcache_namer_t namer)
 Generate the hcache pathname.
 
static char * get_foldername (const char *folder)
 Where should the cache be stored?
 
static void free_raw (struct HeaderCache *hc, void **data)
 Multiplexor for StoreOps::free.
 
static unsigned int generate_hcachever (void)
 Calculate hcache version from dynamic configuration.
 
struct HeaderCachehcache_open (const char *path, const char *folder, hcache_namer_t namer, bool create)
 Multiplexor for StoreOps::open.
 
void hcache_close (struct HeaderCache **ptr)
 Multiplexor for StoreOps::close.
 
struct HCacheEntry hcache_fetch_email (struct HeaderCache *hc, const char *key, size_t keylen, uint32_t uidvalidity)
 Multiplexor for StoreOps::fetch.
 
bool hcache_fetch_raw_obj_full (struct HeaderCache *hc, const char *key, size_t keylen, void *dst, size_t dstlen)
 Fetch a message's header from the cache into a destination object.
 
char * hcache_fetch_raw_str (struct HeaderCache *hc, const char *key, size_t keylen)
 Fetch a string from the cache.
 
int hcache_store_email (struct HeaderCache *hc, const char *key, size_t keylen, struct Email *e, uint32_t uidvalidity)
 Multiplexor for StoreOps::store.
 
int hcache_store_raw (struct HeaderCache *hc, const char *key, size_t keylen, void *data, size_t dlen)
 Store a key / data pair.
 
int hcache_delete_email (struct HeaderCache *hc, const char *key, size_t keylen)
 Multiplexor for StoreOps::delete_record.
 
int hcache_delete_raw (struct HeaderCache *hc, const char *key, size_t keylen)
 Multiplexor for StoreOps::delete_record.
 

Variables

static unsigned int HcacheVer = 0x0
 Header Cache version.
 

Detailed Description

Header cache multiplexor.

Authors
  • Pietro Cerutti
  • Richard Russon
  • Tino Reichardt

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file hcache.c.

Function Documentation

◆ realkey()

static struct RealKey * realkey ( struct HeaderCache * hc,
const char * key,
size_t keylen,
bool compress )
static

Compute the real key used in the backend, taking into account the compression method.

Parameters
hcHeader cache handle
keyOriginal key
keylenLength of original key
compressWill the data be compressed?
Return values
ptrStatic location holding data and length of the real key

Definition at line 80 of file hcache.c.

82{
83 static struct RealKey rk;
84
85 int n = snprintf(rk.key, sizeof(rk.key), "%s/%.*s", hc->folder, (int) keylen, key);
86 if (n < 0)
87 n = 0;
88 rk.keylen = MIN((size_t) n, sizeof(rk.key) - 1);
89
90#ifdef USE_HCACHE_COMPRESSION
91 if (compress && hc->compr_ops)
92 {
93 // Append the compression type, e.g. "-zstd"
94 n = snprintf(rk.key + rk.keylen, sizeof(rk.key) - rk.keylen, "-%s",
95 hc->compr_ops->name);
96 if (n > 0)
97 rk.keylen += MIN((size_t) n, sizeof(rk.key) - 1 - rk.keylen);
98 }
99#endif
100
101 return &rk;
102}
#define MIN(a, b)
Return the minimum of two values.
Definition memory.h:40
const char * name
Compression name.
Definition lib.h:66
char * folder
Folder name.
Definition lib.h:88
const struct ComprOps * compr_ops
Compression backend.
Definition lib.h:92
Hcache key name (including compression method)
Definition hcache.c:67
char key[1024]
Key name.
Definition hcache.c:68
size_t keylen
Length of key.
Definition hcache.c:69
+ Here is the caller graph for this function:

◆ hcache_free()

static void hcache_free ( struct HeaderCache ** ptr)
static

Free a header cache.

Parameters
ptrheader cache to free

Definition at line 108 of file hcache.c.

109{
110 if (!ptr || !*ptr)
111 return;
112
113 struct HeaderCache *hc = *ptr;
114 FREE(&hc->folder);
115
116 FREE(ptr);
117}
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
Header Cache.
Definition lib.h:87
+ Here is the caller graph for this function:

◆ hcache_new()

static struct HeaderCache * hcache_new ( void )
static

Create a new header cache.

Return values
ptrNewly created header cache

Definition at line 123 of file hcache.c.

124{
125 return MUTT_MEM_CALLOC(1, struct HeaderCache);
126}
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
+ Here is the caller graph for this function:

◆ header_size()

static size_t header_size ( void )
static

Compute the size of the header with uuid validity and crc.

Return values
numSize of the header

Definition at line 132 of file hcache.c.

133{
134 return sizeof(int) + sizeof(uint32_t);
135}
+ Here is the caller graph for this function:

◆ email_pack_flags()

static uint32_t email_pack_flags ( const struct Email * e)
inlinestatic

Pack the Email flags into a uint32_t.

Parameters
eEmail to pack
Return values
numuint32_t of packed flags
Note
Order of packing must match email_unpack_flags()

Definition at line 144 of file hcache.c.

145{
146 if (!e)
147 return 0;
148
149 // clang-format off
150 return e->security +
151 (e->expired << 16) +
152 (e->flagged << 17) +
153 (e->mime << 18) +
154 (e->old << 19) +
155 (e->read << 20) +
156 (e->replied << 21) +
157 (e->superseded << 22) +
158 (e->trash << 23);
159 // clang-format on
160}
bool read
Email is read.
Definition email.h:50
bool mime
Has a MIME-Version header?
Definition email.h:48
SecurityFlags security
bit 0-10: flags, bit 11,12: application, bit 13: traditional pgp See: ncrypt/lib.h pgplib....
Definition email.h:43
bool old
Email is seen, but unread.
Definition email.h:49
bool flagged
Marked important?
Definition email.h:47
bool replied
Email has been replied to.
Definition email.h:51
bool expired
Already expired?
Definition email.h:46
bool trash
Message is marked as trashed on disk (used by the maildir_trash option)
Definition email.h:53
bool superseded
Got superseded?
Definition email.h:52
+ Here is the caller graph for this function:

◆ email_unpack_flags()

static void email_unpack_flags ( struct Email * e,
uint32_t packed )
inlinestatic

Unpack the Email flags from a uint32_t.

Parameters
eEmail to unpack into
packedPacked flags
Note
Order of packing must match email_pack_flags()

Definition at line 169 of file hcache.c.

170{
171 if (!e)
172 return;
173
174 // clang-format off
175 e->security = (packed & ((1 << 16) - 1)); // bits 0-15
176 e->expired = (packed & (1 << 16));
177 e->flagged = (packed & (1 << 17));
178 e->mime = (packed & (1 << 18));
179 e->old = (packed & (1 << 19));
180 e->read = (packed & (1 << 20));
181 e->replied = (packed & (1 << 21));
182 e->superseded = (packed & (1 << 22));
183 e->trash = (packed & (1 << 23));
184 // clang-format on
185}
+ Here is the caller graph for this function:

◆ email_pack_timezone()

static uint32_t email_pack_timezone ( const struct Email * e)
inlinestatic

Pack the Email timezone into a uint32_t.

Parameters
eEmail to pack
Return values
numuint32_t of packed timezone
Note
Order of packing must match email_unpack_timezone()

Definition at line 194 of file hcache.c.

195{
196 if (!e)
197 return 0;
198
199 return e->zhours + (e->zminutes << 5) + (e->zoccident << 11);
200}
unsigned int zminutes
Minutes away from UTC.
Definition email.h:57
bool zoccident
True, if west of UTC, False if east.
Definition email.h:58
unsigned int zhours
Hours away from UTC.
Definition email.h:56
+ Here is the caller graph for this function:

◆ email_unpack_timezone()

static void email_unpack_timezone ( struct Email * e,
uint32_t packed )
inlinestatic

Unpack the Email timezone from a uint32_t.

Parameters
eEmail to unpack into
packedPacked timezone
Note
Order of packing must match email_pack_timezone()

Definition at line 209 of file hcache.c.

210{
211 if (!e)
212 return;
213
214 // clang-format off
215 e->zhours = (packed & ((1 << 5) - 1)); // bits 0-4 (5)
216 e->zminutes = ((packed >> 5) & ((1 << 6) - 1)); // bits 5-10 (6)
217 e->zoccident = (packed & (1 << 11)); // bit 11 (1)
218 // clang-format on
219}
+ Here is the caller graph for this function:

◆ dump_email()

static void * dump_email ( struct HeaderCache * hc,
const struct Email * e,
int * off,
uint32_t uidvalidity )
static

Serialise an Email object.

Parameters
hcHeader cache handle
eEmail to serialise
offSize of the binary blob
uidvalidityIMAP server identifier
Return values
ptrBinary blob representing the Email

This function transforms an Email into a binary string so that it can be saved to a database.

Definition at line 232 of file hcache.c.

233{
234 bool convert = !CharsetIsUtf8;
235
236 *off = 0;
237 unsigned char *d = MUTT_MEM_MALLOC(4096, unsigned char);
238
239 d = serial_dump_uint32_t((uidvalidity != 0) ? uidvalidity : mutt_date_now(), d, off);
240 d = serial_dump_int(hc->crc, d, off);
241
242 ASSERT((size_t) *off == header_size());
243
244 uint32_t packed = email_pack_flags(e);
245 d = serial_dump_uint32_t(packed, d, off);
246
247 packed = email_pack_timezone(e);
248 d = serial_dump_uint32_t(packed, d, off);
249
250 uint64_t big = e->date_sent;
251 d = serial_dump_uint64_t(big, d, off);
252 big = e->received;
253 d = serial_dump_uint64_t(big, d, off);
254
255 d = serial_dump_int(e->lines, d, off);
256
257 d = serial_dump_envelope(e->env, d, off, convert);
258 d = serial_dump_body(e->body, d, off, convert);
259 d = serial_dump_tags(&e->tags, d, off);
260
261 return d;
262}
static uint32_t email_pack_flags(const struct Email *e)
Pack the Email flags into a uint32_t.
Definition hcache.c:144
static uint32_t email_pack_timezone(const struct Email *e)
Pack the Email timezone into a uint32_t.
Definition hcache.c:194
static size_t header_size(void)
Compute the size of the header with uuid validity and crc.
Definition hcache.c:132
#define MUTT_MEM_MALLOC(n, type)
Definition memory.h:53
bool CharsetIsUtf8
Is the user's current character set utf-8?
Definition charset.c:66
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition date.c:457
unsigned char * serial_dump_body(const struct Body *b, unsigned char *d, int *off, bool convert)
Pack an Body into a binary blob.
Definition serialize.c:534
unsigned char * serial_dump_tags(const struct TagList *tl, unsigned char *d, int *off)
Pack a TagList into a binary blob.
Definition serialize.c:703
unsigned char * serial_dump_uint64_t(const uint64_t s, unsigned char *d, int *off)
Pack a uint64_t into a binary blob.
Definition serialize.c:105
unsigned char * serial_dump_int(const unsigned int i, unsigned char *d, int *off)
Pack an integer into a binary blob.
Definition serialize.c:73
unsigned char * serial_dump_envelope(const struct Envelope *env, unsigned char *d, int *off, bool convert)
Pack an Envelope into a binary blob.
Definition serialize.c:601
unsigned char * serial_dump_uint32_t(const uint32_t s, unsigned char *d, int *off)
Pack a uint32_t into a binary blob.
Definition serialize.c:89
#define ASSERT(COND)
Definition signal2.h:59
struct Envelope * env
Envelope information.
Definition email.h:68
int lines
How many lines in the body of this message?
Definition email.h:62
struct Body * body
List of MIME parts.
Definition email.h:69
time_t date_sent
Time when the message was sent (UTC)
Definition email.h:60
struct TagList tags
For drivers that support server tagging.
Definition email.h:72
time_t received
Time when the message was placed in the mailbox.
Definition email.h:61
unsigned int crc
CRC of the cache entry.
Definition lib.h:89
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ restore_email()

static struct Email * restore_email ( const unsigned char * d)
static

Restore an Email from data retrieved from the cache.

Parameters
dData retrieved using hcache_fetch_email()
Return values
ptrSuccess, the restored header (can't be NULL)
Note
The returned Email must be free'd by caller code with email_free()

Definition at line 272 of file hcache.c.

273{
274 int off = 0;
275 struct Email *e = email_new();
276 bool convert = !CharsetIsUtf8;
277
278 off += sizeof(uint32_t); // skip validate
279 off += sizeof(unsigned int); // skip crc
280
281 uint32_t packed = 0;
282 serial_restore_uint32_t(&packed, d, &off);
283 email_unpack_flags(e, packed);
284
285 packed = 0;
286 serial_restore_uint32_t(&packed, d, &off);
287 email_unpack_timezone(e, packed);
288
289 uint64_t big = 0;
290 serial_restore_uint64_t(&big, d, &off);
291 e->date_sent = big;
292
293 big = 0;
294 serial_restore_uint64_t(&big, d, &off);
295 e->received = big;
296
297 unsigned int num = 0;
298 serial_restore_int(&num, d, &off);
299 e->lines = num;
300
301 e->env = mutt_env_new();
302 serial_restore_envelope(e->env, d, &off, convert);
303
304 e->body = mutt_body_new();
305 serial_restore_body(e->body, d, &off, convert);
306 serial_restore_tags(&e->tags, d, &off);
307
308 return e;
309}
struct Body * mutt_body_new(void)
Create a new Body.
Definition body.c:44
struct Email * email_new(void)
Create a new Email.
Definition email.c:77
struct Envelope * mutt_env_new(void)
Create a new Envelope.
Definition envelope.c:45
static void email_unpack_flags(struct Email *e, uint32_t packed)
Unpack the Email flags from a uint32_t.
Definition hcache.c:169
static void email_unpack_timezone(struct Email *e, uint32_t packed)
Unpack the Email timezone from a uint32_t.
Definition hcache.c:209
void serial_restore_tags(struct TagList *tl, const unsigned char *d, int *off)
Unpack a TagList from a binary blob.
Definition serialize.c:728
void serial_restore_envelope(struct Envelope *env, const unsigned char *d, int *off, bool convert)
Unpack an Envelope from a binary blob.
Definition serialize.c:649
void serial_restore_uint64_t(uint64_t *s, const unsigned char *d, int *off)
Unpack an uint64_t from a binary blob.
Definition serialize.c:144
void serial_restore_body(struct Body *b, const unsigned char *d, int *off, bool convert)
Unpack a Body from a binary blob.
Definition serialize.c:566
void serial_restore_int(unsigned int *i, const unsigned char *d, int *off)
Unpack an integer from a binary blob.
Definition serialize.c:120
void serial_restore_uint32_t(uint32_t *s, const unsigned char *d, int *off)
Unpack an uint32_t from a binary blob.
Definition serialize.c:132
The envelope/body of an email.
Definition email.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ create_hcache_dir()

static bool create_hcache_dir ( const char * path)
static

Create parent dirs for the hcache database.

Parameters
pathDatabase filename
Return values
trueSuccess
falseFailure (errno set)

Definition at line 317 of file hcache.c.

318{
319 char *dir = mutt_str_dup(path);
320 if (!dir)
321 return false;
322
323 char *p = strrchr(dir, '/');
324 if (!p)
325 {
326 FREE(&dir);
327 return true;
328 }
329
330 *p = '\0';
331
332 int rc = mutt_file_mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
333 if (rc != 0)
334 mutt_error(_("Can't create %s: %s"), dir, strerror(errno));
335
336 FREE(&dir);
337 return (rc == 0);
338}
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition file.c:844
#define mutt_error(...)
Definition logging2.h:94
#define _(a)
Definition message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hcache_per_folder()

static void hcache_per_folder ( struct HeaderCache * hc,
struct Buffer * hcpath,
const char * path,
hcache_namer_t namer )
static

Generate the hcache pathname.

Parameters
hcHeader cache handle
hcpathBuffer for the result
pathBase directory, from $header_cache
namerCallback to generate database filename - Implements hcache_namer_t

Generate the pathname for the hcache database, it will be of the form: BASE/FOLDER/NAME

  • BASE: Base directory (path)
  • FOLDER: Mailbox name (hc->folder)
  • NAME: Create by namer, or md5sum of hc->folder

This function will create any parent directories needed, so the caller just needs to create the database file.

If path exists and is a directory, it is used. If path has a trailing '/' it is assumed to be a directory. Otherwise path is assumed to be a file.

Definition at line 361 of file hcache.c.

363{
364 struct stat st = { 0 };
365
366 int plen = mutt_str_len(path);
367 int rc = stat(path, &st);
368 bool slash = (path[plen - 1] == '/');
369
370 if (((rc == 0) && !S_ISDIR(st.st_mode)) || ((rc == -1) && !slash))
371 {
372 /* An existing file or a non-existing path not ending with a slash */
373 mutt_encode_path(hcpath, path);
375 return;
376 }
377
378 /* We have a directory - no matter whether it exists, or not */
379 struct Buffer *hcfile = buf_pool_get();
380 if (namer)
381 {
382 namer(hc->store_ops, hc->folder, hcfile);
383 mutt_encode_path(hcfile, buf_string(hcfile));
384 buf_concat_path(hcpath, path, buf_string(hcfile));
385 }
386 else
387 {
388 unsigned char m[16]; /* binary md5sum */
389 struct Buffer *name = buf_pool_get();
390
391#ifdef USE_HCACHE_COMPRESSION
392 const char *cm = hc->compr_ops ? hc->compr_ops->name : "";
393 buf_printf(name, "%s|%s%s", hc->store_ops->name, hc->folder, cm);
394#else
395 buf_printf(name, "%s|%s", hc->store_ops->name, hc->folder);
396#endif
397 mutt_md5(buf_string(name), m);
398 buf_reset(name);
399 mutt_md5_toascii(m, name->data);
400 mutt_encode_path(name, buf_string(name));
401 buf_printf(hcpath, "%s%s%s", path, slash ? "" : "/", buf_string(name));
402 buf_pool_release(&name);
403 }
404
406 buf_pool_release(&hcfile);
407}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:76
size_t buf_concat_path(struct Buffer *buf, const char *dir, const char *fname)
Join a directory name and a filename.
Definition buffer.c:509
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
static bool create_hcache_dir(const char *path)
Create parent dirs for the hcache database.
Definition hcache.c:317
void * mutt_md5(const char *str, void *buf)
Calculate the MD5 hash of a NUL-terminated string.
Definition md5.c:317
void mutt_md5_toascii(const void *digest, char *resbuf)
Convert a binary MD5 digest into ASCII Hexadecimal.
Definition md5.c:456
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:503
void mutt_encode_path(struct Buffer *buf, const char *src)
Convert a path to 'us-ascii'.
Definition muttlib.c:803
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
String manipulation buffer.
Definition buffer.h:36
char * data
Pointer to data.
Definition buffer.h:37
const struct StoreOps * store_ops
Store backend.
Definition lib.h:90
const char * name
Store name.
Definition lib.h:67
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_foldername()

static char * get_foldername ( const char * folder)
static

Where should the cache be stored?

Parameters
folderPath to be canonicalised
Return values
ptrNew string with canonical path

Definition at line 414 of file hcache.c.

415{
416 /* if the folder is local, canonify the path to ensure equivalent paths share
417 * the hcache */
418 char *p = MUTT_MEM_MALLOC(PATH_MAX + 1, char);
419 if (!realpath(folder, p))
420 mutt_str_replace(&p, folder);
421
422 return p;
423}
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition string.c:284
#define PATH_MAX
Definition mutt.h:49
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ free_raw()

static void free_raw ( struct HeaderCache * hc,
void ** data )
static

Multiplexor for StoreOps::free.

Definition at line 428 of file hcache.c.

429{
430 hc->store_ops->free(hc->store_handle, data);
431}
StoreHandle * store_handle
Store handle.
Definition lib.h:91
void(* free)(StoreHandle *store, void **ptr)
Definition lib.h:107
+ Here is the caller graph for this function:

◆ generate_hcachever()

static unsigned int generate_hcachever ( void )
static

Calculate hcache version from dynamic configuration.

Return values
numHeader cache version

< MD5 digest as a string

< MD5 digest as an integer

Definition at line 437 of file hcache.c.

438{
439 union
440 {
441 unsigned char charval[16];
442 unsigned int intval;
443 } digest;
444 struct Md5Ctx md5ctx = { 0 };
445
446 mutt_md5_init_ctx(&md5ctx);
447
448 /* Seed with the compiled-in header structure hash */
449 unsigned int ver = HCACHEVER;
450 mutt_md5_process_bytes(&ver, sizeof(ver), &md5ctx);
451
453 ASSERT(md);
454
455 /* Mix in user's spam list */
456 struct Replace *sp = NULL;
457 STAILQ_FOREACH(sp, &md->spam, entries)
458 {
459 mutt_md5_process(sp->regex->pattern, &md5ctx);
460 mutt_md5_process(sp->templ, &md5ctx);
461 }
462
463 /* Mix in user's nospam list */
464 struct RegexNode *np = NULL;
465 STAILQ_FOREACH(np, &md->no_spam, entries)
466 {
467 mutt_md5_process(np->regex->pattern, &md5ctx);
468 }
469
470 /* Get a hash and take its bytes as an (unsigned int) hash version */
471 mutt_md5_finish_ctx(&md5ctx, digest.charval);
472
473 return digest.intval;
474}
void mutt_md5_process_bytes(const void *buf, size_t buflen, struct Md5Ctx *md5ctx)
Process a block of data.
Definition md5.c:373
void mutt_md5_process(const char *str, struct Md5Ctx *md5ctx)
Process a NUL-terminated string.
Definition md5.c:355
void mutt_md5_init_ctx(struct Md5Ctx *md5ctx)
Initialise the MD5 computation.
Definition md5.c:261
void * mutt_md5_finish_ctx(struct Md5Ctx *md5ctx, void *resbuf)
Process the remaining bytes in the buffer.
Definition md5.c:285
@ MODULE_ID_EMAIL
ModuleEmail, Email code
Definition module_api.h:64
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:585
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
Email private Module data.
Definition module_data.h:32
struct ReplaceList spam
Regexes and patterns to match spam emails.
Definition module_data.h:41
struct RegexList no_spam
Regexes to identify non-spam emails.
Definition module_data.h:40
Cursor for the MD5 hashing.
Definition md5.h:37
Container for Accounts, Notifications.
Definition neomutt.h:41
List of regular expressions.
Definition regex3.h:95
struct Regex * regex
Regex containing a regular expression.
Definition regex3.h:96
char * pattern
printable version
Definition regex3.h:86
List of regular expressions.
Definition regex3.h:105
char * templ
Template to match.
Definition regex3.h:108
struct Regex * regex
Regex containing a regular expression.
Definition regex3.h:106
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hcache_open()

struct HeaderCache * hcache_open ( const char * path,
const char * folder,
hcache_namer_t namer,
bool create )

Multiplexor for StoreOps::open.

Open the connection to the header cache.

Definition at line 479 of file hcache.c.

481{
482 if (!path || (path[0] == '\0'))
483 return NULL;
484
485 if (HcacheVer == 0x0)
487
488 struct HeaderCache *hc = hcache_new();
489
491 hc->crc = HcacheVer;
492
493 const char *const c_header_cache_backend = cs_subset_string(NeoMutt->sub, "header_cache_backend");
494 hc->store_ops = store_get_backend_ops(c_header_cache_backend);
495 if (!hc->store_ops)
496 {
497 hcache_free(&hc);
498 return NULL;
499 }
500
501#ifdef USE_HCACHE_COMPRESSION
502 const char *const c_header_cache_compress_method = cs_subset_string(NeoMutt->sub, "header_cache_compress_method");
503 if (c_header_cache_compress_method)
504 {
505 hc->compr_ops = compress_get_ops(c_header_cache_compress_method);
506
507 const short c_header_cache_compress_level = cs_subset_number(NeoMutt->sub, "header_cache_compress_level");
508 hc->compr_handle = hc->compr_ops->open(c_header_cache_compress_level);
509 if (!hc->compr_handle)
510 {
511 hcache_free(&hc);
512 return NULL;
513 }
514
515 /* remember the buffer of database backend */
516 mutt_debug(LL_DEBUG3, "Header cache will use %s compression\n",
517 hc->compr_ops->name);
518 }
519#endif
520
521 struct Buffer *hcpath = buf_pool_get();
522 hcache_per_folder(hc, hcpath, path, namer);
523
524 hc->store_handle = hc->store_ops->open(buf_string(hcpath), create);
525 if (!hc->store_handle)
526 {
527 /* remove a possibly incompatible version */
528 if (unlink(buf_string(hcpath)) == 0)
529 {
530 hc->store_handle = hc->store_ops->open(buf_string(hcpath), create);
531 }
532 }
533
534 if (!hc->store_handle)
535 {
536 if (hc->compr_ops)
537 {
538 hc->compr_ops->close(&hc->compr_handle);
539 }
540 hcache_free(&hc);
541 }
542
543 buf_pool_release(&hcpath);
544 return hc;
545}
const struct ComprOps * compress_get_ops(const char *compr)
Get the API functions for a compress backend.
Definition compress.c:78
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition helpers.c:291
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition helpers.c:143
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
static unsigned int HcacheVer
Header Cache version.
Definition hcache.c:61
static void hcache_free(struct HeaderCache **ptr)
Free a header cache.
Definition hcache.c:108
static struct HeaderCache * hcache_new(void)
Create a new header cache.
Definition hcache.c:123
static unsigned int generate_hcachever(void)
Calculate hcache version from dynamic configuration.
Definition hcache.c:437
static void hcache_per_folder(struct HeaderCache *hc, struct Buffer *hcpath, const char *path, hcache_namer_t namer)
Generate the hcache pathname.
Definition hcache.c:361
static char * get_foldername(const char *folder)
Where should the cache be stored?
Definition hcache.c:414
@ LL_DEBUG3
Log at debug level 3.
Definition logging2.h:47
const struct StoreOps * store_get_backend_ops(const char *str)
Get the API functions for an store backend.
Definition store.c:88
ComprHandle *(* open)(short level)
Definition lib.h:79
void(* close)(ComprHandle **ptr)
Definition lib.h:124
ComprHandle * compr_handle
Compression handle.
Definition lib.h:93
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
StoreHandle *(* open)(const char *path, bool create)
Definition lib.h:83
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hcache_close()

void hcache_close ( struct HeaderCache ** ptr)

Multiplexor for StoreOps::close.

Close the connection to the header cache.

Definition at line 550 of file hcache.c.

551{
552 if (!ptr || !*ptr)
553 return;
554
555 struct HeaderCache *hc = *ptr;
556
557#ifdef USE_HCACHE_COMPRESSION
558 if (hc->compr_ops)
559 hc->compr_ops->close(&hc->compr_handle);
560#endif
561
562 hc->store_ops->close(&hc->store_handle);
563
564 hcache_free(ptr);
565}
void(* close)(StoreHandle **ptr)
Definition lib.h:144
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hcache_fetch_email()

struct HCacheEntry hcache_fetch_email ( struct HeaderCache * hc,
const char * key,
size_t keylen,
uint32_t uidvalidity )

Multiplexor for StoreOps::fetch.

Fetch and validate a message's header from the cache.

Definition at line 570 of file hcache.c.

572{
573 struct HCacheEntry hce = { 0 };
574 if (!hc)
575 return hce;
576
577 size_t dlen = 0;
578 struct RealKey *rk = realkey(hc, key, keylen, true);
579 void *data = hc->store_ops->fetch(hc->store_handle, rk->key, rk->keylen, &dlen);
580 void *to_free = data;
581 if (!data)
582 {
583 goto end;
584 }
585
586 /* restore uidvalidity and crc */
587 size_t hlen = header_size();
588 if (hlen > dlen)
589 {
590 goto end;
591 }
592 int off = 0;
593 serial_restore_uint32_t(&hce.uidvalidity, data, &off);
594 serial_restore_int(&hce.crc, data, &off);
595 ASSERT((size_t) off == hlen);
596 if ((hce.crc != hc->crc) || ((uidvalidity != 0) && (uidvalidity != hce.uidvalidity)))
597 {
598 goto end;
599 }
600
601#ifdef USE_HCACHE_COMPRESSION
602 if (hc->compr_ops)
603 {
604 void *dblob = hc->compr_ops->decompress(hc->compr_handle,
605 (char *) data + hlen, dlen - hlen);
606 if (!dblob)
607 {
608 goto end;
609 }
610 data = (char *) dblob - hlen; /* restore skips uidvalidity and crc */
611 }
612#endif
613
614 hce.email = restore_email(data);
615
616end:
617 free_raw(hc, &to_free);
618 return hce;
619}
static struct Email * restore_email(const unsigned char *d)
Restore an Email from data retrieved from the cache.
Definition hcache.c:272
static void free_raw(struct HeaderCache *hc, void **data)
Multiplexor for StoreOps::free.
Definition hcache.c:428
static struct RealKey * realkey(struct HeaderCache *hc, const char *key, size_t keylen, bool compress)
Compute the real key used in the backend, taking into account the compression method.
Definition hcache.c:80
void *(* decompress)(ComprHandle *handle, const char *cbuf, size_t clen)
Definition lib.h:112
Wrapper for Email retrieved from the header cache.
Definition lib.h:100
uint32_t uidvalidity
IMAP-specific UIDVALIDITY.
Definition lib.h:101
struct Email * email
Retrieved email.
Definition lib.h:103
unsigned int crc
CRC of Email/Body/etc structs.
Definition lib.h:102
void *(* fetch)(StoreHandle *store, const char *key, size_t klen, size_t *vlen)
Definition lib.h:97
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hcache_fetch_raw_obj_full()

bool hcache_fetch_raw_obj_full ( struct HeaderCache * hc,
const char * key,
size_t keylen,
void * dst,
size_t dstlen )

Fetch a message's header from the cache into a destination object.

Parameters
[in]hcPointer to the struct HeaderCache structure got by hcache_open()
[in]keyMessage identification string
[in]keylenLength of the string pointed to by key
[out]dstPointer to the destination object
[in]dstlenSize of the destination object
Return values
trueSuccess, the data was found and the length matches
falseOtherwise

Definition at line 631 of file hcache.c.

633{
634 if (!hc)
635 return false;
636
637 bool rc = true;
638 size_t srclen = 0;
639
640 struct RealKey *rk = realkey(hc, key, keylen, false);
641 void *src = hc->store_ops->fetch(hc->store_handle, rk->key, rk->keylen, &srclen);
642
643 if (src && (srclen == dstlen))
644 {
645 memcpy(dst, src, dstlen);
646 }
647 else
648 {
649 rc = false;
650 }
651 free_raw(hc, &src);
652 return rc;
653}
+ Here is the call graph for this function:

◆ hcache_fetch_raw_str()

char * hcache_fetch_raw_str ( struct HeaderCache * hc,
const char * key,
size_t keylen )

Fetch a string from the cache.

Parameters
[in]hcPointer to the struct HeaderCache structure got by hcache_open()
[in]keyMessage identification string
[in]keylenLength of the string pointed to by key
Return values
ptrSuccess, the data if found
NULLOtherwise

Definition at line 663 of file hcache.c.

664{
665 if (!hc)
666 return NULL;
667
668 char *res = NULL;
669 size_t dlen = 0;
670
671 struct RealKey *rk = realkey(hc, key, keylen, false);
672 void *data = hc->store_ops->fetch(hc->store_handle, rk->key, rk->keylen, &dlen);
673 if (data)
674 {
675 res = mutt_strn_dup(data, dlen);
676 free_raw(hc, &data);
677 }
678 return res;
679}
char * mutt_strn_dup(const char *begin, size_t len)
Duplicate a sub-string.
Definition string.c:384
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hcache_store_email()

int hcache_store_email ( struct HeaderCache * hc,
const char * key,
size_t keylen,
struct Email * e,
uint32_t uidvalidity )

Multiplexor for StoreOps::store.

Store a Header along with a validity datum.

Definition at line 684 of file hcache.c.

686{
687 if (!hc)
688 return -1;
689
690 int dlen = 0;
691 char *data = dump_email(hc, e, &dlen, uidvalidity);
692
693#ifdef USE_HCACHE_COMPRESSION
694 if (hc->compr_ops)
695 {
696 /* We don't compress uidvalidity and the crc, so we can check them before
697 * decompressing on fetch(). */
698 size_t hlen = header_size();
699
700 /* data / dlen gets ptr to compressed data here */
701 size_t clen = dlen;
702 void *cdata = hc->compr_ops->compress(hc->compr_handle, data + hlen, dlen - hlen, &clen);
703 if (!cdata)
704 {
705 FREE(&data);
706 return -1;
707 }
708
709 char *whole = MUTT_MEM_MALLOC(hlen + clen, char);
710 memcpy(whole, data, hlen);
711 memcpy(whole + hlen, cdata, clen);
712
713 FREE(&data);
714
715 data = whole;
716 dlen = hlen + clen;
717 }
718#endif
719
720 struct RealKey *rk = realkey(hc, key, keylen, true);
721 int rc = hc->store_ops->store(hc->store_handle, rk->key, rk->keylen, data, dlen);
722
723 FREE(&data);
724
725 return rc;
726}
static void * dump_email(struct HeaderCache *hc, const struct Email *e, int *off, uint32_t uidvalidity)
Serialise an Email object.
Definition hcache.c:232
void *(* compress)(ComprHandle *handle, const char *data, size_t dlen, size_t *clen)
Definition lib.h:96
int(* store)(StoreHandle *store, const char *key, size_t klen, void *value, size_t vlen)
Definition lib.h:122
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hcache_store_raw()

int hcache_store_raw ( struct HeaderCache * hc,
const char * key,
size_t keylen,
void * data,
size_t dlen )

Store a key / data pair.

Parameters
hcPointer to the struct HeaderCache structure got by hcache_open()
keyMessage identification string
keylenLength of the string pointed to by key
dataPayload to associate with key
dlenLength of the buffer pointed to by the data parameter
Return values
0Success
numGeneric or backend-specific error code otherwise

Definition at line 738 of file hcache.c.

740{
741 if (!hc)
742 return -1;
743
744 struct RealKey *rk = realkey(hc, key, keylen, false);
745 int rc = hc->store_ops->store(hc->store_handle, rk->key, rk->keylen, data, dlen);
746
747 return rc;
748}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hcache_delete_email()

int hcache_delete_email ( struct HeaderCache * hc,
const char * key,
size_t keylen )

Multiplexor for StoreOps::delete_record.

Delete a key / data pair.

Definition at line 753 of file hcache.c.

754{
755 if (!hc)
756 return -1;
757
758 struct RealKey *rk = realkey(hc, key, keylen, true);
759
760 return hc->store_ops->delete_record(hc->store_handle, rk->key, rk->keylen);
761}
int(* delete_record)(StoreHandle *store, const char *key, size_t klen)
Definition lib.h:135
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hcache_delete_raw()

int hcache_delete_raw ( struct HeaderCache * hc,
const char * key,
size_t keylen )

Multiplexor for StoreOps::delete_record.

Delete a key / data pair.

Definition at line 766 of file hcache.c.

767{
768 if (!hc)
769 return -1;
770
771 struct RealKey *rk = realkey(hc, key, keylen, false);
772
773 return hc->store_ops->delete_record(hc->store_handle, rk->key, rk->keylen);
774}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ HcacheVer

unsigned int HcacheVer = 0x0
static

Header Cache version.

Definition at line 61 of file hcache.c.