#include "lib/intl.h"
#include "lib/str.h"
#include "lib/list.h"
+#include "lib/debug.h"
#include <dirent.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
-#ifndef BUFFY_SIZE
#include <utime.h>
-#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) (safe_strcmp (Spoolfile, s) == 0)
/* 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++) {
- t = MX_COMMAND(i,mx_is_magic)(path);
- fprintf (stderr, " test %s for %i == %i\n", NONULL(path), i, t);
+ if (!MX_COMMAND(i,local))
+ t = MX_COMMAND(i,mx_is_magic)(path, NULL);
if (t >= 1)
- return (t-1); /* use type as index for array */
+ 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);
}
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);
if (safe_strlen (path) == 0)
return (-1);
- if ((i = mx_get_idx (path)) >= 0) {
- fprintf (stderr, "%s is %i\n", NONULL(path), i);
+ if ((i = mx_get_idx (path)) >= 0)
return (MX_COMMAND(i,type));
- }
return (-1);
}
+int mx_is_local (int m) {
+ if (!MX_IDX(m))
+ return (0);
+ return (MX_COMMAND(m,local));
+}
+
/*
* set DefaultMagic to the given value
*/
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) {
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)
if (!ctx->quiet)
mutt_message (_("Reading %s..."), ctx->path);
- if ((rc = mx_get_idx (ctx->path)) >= 0)
- rc = MX_COMMAND(rc,mx_open_mailbox)(ctx);
+ rc = MX_COMMAND(ctx->magic-1,mx_open_mailbox)(ctx);
if (rc == 0) {
if ((flags & M_NOSORT) == 0) {
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)
/* 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)
/* try to use server-side copy first */
i = 1;
- if (ctx->magic == M_IMAP && mx_get_magic (mbox) == M_IMAP) {
+ 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
return (rc);
}
-#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;
-}
-#endif
-
/* args:
* dest destintation mailbox
* hdr message being copied (required for maildir support, because
MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
{
MESSAGE *msg;
- int (*func) (MESSAGE *, CONTEXT *, HEADER *);
ADDRESS *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);
}
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);
}
}
- dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
+ debug_print (1, ("null or invalid context.\n"));
return (-1);
}
if (msg->fp == NULL) {
mutt_perror (path);
- dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
- path, strerror (errno), errno));
+ debug_print (1, ("fopen: %s: %s (errno %d).\n", path, strerror (errno), errno));
FREE (&msg);
}
}
#endif /* USE_NNTP */
default:
- dprint (1,
- (debugfile,
- "mx_open_message(): function not implemented for mailbox type %d.\n",
- ctx->magic));
+ debug_print (1, ("function not implemented for mailbox type %d.\n", ctx->magic));
FREE (&msg);
break;
}
int r = 0;
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;
}
(*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);
}
/* FREE (&h->env->supersedes); should I ? */
if (h2) {
h2->superseded = 1;
- if (option (OPTSCORE))
+ if (!ctx->counting && option (OPTSCORE))
mutt_score_message (ctx, h2, 1);
}
}
/* 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;
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;
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 */