From: Kevin McCarthy Date: Sat, 15 Dec 2018 22:49:55 +0000 (-0800) Subject: Add mx operation save_to_header_cache X-Git-Tag: 2019-10-25~396^2~32 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7f29cdc9c09d3f805dda1c0d01b533055550f850;p=neomutt Add mx operation save_to_header_cache This will be used when reading protected headers, to store the encrypted subject in the header cache so it can be searched with. Co-authored-by: Richard Russon --- diff --git a/compress.c b/compress.c index 861884631..8d5e2e66c 100644 --- a/compress.c +++ b/compress.c @@ -843,6 +843,23 @@ static int comp_msg_padding_size(struct Mailbox *m) return ops->msg_padding_size(m); } +/** + * comp_msg_save_hcache - Save message to the header cache - Implements MxOps::msg_save_hcache() + */ +static int comp_msg_save_hcache(struct Mailbox *m, struct Email *e) +{ + if (!m || !m->compress_info) + return 0; + + struct CompressInfo *ci = m->compress_info; + + const struct MxOps *ops = ci->child_ops; + if (!ops || !ops->msg_save_hcache) + return 0; + + return ops->msg_save_hcache(m, e); +} + /** * comp_tags_edit - Implements MxOps::tags_edit() */ @@ -965,6 +982,7 @@ struct MxOps MxCompOps = { .msg_commit = comp_msg_commit, .msg_close = comp_msg_close, .msg_padding_size = comp_msg_padding_size, + .msg_save_hcache = comp_msg_save_hcache, .tags_edit = comp_tags_edit, .tags_commit = comp_tags_commit, .path_probe = comp_path_probe, diff --git a/imap/imap.c b/imap/imap.c index db2f4d775..e18324c8b 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -2527,6 +2527,7 @@ struct MxOps MxImapOps = { .msg_commit = imap_msg_commit, .msg_close = imap_msg_close, .msg_padding_size = NULL, + .msg_save_hcache = imap_msg_save_hcache, .tags_edit = imap_tags_edit, .tags_commit = imap_tags_commit, .path_probe = imap_path_probe, diff --git a/imap/imap_private.h b/imap/imap_private.h index d64fa4524..b511fadcb 100644 --- a/imap/imap_private.h +++ b/imap/imap_private.h @@ -299,6 +299,7 @@ int imap_append_message(struct Mailbox *m, struct Message *msg); int imap_msg_open(struct Mailbox *m, struct Message *msg, int msgno); int imap_msg_close(struct Mailbox *m, struct Message *msg); int imap_msg_commit(struct Mailbox *m, struct Message *msg); +int imap_msg_save_hcache(struct Mailbox *m, struct Email *e); /* util.c */ struct ImapAccountData *imap_adata_get(struct Mailbox *m); diff --git a/imap/message.c b/imap/message.c index 03dac63af..f615e261b 100644 --- a/imap/message.c +++ b/imap/message.c @@ -2057,3 +2057,24 @@ int imap_msg_close(struct Mailbox *m, struct Message *msg) { return mutt_file_fclose(&msg->fp); } + +/** + * imap_msg_save_hcache - Save message to the header cache - Implements MxOps::msg_save_hcache() + */ +int imap_msg_save_hcache(struct Mailbox *m, struct Email *e) +{ + int rc = 0; +#ifdef USE_HCACHE + bool close_hc = true; + struct ImapAccountData *adata = imap_adata_get(m); + struct ImapMboxData *mdata = imap_mdata_get(m); + if (mdata->hcache) + close_hc = false; + else + mdata->hcache = imap_hcache_open(adata, NULL); + rc = imap_hcache_put(mdata, e); + if (close_hc) + imap_hcache_close(mdata); +#endif + return rc; +} diff --git a/maildir/maildir.c b/maildir/maildir.c index 49823acb1..6532eb73d 100644 --- a/maildir/maildir.c +++ b/maildir/maildir.c @@ -655,6 +655,22 @@ static int maildir_msg_commit(struct Mailbox *m, struct Message *msg) return md_commit_message(m, msg, NULL); } +/** + * maildir_msg_save_hcache - Save message to the header cache - Implements MxOps::msg_save_hcache() + */ +static int maildir_msg_save_hcache(struct Mailbox *m, struct Email *e) +{ + int rc = 0; +#ifdef USE_HCACHE + header_cache_t *hc = mutt_hcache_open(HeaderCache, m->path, NULL); + char *key = e->path + 3; + int keylen = maildir_hcache_keylen(key); + rc = mutt_hcache_store(hc, key, keylen, e, 0); + mutt_hcache_close(hc); +#endif + return rc; +} + /** * maildir_path_probe - Is this a Maildir mailbox? - Implements MxOps::path_probe() */ @@ -696,6 +712,7 @@ struct MxOps MxMaildirOps = { .msg_commit = maildir_msg_commit, .msg_close = mh_msg_close, .msg_padding_size = NULL, + .msg_save_hcache = maildir_msg_save_hcache, .tags_edit = NULL, .tags_commit = NULL, .path_probe = maildir_path_probe, diff --git a/maildir/maildir_private.h b/maildir/maildir_private.h index 5efcb9ec9..dd75183b9 100644 --- a/maildir/maildir_private.h +++ b/maildir/maildir_private.h @@ -82,10 +82,12 @@ int mh_mbox_check (struct Mailbox *m, int *index_hint); int mh_mbox_close (struct Mailbox *m); int mh_mbox_sync (struct Mailbox *m, int *index_hint); int mh_msg_close (struct Mailbox *m, struct Message *msg); +int mh_msg_save_hcache (struct Mailbox *m, struct Email *e); /* Maildir/MH shared functions */ void maildir_canon_filename (struct Buffer *dest, const char *src); void maildir_delayed_parsing(struct Mailbox *m, struct Maildir **md, struct Progress *progress); +size_t maildir_hcache_keylen (const char *fn); struct MaildirMboxData *maildir_mdata_get (struct Mailbox *m); int maildir_mh_open_message(struct Mailbox *m, struct Message *msg, int msgno, bool is_maildir); int maildir_move_to_context(struct Mailbox *m, struct Maildir **md); diff --git a/maildir/mh.c b/maildir/mh.c index 0ba59f970..2f4a6bf75 100644 --- a/maildir/mh.c +++ b/maildir/mh.c @@ -815,6 +815,7 @@ struct MxOps MxMhOps = { .msg_commit = mh_msg_commit, .msg_close = mh_msg_close, .msg_padding_size = NULL, + .msg_save_hcache = mh_msg_save_hcache, .tags_edit = NULL, .tags_commit = NULL, .path_probe = mh_path_probe, diff --git a/maildir/shared.c b/maildir/shared.c index 7d53ada04..0a0b63779 100644 --- a/maildir/shared.c +++ b/maildir/shared.c @@ -488,7 +488,7 @@ int maildir_move_to_context(struct Mailbox *m, struct Maildir **md) * * @note This length excludes the flags, which will vary */ -static size_t maildir_hcache_keylen(const char *fn) +size_t maildir_hcache_keylen(const char *fn) { const char *p = strrchr(fn, ':'); return p ? (size_t)(p - fn) : mutt_str_strlen(fn); @@ -1853,3 +1853,17 @@ int mh_msg_close(struct Mailbox *m, struct Message *msg) { return mutt_file_fclose(&msg->fp); } + +/** + * mh_msg_save_hcache - Save message to the header cache - Implements MxOps::msg_save_hcache() + */ +int mh_msg_save_hcache(struct Mailbox *m, struct Email *e) +{ + int rc = 0; +#ifdef USE_HCACHE + header_cache_t *hc = mutt_hcache_open(HeaderCache, m->path, NULL); + rc = mutt_hcache_store(hc, e->path, strlen(e->path), e, 0); + mutt_hcache_close(hc); +#endif + return rc; +} diff --git a/mbox/mbox.c b/mbox/mbox.c index 028f13235..013b7ed6a 100644 --- a/mbox/mbox.c +++ b/mbox/mbox.c @@ -1835,6 +1835,7 @@ struct MxOps MxMboxOps = { .msg_commit = mbox_msg_commit, .msg_close = mbox_msg_close, .msg_padding_size = mbox_msg_padding_size, + .msg_save_hcache = NULL, .tags_edit = NULL, .tags_commit = NULL, .path_probe = mbox_path_probe, @@ -1862,6 +1863,7 @@ struct MxOps MxMmdfOps = { .msg_commit = mmdf_msg_commit, .msg_close = mbox_msg_close, .msg_padding_size = mmdf_msg_padding_size, + .msg_save_hcache = NULL, .tags_edit = NULL, .tags_commit = NULL, .path_probe = mbox_path_probe, diff --git a/mx.c b/mx.c index 2afe4ef50..6331f98e3 100644 --- a/mx.c +++ b/mx.c @@ -1570,3 +1570,20 @@ int mx_mbox_check_stats(struct Mailbox *m, int flags) return m->mx_ops->mbox_check_stats(m, flags); } + +/** + * mx_save_to_header_cache - Save message to the header cache - Wrapper for MxOps::msg_save_hcache() + * @param m Mailbox + * @param e Email + * @retval 0 Success + * @retval -1 Failure + * + * Write a single header out to the header cache. + */ +int mx_save_hcache(struct Mailbox *m, struct Email *e) +{ + if (!m->mx_ops || !m->mx_ops->msg_save_hcache) + return 0; + + return m->mx_ops->msg_save_hcache(m, e); +} diff --git a/mx.h b/mx.h index 0ec265b4d..89ee3feac 100644 --- a/mx.h +++ b/mx.h @@ -205,6 +205,14 @@ struct MxOps * @retval num Bytes of padding */ int (*msg_padding_size)(struct Mailbox *m); + /** + * msg_save_hcache - Save message to the header cache + * @param m Mailbox + * @param e Email + * @retval 0 Success + * @retval -1 Failure + */ + int (*msg_save_hcache) (struct Mailbox *m, struct Email *e); /** * tags_edit - Prompt and validate new messages tags * @param m Mailbox @@ -270,6 +278,7 @@ int mx_msg_commit (struct Mailbox *m, struct Message *msg); struct Message *mx_msg_open_new (struct Mailbox *m, struct Email *e, int flags); struct Message *mx_msg_open (struct Mailbox *m, int msgno); int mx_msg_padding_size(struct Mailbox *m); +int mx_save_hcache (struct Mailbox *m, struct Email *e); int mx_path_canon (char *buf, size_t buflen, const char *folder, int *magic); int mx_path_canon2 (struct Mailbox *m, const char *folder); int mx_path_parent (char *buf, size_t buflen); diff --git a/nntp/nntp.c b/nntp/nntp.c index 54239017b..0f8597da1 100644 --- a/nntp/nntp.c +++ b/nntp/nntp.c @@ -2894,6 +2894,7 @@ struct MxOps MxNntpOps = { .msg_commit = NULL, .msg_close = nntp_msg_close, .msg_padding_size = NULL, + .msg_save_hcache = NULL, .tags_edit = NULL, .tags_commit = NULL, .path_probe = nntp_path_probe, diff --git a/notmuch/mutt_notmuch.c b/notmuch/mutt_notmuch.c index 7a5778032..6e204e533 100644 --- a/notmuch/mutt_notmuch.c +++ b/notmuch/mutt_notmuch.c @@ -2552,6 +2552,7 @@ struct MxOps MxNotmuchOps = { .msg_commit = nm_msg_commit, .msg_close = nm_msg_close, .msg_padding_size = NULL, + .msg_save_hcache = NULL, .tags_edit = nm_tags_edit, .tags_commit = nm_tags_commit, .path_probe = nm_path_probe, diff --git a/pop/pop.c b/pop/pop.c index 4cfd6231a..d0289225e 100644 --- a/pop/pop.c +++ b/pop/pop.c @@ -1211,6 +1211,23 @@ static int pop_msg_close(struct Mailbox *m, struct Message *msg) return mutt_file_fclose(&msg->fp); } +/** + * pop_msg_save_hcache - Save message to the header cache - Implements MxOps::msg_save_hcache() + */ +static int pop_msg_save_hcache(struct Mailbox *m, struct Email *e) +{ + int rc = 0; +#ifdef USE_HCACHE + struct PopAccountData *adata = pop_get_adata(m); + struct PopEmailData *edata = e->edata; + header_cache_t *hc = pop_hcache_open(adata, m->path); + rc = mutt_hcache_store(hc, edata->uid, strlen(edata->uid), e, 0); + mutt_hcache_close(hc); +#endif + + return rc; +} + /** * pop_path_probe - Is this a POP mailbox? - Implements MxOps::path_probe() */ @@ -1276,6 +1293,7 @@ struct MxOps MxPopOps = { .msg_commit = NULL, .msg_close = pop_msg_close, .msg_padding_size = NULL, + .msg_save_hcache = pop_msg_save_hcache, .tags_edit = NULL, .tags_commit = NULL, .path_probe = pop_path_probe,