* uip/mhlistsbr.c, uip/mhlsbr.c, uip/picksbr.c: cast
[mmh] / uip / inc.c
index 7475d5e..fce9cec 100644 (file)
--- a/uip/inc.c
+++ b/uip/inc.c
@@ -3,6 +3,10 @@
  * inc.c -- incorporate messages from a maildrop into 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.
  */
 
 #ifdef MAILGROUP
@@ -25,6 +29,7 @@
 #endif
 
 #include <h/mh.h>
+#include <h/utils.h>
 #include <fcntl.h>
 
 #ifdef POP
@@ -127,11 +132,11 @@ static struct swit switches[] = {
     { "sasl", SASLminc(-4) },
 #define SASLMECHSW                25
     { "saslmech", SASLminc(-8) },
+#define PROXYSW                   26
+    { "proxy command", POPminc(-5) },
     { NULL, 0 }
 };
 
-extern int errno;
-
 /*
  * flags for the mail source
  */
@@ -218,8 +223,8 @@ static FILE *in;
  */
 char *map_name(char *);
 
+static void inc_done(int) NORETURN;
 #ifdef POP
-int done(int);
 static int pop_action(char *);
 static int pop_pack(char *);
 static int map_count(void);
@@ -235,12 +240,14 @@ main (int argc, char **argv)
     int kpop = 0, sasl = 0;
     char *cp, *maildir, *folder = NULL;
     char *format = NULL, *form = NULL;
-    char *host = NULL, *user = NULL;
+    char *host = NULL, *user = NULL, *proxy = NULL;
     char *audfile = NULL, *from = NULL, *saslmech = NULL;
     char buf[BUFSIZ], **argp, *nfs, **arguments;
     struct msgs *mp;
     struct stat st, s1;
     FILE *aud = NULL;
+    char       b[MAXPATHLEN + 1];
+    char       *maildir_copy;          /* copy of mail directory because the static gets overwritten */
 
 #ifdef POP
     int nmsgs, nbytes, p = 0;
@@ -256,6 +263,8 @@ main (int argc, char **argv)
     struct hes_postoffice *po;
 #endif
 
+    done=inc_done;
+
 /* absolutely the first thing we do is save our privileges,
  * and drop them if we can.
  */
@@ -440,13 +449,17 @@ main (int argc, char **argv)
                if (!(saslmech = *argp++) || *saslmech == '-')
                    adios (NULL, "missing argument to %s", argp[-2]);
                continue;
+           case PROXYSW:
+               if (!(proxy = *argp++) || *proxy == '-')
+                   adios (NULL, "missing argument to %s", argp[-2]);
+               continue;
            }
        }
        if (*cp == '+' || *cp == '@') {
            if (folder)
                adios (NULL, "only one folder at a time!");
            else
-               folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
+               folder = pluspath (cp);
        } else {
            adios (NULL, "usage: %s [+folder] [switches]", invo_name);
        }
@@ -496,7 +509,7 @@ main (int argc, char **argv)
        /*
         * initialize POP connection
         */
-       if (pop_init (host, user, pass, snoop, kpop ? 1 : rpop, kpop,
+       if (pop_init (host, user, pass, proxy, snoop, kpop ? 1 : rpop, kpop,
                      sasl, saslmech) == NOTOK)
            adios (NULL, "%s", response);
 
@@ -530,6 +543,11 @@ main (int argc, char **argv)
        }
        if (stat (newmail, &s1) == NOTOK || s1.st_size == 0)
            adios (NULL, "no mail to incorporate");
+
+       if ((cp = strdup(newmail)) == (char *)0)
+           adios (NULL, "error allocating memory to copy newmail");
+
+       newmail = cp;
     }
 
 #ifdef POP
@@ -544,15 +562,17 @@ main (int argc, char **argv)
        folder = getfolder (0);
     maildir = m_maildir (folder);
 
-    if (stat (maildir, &st) == NOTOK) {
-       if (errno != ENOENT)
-           adios (maildir, "error on folder");
-       cp = concat ("Create folder \"", maildir, "\"? ", NULL);
-       if (noisy && !getanswer (cp))
-           done (1);
-       free (cp);
-       if (!makedir (maildir))
-           adios (NULL, "unable to create folder %s", maildir);
+    if ((maildir_copy = strdup(maildir)) == (char *)0)
+        adios (maildir, "error allocating memory to copy maildir");
+
+    if (!folder_exists(maildir)) {
+        /* If the folder doesn't exist, and we're given the -silent flag,
+         * just fail.
+         */
+        if (noisy)
+            create_folder(maildir, 0, done);
+        else
+            done (1);
     }
 
     if (chdir (maildir) == NOTOK)
@@ -798,7 +818,6 @@ go_to_it:
            /* link message into folder */
            newmsg = folder_addmsg(mp, tmpfilenam);
 #endif
-
            /* create scanline for new message */
            switch (i = scan (in, msgnum + 1, msgnum + 1, nfs, width,
                              msgnum == hghnum && chgflag, 1, NULL, 0L, noisy)) {
@@ -822,6 +841,13 @@ go_to_it:
 
            case SCNMSG:
            case SCNENC:
+               /*
+                *  Run the external program hook on the message.
+                */
+
+               (void)snprintf(b, sizeof (b), "%s/%d", maildir_copy, msgnum + 1);
+               (void)ext_hook("add-hook", b, (char *)0);
+
                if (aud)
                    fputs (scanl, aud);
 #ifdef MHE
@@ -833,6 +859,9 @@ go_to_it:
 
                msgnum++;
                mp->hghmsg++;
+               mp->nummsg++;
+               if (mp->lowmsg == 0) mp->lowmsg = 1;
+
                clear_msg_flags (mp, msgnum);
                set_exists (mp, msgnum);
                set_unseen (mp, msgnum);
@@ -923,7 +952,8 @@ go_to_it:
     seq_setunseen (mp, 0);     /* set the Unseen-Sequence */
     seq_save (mp);             /* synchronize sequences   */
     context_save ();           /* save the context file   */
-    return done (0);
+    done (0);
+    return 1;
 }
 
 
@@ -960,8 +990,8 @@ cpymsg (FILE *in, FILE *out)
 #endif /* if 0 */
 
 
-int
-done (int status)
+static void
+inc_done (int status)
 {
 #ifdef POP
     if (packfile && pd != NOTOK)
@@ -974,7 +1004,6 @@ done (int status)
         DROPGROUPPRIVS();
     }
     exit (status);
-    return 1;  /* dead code to satisfy the compiler */
 }
 
 #ifdef POP