NeoMutt
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
msg_open()

Open an email message in a Mailbox. More...

+ Collaboration diagram for msg_open():

Functions

static bool comp_msg_open (struct Mailbox *m, struct Message *msg, struct Email *e)
 Open an email message in a Mailbox - Implements MxOps::msg_open() -.
 
bool imap_msg_open (struct Mailbox *m, struct Message *msg, struct Email *e)
 Open an email message in a Mailbox - Implements MxOps::msg_open() -.
 
static bool maildir_msg_open (struct Mailbox *m, struct Message *msg, struct Email *e)
 Open an email message in a Mailbox - Implements MxOps::msg_open() -.
 
static bool mh_msg_open (struct Mailbox *m, struct Message *msg, struct Email *e)
 Open an email message in a Mailbox - Implements MxOps::msg_open() -.
 
static bool mbox_msg_open (struct Mailbox *m, struct Message *msg, struct Email *e)
 Open an email message in a Mailbox - Implements MxOps::msg_open() -.
 
static bool nntp_msg_open (struct Mailbox *m, struct Message *msg, struct Email *e)
 Open an email message in a Mailbox - Implements MxOps::msg_open() -.
 
static bool nm_msg_open (struct Mailbox *m, struct Message *msg, struct Email *e)
 Open an email message in a Mailbox - Implements MxOps::msg_open() -.
 
static bool pop_msg_open (struct Mailbox *m, struct Message *msg, struct Email *e)
 Open an email message in a Mailbox - Implements MxOps::msg_open() -.
 

Detailed Description

Open an email message in a Mailbox.

Parameters
mMailbox
msgMessage to open
eEmail to open
Return values
trueSuccess
falseError
Precondition
m is not NULL
msg is not NULL
e is not NULL

Function Documentation

◆ comp_msg_open()

static bool comp_msg_open ( struct Mailbox m,
struct Message msg,
struct Email e 
)
static

Open an email message in a Mailbox - Implements MxOps::msg_open() -.

Definition at line 755 of file compress.c.

756{
757 if (!m->compress_info)
758 return false;
759
760 struct CompressInfo *ci = m->compress_info;
761
762 const struct MxOps *ops = ci->child_ops;
763 if (!ops)
764 return false;
765
766 /* Delegate */
767 return ops->msg_open(m, msg, e);
768}
Private data for compress.
Definition: lib.h:47
const struct MxOps * child_ops
callbacks of de-compressed file
Definition: lib.h:52
void * compress_info
Compressed mbox module private data.
Definition: mailbox.h:121
Definition: mxapi.h:91
bool(* msg_open)(struct Mailbox *m, struct Message *msg, struct Email *e)
Definition: mxapi.h:216

◆ imap_msg_open()

bool imap_msg_open ( struct Mailbox m,
struct Message msg,
struct Email e 
)

Open an email message in a Mailbox - Implements MxOps::msg_open() -.

Definition at line 1956 of file message.c.

1957{
1958 struct Envelope *newenv = NULL;
1959 char buf[1024] = { 0 };
1960 char *pc = NULL;
1961 unsigned int bytes;
1962 unsigned int uid;
1963 bool retried = false;
1964 bool read;
1965 int rc;
1966
1967 /* Sam's weird courier server returns an OK response even when FETCH
1968 * fails. Thanks Sam. */
1969 bool fetched = false;
1970
1972
1973 if (!adata || (adata->mailbox != m))
1974 return false;
1975
1976 msg->fp = msg_cache_get(m, e);
1977 if (msg->fp)
1978 {
1979 if (imap_edata_get(e)->parsed)
1980 return true;
1981 goto parsemsg;
1982 }
1983
1984 /* This function is called in a few places after endwin()
1985 * e.g. mutt_pipe_message(). */
1986 bool output_progress = !isendwin() && m->verbose;
1987 if (output_progress)
1988 mutt_message(_("Fetching message..."));
1989
1990 msg->fp = msg_cache_put(m, e);
1991 if (!msg->fp)
1992 {
1993 struct Buffer *path = buf_pool_get();
1994 buf_mktemp(path);
1995 msg->fp = mutt_file_fopen(buf_string(path), "w+");
1996 unlink(buf_string(path));
1997 buf_pool_release(&path);
1998
1999 if (!msg->fp)
2000 return false;
2001 }
2002
2003 /* mark this header as currently inactive so the command handler won't
2004 * also try to update it. HACK until all this code can be moved into the
2005 * command handler */
2006 e->active = false;
2007
2008 const bool c_imap_peek = cs_subset_bool(NeoMutt->sub, "imap_peek");
2009 snprintf(buf, sizeof(buf), "UID FETCH %u %s", imap_edata_get(e)->uid,
2010 ((adata->capabilities & IMAP_CAP_IMAP4REV1) ?
2011 (c_imap_peek ? "BODY.PEEK[]" : "BODY[]") :
2012 "RFC822"));
2013
2014 imap_cmd_start(adata, buf);
2015 do
2016 {
2017 rc = imap_cmd_step(adata);
2018 if (rc != IMAP_RES_CONTINUE)
2019 break;
2020
2021 pc = adata->buf;
2022 pc = imap_next_word(pc);
2023 pc = imap_next_word(pc);
2024
2025 if (mutt_istr_startswith(pc, "FETCH"))
2026 {
2027 while (*pc)
2028 {
2029 pc = imap_next_word(pc);
2030 if (pc[0] == '(')
2031 pc++;
2032 if (mutt_istr_startswith(pc, "UID"))
2033 {
2034 pc = imap_next_word(pc);
2035 if (!mutt_str_atoui(pc, &uid))
2036 goto bail;
2037 if (uid != imap_edata_get(e)->uid)
2038 {
2039 mutt_error(_("The message index is incorrect. Try reopening the mailbox."));
2040 }
2041 }
2042 else if (mutt_istr_startswith(pc, "RFC822") || mutt_istr_startswith(pc, "BODY[]"))
2043 {
2044 pc = imap_next_word(pc);
2045 if (imap_get_literal_count(pc, &bytes) < 0)
2046 {
2047 imap_error("imap_msg_open()", buf);
2048 goto bail;
2049 }
2050
2051 const int res = imap_read_literal(msg->fp, adata, bytes, NULL);
2052 if (res < 0)
2053 {
2054 goto bail;
2055 }
2056 /* pick up trailing line */
2057 rc = imap_cmd_step(adata);
2058 if (rc != IMAP_RES_CONTINUE)
2059 goto bail;
2060 pc = adata->buf;
2061
2062 fetched = true;
2063 }
2064 else if (!e->changed && mutt_istr_startswith(pc, "FLAGS"))
2065 {
2066 /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
2067 * change (eg from \Unseen to \Seen).
2068 * Uncommitted changes in neomutt take precedence. If we decide to
2069 * incrementally update flags later, this won't stop us syncing */
2070 pc = imap_set_flags(m, e, pc, NULL);
2071 if (!pc)
2072 goto bail;
2073 }
2074 }
2075 }
2076 } while (rc == IMAP_RES_CONTINUE);
2077
2078 /* see comment before command start. */
2079 e->active = true;
2080
2081 fflush(msg->fp);
2082 if (ferror(msg->fp))
2083 goto bail;
2084
2085 if (rc != IMAP_RES_OK)
2086 goto bail;
2087
2088 if (!fetched || !imap_code(adata->buf))
2089 goto bail;
2090
2091 if (msg_cache_commit(m, e) < 0)
2092 mutt_debug(LL_DEBUG1, "failed to add message to cache\n");
2093
2094parsemsg:
2095 /* Update the header information. Previously, we only downloaded a
2096 * portion of the headers, those required for the main display. */
2097 rewind(msg->fp);
2098 /* It may be that the Status header indicates a message is read, but the
2099 * IMAP server doesn't know the message has been \Seen. So we capture
2100 * the server's notion of 'read' and if it differs from the message info
2101 * picked up in mutt_rfc822_read_header, we mark the message (and context
2102 * changed). Another possibility: ignore Status on IMAP? */
2103 read = e->read;
2104 newenv = mutt_rfc822_read_header(msg->fp, e, false, false);
2105 mutt_env_merge(e->env, &newenv);
2106
2107 /* see above. We want the new status in e->read, so we unset it manually
2108 * and let mutt_set_flag set it correctly, updating context. */
2109 if (read != e->read)
2110 {
2111 e->read = read;
2112 mutt_set_flag(m, e, MUTT_NEW, read, true);
2113 }
2114
2115 e->lines = 0;
2116 while (fgets(buf, sizeof(buf), msg->fp) && !feof(msg->fp))
2117 {
2118 e->lines++;
2119 }
2120
2121 e->body->length = ftell(msg->fp) - e->body->offset;
2122
2124 rewind(msg->fp);
2125 imap_edata_get(e)->parsed = true;
2126
2127 /* retry message parse if cached message is empty */
2128 if (!retried && ((e->lines == 0) || (e->body->length == 0)))
2129 {
2130 imap_cache_del(m, e);
2131 retried = true;
2132 goto parsemsg;
2133 }
2134
2135 return true;
2136
2137bail:
2138 e->active = true;
2139 mutt_file_fclose(&msg->fp);
2140 imap_cache_del(m, e);
2141 return false;
2142}
const char * mutt_str_atoui(const char *str, unsigned int *dst)
Convert ASCII string to an unsigned integer.
Definition: atoi.c:213
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:93
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
struct Envelope * mutt_rfc822_read_header(FILE *fp, struct Email *e, bool user_hdrs, bool weed)
Parses an RFC822 header.
Definition: parse.c:1170
void mutt_env_merge(struct Envelope *base, struct Envelope **extra)
Merge the headers of two Envelopes.
Definition: envelope.c:166
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:636
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
void mutt_set_flag(struct Mailbox *m, struct Email *e, enum MessageType flag, bool bf, bool upd_mbox)
Set a flag on an email.
Definition: flags.c:53
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_message(...)
Definition: logging2.h:91
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
struct ImapAccountData * imap_adata_get(struct Mailbox *m)
Get the Account data for this mailbox.
Definition: adata.c:123
int imap_cmd_start(struct ImapAccountData *adata, const char *cmdstr)
Given an IMAP command, send it to the server.
Definition: command.c:1115
int imap_cmd_step(struct ImapAccountData *adata)
Reads server responses from an IMAP command.
Definition: command.c:1129
bool imap_code(const char *s)
Was the command successful.
Definition: command.c:1256
struct ImapEmailData * imap_edata_get(struct Email *e)
Get the private data for this Email.
Definition: edata.c:66
static FILE * msg_cache_put(struct Mailbox *m, struct Email *e)
Put an email into the message cache.
Definition: message.c:126
char * imap_set_flags(struct Mailbox *m, struct Email *e, char *s, bool *server_changes)
Fill the message header according to the server flags.
Definition: message.c:1899
static FILE * msg_cache_get(struct Mailbox *m, struct Email *e)
Get the message cache entry for an email.
Definition: message.c:105
int imap_cache_del(struct Mailbox *m, struct Email *e)
Delete an email from the body cache.
Definition: message.c:1848
static int msg_cache_commit(struct Mailbox *m, struct Email *e)
Add to the message cache.
Definition: message.c:147
int imap_get_literal_count(const char *buf, unsigned int *bytes)
Write number of bytes in an IMAP literal into bytes.
Definition: util.c:738
#define IMAP_RES_OK
<tag> OK ...
Definition: private.h:55
#define IMAP_CAP_IMAP4REV1
Server supports IMAP4rev1.
Definition: private.h:122
void imap_error(const char *where, const char *msg)
Show an error and abort.
Definition: util.c:654
#define IMAP_RES_CONTINUE
* ...
Definition: private.h:56
char * imap_next_word(char *s)
Find where the next IMAP word begins.
Definition: util.c:782
int imap_read_literal(FILE *fp, struct ImapAccountData *adata, unsigned long bytes, struct Progress *progress)
Read bytes bytes from server into file.
Definition: imap.c:582
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
#define _(a)
Definition: message.h:28
size_t mutt_istr_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix, ignoring case.
Definition: string.c:240
@ MUTT_NEW
New messages.
Definition: mutt.h:69
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:73
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
LOFF_T offset
offset where the actual data begins
Definition: body.h:52
LOFF_T length
length (in bytes) of attachment
Definition: body.h:53
String manipulation buffer.
Definition: buffer.h:34
bool read
Email is read.
Definition: email.h:48
struct Envelope * env
Envelope information.
Definition: email.h:66
int lines
How many lines in the body of this message?
Definition: email.h:60
struct Body * body
List of MIME parts.
Definition: email.h:67
bool active
Message is not to be removed.
Definition: email.h:74
bool changed
Email has been edited.
Definition: email.h:75
The header of an Email.
Definition: envelope.h:57
IMAP-specific Account data -.
Definition: adata.h:40
ImapCapFlags capabilities
Capability flags.
Definition: adata.h:55
char * buf
Definition: adata.h:59
bool parsed
Definition: edata.h:42
bool verbose
Display status messages?
Definition: mailbox.h:116
FILE * fp
pointer to the message data
Definition: message.h:35
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
#define buf_mktemp(buf)
Definition: tmp.h:33
+ Here is the call graph for this function:

◆ maildir_msg_open()

static bool maildir_msg_open ( struct Mailbox m,
struct Message msg,
struct Email e 
)
static

Open an email message in a Mailbox - Implements MxOps::msg_open() -.

Definition at line 1494 of file maildir.c.

1495{
1496 char path[PATH_MAX] = { 0 };
1497
1498 snprintf(path, sizeof(path), "%s/%s", mailbox_path(m), e->path);
1499
1500 msg->fp = fopen(path, "r");
1501 if (!msg->fp && (errno == ENOENT))
1502 msg->fp = maildir_open_find_message(mailbox_path(m), e->path, NULL);
1503
1504 if (!msg->fp)
1505 {
1506 mutt_perror("%s", path);
1507 mutt_debug(LL_DEBUG1, "fopen: %s: %s (errno %d)\n", path, strerror(errno), errno);
1508 return false;
1509 }
1510
1511 return true;
1512}
#define mutt_perror(...)
Definition: logging2.h:93
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:210
FILE * maildir_open_find_message(const char *folder, const char *msg, char **newname)
Find a message by name.
Definition: maildir.c:1023
#define PATH_MAX
Definition: mutt.h:41
char * path
Path of Email (for local Mailboxes)
Definition: email.h:68
+ Here is the call graph for this function:

◆ mh_msg_open()

static bool mh_msg_open ( struct Mailbox m,
struct Message msg,
struct Email e 
)
static

Open an email message in a Mailbox - Implements MxOps::msg_open() -.

Definition at line 1118 of file mh.c.

1119{
1120 char path[PATH_MAX] = { 0 };
1121
1122 snprintf(path, sizeof(path), "%s/%s", mailbox_path(m), e->path);
1123
1124 msg->fp = fopen(path, "r");
1125 if (!msg->fp)
1126 {
1127 mutt_perror("%s", path);
1128 mutt_debug(LL_DEBUG1, "fopen: %s: %s (errno %d)\n", path, strerror(errno), errno);
1129 return false;
1130 }
1131
1132 return true;
1133}
+ Here is the call graph for this function:

◆ mbox_msg_open()

static bool mbox_msg_open ( struct Mailbox m,
struct Message msg,
struct Email e 
)
static

Open an email message in a Mailbox - Implements MxOps::msg_open() -.

Definition at line 1480 of file mbox.c.

1481{
1483 if (!adata)
1484 return false;
1485
1486 msg->fp = mutt_file_fopen(mailbox_path(m), "r");
1487 if (!msg->fp)
1488 return false;
1489
1490 return true;
1491}
static struct MboxAccountData * mbox_adata_get(struct Mailbox *m)
Get the private data associated with a Mailbox.
Definition: mbox.c:123
Mbox-specific Account data -.
Definition: lib.h:49
+ Here is the call graph for this function:

◆ nntp_msg_open()

static bool nntp_msg_open ( struct Mailbox m,
struct Message msg,
struct Email e 
)
static

Open an email message in a Mailbox - Implements MxOps::msg_open() -.

Definition at line 2593 of file nntp.c.

2594{
2595 struct NntpMboxData *mdata = m->mdata;
2596 char article[16] = { 0 };
2597
2598 /* try to get article from cache */
2599 struct NntpAcache *acache = &mdata->acache[e->index % NNTP_ACACHE_LEN];
2600 if (acache->path)
2601 {
2602 if (acache->index == e->index)
2603 {
2604 msg->fp = mutt_file_fopen(acache->path, "r");
2605 if (msg->fp)
2606 return true;
2607 }
2608 else
2609 {
2610 /* clear previous entry */
2611 unlink(acache->path);
2612 FREE(&acache->path);
2613 }
2614 }
2615 snprintf(article, sizeof(article), ANUM, nntp_edata_get(e)->article_num);
2616 msg->fp = mutt_bcache_get(mdata->bcache, article);
2617 if (msg->fp)
2618 {
2619 if (nntp_edata_get(e)->parsed)
2620 return true;
2621 }
2622 else
2623 {
2624 /* don't try to fetch article from removed newsgroup */
2625 if (mdata->deleted)
2626 return false;
2627
2628 /* create new cache file */
2629 const char *fetch_msg = _("Fetching message...");
2630 mutt_message("%s", fetch_msg);
2631 msg->fp = mutt_bcache_put(mdata->bcache, article);
2632 if (!msg->fp)
2633 {
2634 struct Buffer *tempfile = buf_pool_get();
2635 buf_mktemp(tempfile);
2636 acache->path = buf_strdup(tempfile);
2637 buf_pool_release(&tempfile);
2638 acache->index = e->index;
2639 msg->fp = mutt_file_fopen(acache->path, "w+");
2640 if (!msg->fp)
2641 {
2642 mutt_perror("%s", acache->path);
2643 unlink(acache->path);
2644 FREE(&acache->path);
2645 return false;
2646 }
2647 }
2648
2649 /* fetch message to cache file */
2650 char buf[2048] = { 0 };
2651 snprintf(buf, sizeof(buf), "ARTICLE %s\r\n",
2652 nntp_edata_get(e)->article_num ? article : e->env->message_id);
2653 const int rc = nntp_fetch_lines(mdata, buf, sizeof(buf), fetch_msg,
2654 fetch_tempfile, msg->fp);
2655 if (rc)
2656 {
2657 mutt_file_fclose(&msg->fp);
2658 if (acache->path)
2659 {
2660 unlink(acache->path);
2661 FREE(&acache->path);
2662 }
2663 if (rc > 0)
2664 {
2665 if (mutt_str_startswith(buf, nntp_edata_get(e)->article_num ? "423" : "430"))
2666 {
2667 mutt_error(_("Article %s not found on the server"),
2668 nntp_edata_get(e)->article_num ? article : e->env->message_id);
2669 }
2670 else
2671 {
2672 mutt_error("ARTICLE: %s", buf);
2673 }
2674 }
2675 return false;
2676 }
2677
2678 if (!acache->path)
2679 mutt_bcache_commit(mdata->bcache, article);
2680 }
2681
2682 /* replace envelope with new one
2683 * hash elements must be updated because pointers will be changed */
2684 if (m->id_hash && e->env->message_id)
2686 if (m->subj_hash && e->env->real_subj)
2688
2689 mutt_env_free(&e->env);
2690 e->env = mutt_rfc822_read_header(msg->fp, e, false, false);
2691
2692 if (m->id_hash && e->env->message_id)
2694 if (m->subj_hash && e->env->real_subj)
2696
2697 /* fix content length */
2698 if (!mutt_file_seek(msg->fp, 0, SEEK_END))
2699 {
2700 return false;
2701 }
2702 e->body->length = ftell(msg->fp) - e->body->offset;
2703
2704 /* this is called in neomutt before the open which fetches the message,
2705 * which is probably wrong, but we just call it again here to handle
2706 * the problem instead of fixing it */
2707 nntp_edata_get(e)->parsed = true;
2708 mutt_parse_mime_message(e, msg->fp);
2709
2710 /* these would normally be updated in mview_update(), but the
2711 * full headers aren't parsed with overview, so the information wasn't
2712 * available then */
2713 if (WithCrypto)
2714 e->security = crypt_query(e->body);
2715
2716 rewind(msg->fp);
2718 return true;
2719}
void mutt_parse_mime_message(struct Email *e, FILE *fp)
Parse a MIME email.
Definition: attachments.c:596
int mutt_bcache_commit(struct BodyCache *bcache, const char *id)
Move a temporary file into the Body Cache.
Definition: bcache.c:249
FILE * mutt_bcache_get(struct BodyCache *bcache, const char *id)
Open a file in the Body Cache.
Definition: bcache.c:182
FILE * mutt_bcache_put(struct BodyCache *bcache, const char *id)
Create a file in the Body Cache.
Definition: bcache.c:209
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:542
SecurityFlags crypt_query(struct Body *b)
Check out the type of encryption used.
Definition: crypt.c:676
void mutt_env_free(struct Envelope **ptr)
Free an Envelope.
Definition: envelope.c:97
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition: file.c:733
struct HashElem * mutt_hash_insert(struct HashTable *table, const char *strkey, void *data)
Add a new element to the Hash Table (with string keys)
Definition: hash.c:335
void mutt_hash_delete(struct HashTable *table, const char *strkey, const void *data)
Remove an element from a Hash Table.
Definition: hash.c:427
#define FREE(x)
Definition: memory.h:45
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:228
#define WithCrypto
Definition: lib.h:117
struct NntpEmailData * nntp_edata_get(struct Email *e)
Get the private data for this Email.
Definition: edata.c:60
#define NNTP_ACACHE_LEN
Definition: lib.h:83
#define ANUM
Definition: lib.h:62
static int nntp_fetch_lines(struct NntpMboxData *mdata, char *query, size_t qlen, const char *msg, int(*func)(char *, void *), void *data)
Read lines, calling a callback function for each.
Definition: nntp.c:782
static int fetch_tempfile(char *line, void *data)
Write line to temporary file.
Definition: nntp.c:974
SecurityFlags security
bit 0-10: flags, bit 11,12: application, bit 13: traditional pgp See: ncrypt/lib.h pgplib....
Definition: email.h:41
int index
The absolute (unsorted) message number.
Definition: email.h:109
char * message_id
Message ID.
Definition: envelope.h:73
char * real_subj
Offset of the real subject.
Definition: envelope.h:71
void * mdata
Driver specific data.
Definition: mailbox.h:133
struct HashTable * subj_hash
Hash Table: "subject" -> Email.
Definition: mailbox.h:125
struct HashTable * id_hash
Hash Table: "message-id" -> Email.
Definition: mailbox.h:124
NNTP article cache.
Definition: lib.h:68
char * path
Cache path.
Definition: lib.h:70
unsigned int index
Index number.
Definition: lib.h:69
bool parsed
Email has been parse.
Definition: edata.h:37
NNTP-specific Mailbox data -.
Definition: mdata.h:34
bool deleted
Definition: mdata.h:45
struct BodyCache * bcache
Definition: mdata.h:50
struct NntpAcache acache[NNTP_ACACHE_LEN]
Definition: mdata.h:49
+ Here is the call graph for this function:

◆ nm_msg_open()

static bool nm_msg_open ( struct Mailbox m,
struct Message msg,
struct Email e 
)
static

Open an email message in a Mailbox - Implements MxOps::msg_open() -.

Definition at line 2352 of file notmuch.c.

2353{
2354 char path[PATH_MAX] = { 0 };
2355 char *folder = nm_email_get_folder(e);
2356
2357 snprintf(path, sizeof(path), "%s/%s", folder, e->path);
2358
2359 msg->fp = fopen(path, "r");
2360 if (!msg->fp && (errno == ENOENT) && ((m->type == MUTT_MAILDIR) || (m->type == MUTT_NOTMUCH)))
2361 {
2362 msg->fp = maildir_open_find_message(folder, e->path, NULL);
2363 }
2364
2365 return msg->fp != NULL;
2366}
@ MUTT_NOTMUCH
'Notmuch' (virtual) Mailbox type
Definition: mailbox.h:51
@ MUTT_MAILDIR
'Maildir' Mailbox type
Definition: mailbox.h:48
FILE * maildir_open_find_message(const char *folder, const char *msg, char **newname)
Find a message by name.
Definition: maildir.c:1023
char * nm_email_get_folder(struct Email *e)
Get the folder for a Email.
Definition: notmuch.c:1459
enum MailboxType type
Mailbox type.
Definition: mailbox.h:102
+ Here is the call graph for this function:

◆ pop_msg_open()

static bool pop_msg_open ( struct Mailbox m,
struct Message msg,
struct Email e 
)
static

Open an email message in a Mailbox - Implements MxOps::msg_open() -.

Definition at line 974 of file pop.c.

975{
976 char buf[1024] = { 0 };
978 struct PopEmailData *edata = pop_edata_get(e);
979 bool bcache = true;
980 bool success = false;
981 struct Buffer *path = NULL;
982
983 /* see if we already have the message in body cache */
984 msg->fp = mutt_bcache_get(adata->bcache, cache_id(edata->uid));
985 if (msg->fp)
986 return true;
987
988 /* see if we already have the message in our cache in
989 * case $message_cache_dir is unset */
990 struct PopCache *cache = &adata->cache[e->index % POP_CACHE_LEN];
991
992 if (cache->path)
993 {
994 if (cache->index == e->index)
995 {
996 /* yes, so just return a pointer to the message */
997 msg->fp = fopen(cache->path, "r");
998 if (msg->fp)
999 return true;
1000
1001 mutt_perror("%s", cache->path);
1002 return false;
1003 }
1004 else
1005 {
1006 /* clear the previous entry */
1007 unlink(cache->path);
1008 FREE(&cache->path);
1009 }
1010 }
1011
1012 path = buf_pool_get();
1013
1014 while (true)
1015 {
1016 if (pop_reconnect(m) < 0)
1017 goto cleanup;
1018
1019 /* verify that massage index is correct */
1020 if (edata->refno < 0)
1021 {
1022 mutt_error(_("The message index is incorrect. Try reopening the mailbox."));
1023 goto cleanup;
1024 }
1025
1026 /* see if we can put in body cache; use our cache as fallback */
1027 msg->fp = mutt_bcache_put(adata->bcache, cache_id(edata->uid));
1028 if (!msg->fp)
1029 {
1030 /* no */
1031 bcache = false;
1033 msg->fp = mutt_file_fopen(buf_string(path), "w+");
1034 if (!msg->fp)
1035 {
1036 mutt_perror("%s", buf_string(path));
1037 goto cleanup;
1038 }
1039 }
1040
1041 snprintf(buf, sizeof(buf), "RETR %d\r\n", edata->refno);
1042
1043 struct Progress *progress = progress_new(_("Fetching message..."), MUTT_PROGRESS_NET,
1044 e->body->length + e->body->offset - 1);
1045 const int rc = pop_fetch_data(adata, buf, progress, fetch_message, msg->fp);
1046 progress_free(&progress);
1047
1048 if (rc == 0)
1049 break;
1050
1051 mutt_file_fclose(&msg->fp);
1052
1053 /* if RETR failed (e.g. connection closed), be sure to remove either
1054 * the file in bcache or from POP's own cache since the next iteration
1055 * of the loop will re-attempt to put() the message */
1056 if (!bcache)
1057 unlink(buf_string(path));
1058
1059 if (rc == -2)
1060 {
1061 mutt_error("%s", adata->err_msg);
1062 goto cleanup;
1063 }
1064
1065 if (rc == -3)
1066 {
1067 mutt_error(_("Can't write message to temporary file"));
1068 goto cleanup;
1069 }
1070 }
1071
1072 /* Update the header information. Previously, we only downloaded a
1073 * portion of the headers, those required for the main display. */
1074 if (bcache)
1075 {
1076 mutt_bcache_commit(adata->bcache, cache_id(edata->uid));
1077 }
1078 else
1079 {
1080 cache->index = e->index;
1081 cache->path = buf_strdup(path);
1082 }
1083 rewind(msg->fp);
1084
1085 /* Detach the private data */
1086 e->edata = NULL;
1087
1088 /* we replace envelope, key in subj_hash has to be updated as well */
1089 if (m->subj_hash && e->env->real_subj)
1092 mutt_env_free(&e->env);
1093 e->env = mutt_rfc822_read_header(msg->fp, e, false, false);
1094 if (m->subj_hash && e->env->real_subj)
1096 mutt_label_hash_add(m, e);
1097
1098 /* Reattach the private data */
1099 e->edata = edata;
1101
1102 e->lines = 0;
1103 while (fgets(buf, sizeof(buf), msg->fp) && !feof(msg->fp))
1104 {
1105 e->lines++;
1106 }
1107
1108 e->body->length = ftello(msg->fp) - e->body->offset;
1109
1110 /* This needs to be done in case this is a multipart message */
1111 if (!WithCrypto)
1112 e->security = crypt_query(e->body);
1113
1115 rewind(msg->fp);
1116
1117 success = true;
1118
1119cleanup:
1120 buf_pool_release(&path);
1121 return success;
1122}
static int fetch_message(const char *line, void *data)
Write line to file - Implements pop_fetch_t -.
Definition: pop.c:97
void mutt_label_hash_remove(struct Mailbox *m, struct Email *e)
Remove a message's labels from the Hash Table.
Definition: mutt_header.c:444
void mutt_label_hash_add(struct Mailbox *m, struct Email *e)
Add a message's labels to the Hash Table.
Definition: mutt_header.c:431
struct PopAccountData * pop_adata_get(struct Mailbox *m)
Get the Account data for this mailbox.
Definition: adata.c:73
void pop_edata_free(void **ptr)
Free the private Email data - Implements Email::edata_free()
Definition: edata.c:41
struct PopEmailData * pop_edata_get(struct Email *e)
Get the private data for this Email.
Definition: edata.c:68
int pop_fetch_data(struct PopAccountData *adata, const char *query, struct Progress *progress, pop_fetch_t callback, void *data)
Read Headers with callback function.
Definition: lib.c:510
int pop_reconnect(struct Mailbox *m)
Reconnect and verify indexes if connection was lost.
Definition: lib.c:606
#define POP_CACHE_LEN
Definition: private.h:39
static const char * cache_id(const char *id)
Make a message-cache-compatible id.
Definition: pop.c:82
@ MUTT_PROGRESS_NET
Progress tracks bytes, according to $net_inc
Definition: lib.h:51
void progress_free(struct Progress **ptr)
Free a Progress Bar.
Definition: progress.c:92
struct Progress * progress_new(const char *msg, enum ProgressType type, size_t size)
Create a new Progress Bar.
Definition: progress.c:124
void * edata
Driver-specific data.
Definition: email.h:72
void(* edata_free)(void **ptr)
Free the private data attached to the Email.
Definition: email.h:86
POP-specific Account data -.
Definition: adata.h:37
char err_msg[POP_CMD_RESPONSE]
Definition: adata.h:56
struct PopCache cache[POP_CACHE_LEN]
Definition: adata.h:57
struct BodyCache * bcache
body cache
Definition: adata.h:55
POP-specific email cache.
Definition: private.h:69
unsigned int index
Definition: private.h:70
char * path
Definition: private.h:71
POP-specific Email data -.
Definition: edata.h:32
int refno
Message number on server.
Definition: edata.h:34
const char * uid
UID of email.
Definition: edata.h:33
+ Here is the call graph for this function: