Simplifications.
[apps/madmutt.git] / lib-mx / mx.c
index 6a64115..493b421 100644 (file)
 
 #include <lib-lib/lib-lib.h>
 
-#include <utime.h>
+#include <lockfile.h>
 
 #include <lib-lua/lib-lua.h>
 #include <lib-sys/unix.h>
 #include <lib-mime/mime.h>
-#include <lib-ui/sidebar.h>
+#include <lib-ui/lib-ui.h>
 
 #include "mutt.h"
 #include "crypt.h"
 #include "pattern.h"
-#include "buffy.h"
 #include "mx.h"
 #include "mbox.h"
 #include "mh.h"
 #include "copy.h"
 #include "keymap.h"
 #include "compress.h"
-#include "dotlock.h"
+#include "score.h"
 
 #include <imap/imap.h>
-#include <pop/pop.h>
+#include "pop.h"
+
+#define MAXLOCKATTEMPT 5
 
-#ifdef USE_NNTP
-#include <nntp/nntp.h>
-#endif
 
 static mx_t const *mxfmts[] = {
     &mbox_mx,
@@ -44,75 +42,29 @@ static mx_t const *mxfmts[] = {
     &maildir_mx,
     &imap_mx,
     &pop_mx,
-#ifdef USE_NNTP
-    &nntp_mx,
-#endif
     &compress_mx,
 };
 
 #define MX_IDX(idx)          (idx >= 0 && idx < countof(mxfmts))
 #define mutt_is_spool(s)     (m_strcmp(Spoolfile, s) == 0)
 
-/* parameters: 
- * path - file to lock
- * retry - should retry if unable to lock?
- */
-static int invoke_dotlock (const char *path, int flags, int retry)
+static int dotlock_file(const char *path, int retry)
 {
-  char cmd[LONG_STRING + _POSIX_PATH_MAX];
-  char f[STRING + _POSIX_PATH_MAX];
-  char r[STRING];
-
-  if (flags & DL_FL_RETRY)
-    snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
-
-  mutt_quote_filename (f, sizeof (f), path);
-
-  snprintf(cmd, sizeof(cmd), "%s %s%s%s%s%s%s%s",
-           MCore.dotlock,
-           flags & DL_FL_TRY ? "-t " : "",
-           flags & DL_FL_UNLOCK ? "-u " : "",
-           flags & DL_FL_USEPRIV ? "-p " : "",
-           flags & DL_FL_FORCE ? "-f " : "",
-           flags & DL_FL_UNLINK ? "-d " : "",
-           flags & DL_FL_RETRY ? r : "", f);
-
-  return mutt_system (cmd);
-}
+    char lockfile[_POSIX_PATH_MAX];
+    snprintf(lockfile, sizeof(lockfile), "%s.lock", path);
 
-static int dotlock_file (const char *path, int retry)
-{
-  int r;
-  int flags = DL_FL_USEPRIV | DL_FL_RETRY;
-
-  if (retry)
-    retry = 1;
-
-retry_lock:
-  if ((r = invoke_dotlock (path, flags, retry)) == DL_EX_EXIST) {
-    if (!option (OPTNOCURSES)) {
-      char msg[LONG_STRING];
-
-      snprintf (msg, sizeof (msg),
-                _("Lock count exceeded, remove lock for %s?"), path);
-      if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
-        flags |= DL_FL_FORCE;
-        retry--;
-        mutt_clear_error ();
-        goto retry_lock;
-      }
-    }
-    else {
-      mutt_error (_("Can't dotlock %s.\n"), path);
+    if (lockfile_create(lockfile, retry ? 1 : 0, 0)) {
+        if (retry)
+            mutt_error (_("Can't dotlock %s.\n"), lockfile);
     }
-  }
-  return (r == DL_EX_OK ? 0 : -1);
+    return 0;
 }
 
 static int undotlock_file (const char *path)
 {
-  return (invoke_dotlock (path,  DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
-          DL_EX_OK ? 0 : -1);
+    char lockfile[_POSIX_PATH_MAX];
+    snprintf(lockfile, sizeof(lockfile), "%s.lock", path);
+    return lockfile_remove(lockfile);
 }
 
 /* looks up index of type for path in mxfmts */
@@ -137,138 +89,54 @@ static int mx_get_idx (const char* path) {
                 return (t-1);
         }
     }
-    return (-1);
+    return -1;
 }
 
 /* Args:
  *     excl            if excl != 0, request an exclusive lock
- *     dot             if dot != 0, try to dotlock the file
  *     time_out        should retry locking?
  */
-int mx_lock_file (const char *path, int fd, int excl, int dot, int time_out)
+int mx_lock_file(const char *path, int fd, int excl, int time_out)
 {
-#if defined (USE_FCNTL) || defined (USE_FLOCK)
-    int count;
-    int attempt;
-    struct stat prev_sb;
-#endif
-    int r = 0;
+    int count = 0, attempt = 0;
+    struct flock lck = {
+        .l_type   = excl ? F_WRLCK : F_RDLCK,
+        .l_whence = SEEK_SET,
+    };
 
-#ifdef USE_FCNTL
-    struct flock lck;
-
-
-    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;
+    if (dotlock_file(path, time_out) < 0)
+        return -1;
 
+    while (fcntl(fd, F_SETLK, &lck) == -1) {
         if (errno != EAGAIN && errno != EACCES) {
-            mutt_perror ("fcntl");
-            return (-1);
+            mutt_perror("fcntl");
+            goto error;
         }
 
-        if (fstat (fd, &sb) != 0)
-            sb.st_size = 0;
-
-        if (count == 0)
-            prev_sb = sb;
-
-        /* only unlock file if it is unchanged */
-        if (prev_sb.st_size == sb.st_size
-            && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
+        if (++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
             if (time_out)
                 mutt_error _("Timeout exceeded while attempting fcntl lock!");
-
-            return (-1);
+            goto error;
         }
-
-        prev_sb = sb;
-
-        mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
-        sleep (1);
+        mutt_message(_("Waiting for fcntl lock... %d"), ++attempt);
+        mutt_sleep(1);
     }
-#endif /* USE_FCNTL */
-
-#ifdef USE_FLOCK
-    count = 0;
-    attempt = 0;
-    while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
-        struct stat sb;
-
-        if (errno != EWOULDBLOCK) {
-            mutt_perror ("flock");
-            r = -1;
-            break;
-        }
-
-        if (fstat (fd, &sb) != 0)
-            sb.st_size = 0;
-
-        if (count == 0)
-            prev_sb = sb;
-
-        /* only unlock file if it is unchanged */
-        if (prev_sb.st_size == sb.st_size
-            && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
-            if (time_out)
-                mutt_error _("Timeout exceeded while attempting flock lock!");
-
-            r = -1;
-            break;
-        }
-
-        prev_sb = sb;
-
-        mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
-        sleep (1);
-    }
-#endif /* USE_FLOCK */
-
-    if (r == 0 && dot)
-        r = dotlock_file (path, time_out);
-
-    if (r == -1) {
-        /* release any other locks obtained in this routine */
-
-#ifdef USE_FCNTL
-        lck.l_type = F_UNLCK;
-        fcntl (fd, F_SETLK, &lck);
-#endif /* USE_FCNTL */
-
-#ifdef USE_FLOCK
-        flock (fd, LOCK_UN);
-#endif /* USE_FLOCK */
-
-        return (-1);
-    }
-
     return 0;
+
+  error:
+    undotlock_file(path);
+    return -1;
 }
 
-int mx_unlock_file (const char *path, int fd, int dot)
+int mx_unlock_file(const char *path, int fd)
 {
-#ifdef USE_FCNTL
     struct flock unlockit;
 
     p_clear(&unlockit, 1);
     unlockit.l_type = F_UNLCK;
     unlockit.l_whence = SEEK_SET;
-    fcntl (fd, F_SETLK, &unlockit);
-#endif
-
-#ifdef USE_FLOCK
-    flock (fd, LOCK_UN);
-#endif
-
-    if (dot)
-        undotlock_file (path);
-
+    fcntl(fd, F_SETLK, &unlockit);
+    undotlock_file(path);
     return 0;
 }
 
@@ -277,10 +145,10 @@ int mx_get_magic (const char *path) {
   int i = 0;
 
   if (m_strlen(path) == 0)
-    return (-1);
+    return -1;
   if ((i = mx_get_idx (path)) >= 0)
     return (mxfmts[i]->type);
-  return (-1);
+  return -1;
 }
 
 int mx_is_local (int m) {
@@ -289,23 +157,6 @@ int mx_is_local (int m) {
   return (mxfmts[m]->local);
 }
 
-/*
- * set DefaultMagic to the given value
- */
-int mx_set_magic (const char *s)
-{
-  if (ascii_strcasecmp (s, "mbox") == 0)
-    DefaultMagic = M_MBOX;
-  else if (ascii_strcasecmp (s, "mh") == 0)
-    DefaultMagic = M_MH;
-  else if (ascii_strcasecmp (s, "maildir") == 0)
-    DefaultMagic = M_MAILDIR;
-  else
-    return (-1);
-
-  return 0;
-}
-
 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
  *   We may be interested in using ACL-style flags at some point, currently
  *   we use the normal access() flags. */
@@ -340,90 +191,64 @@ static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
       mutt_error (_("%s is not a mailbox."), ctx->path);
       /* fall through */
     case -1:
-      return (-1);
+      return -1;
     }
-  }
-  else if (errno == ENOENT) {
-    ctx->magic = DefaultMagic;
-
-    if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
+  } else if (errno == ENOENT) {
+    int len = m_strlen(ctx->path);
+    if (len > 0 && ctx->path[len - 1] == '/') {
       char tmp[_POSIX_PATH_MAX];
 
       if (mkdir (ctx->path, S_IRWXU)) {
         mutt_perror (ctx->path);
-        return (-1);
+        return -1;
       }
 
-      if (ctx->magic == M_MAILDIR) {
-        snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
-        if (mkdir (tmp, S_IRWXU)) {
-          mutt_perror (tmp);
-          rmdir (ctx->path);
-          return (-1);
-        }
+      snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
+      if (mkdir (tmp, S_IRWXU)) {
+        mutt_perror (tmp);
+        rmdir (ctx->path);
+        return -1;
+      }
 
-        snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
-        if (mkdir (tmp, S_IRWXU)) {
-          mutt_perror (tmp);
-          snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
-          rmdir (tmp);
-          rmdir (ctx->path);
-          return (-1);
-        }
-        snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
-        if (mkdir (tmp, S_IRWXU)) {
-          mutt_perror (tmp);
-          snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
-          rmdir (tmp);
-          snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
-          rmdir (tmp);
-          rmdir (ctx->path);
-          return (-1);
-        }
+      snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
+      if (mkdir (tmp, S_IRWXU)) {
+        mutt_perror (tmp);
+        snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
+        rmdir (tmp);
+        rmdir (ctx->path);
+        return -1;
       }
-      else {
-        int i;
-
-        snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
-        if ((i = creat (tmp, S_IRWXU)) == -1) {
-          mutt_perror (tmp);
-          rmdir (ctx->path);
-          return (-1);
-        }
-        close (i);
+      snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
+      if (mkdir (tmp, S_IRWXU)) {
+        mutt_perror (tmp);
+        snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
+        rmdir (tmp);
+        snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
+        rmdir (tmp);
+        rmdir (ctx->path);
+        return -1;
       }
+      ctx->magic = M_MAILDIR;
+    } else {
+      ctx->magic = M_MBOX;
     }
-  }
-  else {
+  } else {
     mutt_perror (ctx->path);
-    return (-1);
+    return -1;
   }
 
-  switch (ctx->magic) {
-  case M_MBOX:
-    if ((ctx->fp =
-         safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
-        || mbox_lock_mailbox (ctx, 1, 1) != 0) {
-      if (!ctx->fp)
-        mutt_perror (ctx->path);
-      else {
-        mutt_error (_("Couldn't lock %s\n"), ctx->path);
-        m_fclose(&ctx->fp);
-      }
-      return (-1);
+  if (ctx->magic == M_MBOX) {
+    if (!(ctx->fp = safe_fopen(ctx->path, flags & M_NEWFOLDER ? "w" : "a"))) {
+      mutt_perror(ctx->path);
+      return -1;
     }
-    fseeko (ctx->fp, 0, 2);
-    break;
-
-  case M_MH:
-  case M_MAILDIR:
-    /* nothing to do */
-    break;
-
-  default:
-    return (-1);
+    if (mbox_lock_mailbox(ctx, 1, 1) != 0) {
+      mutt_error(_("Couldn't lock %s\n"), ctx->path);
+      m_fclose(&ctx->fp);
+      return -1;
+    }
+    fseeko(ctx->fp, 0, 2);
   }
-
   return 0;
 }
 
@@ -628,15 +453,6 @@ static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
 
   ctx->closing = 1;
 
-#ifdef USE_NNTP
-  if (ctx->magic == M_NNTP) {
-    int ret;
-
-    ret = nntp_close_mailbox (ctx);
-    mx_fastclose_mailbox (ctx);
-    return ret;
-  }
-#endif
   if (ctx->readonly || ctx->dontwrite) {
     /* mailbox is readonly or we don't want to write */
     mx_fastclose_mailbox (ctx);
@@ -659,23 +475,15 @@ static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
   }
 
   if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
-    char *p;
-
-    if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
-      isSpool = 1;
-      m_strcpy(mbox, sizeof(mbox), p);
-    }
-    else {
-      m_strcpy(mbox, sizeof(mbox), NONULL(Inbox));
-      isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
-    }
+    m_strcpy(mbox, sizeof(mbox), NONULL(Inbox));
+    isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
     mutt_expand_path (mbox, sizeof (mbox));
 
     if (isSpool) {
       snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
       if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
         ctx->closing = 0;
-        return (-1);
+        return -1;
       }
     }
   }
@@ -690,7 +498,7 @@ static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
               _("Purge %d deleted messages?"), ctx->deleted);
     if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
       ctx->closing = 0;
-      return (-1);
+      return -1;
     }
   }
 
@@ -799,7 +607,7 @@ static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
                   ctx->msgcount - ctx->deleted, ctx->deleted);
 
   if (ctx->cinfo && mutt_slow_close_compressed (ctx))
-    return (-1);
+    return -1;
 
   mx_fastclose_mailbox (ctx);
 
@@ -938,7 +746,7 @@ static int _mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
               ? _("Purge %d deleted message?") :
               _("Purge %d deleted messages?"), ctx->deleted);
     if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
-      return (-1);
+      return -1;
     else if (purge == M_NO) {
       if (!ctx->changed)
         return 0;               /* nothing to do! */
@@ -1041,7 +849,7 @@ MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
           p = hdr->env->from;
       }
 
-      fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL(MCore.username),
+      fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL(mod_core.username),
                ctime (&msg->received));
     }
   }
@@ -1063,7 +871,7 @@ int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) {
       return (mxfmts[ctx->magic-1]->mx_check_mailbox(ctx, index_hint, lock));
   }
 
-  return (-1);
+  return -1;
 }
 
 /* return a stream pointer for a message */
@@ -1110,15 +918,6 @@ MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
       break;
     }
 
-#ifdef USE_NNTP
-  case M_NNTP:
-    {
-      if (nntp_fetch_message (msg, ctx, msgno) != 0)
-        p_delete(&msg);
-      break;
-    }
-#endif /* USE_NNTP */
-
   default:
     p_delete(&msg);
     break;
@@ -1133,7 +932,7 @@ int mx_commit_message (MESSAGE * msg, CONTEXT * ctx) {
     return -1;
   }
   if (!ctx || !MX_IDX(ctx->magic-1) || !mxfmts[ctx->magic-1]->mx_commit_message)
-    return (-1);
+    return -1;
   return (mxfmts[ctx->magic-1]->mx_commit_message (msg, ctx));
 }
 
@@ -1143,12 +942,8 @@ int mx_close_message (MESSAGE ** msg)
   int r = 0;
 
   if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
-      || (*msg)->magic == M_IMAP
-      || (*msg)->magic == M_POP
-#ifdef USE_NNTP
-      || (*msg)->magic == M_NNTP
-#endif
-    ) {
+  ||  (*msg)->magic == M_IMAP || (*msg)->magic == M_POP)
+  {
     r = m_fclose(&(*msg)->fp);
   }
   else
@@ -1165,7 +960,7 @@ int mx_close_message (MESSAGE ** msg)
 
 void mx_alloc_memory (CONTEXT * ctx)
 {
-    ctx->hdrmax += 25;
+    ctx->hdrmax += 32;
 
     p_realloc(&ctx->hdrs, ctx->hdrmax);
     p_realloc(&ctx->v2r, ctx->hdrmax);
@@ -1206,7 +1001,7 @@ void mx_update_context (CONTEXT * ctx, int new_messages)
       /* p_delete(&h->env->supersedes); should I ? */
       if (h2) {
         h2->superseded = 1;
-        if (!ctx->counting && option (OPTSCORE))
+        if (!ctx->counting && mod_score.enable)
           mutt_score_message (ctx, h2, 1);
       }
     }
@@ -1218,7 +1013,7 @@ void mx_update_context (CONTEXT * ctx, int new_messages)
       if (ctx->subj_hash && h->env->real_subj)
         hash_insert (ctx->subj_hash, h->env->real_subj, h);
 
-      if (option (OPTSCORE))
+      if (mod_score.enable)
         mutt_score_message (ctx, h, 0);
     }
 
@@ -1250,7 +1045,7 @@ int mx_check_empty (const char *path)
   if ((i = mx_get_idx (path)) >= 0 && mxfmts[i]->mx_check_empty)
     return (mxfmts[i]->mx_check_empty(path));
   errno = EINVAL;
-  return (-1);
+  return -1;
 }
 
 int mx_acl_check(CONTEXT *ctx, int flag)