Fixed -mime for forw and repl.
[mmh] / uip / forw.c
index 13aaaa9..b0d4601 100644 (file)
@@ -85,7 +85,7 @@ static struct msgs *mp = NULL;  /* used a lot */
 */
 static void mhl_draft(int, char *, int, int, char *, char *, int);
 static void copy_draft(int, char *, char *, int, int, int);
-static void copy_mime_draft(int);
+static void add_forw_hdr(char *);
 static int build_form(char *, char *, int, int);
 
 
@@ -120,120 +120,131 @@ main(int argc, char **argv)
        while ((cp = *argp++)) {
                if (*cp == '-') {
                        switch (smatch(++cp, switches)) {
-                               case AMBIGSW:
-                                       ambigsw(cp, switches);
-                                       done(1);
-                               case UNKWNSW:
-                                       adios(NULL, "-%s unknown", cp);
-
-                               case HELPSW:
-                                       snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
-                                       print_help(buf, switches, 1);
-                                       done(1);
-                               case VERSIONSW:
-                                       print_version(invo_name);
-                                       done(1);
-
-                               case ANNOSW:
-                                       anot++;
-                                       continue;
-                               case NANNOSW:
-                                       anot = 0;
-                                       continue;
-
-                               case EDITRSW:
-                                       if (!(ed = *argp++) || *ed == '-')
-                                               adios(NULL, "missing argument to %s", argp[-2]);
-                                       nedit = 0;
-                                       continue;
-                               case NEDITSW:
-                                       nedit++;
-                                       continue;
-
-                               case WHATSW:
-                                       if (!(whatnowproc = *argp++) || *whatnowproc == '-')
-                                               adios(NULL, "missing argument to %s", argp[-2]);
-                                       nwhat = 0;
-                                       continue;
+                       case AMBIGSW:
+                               ambigsw(cp, switches);
+                               done(1);
+                       case UNKWNSW:
+                               adios(NULL, "-%s unknown", cp);
+
+                       case HELPSW:
+                               snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
+                               print_help(buf, switches, 1);
+                               done(1);
+                       case VERSIONSW:
+                               print_version(invo_name);
+                               done(1);
+
+                       case ANNOSW:
+                               anot++;
+                               continue;
+                       case NANNOSW:
+                               anot = 0;
+                               continue;
+
+                       case EDITRSW:
+                               if (!(ed = *argp++) || *ed == '-')
+                                       adios(NULL, "missing argument to %s",
+                                                       argp[-2]);
+                               nedit = 0;
+                               continue;
+                       case NEDITSW:
+                               nedit++;
+                               continue;
+
+                       case WHATSW:
+                               if (!(whatnowproc = *argp++) ||
+                                               *whatnowproc == '-')
+                                       adios(NULL, "missing argument to %s",
+                                                       argp[-2]);
+                               nwhat = 0;
+                               continue;
 #ifdef MHE
-                               case BILDSW:
-                                       buildsw++;  /* fall... */
+                       case BILDSW:
+                               buildsw++;  /* fall... */
 #endif /* MHE */
-                               case NWHATSW:
-                                       nwhat++;
-                                       continue;
-
-                               case FILESW:
-                                       if (file)
-                                               adios(NULL, "only one file at a time!");
-                                       if (!(cp = *argp++) || *cp == '-')
-                                               adios(NULL, "missing argument to %s", argp[-2]);
-                                       file = path(cp, TFILE);
-                                       continue;
-                               case FILTSW:
-                                       if (!(cp = *argp++) || *cp == '-')
-                                               adios(NULL, "missing argument to %s", argp[-2]);
-                                       filter = getcpy(etcpath(cp));
-                                       mime = 0;
-                                       continue;
-                               case FORMSW:
-                                       if (!(form = *argp++) || *form == '-')
-                                               adios(NULL, "missing argument to %s", argp[-2]);
-                                       continue;
-
-                               case FRMTSW:
-                                       filter = getcpy(etcpath(mhlforward));
-                                       continue;
-                               case NFRMTSW:
-                                       filter = NULL;
-                                       continue;
-
-                               case INPLSW:
-                                       inplace++;
-                                       continue;
-                               case NINPLSW:
-                                       inplace = 0;
-                                       continue;
-
-                               case MIMESW:
-                                       mime++;
-                                       filter = NULL;
-                                       continue;
-                               case NMIMESW:
-                                       mime = 0;
-                                       continue;
-
-                               case DGSTSW:
-                                       if (!(digest = *argp++) || *digest == '-')
-                                               adios(NULL, "missing argument to %s", argp[-2]);
-                                       mime = 0;
-                                       continue;
-                               case ISSUESW:
-                                       if (!(cp = *argp++) || *cp == '-')
-                                               adios(NULL, "missing argument to %s", argp[-2]);
-                                       if ((issue = atoi(cp)) < 1)
-                                               adios(NULL, "bad argument %s %s", argp[-2], cp);
-                                       continue;
-                               case VOLUMSW:
-                                       if (!(cp = *argp++) || *cp == '-')
-                                               adios(NULL, "missing argument to %s", argp[-2]);
-                                       if ((volume = atoi(cp)) < 1)
-                                               adios(NULL, "bad argument %s %s", argp[-2], cp);
-                                       continue;
-
-                               case BITSTUFFSW:
-                                       dashstuff = 1;  /* trinary logic */
-                                       continue;
-                               case NBITSTUFFSW:
-                                       dashstuff = -1;  /* trinary logic */
-                                       continue;
+                       case NWHATSW:
+                               nwhat++;
+                               continue;
+
+                       case FILESW:
+                               if (file)
+                                       adios(NULL, "only one file at a time!");
+                               if (!(cp = *argp++) || *cp == '-')
+                                       adios(NULL, "missing argument to %s",
+                                                       argp[-2]);
+                               file = getcpy(expanddir(cp));
+                               continue;
+                       case FILTSW:
+                               if (!(cp = *argp++) || *cp == '-')
+                                       adios(NULL, "missing argument to %s",
+                                                       argp[-2]);
+                               filter = getcpy(etcpath(cp));
+                               mime = 0;
+                               continue;
+                       case FORMSW:
+                               if (!(form = *argp++) || *form == '-')
+                                       adios(NULL, "missing argument to %s",
+                                                       argp[-2]);
+                               continue;
+
+                       case FRMTSW:
+                               filter = getcpy(etcpath(mhlforward));
+                               continue;
+                       case NFRMTSW:
+                               filter = NULL;
+                               continue;
+
+                       case INPLSW:
+                               inplace++;
+                               continue;
+                       case NINPLSW:
+                               inplace = 0;
+                               continue;
+
+                       case MIMESW:
+                               mime++;
+                               filter = NULL;
+                               continue;
+                       case NMIMESW:
+                               mime = 0;
+                               continue;
+
+                       case DGSTSW:
+                               if (!(digest = *argp++) || *digest == '-')
+                                       adios(NULL, "missing argument to %s",
+                                                       argp[-2]);
+                               mime = 0;
+                               continue;
+                       case ISSUESW:
+                               if (!(cp = *argp++) || *cp == '-')
+                                       adios(NULL, "missing argument to %s",
+                                                       argp[-2]);
+                               if ((issue = atoi(cp)) < 1)
+                                       adios(NULL, "bad argument %s %s",
+                                                       argp[-2], cp);
+                               continue;
+                       case VOLUMSW:
+                               if (!(cp = *argp++) || *cp == '-')
+                                       adios(NULL, "missing argument to %s",
+                                                       argp[-2]);
+                               if ((volume = atoi(cp)) < 1)
+                                       adios(NULL, "bad argument %s %s",
+                                                       argp[-2], cp);
+                               continue;
+
+                       case BITSTUFFSW:
+                               dashstuff = 1;  /* trinary logic */
+                               continue;
+                       case NBITSTUFFSW:
+                               dashstuff = -1;  /* trinary logic */
+                               continue;
                        }
                }
                if (*cp == '+' || *cp == '@') {
                        if (folder)
                                adios(NULL, "only one folder at a time!");
                        else
-                               folder = pluspath(cp);
+                               folder = getcpy(expandfol(cp));
                } else {
                        msgs[msgp++] = cp;
                }
@@ -241,16 +252,14 @@ main(int argc, char **argv)
 
        cwd = getcpy(pwd());
 
-       if (!context_find("path"))
-               free(path("./", TFOLDER));
        if (file && (msgp || folder))
                adios(NULL, "can't mix files and folders/msgs");
 
 #ifdef MHE
-       strncpy(drft, buildsw ? m_maildir("draft")
-               : m_draft("new"), sizeof(drft));
+       strncpy(drft, buildsw ? toabsdir("draft")
+               : m_draft(seq_beyond), sizeof(drft));
 #else
-       strncpy(drft, m_draft("new"), sizeof(drft));
+       strncpy(drft, m_draft(seq_beyond), sizeof(drft));
 #endif /* MHE */
 
        if (file) {
@@ -263,10 +272,10 @@ main(int argc, char **argv)
                ** Forwarding a message.
                */
                if (!msgp)
-                       msgs[msgp++] = "cur";
+                       msgs[msgp++] = seq_cur;
                if (!folder)
-                       folder = getfolder(1);
-               maildir = m_maildir(folder);
+                       folder = getcurfol();
+               maildir = toabsdir(folder);
 
                if (chdir(maildir) == NOTOK)
                        adios(maildir, "unable to change directory to");
@@ -332,15 +341,18 @@ main(int argc, char **argv)
                ** If filter file is defined, then format the
                ** messages into the draft using mhlproc.
                */
-               if (filter)
+               if (filter) {
                        mhl_draft(out, digest, volume, issue, drft, filter,
                                        dashstuff);
-               else if (mime)
-                       copy_mime_draft(out);
-               else
+                       close(out);
+               } else if (mime) {
+                       close(out);
+                       add_forw_hdr(drft);
+               } else {
                        copy_draft(out, digest, drft, volume, issue,
                                        dashstuff);
-               close(out);
+                       close(out);
+               }
 
                if (digest) {
                        snprintf(buf, sizeof(buf), IFORMAT, digest);
@@ -351,7 +363,7 @@ main(int argc, char **argv)
                        context_replace(buf, getcpy(value));
                }
 
-               context_replace(pfolder, folder);  /* update current folder */
+               context_replace(curfolder, folder); /* update current folder */
                seq_setcur(mp, mp->lowsel);  /* update current message */
                seq_save(mp);  /* synchronize sequences */
                context_save();  /* save the context file */
@@ -371,7 +383,6 @@ main(int argc, char **argv)
 ** draft calling the mhlproc, and reading its output
 ** from a pipe.
 */
-
 static void
 mhl_draft(int out, char *digest, int volume, int issue,
        char *file, char *filter, int dashstuff)
@@ -390,66 +401,67 @@ mhl_draft(int out, char *digest, int volume, int issue,
        for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
                sleep(5);
        switch (child_id) {
-               case NOTOK:
-                       adios("fork", "unable to");
+       case NOTOK:
+               adios("fork", "unable to");
 
-               case OK:
-                       close(pd[0]);
-                       dup2(pd[1], 1);
-                       close(pd[1]);
+       case OK:
+               close(pd[0]);
+               dup2(pd[1], 1);
+               close(pd[1]);
 
-                       i = 1;
-                       vec[i++] = "-forwall";
-                       vec[i++] = "-form";
-                       vec[i++] = filter;
+               i = 1;
+               vec[i++] = "-forwall";
+               vec[i++] = "-form";
+               vec[i++] = filter;
 
-                       if (digest) {
-                               vec[i++] = "-digest";
-                               vec[i++] = digest;
-                               vec[i++] = "-issue";
-                               snprintf(buf1, sizeof(buf1), "%d", issue);
-                               vec[i++] = buf1;
-                               vec[i++] = "-volume";
-                               snprintf(buf2, sizeof(buf2), "%d", volume);
-                               vec[i++] = buf2;
-                       }
+               if (digest) {
+                       vec[i++] = "-digest";
+                       vec[i++] = digest;
+                       vec[i++] = "-issue";
+                       snprintf(buf1, sizeof(buf1), "%d", issue);
+                       vec[i++] = buf1;
+                       vec[i++] = "-volume";
+                       snprintf(buf2, sizeof(buf2), "%d", volume);
+                       vec[i++] = buf2;
+               }
 
-                       /*
-                       ** Are we dashstuffing (quoting) the lines that begin
-                       ** with `-'.  We use the mhl default (don't add any
-                       ** flag) unless the user has specified a specific flag.
-                       */
-                       if (dashstuff > 0)
-                               vec[i++] = "-dashstuffing";
-                       else if (dashstuff < 0)
-                               vec[i++] = "-nodashstuffing";
+               /*
+               ** Are we dashstuffing (quoting) the lines that begin
+               ** with `-'.  We use the mhl default (don't add any
+               ** flag) unless the user has specified a specific flag.
+               */
+               if (dashstuff > 0)
+                       vec[i++] = "-dashstuffing";
+               else if (dashstuff < 0)
+                       vec[i++] = "-nodashstuffing";
 
-                       if (mp->numsel >= MAXARGS - i)
-                               adios(NULL, "more than %d messages for %s exec", MAXARGS - i, vec[0]);
+               if (mp->numsel >= MAXARGS - i)
+                       adios(NULL, "more than %d messages for %s exec",
+                                       MAXARGS - i, vec[0]);
 
-                       /*
-                       ** Now add the message names to filter.  We can only
-                       ** handle about 995 messages (because vec is fixed
-                       ** size), but that should be plenty.
-                       */
-                       for (msgnum = mp->lowsel;
-                                       msgnum <= mp->hghsel && i < sizeof(vec) - 1;
-                                       msgnum++)
-                               if (is_selected(mp, msgnum))
-                                       vec[i++] = getcpy(m_name(msgnum));
-                       vec[i] = NULL;
-
-                       execvp(mhlproc, vec);
-                       fprintf(stderr, "unable to exec ");
-                       perror(mhlproc);
-                       _exit(-1);
-
-               default:
-                       close(pd[1]);
-                       cpydata(pd[0], out, vec[0], file);
-                       close(pd[0]);
-                       pidXwait(child_id, mhlproc);
-                       break;
+               /*
+               ** Now add the message names to filter.  We can only
+               ** handle about 995 messages (because vec is fixed
+               ** size), but that should be plenty.
+               */
+               for (msgnum = mp->lowsel;
+                               msgnum <= mp->hghsel && i < sizeof(vec) - 1;
+                               msgnum++)
+                       if (is_selected(mp, msgnum))
+                               vec[i++] = getcpy(m_name(msgnum));
+               vec[i] = NULL;
+
+               execvp(mhlproc, vec);
+               fprintf(stderr, "unable to exec ");
+               perror(mhlproc);
+               _exit(-1);
+
+       default:
+               close(pd[1]);
+               cpydata(pd[0], out, vec[0], file);
+               close(pd[0]);
+               pidXwait(child_id, mhlproc);
+               break;
        }
 }
 
@@ -459,7 +471,6 @@ mhl_draft(int out, char *digest, int volume, int issue,
 ** not filtered through the mhlproc.  Do dashstuffing if
 ** necessary.
 */
-
 static void
 copy_draft(int out, char *digest, char *file, int volume, int issue,
                int dashstuff)
@@ -542,25 +553,25 @@ copy_draft(int out, char *digest, char *file, int volume, int issue,
 
 
 /*
-** Create a mhbuild composition file for forwarding message.
+** Create an attachment header for the to be forward messages.
 */
-
 static void
-copy_mime_draft(int out)
+add_forw_hdr(char *draft)
 {
        int msgnum;
        char buffer[BUFSIZ];
 
-       snprintf(buffer, sizeof(buffer), "#forw [forwarded message%s] +%s",
-                       mp->numsel == 1 ? "" : "s", mp->foldpath);
-       write(out, buffer, strlen(buffer));
-       for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
-               if (is_selected(mp, msgnum)) {
-                       snprintf(buffer, sizeof(buffer), " %s",
-                                       m_name(msgnum));
-                       write(out, buffer, strlen(buffer));
+       snprintf(buffer, sizeof(buffer), "+%s", mp->foldpath);
+       for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
+               if (!is_selected(mp, msgnum)) {
+                       continue;
                }
-       write(out, "\n", 1);
+               /* TODO: improve the code */
+               strncat(buffer, " ", sizeof(buffer)-strlen(buffer)-1);
+               strncat(buffer, m_name(msgnum),
+                               sizeof(buffer)-strlen(buffer)-1);
+       }
+       annotate(draft, attach_hdr, buffer, 1, 0, -2, 1);
 }