X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=mx.c;h=6f6f779dc087eb1a9e06af76a76ef43674aa977d;hp=ec090086ef2634136375b3cb8b8e5b999dc9dc4e;hb=a8aba140a61d5b86093cea46afcb613092d4978d;hpb=df70e07e24add1869bcc9b7af2277d9d0c09a281 diff --git a/mx.c b/mx.c index ec09008..6f6f779 100644 --- a/mx.c +++ b/mx.c @@ -1,53 +1,54 @@ /* + * Copyright notice from original mutt: * Copyright (C) 1996-2002 Michael R. Elkins * Copyright (C) 1999-2000 Thomas Roessler - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + * + * This file is part of mutt-ng, see http://www.muttng.org/. + * It's licensed under the GNU General Public License, + * please see the file GPL in the top level source directory. */ #if HAVE_CONFIG_H # include "config.h" #endif +#include +#include +#include +#include +#include + +#include + #include "mutt.h" +#include "buffy.h" #include "mx.h" -#include "rfc2047.h" +#include "mbox.h" +#include "mh.h" #include "sort.h" -#include "mailbox.h" +#include "thread.h" #include "copy.h" #include "keymap.h" #include "url.h" +#include "sidebar.h" #ifdef USE_COMPRESSED #include "compress.h" #endif #ifdef USE_IMAP -#include "imap.h" +#include "imap/imap.h" +#include "imap/mx_imap.h" #endif #ifdef USE_POP -#include "pop.h" +#include "pop/pop.h" +#include "pop/mx_pop.h" #endif #ifdef USE_NNTP -#include "nntp.h" -#endif - -#ifdef BUFFY_SIZE -#include "buffy.h" +#include "nntp/nntp.h" +#include "nntp/mx_nntp.h" #endif #ifdef USE_DOTLOCK @@ -56,6 +57,9 @@ #include "mutt_crypt.h" +#include "lib/list.h" +#include "lib/debug.h" + #include #include #include @@ -65,12 +69,13 @@ #include #include #include -#ifndef BUFFY_SIZE #include -#endif +static list2_t* MailboxFormats = NULL; +#define MX_COMMAND(idx,cmd) ((mx_t*) MailboxFormats->data[idx])->cmd +#define MX_IDX(idx) (idx >= 0 && idx < MailboxFormats->length) -#define mutt_is_spool(s) (mutt_strcmp (Spoolfile, s) == 0) +#define mutt_is_spool(s) (m_strcmp(Spoolfile, s) == 0) #ifdef USE_DOTLOCK /* parameters: @@ -147,6 +152,31 @@ static int undotlock_file (const char *path, int fd) #endif /* USE_DOTLOCK */ +/* looks up index of type for path in MailboxFormats */ +static int mx_get_idx (const char* path) { + int i = 0, t = 0; + struct stat st; + + /* first, test all non-local folders to avoid stat() call */ + for (i = 0; i < MailboxFormats->length; i++) { + if (!MX_COMMAND(i,local)) + t = MX_COMMAND(i,mx_is_magic)(path, NULL); + if (t >= 1) + return (t-1); + } + if (stat (path, &st) == 0) { + /* if stat() succeeded, keep testing until success and + * pass stat() info so that we only need to do it once */ + for (i = 0; i < MailboxFormats->length; i++) { + if (MX_COMMAND(i,local)) + t = MX_COMMAND(i,mx_is_magic)(path, &st); + if (t >= 1) + return (t-1); + } + } + return (-1); +} + /* Args: * excl if excl != 0, request an exclusive lock * dot if dot != 0, try to dotlock the file @@ -165,16 +195,17 @@ int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout) struct flock lck; - memset (&lck, 0, sizeof (struct flock)); + p_clear(&lck, 1); lck.l_type = excl ? F_WRLCK : F_RDLCK; lck.l_whence = SEEK_SET; count = 0; attempt = 0; + prev_sb.st_size = 0; while (fcntl (fd, F_SETLK, &lck) == -1) { struct stat sb; - dprint (1, (debugfile, "mx_lock_file(): fcntl errno %d.\n", errno)); + debug_print (1, ("fcntl errno %d.\n", errno)); if (errno != EAGAIN && errno != EACCES) { mutt_perror ("fcntl"); return (-1); @@ -263,9 +294,9 @@ int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout) int mx_unlock_file (const char *path, int fd, int dot) { #ifdef USE_FCNTL - struct flock unlockit = { F_UNLCK, 0, 0, 0 }; + struct flock unlockit; - memset (&unlockit, 0, sizeof (struct flock)); + p_clear(&unlockit, 1); unlockit.l_type = F_UNLCK; unlockit.l_whence = SEEK_SET; fcntl (fd, F_SETLK, &unlockit); @@ -310,177 +341,21 @@ void mx_unlink_empty (const char *path) close (fd); } -/* try to figure out what type of mailbox ``path'' is - * - * return values: - * M_* mailbox type - * 0 not a mailbox - * -1 error - */ - -#ifdef USE_IMAP - -int mx_is_imap (const char *p) -{ - url_scheme_t scheme; - - if (!p) - return 0; - - if (*p == '{') - return 1; - - scheme = url_check_scheme (p); - if (scheme == U_IMAP || scheme == U_IMAPS) - return 1; - - return 0; -} - -#endif - -#ifdef USE_POP -int mx_is_pop (const char *p) -{ - url_scheme_t scheme; - - if (!p) - return 0; - - scheme = url_check_scheme (p); - if (scheme == U_POP || scheme == U_POPS) - return 1; - - return 0; -} -#endif - -#ifdef USE_NNTP -int mx_is_nntp (const char *p) -{ - url_scheme_t scheme; - - if (!p) - return 0; - - scheme = url_check_scheme (p); - if (scheme == U_NNTP || scheme == U_NNTPS) - return 1; - - return 0; -} -#endif - -int mx_get_magic (const char *path) -{ - struct stat st; - int magic = 0; - char tmp[_POSIX_PATH_MAX]; - FILE *f; - -#ifdef USE_IMAP - if (mx_is_imap (path)) - return M_IMAP; -#endif /* USE_IMAP */ - -#ifdef USE_POP - if (mx_is_pop (path)) - return M_POP; -#endif /* USE_POP */ - -#ifdef USE_NNTP - if (mx_is_nntp (path)) - return M_NNTP; -#endif /* USE_NNTP */ - - if (stat (path, &st) == -1) { - dprint (1, - (debugfile, "mx_get_magic(): unable to stat %s: %s (errno %d).\n", - path, strerror (errno), errno)); - return (-1); - } - - if (S_ISDIR (st.st_mode)) { - /* check for maildir-style mailbox */ - - snprintf (tmp, sizeof (tmp), "%s/cur", path); - if (stat (tmp, &st) == 0 && S_ISDIR (st.st_mode)) - return (M_MAILDIR); - - /* check for mh-style mailbox */ - - snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path); - if (access (tmp, F_OK) == 0) - return (M_MH); - - snprintf (tmp, sizeof (tmp), "%s/.xmhcache", path); - if (access (tmp, F_OK) == 0) - return (M_MH); - - snprintf (tmp, sizeof (tmp), "%s/.mew_cache", path); - if (access (tmp, F_OK) == 0) - return (M_MH); - - snprintf (tmp, sizeof (tmp), "%s/.mew-cache", path); - if (access (tmp, F_OK) == 0) - return (M_MH); - - snprintf (tmp, sizeof (tmp), "%s/.sylpheed_cache", path); - if (access (tmp, F_OK) == 0) - return (M_MH); - - /* - * ok, this isn't an mh folder, but mh mode can be used to read - * Usenet news from the spool. ;-) - */ - - snprintf (tmp, sizeof (tmp), "%s/.overview", path); - if (access (tmp, F_OK) == 0) - return (M_MH); - - } - else if (st.st_size == 0) { - /* hard to tell what zero-length files are, so assume the default magic */ - if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF) - return (DefaultMagic); - else - return (M_MBOX); - } - else if ((f = fopen (path, "r")) != NULL) { -#ifndef BUFFY_SIZE - struct utimbuf times; -#endif +/* try to figure out what type of mailbox ``path'' is */ +int mx_get_magic (const char *path) { + int i = 0; - fgets (tmp, sizeof (tmp), f); - if (mutt_strncmp ("From ", tmp, 5) == 0) - magic = M_MBOX; - else if (mutt_strcmp (MMDF_SEP, tmp) == 0) - magic = M_MMDF; - safe_fclose (&f); -#ifndef BUFFY_SIZE - /* need to restore the times here, the file was not really accessed, - * only the type was accessed. This is important, because detection - * of "new mail" depends on those times set correctly. - */ - times.actime = st.st_atime; - times.modtime = st.st_mtime; - utime (path, ×); -#endif - } - else { - dprint (1, - (debugfile, - "mx_get_magic(): unable to open file %s for reading.\n", path)); - mutt_perror (path); + if (m_strlen(path) == 0) return (-1); - } - -#ifdef USE_COMPRESSED - if (magic == 0 && mutt_can_read_compressed (path)) - return M_COMPRESSED; -#endif + if ((i = mx_get_idx (path)) >= 0) + return (MX_COMMAND(i,type)); + return (-1); +} - return (magic); +int mx_is_local (int m) { + if (!MX_IDX(m)) + return (0); + return (MX_COMMAND(m,local)); } /* @@ -507,12 +382,11 @@ int mx_set_magic (const char *s) * we use the normal access() flags. */ int mx_access (const char *path, int flags) { -#ifdef USE_IMAP - if (mx_is_imap (path)) - return imap_access (path, flags); -#endif + int i = 0; - return access (path, flags); + if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_access)) + return (MX_COMMAND(i,mx_access)(path,flags)); + return (0); } static int mx_open_mailbox_append (CONTEXT * ctx, int flags) @@ -530,7 +404,7 @@ static int mx_open_mailbox_append (CONTEXT * ctx, int flags) #ifdef USE_IMAP - if (mx_is_imap (ctx->path)) + if (mx_get_magic (ctx->path) == M_IMAP) return imap_open_mailbox_append (ctx); #endif @@ -616,7 +490,7 @@ static int mx_open_mailbox_append (CONTEXT * ctx, int flags) } return (-1); } - fseek (ctx->fp, 0, 2); + fseeko (ctx->fp, 0, 2); break; case M_MH: @@ -647,9 +521,9 @@ CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx) int rc; if (!ctx) - ctx = safe_malloc (sizeof (CONTEXT)); - memset (ctx, 0, sizeof (CONTEXT)); - ctx->path = safe_strdup (path); + ctx = p_new(CONTEXT, 1); + p_clear(ctx, 1); + ctx->path = m_strdup(path); ctx->msgnotreadyet = -1; ctx->collapsed = 0; @@ -658,18 +532,21 @@ CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx) ctx->quiet = 1; if (flags & M_READONLY) ctx->readonly = 1; + if (flags & M_COUNT) + ctx->counting = 1; if (flags & (M_APPEND | M_NEWFOLDER)) { if (mx_open_mailbox_append (ctx, flags) != 0) { mx_fastclose_mailbox (ctx); if (!pctx) - FREE (&ctx); + p_delete(&ctx); return NULL; } return ctx; } - ctx->magic = mx_get_magic (path); + if (!MX_IDX(ctx->magic-1)) + ctx->magic = mx_get_magic (path); #ifdef USE_COMPRESSED if (ctx->magic == M_COMPRESSED) @@ -685,7 +562,7 @@ CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx) if (ctx->magic <= 0) { mx_fastclose_mailbox (ctx); if (!pctx) - FREE (&ctx); + p_delete(&ctx); return (NULL); } @@ -699,42 +576,7 @@ CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx) if (!ctx->quiet) mutt_message (_("Reading %s..."), ctx->path); - switch (ctx->magic) { - case M_MH: - rc = mh_read_dir (ctx, NULL); - break; - - case M_MAILDIR: - rc = maildir_read_dir (ctx); - break; - - case M_MMDF: - case M_MBOX: - rc = mbox_open_mailbox (ctx); - break; - -#ifdef USE_IMAP - case M_IMAP: - rc = imap_open_mailbox (ctx); - break; -#endif /* USE_IMAP */ - -#ifdef USE_POP - case M_POP: - rc = pop_open_mailbox (ctx); - break; -#endif /* USE_POP */ - -#ifdef USE_NNTP - case M_NNTP: - rc = nntp_open_mailbox (ctx); - break; -#endif /* USE_NNTP */ - - default: - rc = -1; - break; - } + rc = MX_COMMAND(ctx->magic-1,mx_open_mailbox)(ctx); if (rc == 0) { if ((flags & M_NOSORT) == 0) { @@ -750,7 +592,7 @@ CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx) else { mx_fastclose_mailbox (ctx); if (!pctx) - FREE (&ctx); + p_delete(&ctx); } unset_option (OPTFORCEREFRESH); @@ -765,18 +607,8 @@ void mx_fastclose_mailbox (CONTEXT * ctx) if (!ctx) return; -#ifdef USE_IMAP - if (ctx->magic == M_IMAP) - imap_close_mailbox (ctx); -#endif /* USE_IMAP */ -#ifdef USE_POP - if (ctx->magic == M_POP) - pop_close_mailbox (ctx); -#endif /* USE_POP */ -#ifdef USE_NNTP - if (ctx->magic == M_NNTP) - nntp_fastclose_mailbox (ctx); -#endif /* USE_NNTP */ + if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox)) + MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox(ctx)); if (ctx->subj_hash) hash_destroy (&ctx->subj_hash, NULL); if (ctx->id_hash) @@ -784,74 +616,31 @@ void mx_fastclose_mailbox (CONTEXT * ctx) mutt_clear_threads (ctx); for (i = 0; i < ctx->msgcount; i++) mutt_free_header (&ctx->hdrs[i]); - FREE (&ctx->hdrs); - FREE (&ctx->v2r); + p_delete(&ctx->hdrs); + p_delete(&ctx->v2r); #ifdef USE_COMPRESSED if (ctx->compressinfo) mutt_fast_close_compressed (ctx); #endif - FREE (&ctx->path); - FREE (&ctx->pattern); + p_delete(&ctx->path); + p_delete(&ctx->pattern); if (ctx->limit_pattern) mutt_pattern_free (&ctx->limit_pattern); safe_fclose (&ctx->fp); - memset (ctx, 0, sizeof (CONTEXT)); + p_clear(ctx, 1); } /* save changes to disk */ static int sync_mailbox (CONTEXT * ctx, int *index_hint) { -#ifdef BUFFY_SIZE - BUFFY *tmp = NULL; -#endif int rc = -1; if (!ctx->quiet) mutt_message (_("Writing %s..."), ctx->path); - switch (ctx->magic) { - case M_MBOX: - case M_MMDF: - rc = mbox_sync_mailbox (ctx, index_hint); -#ifdef BUFFY_SIZE - tmp = mutt_find_mailbox (ctx->path); -#endif - break; - - case M_MH: - case M_MAILDIR: - rc = mh_sync_mailbox (ctx, index_hint); - break; - -#ifdef USE_IMAP - case M_IMAP: - /* extra argument means EXPUNGE */ - rc = imap_sync_mailbox (ctx, 1, index_hint); - break; -#endif /* USE_IMAP */ - -#ifdef USE_POP - case M_POP: - rc = pop_sync_mailbox (ctx, index_hint); - break; -#endif /* USE_POP */ - -#ifdef USE_NNTP - case M_NNTP: - rc = nntp_sync_mailbox (ctx); - break; -#endif /* USE_NNTP */ - } - -#if 0 - if (!ctx->quiet && !ctx->shutup && rc == -1) - mutt_error (_("Could not synchronize mailbox %s!"), ctx->path); -#endif - -#ifdef BUFFY_SIZE - if (tmp && tmp->new == 0) - mutt_update_mailbox (tmp); -#endif + if (MX_IDX(ctx->magic-1)) + /* the 1 is only of interest for IMAP and means EXPUNGE */ + rc = MX_COMMAND(ctx->magic-1,mx_sync_mailbox(ctx,1,index_hint)); #ifdef USE_COMPRESSED if (rc == 0 && ctx->compressinfo) @@ -908,7 +697,7 @@ static int trash_append (CONTEXT * ctx) } /* save changes and close mailbox */ -int mx_close_mailbox (CONTEXT * ctx, int *index_hint) +static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint) { int i, move_messages = 0, purge = 1, read_msgs = 0; int check; @@ -957,10 +746,10 @@ int mx_close_mailbox (CONTEXT * ctx, int *index_hint) if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) { isSpool = 1; - strfcpy (mbox, p, sizeof (mbox)); + m_strcpy(mbox, sizeof(mbox), p); } else { - strfcpy (mbox, NONULL (Inbox), sizeof (mbox)); + m_strcpy(mbox, sizeof(mbox), NONULL(Inbox)); isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox); } mutt_expand_path (mbox, sizeof (mbox)); @@ -1006,7 +795,7 @@ int mx_close_mailbox (CONTEXT * ctx, int *index_hint) /* try to use server-side copy first */ i = 1; - if (ctx->magic == M_IMAP && mx_is_imap (mbox)) { + if (ctx->magic == M_IMAP && imap_is_magic (mbox, NULL) == M_IMAP) { /* tag messages for moving, and clear old tags, if any */ for (i = 0; i < ctx->msgcount; i++) if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted @@ -1113,6 +902,14 @@ int mx_close_mailbox (CONTEXT * ctx, int *index_hint) return 0; } +int mx_close_mailbox (CONTEXT * ctx, int *index_hint) { + int ret = 0; + if (!ctx) + return (0); + ret = _mx_close_mailbox (ctx, index_hint); + sidebar_set_buffystats (ctx); + return (ret); +} /* update a Context structure's internal tables. */ @@ -1188,6 +985,9 @@ void mx_update_tables (CONTEXT * ctx, int committing) } #undef this_body ctx->msgcount = j; + + /* update sidebar count */ + sidebar_set_buffystats (ctx); } @@ -1197,7 +997,7 @@ void mx_update_tables (CONTEXT * ctx, int committing) * 0 success * -1 error */ -int mx_sync_mailbox (CONTEXT * ctx, int *index_hint) +static int _mx_sync_mailbox (CONTEXT * ctx, int *index_hint) { int rc, i; int purge = 1; @@ -1210,8 +1010,7 @@ int mx_sync_mailbox (CONTEXT * ctx, int *index_hint) km_find_func (MENU_MAIN, OP_TOGGLE_WRITE))) snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf); else - strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"), - sizeof (tmp)); + m_strcpy(tmp, sizeof(tmp), _("Use 'toggle-write' to re-enable write!")); mutt_error (_("Mailbox is marked unwritable. %s"), tmp); return -1; @@ -1310,27 +1109,11 @@ int mx_sync_mailbox (CONTEXT * ctx, int *index_hint) return (rc); } - -/* {maildir,mh}_open_new_message are in mh.c. */ - -int mbox_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr) -{ - msg->fp = dest->fp; - return 0; -} - -#ifdef USE_IMAP -int imap_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr) -{ - char tmp[_POSIX_PATH_MAX]; - - mutt_mktemp (tmp); - if ((msg->fp = safe_fopen (tmp, "w")) == NULL) - return (-1); - msg->path = safe_strdup (tmp); - return 0; +int mx_sync_mailbox (CONTEXT* ctx, int* index_hint) { + int ret = _mx_sync_mailbox (ctx, index_hint); + sidebar_set_buffystats (ctx); + return (ret); } -#endif /* args: * dest destintation mailbox @@ -1340,34 +1123,14 @@ 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 *); - ADDRESS *p = NULL; + address_t *p = NULL; - switch (dest->magic) { - case M_MMDF: - case M_MBOX: - func = mbox_open_new_message; - break; - case M_MAILDIR: - func = maildir_open_new_message; - break; - case M_MH: - func = mh_open_new_message; - break; -#ifdef USE_IMAP - case M_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)); + if (!MX_IDX(dest->magic-1)) { + debug_print (1, ("function unimplemented for mailbox type %d.\n", dest->magic)); return (NULL); } - msg = safe_calloc (1, sizeof (MESSAGE)); + msg = p_new(MESSAGE, 1); msg->magic = dest->magic; msg->write = 1; @@ -1381,7 +1144,7 @@ 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 (MX_COMMAND(dest->magic-1,mx_open_new_message)(msg, dest, hdr) == 0) { if (dest->magic == M_MMDF) fputs (MMDF_SEP, msg->fp); @@ -1400,16 +1163,13 @@ MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags) } } else - FREE (&msg); + p_delete(&msg); return msg; } /* check for new mail */ -int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) -{ - int rc; - +int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) { #ifdef USE_COMPRESSED if (ctx->compressinfo) return mutt_check_mailbox_compressed (ctx); @@ -1418,53 +1178,13 @@ int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) if (ctx) { if (ctx->locked) lock = 0; - - switch (ctx->magic) { - case M_MBOX: - case M_MMDF: - - if (lock) { - mutt_block_signals (); - if (mbox_lock_mailbox (ctx, 0, 0) == -1) { - mutt_unblock_signals (); - return M_LOCKED; - } - } - - rc = mbox_check_mailbox (ctx, index_hint); - - if (lock) { - mutt_unblock_signals (); - mbox_unlock_mailbox (ctx); - } - - return rc; - - - case M_MH: - return (mh_check_mailbox (ctx, index_hint)); - case M_MAILDIR: - return (maildir_check_mailbox (ctx, index_hint)); - -#ifdef USE_IMAP - case M_IMAP: - return (imap_check_mailbox (ctx, index_hint, 0)); -#endif /* USE_IMAP */ - -#ifdef USE_POP - case M_POP: - return (pop_check_mailbox (ctx, index_hint)); -#endif /* USE_POP */ - -#ifdef USE_NNTP - case M_NNTP: - return (nntp_check_mailbox (ctx)); -#endif /* USE_NNTP */ - } + if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_check_mailbox)) + return (MX_COMMAND(ctx->magic-1,mx_check_mailbox)(ctx, index_hint, lock)); } - dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n")); + debug_print (1, ("null or invalid context.\n")); return (-1); + } /* return a stream pointer for a message */ @@ -1472,7 +1192,7 @@ MESSAGE *mx_open_message (CONTEXT * ctx, int msgno) { MESSAGE *msg; - msg = safe_calloc (1, sizeof (MESSAGE)); + msg = p_new(MESSAGE, 1); switch (msg->magic = ctx->magic) { case M_MBOX: case M_MMDF: @@ -1493,9 +1213,8 @@ MESSAGE *mx_open_message (CONTEXT * ctx, int msgno) 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); + debug_print (1, ("fopen: %s: %s (errno %d).\n", path, strerror (errno), errno)); + p_delete(&msg); } } break; @@ -1504,7 +1223,7 @@ MESSAGE *mx_open_message (CONTEXT * ctx, int msgno) case M_IMAP: { if (imap_fetch_message (msg, ctx, msgno) != 0) - FREE (&msg); + p_delete(&msg); break; } #endif /* USE_IMAP */ @@ -1513,7 +1232,7 @@ MESSAGE *mx_open_message (CONTEXT * ctx, int msgno) case M_POP: { if (pop_fetch_message (msg, ctx, msgno) != 0) - FREE (&msg); + p_delete(&msg); break; } #endif /* USE_POP */ @@ -1522,17 +1241,14 @@ MESSAGE *mx_open_message (CONTEXT * ctx, int msgno) case M_NNTP: { if (nntp_fetch_message (msg, ctx, msgno) != 0) - FREE (&msg); + p_delete(&msg); break; } #endif /* USE_NNTP */ default: - dprint (1, - (debugfile, - "mx_open_message(): function not implemented for mailbox type %d.\n", - ctx->magic)); - FREE (&msg); + debug_print (1, ("function not implemented for mailbox type %d.\n", ctx->magic)); + p_delete(&msg); break; } return (msg); @@ -1540,63 +1256,14 @@ MESSAGE *mx_open_message (CONTEXT * ctx, int msgno) /* commit a message to a folder */ -int mx_commit_message (MESSAGE * msg, CONTEXT * ctx) -{ - int r = 0; - +int mx_commit_message (MESSAGE * msg, CONTEXT * ctx) { if (!(msg->write && ctx->append)) { - dprint (1, - (debugfile, - "mx_commit_message(): msg->write = %d, ctx->append = %d\n", - msg->write, ctx->append)); + debug_print (1, ("msg->write = %d, ctx->append = %d\n", msg->write, ctx->append)); return -1; } - - switch (msg->magic) { - case M_MMDF: - { - if (fputs (MMDF_SEP, msg->fp) == EOF) - r = -1; - break; - } - - case M_MBOX: - { - if (fputc ('\n', msg->fp) == EOF) - r = -1; - break; - } - -#ifdef USE_IMAP - case M_IMAP: - { - if ((r = safe_fclose (&msg->fp)) == 0) - r = imap_append_message (ctx, msg); - break; - } -#endif - - case M_MAILDIR: - { - r = maildir_commit_message (ctx, msg, NULL); - break; - } - - case M_MH: - { - r = mh_commit_message (ctx, msg, NULL); - break; - } - } - - if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF) - && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) { - mutt_perror _("Can't write message"); - - r = -1; - } - - return r; + if (!ctx || !MX_IDX(ctx->magic-1) || !MX_COMMAND(ctx->magic-1,mx_commit_message)) + return (-1); + return (MX_COMMAND(ctx->magic-1,mx_commit_message) (msg, ctx)); } /* close a pointer to a message */ @@ -1621,13 +1288,12 @@ int mx_close_message (MESSAGE ** msg) (*msg)->fp = NULL; if ((*msg)->path) { - dprint (1, (debugfile, "mx_close_message (): unlinking %s\n", - (*msg)->path)); + debug_print (1, ("unlinking %s\n", (*msg)->path)); unlink ((*msg)->path); - FREE (&(*msg)->path); + p_delete(&(*msg)->path); } - FREE (msg); + p_delete(msg); return (r); } @@ -1644,12 +1310,12 @@ void mx_alloc_memory (CONTEXT * ctx) } if (ctx->hdrs) { - safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25)); - safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax); + p_realloc(&ctx->hdrs, ctx->hdrmax += 25); + p_realloc(&ctx->v2r, ctx->hdrmax); } else { - ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *)); - ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int)); + ctx->hdrs = p_new(HEADER *, (ctx->hdrmax += 25)); + ctx->v2r = p_new(int, ctx->hdrmax); } for (i = ctx->msgcount; i < ctx->hdrmax; i++) { ctx->hdrs[i] = NULL; @@ -1689,10 +1355,10 @@ void mx_update_context (CONTEXT * ctx, int new_messages) h2 = hash_find (ctx->id_hash, h->env->supersedes); - /* FREE (&h->env->supersedes); should I ? */ + /* p_delete(&h->env->supersedes); should I ? */ if (h2) { h2->superseded = 1; - if (option (OPTSCORE)) + if (!ctx->counting && option (OPTSCORE)) mutt_score_message (ctx, h2, 1); } } @@ -1700,11 +1366,13 @@ void mx_update_context (CONTEXT * ctx, int new_messages) /* add this message to the hash tables */ if (ctx->id_hash && h->env->message_id) hash_insert (ctx->id_hash, h->env->message_id, h, 0); - if (ctx->subj_hash && h->env->real_subj) - hash_insert (ctx->subj_hash, h->env->real_subj, h, 1); + if (!ctx->counting) { + if (ctx->subj_hash && h->env->real_subj) + hash_insert (ctx->subj_hash, h->env->real_subj, h, 1); - if (option (OPTSCORE)) - mutt_score_message (ctx, h, 0); + if (option (OPTSCORE)) + mutt_score_message (ctx, h, 0); + } if (h->changed) ctx->changed = 1; @@ -1718,6 +1386,8 @@ void mx_update_context (CONTEXT * ctx, int new_messages) ctx->new++; } } + /* update sidebar count */ + sidebar_set_buffystats (ctx); } /* @@ -1728,17 +1398,91 @@ void mx_update_context (CONTEXT * ctx, int new_messages) */ int mx_check_empty (const char *path) { - switch (mx_get_magic (path)) { - case M_MBOX: - case M_MMDF: - return mbox_check_empty (path); - case M_MH: - return mh_check_empty (path); - case M_MAILDIR: - return maildir_check_empty (path); - default: - errno = EINVAL; - return -1; + int i = 0; + if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_check_empty)) + return (MX_COMMAND(i,mx_check_empty)(path)); + errno = EINVAL; + return (-1); +} + +int mx_acl_check (CONTEXT* ctx, int flag) { + if (!ctx || !MX_IDX(ctx->magic-1)) + return (0); + /* if no acl_check defined for module, assume permission is granted */ + if (!MX_COMMAND(ctx->magic-1,mx_acl_check)) + return (1); + return (MX_COMMAND(ctx->magic-1,mx_acl_check)(ctx,flag)); +} + +void mx_init (void) { +#ifdef DEBUG + int i = 0; +#endif + list_push_back (&MailboxFormats, (void*) mbox_reg_mx ()); + list_push_back (&MailboxFormats, (void*) mmdf_reg_mx ()); + list_push_back (&MailboxFormats, (void*) mh_reg_mx ()); + list_push_back (&MailboxFormats, (void*) maildir_reg_mx ()); +#ifdef USE_IMAP + list_push_back (&MailboxFormats, (void*) imap_reg_mx ()); +#endif +#ifdef USE_POP + list_push_back (&MailboxFormats, (void*) pop_reg_mx ()); +#endif +#ifdef USE_NNTP + list_push_back (&MailboxFormats, (void*) nntp_reg_mx ()); +#endif +#ifdef USE_COMPRESSED + list_push_back (&MailboxFormats, (void*) compress_reg_mx ()); +#endif +#ifdef DEBUG + /* check module registration for completeness with debug versions */ +#define EXITWITHERR(m) do { fprintf(stderr, "error: incomplete mx module: %s is missing for type %i\n",m,i);exit(1); } while (0) + for (i = 0; i < MailboxFormats->length; i++) { + if (MX_COMMAND(i,type) < 1) EXITWITHERR("type"); + if (!MX_COMMAND(i,mx_is_magic)) EXITWITHERR("mx_is_magic"); + if (!MX_COMMAND(i,mx_open_mailbox)) EXITWITHERR("mx_open_mailbox"); +/* if (!MX_COMMAND(i,mx_sync_mailbox)) EXITWITHERR("mx_sync_mailbox");*/ + } +#undef EXITWITHERR +#endif /* DEBUG */ +} + +int mx_rebuild_cache (void) { +#ifndef USE_HCACHE + mutt_error (_("Support for header caching was not build in.")); + return (1); +#else + int i = 0, magic = 0; + CONTEXT* ctx = NULL; + BUFFY* b = NULL; + + if (list_empty(Incoming)) { + mutt_error (_("No mailboxes defined.")); + return (1); } - /* not reached */ + + for (i = 0; i < Incoming->length; i++) { + b = (BUFFY*) Incoming->data[i]; + magic = mx_get_magic (b->path); + if (magic != M_MAILDIR && magic != M_MH +#ifdef USE_IMAP + && magic != M_IMAP +#endif + ) + continue; + sidebar_set_current (b->path); + sidebar_draw (CurrentMenu); + if ((ctx = mx_open_mailbox (b->path, + M_READONLY | M_NOSORT | M_COUNT, + NULL)) != NULL) + mx_close_mailbox (ctx, 0); + } + mutt_clear_error (); + + if (Context && Context->path) + sidebar_set_current (Context->path); + sidebar_draw (CurrentMenu); + + return (0); +#endif }