]> diplodocus.org Git - nmh/blobdiff - sbr/folder_delmsgs.c
Rearranged mhn.defaults.sh a bit: consolidated web-browser specific
[nmh] / sbr / folder_delmsgs.c
index 3d2e5497a68b45381965b3dbe66b7d524bef6666..ebbca2d48cfef6ab8ec5ef7fbb75e8d3e2aba906 100644 (file)
@@ -2,14 +2,13 @@
 /*
  * folder_delmsgs.c -- "remove" SELECTED messages from a folder
  *
- * $Id$
- *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
  * complete copyright information.
  */
 
 #include <h/mh.h>
+#include <h/utils.h>
 
 /*
  * 1) If we are using an external rmmproc, then exec it.
  */
 
 int
-folder_delmsgs (struct msgs *mp, int unlink_msgs)
+folder_delmsgs (struct msgs *mp, int unlink_msgs, int nohook)
 {
     pid_t pid;
     int msgnum, vecp, retval = 0;
-    char buf[100], *dp, **vec;
+    char buf[100], *dp, **vec, *prog;
+    char       msgpath[BUFSIZ];
 
     /*
      * If "rmmproc" is defined, exec it to remove messages.
@@ -41,13 +41,27 @@ folder_delmsgs (struct msgs *mp, int unlink_msgs)
        /* Mark that the sequence information has changed */
        mp->msgflags |= SEQMOD;
 
-       if (mp->numsel > MAXARGS - 2)
-           adios (NULL, "more than %d messages for %s exec", MAXARGS - 2,
-                  rmmproc);
-       vec = (char **) calloc ((size_t) (mp->numsel + 2), sizeof(*vec));
+       /*
+        * Write out the sequence and context files; this will release
+        * any locks before the rmmproc is called.
+        */
+
+       seq_save (mp);
+       context_save ();
+
+       vec = argsplit(rmmproc, &prog, &vecp);
+
+       /*
+        * argsplit allocates a MAXARGS vector by default,  If we need
+        * something bigger, allocate it ourselves
+        */
+
+       if (mp->numsel + vecp + 1 > MAXARGS)
+           vec = (char **) mh_xrealloc (vec,
+                                         (size_t) ((mp->numsel + vecp + 1) *
+                                                    sizeof(*vec)));
        if (vec == NULL)
            adios (NULL, "unable to allocate exec vector");
-       vecp = 1;
        for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
            if (is_selected (mp, msgnum) &&
                !(vec[vecp++] = strdup (m_name (msgnum))))
@@ -56,20 +70,20 @@ folder_delmsgs (struct msgs *mp, int unlink_msgs)
        vec[vecp] = NULL;
 
        fflush (stdout);
-       vec[0] = r1bindex (rmmproc, '/');
 
-       switch (pid = vfork()) {
+       switch (pid = fork()) {
        case -1:
            advise ("fork", "unable to");
            return -1;
 
        case 0:
-           execvp (rmmproc, vec);
+           execvp (prog, vec);
            fprintf (stderr, "unable to exec ");
            perror (rmmproc);
            _exit (-1);
 
        default:
+           arglist_free(prog, vec);
            return (pidwait (pid, -1));
        }
     }
@@ -83,11 +97,24 @@ folder_delmsgs (struct msgs *mp, int unlink_msgs)
            unset_selected (mp, msgnum);
            mp->numsel--;
 
+           /*
+            *  Run the external hook on the message if one was specified in the context.
+            *  All we have is the message number; we have changed to the directory
+            *  containing the message.  So, we need to extract that directory to form
+            *  the complete path.  Note that the caller knows the directory, but has
+            *  no way of passing that to us.
+            */
+
+           if (!nohook) {
+                   (void)snprintf(msgpath, sizeof (msgpath), "%s/%d", mp->foldpath, msgnum);
+                   (void)ext_hook("del-hook", msgpath, (char *)0);
+               }
+
            dp = m_name (msgnum);
 
            if (unlink_msgs) {
                /* just unlink the messages */
-               if (unlink (dp) == -1) {
+               if (m_unlink (dp) == -1) {
                    admonish (dp, "unable to unlink");
                    retval = -1;
                    continue;
@@ -115,5 +142,12 @@ folder_delmsgs (struct msgs *mp, int unlink_msgs)
     /* Mark that the sequence information has changed */
     mp->msgflags |= SEQMOD;
 
+    /*
+     * Write out sequence and context files
+     */
+
+    seq_save (mp);
+    context_save ();
+
     return retval;
 }