From: Damien Riegel Date: Wed, 25 May 2016 22:53:07 +0000 (-0700) Subject: add open_new_msg operation to struct mx_ops X-Git-Tag: neomutt-20160822~145 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ec97dd53c0bc061bd10bd669e275d02c05dfdcc6;p=neomutt add open_new_msg operation to struct mx_ops The code was already using a function pointer to do this operation. This commit moves this function pointer to the mx_ops structure and the open_new_message functions to their respective source files if it needs to. --- diff --git a/imap/imap.c b/imap/imap.c index e9e84191d..c2db3c222 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -828,6 +828,20 @@ void imap_logout (IMAP_DATA** idata) imap_free_idata (idata); } +static int imap_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr) +{ + char tmp[_POSIX_PATH_MAX]; + + mutt_mktemp (tmp, sizeof (tmp)); + if ((msg->fp = safe_fopen (tmp, "w")) == NULL) + { + mutt_perror (tmp); + return (-1); + } + msg->path = safe_strdup(tmp); + return 0; +} + /* imap_set_flag: append str to flags if we currently have permission * according to aclbit */ static void imap_set_flag (IMAP_DATA* idata, int aclbit, int flag, @@ -2041,4 +2055,5 @@ 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_new_msg = imap_open_new_message, }; diff --git a/mailbox.h b/mailbox.h index 1b1a7880d..f61e5865f 100644 --- a/mailbox.h +++ b/mailbox.h @@ -41,7 +41,7 @@ enum MUTT_FLAGS /* nondestructive flags change (IMAP) */ }; -typedef struct +typedef struct _message { FILE *fp; /* pointer to the message data */ char *path; /* path to temp file */ diff --git a/mbox.c b/mbox.c index b7dda1449..823c8e9c2 100644 --- a/mbox.c +++ b/mbox.c @@ -445,6 +445,12 @@ static int mbox_close_mailbox (CONTEXT *ctx) return 0; } +static int mbox_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr) +{ + msg->fp = dest->fp; + return 0; +} + /* return 1 if address lists are strictly identical */ static int strict_addrcmp (const ADDRESS *a, const ADDRESS *b) { @@ -1266,9 +1272,11 @@ int mbox_check_empty (const char *path) struct mx_ops mx_mbox_ops = { .open = mbox_open_mailbox, .close = mbox_close_mailbox, + .open_new_msg = mbox_open_new_message, }; struct mx_ops mx_mmdf_ops = { .open = mbox_open_mailbox, .close = mbox_close_mailbox, + .open_new_msg = mbox_open_new_message, }; diff --git a/mh.c b/mh.c index c156a10c2..1bca8ddd4 100644 --- a/mh.c +++ b/mh.c @@ -1248,7 +1248,7 @@ static int mh_open_mailbox (CONTEXT *ctx) * Open a new (temporary) message in an MH folder. */ -int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr) +static int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr) { return mh_mkstemp (dest, &msg->fp, &msg->path); } @@ -1294,7 +1294,7 @@ static void maildir_flags (char *dest, size_t destlen, HEADER * hdr) * */ -int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr) +static int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr) { int fd; char path[_POSIX_PATH_MAX]; @@ -2361,9 +2361,11 @@ int mx_is_mh (const char *path) struct mx_ops mx_maildir_ops = { .open = maildir_open_mailbox, .close = mh_close_mailbox, + .open_new_msg = maildir_open_new_message, }; struct mx_ops mx_mh_ops = { .open = mh_open_mailbox, .close = mh_close_mailbox, + .open_new_msg = mh_open_new_message, }; diff --git a/mutt.h b/mutt.h index 582904192..00529624d 100644 --- a/mutt.h +++ b/mutt.h @@ -869,11 +869,22 @@ enum }; struct _context; +struct _message; +/* + * struct mx_ops - a structure to store operations on a mailbox + * The following operations are mandatory: + * - open + * - close + * + * Optional operations + * - open_new_msg + */ struct mx_ops { int (*open)(struct _context *); int (*close)(struct _context *); + int (*open_new_msg) (struct _message *, struct _context *, HEADER *); }; typedef struct _context diff --git a/mx.c b/mx.c index 256248c13..5ac8804fb 100644 --- a/mx.c +++ b/mx.c @@ -1197,31 +1197,6 @@ int mx_sync_mailbox (CONTEXT *ctx, int *index_hint) return (rc); } - -/* {maildir,mh}_open_new_message are in mh.c. */ - -static int mbox_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr) -{ - msg->fp = dest->fp; - return 0; -} - -#ifdef USE_IMAP -static int imap_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr) -{ - char tmp[_POSIX_PATH_MAX]; - - mutt_mktemp (tmp, sizeof (tmp)); - if ((msg->fp = safe_fopen (tmp, "w")) == NULL) - { - mutt_perror (tmp); - return (-1); - } - msg->path = safe_strdup(tmp); - return 0; -} -#endif - /* args: * dest destination mailbox * hdr message being copied (required for maildir support, because @@ -1229,31 +1204,15 @@ static int imap_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr) */ MESSAGE *mx_open_new_message (CONTEXT *dest, HEADER *hdr, int flags) { - MESSAGE *msg; - int (*func) (MESSAGE *, CONTEXT *, HEADER *); + struct mx_ops *ops = mx_get_ops (dest->magic); ADDRESS *p = NULL; + MESSAGE *msg; - switch (dest->magic) + if (!ops || !ops->open_new_msg) { - case MUTT_MMDF: - case MUTT_MBOX: - func = mbox_open_new_message; - break; - case MUTT_MAILDIR: - func = maildir_open_new_message; - break; - case MUTT_MH: - func = mh_open_new_message; - break; -#ifdef USE_IMAP - case MUTT_IMAP: - func = imap_open_new_message; - break; -#endif - default: dprint (1, (debugfile, "mx_open_new_message(): function unimplemented for mailbox type %d.\n", - dest->magic)); - return (NULL); + dest->magic)); + return NULL; } msg = safe_calloc (1, sizeof (MESSAGE)); @@ -1271,8 +1230,8 @@ MESSAGE *mx_open_new_message (CONTEXT *dest, HEADER *hdr, int flags) if(msg->received == 0) time(&msg->received); - - if (func (msg, dest, hdr) == 0) + + if (ops->open_new_msg (msg, dest, hdr) == 0) { if (dest->magic == MUTT_MMDF) fputs (MMDF_SEP, msg->fp); diff --git a/mx.h b/mx.h index dfc549e04..bb6a3a354 100644 --- a/mx.h +++ b/mx.h @@ -62,9 +62,6 @@ int maildir_check_empty (const char *); int maildir_commit_message (CONTEXT *, MESSAGE *, HEADER *); int mh_commit_message (CONTEXT *, MESSAGE *, HEADER *); -int maildir_open_new_message (MESSAGE *, CONTEXT *, HEADER *); -int mh_open_new_message (MESSAGE *, CONTEXT *, HEADER *); - FILE *maildir_open_find_message (const char *, const char *); int mbox_strict_cmp_headers (const HEADER *, const HEADER *);