X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=mx.c;h=6f6f779dc087eb1a9e06af76a76ef43674aa977d;hp=5764a51d1edfb8fe8a231b24eefaab3cd2ac764d;hb=a8aba140a61d5b86093cea46afcb613092d4978d;hpb=ce220f6dc8789ed75468c63adce953fbdfed11a8 diff --git a/mx.c b/mx.c index 5764a51..6f6f779 100644 --- a/mx.c +++ b/mx.c @@ -12,15 +12,25 @@ # include "config.h" #endif +#include +#include +#include +#include +#include + +#include + #include "mutt.h" +#include "buffy.h" #include "mx.h" #include "mbox.h" #include "mh.h" -#include "rfc2047.h" #include "sort.h" +#include "thread.h" #include "copy.h" #include "keymap.h" #include "url.h" +#include "sidebar.h" #ifdef USE_COMPRESSED #include "compress.h" @@ -41,20 +51,14 @@ #include "nntp/mx_nntp.h" #endif -#ifdef BUFFY_SIZE -#include "buffy.h" -#endif - #ifdef USE_DOTLOCK #include "dotlock.h" #endif #include "mutt_crypt.h" -#include "lib/mem.h" -#include "lib/intl.h" -#include "lib/str.h" #include "lib/list.h" +#include "lib/debug.h" #include #include @@ -71,7 +75,7 @@ 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) +#define mutt_is_spool(s) (m_strcmp(Spoolfile, s) == 0) #ifdef USE_DOTLOCK /* parameters: @@ -191,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); @@ -289,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); @@ -340,7 +345,7 @@ void mx_unlink_empty (const char *path) int mx_get_magic (const char *path) { int i = 0; - if (safe_strlen (path) == 0) + if (m_strlen(path) == 0) return (-1); if ((i = mx_get_idx (path)) >= 0) return (MX_COMMAND(i,type)); @@ -485,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: @@ -516,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; @@ -534,7 +539,7 @@ CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx) if (mx_open_mailbox_append (ctx, flags) != 0) { mx_fastclose_mailbox (ctx); if (!pctx) - FREE (&ctx); + p_delete(&ctx); return NULL; } return ctx; @@ -557,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); } @@ -587,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); @@ -611,18 +616,18 @@ 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 */ @@ -692,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; @@ -741,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)); @@ -897,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. */ @@ -972,6 +985,9 @@ void mx_update_tables (CONTEXT * ctx, int committing) } #undef this_body ctx->msgcount = j; + + /* update sidebar count */ + sidebar_set_buffystats (ctx); } @@ -981,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; @@ -994,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; @@ -1094,6 +1109,12 @@ int mx_sync_mailbox (CONTEXT * ctx, int *index_hint) return (rc); } +int mx_sync_mailbox (CONTEXT* ctx, int* index_hint) { + int ret = _mx_sync_mailbox (ctx, index_hint); + sidebar_set_buffystats (ctx); + return (ret); +} + /* args: * dest destintation mailbox * hdr message being copied (required for maildir support, because @@ -1102,15 +1123,14 @@ int mx_sync_mailbox (CONTEXT * ctx, int *index_hint) MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags) { MESSAGE *msg; - ADDRESS *p = NULL; + address_t *p = NULL; if (!MX_IDX(dest->magic-1)) { - dprint (1, (debugfile, "mx_open_new_message(): function " - "unimplemented for mailbox type %d.\n", dest->magic)); + 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; @@ -1143,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); @@ -1161,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 */ @@ -1215,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: @@ -1236,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; @@ -1247,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 */ @@ -1256,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 */ @@ -1265,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); @@ -1283,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 */ @@ -1364,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); } @@ -1387,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; @@ -1432,7 +1355,7 @@ 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 (!ctx->counting && option (OPTSCORE)) @@ -1463,6 +1386,8 @@ void mx_update_context (CONTEXT * ctx, int new_messages) ctx->new++; } } + /* update sidebar count */ + sidebar_set_buffystats (ctx); } /* @@ -1521,3 +1446,43 @@ void mx_init (void) { #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); + } + + 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 +}