From 8297f3a57ccff9c0663551658cdd5d3bf166249d Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Wed, 28 Nov 2007 18:09:40 +0100 Subject: [PATCH 1/1] More modular way to open messages. Signed-off-by: Pierre Habouzit --- imap/imap.c | 1 + lib-mx/compress.c | 1 + lib-mx/mbox.c | 7 +++++++ lib-mx/mh.c | 19 +++++++++++++++++ lib-mx/mx.c | 52 ++++++++--------------------------------------- lib-mx/mx.h | 1 + lib-mx/pop.c | 5 ++++- lib-mx/pop.h | 2 -- 8 files changed, 41 insertions(+), 47 deletions(-) diff --git a/imap/imap.c b/imap/imap.c index 38a633c..ea2eb80 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -1595,6 +1595,7 @@ mx_t const imap_mx = { imap_access, imap_open_mailbox, imap_open_new_message, + imap_fetch_message, acl_check_imap, _imap_check_mailbox, imap_close_mailbox, diff --git a/lib-mx/compress.c b/lib-mx/compress.c index 65093c5..ec4db2b 100644 --- a/lib-mx/compress.c +++ b/lib-mx/compress.c @@ -470,4 +470,5 @@ mx_t const compress_mx = { NULL, NULL, NULL, + NULL, }; diff --git a/lib-mx/mbox.c b/lib-mx/mbox.c index 22f1172..9eb2809 100644 --- a/lib-mx/mbox.c +++ b/lib-mx/mbox.c @@ -38,6 +38,12 @@ static int mbox_open_new_message(MESSAGE *msg, CONTEXT *dest, HEADER *hdr) return 0; } +static int mbox_open_message(MESSAGE *msg, CONTEXT *ctx, int msgno) +{ + msg->fp = ctx->fp; + return 0; +} + /* prototypes */ static int mbox_reopen_mailbox (CONTEXT*, int*); @@ -876,6 +882,7 @@ mx_t const mbox_mx = { access, mbox_open_mailbox, mbox_open_new_message, + mbox_open_message, NULL, mbox_check_mailbox, NULL, diff --git a/lib-mx/mh.c b/lib-mx/mh.c index 23d21fe..5e889d6 100644 --- a/lib-mx/mh.c +++ b/lib-mx/mh.c @@ -922,7 +922,24 @@ static int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr return 0; } +static int maildir_open_message(MESSAGE *msg, CONTEXT *ctx, int msgno) +{ + 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 == M_MAILDIR) { + msg->fp = maildir_open_find_message (ctx->path, cur->path); + } + + if (msg->fp == NULL) { + mutt_perror (path); + return -1; + } + return 0; +} /* * Commit a message to a maildir folder. @@ -1812,6 +1829,7 @@ mx_t const maildir_mx = { access, maildir_read_dir, maildir_open_new_message, + maildir_open_message, NULL, maildir_check_mailbox, NULL, @@ -1827,6 +1845,7 @@ mx_t const mh_mx = { access, mh_read_dir, mh_open_new_message, + maildir_open_message, NULL, mh_check_mailbox, NULL, diff --git a/lib-mx/mx.c b/lib-mx/mx.c index 609b047..1d0e8d7 100644 --- a/lib-mx/mx.c +++ b/lib-mx/mx.c @@ -35,7 +35,6 @@ #define MAXLOCKATTEMPT 5 - static mx_t const *mxfmts[] = { &mbox_mx, &mh_mx, @@ -817,6 +816,7 @@ int mx_sync_mailbox (CONTEXT* ctx, int* index_hint) { */ MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags) { + MESSAGE *msg; address_t *p = NULL; @@ -877,52 +877,16 @@ int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) { /* return a stream pointer for a message */ MESSAGE *mx_open_message (CONTEXT * ctx, int msgno) { - MESSAGE *msg; - - msg = p_new(MESSAGE, 1); - switch (msg->magic = ctx->magic) { - case M_MBOX: - msg->fp = ctx->fp; - break; - - case M_MH: - case M_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 == M_MAILDIR) - msg->fp = maildir_open_find_message (ctx->path, cur->path); - - if (msg->fp == NULL) { - mutt_perror (path); - p_delete(&msg); - } - } - break; + MESSAGE *msg; - case M_IMAP: - { - if (imap_fetch_message (msg, ctx, msgno) != 0) - p_delete(&msg); - break; - } + if (!MX_IDX(ctx->magic) || !mxfmts[ctx->magic - 1]->mx_open_message) + return NULL; - case M_POP: - { - if (pop_fetch_message (msg, ctx, msgno) != 0) + msg = p_new(MESSAGE, 1); + msg->magic = ctx->magic; + if (mxfmts[ctx->magic - 1]->mx_open_message(msg, ctx, msgno)) p_delete(&msg); - break; - } - - default: - p_delete(&msg); - break; - } - return msg; + return msg; } /* commit a message to a folder */ diff --git a/lib-mx/mx.h b/lib-mx/mx.h index 9ccae83..deccf85 100644 --- a/lib-mx/mx.h +++ b/lib-mx/mx.h @@ -81,6 +81,7 @@ typedef struct mx_t { int (*mx_open_mailbox) (CONTEXT*); /* open new message */ int (*mx_open_new_message) (MESSAGE*, CONTEXT*, HEADER*); + int (*mx_open_message)(MESSAGE*, CONTEXT*, int); /* check ACL flags; if not implemented, always assume granted * permissions */ int (*mx_acl_check) (CONTEXT*, int); diff --git a/lib-mx/pop.c b/lib-mx/pop.c index 2258d0f..8f522c7 100644 --- a/lib-mx/pop.c +++ b/lib-mx/pop.c @@ -1179,6 +1179,8 @@ static int pop_sync_mailbox(CONTEXT * ctx, int unused, int *index_hint) } } +static int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno); + /* }}} */ mx_t const pop_mx = { @@ -1189,6 +1191,7 @@ mx_t const pop_mx = { NULL, pop_open_mailbox, NULL, + pop_fetch_message, pop_acl_check, pop_check_mailbox, pop_close_mailbox, @@ -1347,7 +1350,7 @@ fail: } /* fetch message from POP server */ -int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno) +static int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno) { int ret; void *uidl; diff --git a/lib-mx/pop.h b/lib-mx/pop.h index 22806cb..c0f6cd2 100644 --- a/lib-mx/pop.h +++ b/lib-mx/pop.h @@ -14,8 +14,6 @@ #include extern mx_t const pop_mx; - -int pop_fetch_message(MESSAGE *, CONTEXT *, int); void pop_fetch_mail(void); #endif -- 2.20.1