From: Damien Riegel Date: Sat, 18 Jun 2016 02:01:31 +0000 (-0700) Subject: Add open_msg to struct mx_ops X-Git-Tag: mutt-1-7-rel~67 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=35c3ae29bfe4d97723e584323abab57808569c70;p=mutt Add open_msg to struct mx_ops Add the callback to open an existing message to struct mx_ops. For mbox, mmdf, maildir, and mh, the code was implemented directly into mx_open_message, so it is moved in their respective source files. For imap and pop, there were already _fetch_message functions, but their argument order has been changed to pass the context as a first argument. --- diff --git a/imap/imap.c b/imap/imap.c index 8d04012c..87a9ed59 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -2074,6 +2074,7 @@ int imap_complete(char* dest, size_t dlen, char* path) { struct mx_ops mx_imap_ops = { .open = imap_open_mailbox, .close = imap_close_mailbox, + .open_msg = imap_fetch_message, .open_new_msg = imap_open_new_message, .check = imap_check_mailbox_reopen, }; diff --git a/imap/imap.h b/imap/imap.h index 693008b1..ff0a1ba0 100644 --- a/imap/imap.h +++ b/imap/imap.h @@ -58,7 +58,6 @@ int imap_mailbox_rename (const char* mailbox); /* message.c */ int imap_append_message (CONTEXT* ctx, MESSAGE* msg); int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete); -int imap_fetch_message (MESSAGE* msg, CONTEXT* ctx, int msgno); /* socket.c */ void imap_logout_all (void); diff --git a/imap/imap_private.h b/imap/imap_private.h index b5579744..f5dd83f3 100644 --- a/imap/imap_private.h +++ b/imap/imap_private.h @@ -268,6 +268,8 @@ char* imap_set_flags (IMAP_DATA* idata, HEADER* h, char* s); int imap_cache_del (IMAP_DATA* idata, HEADER* h); int imap_cache_clean (IMAP_DATA* idata); +int imap_fetch_message (CONTEXT *ctx, MESSAGE *msg, int msgno); + /* util.c */ #ifdef USE_HCACHE header_cache_t* imap_hcache_open (IMAP_DATA* idata, const char* path); diff --git a/imap/message.c b/imap/message.c index 5e66b1db..f6316171 100644 --- a/imap/message.c +++ b/imap/message.c @@ -391,7 +391,7 @@ error_out_0: return retval; } -int imap_fetch_message (MESSAGE *msg, CONTEXT *ctx, int msgno) +int imap_fetch_message (CONTEXT *ctx, MESSAGE *msg, int msgno) { IMAP_DATA* idata; HEADER* h; diff --git a/mbox.c b/mbox.c index b3ee560b..5d9235fb 100644 --- a/mbox.c +++ b/mbox.c @@ -447,6 +447,13 @@ static int mbox_close_mailbox (CONTEXT *ctx) return 0; } +static int mbox_open_message (CONTEXT *ctx, MESSAGE *msg, int msgno) +{ + msg->fp = ctx->fp; + + return 0; +} + static int mbox_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr) { msg->fp = dest->fp; @@ -1274,6 +1281,7 @@ int mbox_check_empty (const char *path) struct mx_ops mx_mbox_ops = { .open = mbox_open_mailbox, .close = mbox_close_mailbox, + .open_msg = mbox_open_message, .open_new_msg = mbox_open_new_message, .check = mbox_check_mailbox, }; @@ -1281,6 +1289,7 @@ struct mx_ops mx_mbox_ops = { struct mx_ops mx_mmdf_ops = { .open = mbox_open_mailbox, .close = mbox_close_mailbox, + .open_msg = mbox_open_message, .open_new_msg = mbox_open_new_message, .check = mbox_check_mailbox, }; diff --git a/mh.c b/mh.c index d15b8513..3a0adcc1 100644 --- a/mh.c +++ b/mh.c @@ -1336,6 +1336,38 @@ static void maildir_flags (char *dest, size_t destlen, HEADER * hdr) } } +static int maildir_mh_open_message (CONTEXT *ctx, MESSAGE *msg, int msgno, + int is_maildir) +{ + HEADER *cur = ctx->hdrs[msgno]; + char path[_POSIX_PATH_MAX]; + + snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path); + + msg->fp = fopen (path, "r"); + if (msg->fp == NULL && errno == ENOENT && is_maildir) + msg->fp = maildir_open_find_message (ctx->path, cur->path); + + if (!msg->fp) + { + mutt_perror (path); + dprint (1, (debugfile, "maildir_mh_open_message: fopen: %s: %s (errno %d).\n", + path, strerror (errno), errno)); + return -1; + } + + return 0; +} + +static int maildir_open_message (CONTEXT *ctx, MESSAGE *msg, int msgno) +{ + return maildir_mh_open_message (ctx, msg, msgno, 1); +} + +static int mh_open_message (CONTEXT *ctx, MESSAGE *msg, int msgno) +{ + return maildir_mh_open_message (ctx, msg, msgno, 0); +} /* * Open a new (temporary) message in a maildir folder. @@ -2412,6 +2444,7 @@ int mx_is_mh (const char *path) struct mx_ops mx_maildir_ops = { .open = maildir_open_mailbox, .close = mh_close_mailbox, + .open_msg = maildir_open_message, .open_new_msg = maildir_open_new_message, .check = maildir_check_mailbox, }; @@ -2419,6 +2452,7 @@ struct mx_ops mx_maildir_ops = { struct mx_ops mx_mh_ops = { .open = mh_open_mailbox, .close = mh_close_mailbox, + .open_msg = mh_open_message, .open_new_msg = mh_open_new_message, .check = mh_check_mailbox, }; diff --git a/mutt.h b/mutt.h index aa18241e..74cede32 100644 --- a/mutt.h +++ b/mutt.h @@ -894,6 +894,7 @@ struct mx_ops int (*open)(struct _context *); int (*close)(struct _context *); int (*check) (struct _context *ctx, int *index_hint); + int (*open_msg) (struct _context *, struct _message *, int msgno); int (*open_new_msg) (struct _message *, struct _context *, HEADER *); }; diff --git a/mx.c b/mx.c index b2397740..8e5b57e7 100644 --- a/mx.c +++ b/mx.c @@ -1308,62 +1308,23 @@ int mx_check_mailbox (CONTEXT *ctx, int *index_hint) /* return a stream pointer for a message */ MESSAGE *mx_open_message (CONTEXT *ctx, int msgno) { + struct mx_ops *ops = mx_get_ops (ctx->magic); MESSAGE *msg; - - msg = safe_calloc (1, sizeof (MESSAGE)); - switch (msg->magic = ctx->magic) - { - case MUTT_MBOX: - case MUTT_MMDF: - msg->fp = ctx->fp; - break; + int ret; - case MUTT_MH: - case MUTT_MAILDIR: - { - HEADER *cur = ctx->hdrs[msgno]; - char path[_POSIX_PATH_MAX]; - - snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path); - - if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT && - ctx->magic == MUTT_MAILDIR) - msg->fp = maildir_open_find_message (ctx->path, cur->path); - - if (msg->fp == NULL) - { - mutt_perror (path); - dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n", - path, strerror (errno), errno)); - FREE (&msg); - } - } - break; - -#ifdef USE_IMAP - case MUTT_IMAP: - { - if (imap_fetch_message (msg, ctx, msgno) != 0) - FREE (&msg); - break; - } -#endif /* USE_IMAP */ + if (!ops || !ops->open_msg) + { + dprint (1, (debugfile, "mx_open_message(): function not implemented for mailbox type %d.\n", ctx->magic)); + return NULL; + } -#ifdef USE_POP - case MUTT_POP: - { - if (pop_fetch_message (msg, ctx, msgno) != 0) - FREE (&msg); - break; - } -#endif /* USE_POP */ + msg = safe_calloc (1, sizeof (MESSAGE)); + msg->magic = ctx->magic; + ret = ops->open_msg (ctx, msg, msgno); + if (ret) + FREE (&msg); - default: - dprint (1, (debugfile, "mx_open_message(): function not implemented for mailbox type %d.\n", ctx->magic)); - FREE (&msg); - break; - } - return (msg); + return msg; } /* commit a message to a folder */ diff --git a/pop.c b/pop.c index 4f0124ab..15c99792 100644 --- a/pop.c +++ b/pop.c @@ -512,7 +512,7 @@ int pop_close_mailbox (CONTEXT *ctx) } /* fetch message from POP server */ -int pop_fetch_message (MESSAGE* msg, CONTEXT* ctx, int msgno) +static int pop_fetch_message (CONTEXT* ctx, MESSAGE* msg, int msgno) { int ret; void *uidl; @@ -931,5 +931,6 @@ fail: struct mx_ops mx_pop_ops = { .open = pop_open_mailbox, .close = pop_close_mailbox, + .open_msg = pop_fetch_message, .check = pop_check_mailbox, }; diff --git a/pop.h b/pop.h index edc1ed19..53454c67 100644 --- a/pop.h +++ b/pop.h @@ -106,7 +106,6 @@ void pop_error (POP_DATA *, char *); /* pop.c */ int pop_sync_mailbox (CONTEXT *, int *); -int pop_fetch_message (MESSAGE *, CONTEXT *, int); int pop_close_mailbox (CONTEXT *); void pop_fetch_mail (void);