From: Mehdi Abaakouk Date: Sat, 8 Dec 2018 17:59:35 +0000 (+0100) Subject: imap: move some functions Mailbox X-Git-Tag: 2019-10-25~443^2~3 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=13da1b4a7776696a5a9e1186cf062cea72fa124d;p=neomutt imap: move some functions Mailbox To increase readability some functions take Mailbox instead of ImapAccountData. * imap_read_headers() * imap_set_flags() * imap_cache_del() * imap_cache_clean() * imap_cache_put() * msg_cache_commit() --- diff --git a/imap/command.c b/imap/command.c index 6bbb6a262..0299cea7e 100644 --- a/imap/command.c +++ b/imap/command.c @@ -506,7 +506,7 @@ static void cmd_parse_fetch(struct ImapAccountData *adata, char *s) if (flags) { - imap_set_flags(adata, e, flags, &server_changes); + imap_set_flags(adata->mailbox, e, flags, &server_changes); if (server_changes) { /* If server flags could conflict with mutt's flags, reopen the mailbox. */ @@ -1324,7 +1324,7 @@ void imap_cmd_finish(struct ImapAccountData *adata) mutt_debug(2, "Fetching new mails from %d to %d\n", mdata->max_msn + 1, mdata->new_mail_count); - imap_read_headers(adata, mdata->max_msn + 1, mdata->new_mail_count, false); + imap_read_headers(adata->mailbox, mdata->max_msn + 1, mdata->new_mail_count, false); } // And to finish inform about MUTT_REOPEN if needed diff --git a/imap/imap.c b/imap/imap.c index 8c53eeb96..5e84664c4 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -801,7 +801,7 @@ void imap_expunge_mailbox(struct Mailbox *m) e->active = false; m->size -= e->content->length; - imap_cache_del(adata, e); + imap_cache_del(m, e); #ifdef USE_HCACHE imap_hcache_del(mdata, imap_edata_get(e)->uid); #endif @@ -1694,7 +1694,7 @@ int imap_sync_mailbox(struct Context *ctx, bool expunge, bool close) if (e->deleted) { - imap_cache_del(adata, e); + imap_cache_del(m, e); #ifdef USE_HCACHE imap_hcache_del(mdata, imap_edata_get(e)->uid); #endif @@ -1812,7 +1812,7 @@ int imap_sync_mailbox(struct Context *ctx, bool expunge, bool close) } if (MessageCacheClean) - imap_cache_clean(adata); + imap_cache_clean(m); return 0; } @@ -2162,7 +2162,7 @@ static int imap_mbox_open(struct Mailbox *m, struct Context *ctx) m->size = 0; m->vcount = 0; - if (count && (imap_read_headers(adata, 1, count, true) < 0)) + if (count && (imap_read_headers(m, 1, count, true) < 0)) { mutt_error(_("Error opening mailbox")); goto fail; diff --git a/imap/imap_private.h b/imap/imap_private.h index 9efc471d9..781bcfc44 100644 --- a/imap/imap_private.h +++ b/imap/imap_private.h @@ -301,10 +301,10 @@ int imap_cmd_idle(struct ImapAccountData *adata); /* message.c */ void imap_edata_free(void **ptr); struct ImapEmailData *imap_edata_get(struct Email *e); -int imap_read_headers(struct ImapAccountData *adata, unsigned int msn_begin, unsigned int msn_end, bool initial_download); -char *imap_set_flags(struct ImapAccountData *adata, struct Email *e, char *s, int *server_changes); -int imap_cache_del(struct ImapAccountData *adata, struct Email *e); -int imap_cache_clean(struct ImapAccountData *adata); +int imap_read_headers(struct Mailbox *m, unsigned int msn_begin, unsigned int msn_end, bool initial_download); +char *imap_set_flags(struct Mailbox *m, struct Email *e, char *s, int *server_changes); +int imap_cache_del(struct Mailbox *m, struct Email *e); +int imap_cache_clean(struct Mailbox *m); int imap_append_message(struct Mailbox *m, struct Message *msg); int imap_msg_open(struct Mailbox *m, struct Message *msg, int msgno); diff --git a/imap/message.c b/imap/message.c index 95429577a..d24e782c6 100644 --- a/imap/message.c +++ b/imap/message.c @@ -106,14 +106,19 @@ struct ImapEmailData *imap_edata_get(struct Email *e) /** * msg_cache_open - Open a message cache - * @param adata Imap Account data + * @param m Selected Imap Mailbox * @retval ptr Success, using existing cache * @retval ptr Success, opened new cache * @retval NULL Failure */ -static struct BodyCache *msg_cache_open(struct ImapAccountData *adata) +static struct BodyCache *msg_cache_open(struct Mailbox *m) { - struct ImapMboxData *mdata = adata->mailbox->mdata; + struct ImapAccountData *adata = imap_adata_get(m); + struct ImapMboxData *mdata = imap_mdata_get(m); + + if (!adata || adata->mailbox != m) + return NULL; + char mailbox[PATH_MAX]; if (mdata->bcache) @@ -126,19 +131,20 @@ static struct BodyCache *msg_cache_open(struct ImapAccountData *adata) /** * msg_cache_get - Get the message cache entry for an email - * @param adata Imap Account data + * @param m Selected Imap Mailbox * @param e Email * @retval ptr Success, handle of cache entry * @retval NULL Failure */ -static FILE *msg_cache_get(struct ImapAccountData *adata, struct Email *e) +static FILE *msg_cache_get(struct Mailbox *m, struct Email *e) { - if (!adata || !e) - return NULL; + struct ImapAccountData *adata = imap_adata_get(m); + struct ImapMboxData *mdata = imap_mdata_get(m); - struct ImapMboxData *mdata = adata->mailbox->mdata; + if (!e || !adata || adata->mailbox != m) + return NULL; - mdata->bcache = msg_cache_open(adata); + mdata->bcache = msg_cache_open(m); char id[64]; snprintf(id, sizeof(id), "%u-%u", mdata->uid_validity, imap_edata_get(e)->uid); return mutt_bcache_get(mdata->bcache, id); @@ -146,19 +152,20 @@ static FILE *msg_cache_get(struct ImapAccountData *adata, struct Email *e) /** * msg_cache_put - Put an email into the message cache - * @param adata Imap Account data + * @param m Selected Imap Mailbox * @param e Email * @retval ptr Success, handle of cache entry * @retval NULL Failure */ -static FILE *msg_cache_put(struct ImapAccountData *adata, struct Email *e) +static FILE *msg_cache_put(struct Mailbox *m, struct Email *e) { - if (!adata || !e) - return NULL; + struct ImapAccountData *adata = imap_adata_get(m); + struct ImapMboxData *mdata = imap_mdata_get(m); - struct ImapMboxData *mdata = adata->mailbox->mdata; + if (!e || !adata || adata->mailbox != m) + return NULL; - mdata->bcache = msg_cache_open(adata); + mdata->bcache = msg_cache_open(m); char id[64]; snprintf(id, sizeof(id), "%u-%u", mdata->uid_validity, imap_edata_get(e)->uid); return mutt_bcache_put(mdata->bcache, id); @@ -166,19 +173,20 @@ static FILE *msg_cache_put(struct ImapAccountData *adata, struct Email *e) /** * msg_cache_commit - Add to the message cache - * @param adata Imap Account data + * @param m Selected Imap Mailbox * @param e Email * @retval 0 Success * @retval -1 Failure */ -static int msg_cache_commit(struct ImapAccountData *adata, struct Email *e) +static int msg_cache_commit(struct Mailbox *m, struct Email *e) { - if (!adata || !e) - return -1; + struct ImapAccountData *adata = imap_adata_get(m); + struct ImapMboxData *mdata = imap_mdata_get(m); - struct ImapMboxData *mdata = adata->mailbox->mdata; + if (!e || !adata || adata->mailbox != m) + return -1; - mdata->bcache = msg_cache_open(adata); + mdata->bcache = msg_cache_open(m); char id[64]; snprintf(id, sizeof(id), "%u-%u", mdata->uid_validity, imap_edata_get(e)->uid); @@ -192,8 +200,7 @@ static int msg_cache_commit(struct ImapAccountData *adata, struct Email *e) static int msg_cache_clean_cb(const char *id, struct BodyCache *bcache, void *data) { unsigned int uv, uid; - struct ImapAccountData *adata = data; - struct ImapMboxData *mdata = adata->mailbox->mdata; + struct ImapMboxData *mdata = data; if (sscanf(id, "%u-%u", &uv, &uid) != 2) return 0; @@ -988,7 +995,7 @@ static int read_headers_condstore_qresync_updates(struct ImapAccountData *adata, /** * read_headers_fetch_new - Retrieve new messages from the server - * @param[in] adata Imap Account data + * @param[in] m Imap Selected Mailbox * @param[in] msn_begin First Message Sequence number * @param[in] msn_end Last Message Sequence number * @param[in] evalhc if true, check the Header Cache @@ -997,7 +1004,7 @@ static int read_headers_condstore_qresync_updates(struct ImapAccountData *adata, * @retval 0 Success * @retval -1 Error */ -static int read_headers_fetch_new(struct ImapAccountData *adata, unsigned int msn_begin, +static int read_headers_fetch_new(struct Mailbox *m, unsigned int msn_begin, unsigned int msn_end, bool evalhc, unsigned int *maxuid, bool initial_download) { @@ -1013,10 +1020,13 @@ static int read_headers_fetch_new(struct ImapAccountData *adata, unsigned int ms "CONTENT-DESCRIPTION IN-REPLY-TO REPLY-TO LINES LIST-POST X-LABEL " "X-ORIGINAL-TO"; - struct Mailbox *m = adata->mailbox; - struct ImapMboxData *mdata = adata->mailbox->mdata; + struct ImapAccountData *adata = imap_adata_get(m); + struct ImapMboxData *mdata = imap_mdata_get(m); int idx = m->msg_count; + if (!adata || adata->mailbox != m) + return -1; + if (mutt_bit_isset(adata->capabilities, IMAP_CAP_IMAP4REV1)) { safe_asprintf(&hdrreq, "BODY.PEEK[HEADER.FIELDS (%s%s%s)]", want_headers, @@ -1200,7 +1210,7 @@ bail: /** * imap_read_headers - Read headers from the server - * @param adata Imap Account data + * @param m Imap Selected Mailbox * @param msn_begin First Message Sequence Number * @param msn_end Last Message Sequence Number * @param initial_download true, if this is the first opening of the mailbox @@ -1211,7 +1221,7 @@ bail: * the last message read. It will return a value other than msn_end if mail * comes in while downloading headers (in theory). */ -int imap_read_headers(struct ImapAccountData *adata, unsigned int msn_begin, +int imap_read_headers(struct Mailbox *m, unsigned int msn_begin, unsigned int msn_end, bool initial_download) { int oldmsgcount; @@ -1232,8 +1242,10 @@ int imap_read_headers(struct ImapAccountData *adata, unsigned int msn_begin, char *uid_seqset = NULL; #endif /* USE_HCACHE */ - struct Mailbox *m = adata->mailbox; + struct ImapAccountData *adata = imap_adata_get(m); struct ImapMboxData *mdata = imap_mdata_get(m); + if (!adata || adata->mailbox != m) + return -1; /* make sure context has room to hold the mailbox */ while (msn_end > m->hdrmax) @@ -1328,7 +1340,7 @@ int imap_read_headers(struct ImapAccountData *adata, unsigned int msn_begin, } #endif /* USE_HCACHE */ - if (read_headers_fetch_new(adata, msn_begin, msn_end, evalhc, &maxuid, initial_download) < 0) + if (read_headers_fetch_new(m, msn_begin, msn_end, evalhc, &maxuid, initial_download) < 0) goto bail; if (maxuid && mdata->uid_next < maxuid + 1) @@ -1712,18 +1724,20 @@ out: /** * imap_cache_del - Delete an email from the body cache - * @param adata Imap Account data + * @param m Selected Imap Mailb * @param e Email * @retval 0 Success * @retval -1 Failure */ -int imap_cache_del(struct ImapAccountData *adata, struct Email *e) +int imap_cache_del(struct Mailbox *m, struct Email *e) { - if (!adata || !e) + struct ImapAccountData *adata = imap_adata_get(m); + struct ImapMboxData *mdata = imap_mdata_get(m); + + if (!e || !adata || adata->mailbox != m) return -1; - struct ImapMboxData *mdata = adata->mailbox->mdata; - mdata->bcache = msg_cache_open(adata); + mdata->bcache = msg_cache_open(m); char id[64]; snprintf(id, sizeof(id), "%u-%u", mdata->uid_validity, imap_edata_get(e)->uid); return mutt_bcache_del(mdata->bcache, id); @@ -1731,21 +1745,26 @@ int imap_cache_del(struct ImapAccountData *adata, struct Email *e) /** * imap_cache_clean - Delete all the entries in the message cache - * @param adata Imap Account data + * @param m SelectedImap Mailbox * @retval 0 Always */ -int imap_cache_clean(struct ImapAccountData *adata) +int imap_cache_clean(struct Mailbox *m) { - struct ImapMboxData *mdata = adata->mailbox->mdata; - mdata->bcache = msg_cache_open(adata); - mutt_bcache_list(mdata->bcache, msg_cache_clean_cb, adata); + struct ImapAccountData *adata = imap_adata_get(m); + struct ImapMboxData *mdata = imap_mdata_get(m); + + if (!adata || adata->mailbox != m) + return -1; + + mdata->bcache = msg_cache_open(m); + mutt_bcache_list(mdata->bcache, msg_cache_clean_cb, mdata); return 0; } /** * imap_set_flags - fill the message header according to the server flags - * @param[in] adata Imap Account data + * @param[in] m Imap Selected Mailbox * @param[in] e Email * @param[in] s Command string * @param[out] server_changes Flags have changed @@ -1761,9 +1780,12 @@ int imap_cache_clean(struct ImapAccountData *adata) * case of e->changed, if a change to a flag _would_ have been * made. */ -char *imap_set_flags(struct ImapAccountData *adata, struct Email *e, char *s, int *server_changes) +char *imap_set_flags(struct Mailbox *m, struct Email *e, char *s, int *server_changes) { - struct Mailbox *m = adata->mailbox; + struct ImapAccountData *adata = imap_adata_get(m); + if (!adata || adata->mailbox != m) + return NULL; + struct ImapHeader newh = { 0 }; struct ImapEmailData old_edata; bool readonly; @@ -1846,9 +1868,13 @@ int imap_msg_open(struct Mailbox *m, struct Message *msg, int msgno) struct ImapAccountData *adata = imap_adata_get(m); struct ImapMboxData *mdata = imap_mdata_get(m); + + if (!adata || adata->mailbox != m) + return -1; + struct Email *e = m->hdrs[msgno]; - msg->fp = msg_cache_get(adata, e); + msg->fp = msg_cache_get(m, e); if (msg->fp) { if (imap_edata_get(e)->parsed) @@ -1880,7 +1906,7 @@ int imap_msg_open(struct Mailbox *m, struct Message *msg, int msgno) if (output_progress) mutt_message(_("Fetching message...")); - msg->fp = msg_cache_put(adata, e); + msg->fp = msg_cache_put(m, e); if (!msg->fp) { cache->uid = imap_edata_get(e)->uid; @@ -1966,7 +1992,7 @@ int imap_msg_open(struct Mailbox *m, struct Message *msg, int msgno) * incrementally update flags later, this won't stop us syncing */ else if (mutt_str_startswith(pc, "FLAGS", CASE_IGNORE) && !e->changed) { - pc = imap_set_flags(adata, e, pc, NULL); + pc = imap_set_flags(m, e, pc, NULL); if (!pc) goto bail; } @@ -1990,7 +2016,7 @@ int imap_msg_open(struct Mailbox *m, struct Message *msg, int msgno) if (!fetched || !imap_code(adata->buf)) goto bail; - msg_cache_commit(adata, e); + msg_cache_commit(m, e); parsemsg: /* Update the header information. Previously, we only downloaded a @@ -2031,7 +2057,7 @@ parsemsg: /* retry message parse if cached message is empty */ if (!retried && ((e->lines == 0) || (e->content->length == 0))) { - imap_cache_del(adata, e); + imap_cache_del(m, e); retried = true; goto parsemsg; } @@ -2040,7 +2066,7 @@ parsemsg: bail: mutt_file_fclose(&msg->fp); - imap_cache_del(adata, e); + imap_cache_del(m, e); if (cache->path) { unlink(cache->path);