]>
diplodocus.org Git - nmh/blob - uip/whatnowsbr.c
07b13e77306631da0b7628605ebc887dcd6a423a
1 /* whatnowsbr.c -- the WhatNow shell
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
7 * Several options have been added to ease the inclusion of attachments
8 * using the header field name mechanism added to anno and send. The
9 * -attach option is used to specify the header field name for attachments.
11 * Several commands have been added at the whatnow prompt:
13 * cd [ directory ] This option works just like the shell's
14 * cd command and lets the user change the
15 * directory from which attachments are
16 * taken so that long path names are not
17 * needed with every file.
19 * ls [ ls-options ] This option works just like the normal
20 * ls command and exists to allow the user
21 * to verify file names in the directory.
23 * pwd This option works just like the normal
24 * pwd command and exists to allow the user
25 * to verify the directory.
27 * attach [-v] files This option attaches the named files to
28 * the draft. -v displays the mhbuild
29 * directive that send(1) will use.
31 * alist [-ln] This option lists the attachments on the
32 * draft. -l gets long listings, -n gets
35 * detach files This option removes attachments from the
36 * detach -n numbers draft. This can be done by file name or
37 * by attachment number.
41 #include "sbr/ambigsw.h"
44 #include "sbr/print_version.h"
45 #include "sbr/print_help.h"
46 #include "sbr/arglist.h"
47 #include "sbr/error.h"
55 #include "sbr/m_maildir.h"
56 #include "sbr/m_mktemp.h"
57 #include "sbr/mime_type.h"
59 #define WHATNOW_SWITCHES \
60 X("draftfolder +folder", 0, DFOLDSW) \
61 X("draftmessage msg", 0, DMSGSW) \
62 X("nodraftfolder", 0, NDFLDSW) \
63 X("editor editor", 0, EDITRSW) \
64 X("noedit", 0, NEDITSW) \
65 X("prompt string", 4, PRMPTSW) \
66 X("version", 0, VERSIONSW) \
67 X("help", 0, HELPSW) \
70 #define X(sw, minchars, id) id,
71 DEFINE_SWITCH_ENUM(WHATNOW
);
74 #define X(sw, minchars, id) { sw, minchars, id },
75 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
79 * Options at the "whatnow" prompt
81 #define PROMPT_SWITCHES \
82 X("edit [<editor> <switches>]", 0, EDITSW) \
83 X("refile [<switches>] +folder", 0, REFILEOPT) \
84 X("mime [<switches>]", 0, BUILDMIMESW) \
85 X("display [<switches>]", 0, DISPSW) \
86 X("list [<switches>]", 0, LISTSW) \
87 X("send [<switches>]", 0, SENDSW) \
88 X("push [<switches>]", 0, PUSHSW) \
89 X("whom [<switches>]", 0, WHOMSW) \
90 X("quit [-delete]", 0, QUITSW) \
91 X("delete", 0, DELETESW) \
92 X("cd [directory]", 0, CDCMDSW) \
93 X("pwd", 0, PWDCMDSW) \
95 X("attach [-v]", 0, ATTACHCMDSW) \
96 X("detach [-n]", 0, DETACHCMDSW) \
97 X("alist [-ln] ", 2, ALISTCMDSW) \
99 #define X(sw, minchars, id) id,
100 DEFINE_SWITCH_ENUM(PROMPT
);
103 #define X(sw, minchars, id) { sw, minchars, id },
104 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
107 static char *myprompt
= "\nWhat now? ";
112 static int editfile (char **, char **, char *, int, struct msgs
*,
113 char *, char *, int, int);
114 static int sendfile (char **, char *, int);
115 static void sendit (char *, char **, char *, int);
116 static int buildfile (char **, char *);
117 static int whomfile (char **, char *);
118 static int removefile (char *);
119 static int checkmimeheader (char *);
120 static void writelscmd(char *, int, char *, char **);
121 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
122 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
123 static int system_in_dir(const char *dir
, const char *cmd
);
124 static int copyf (char *, char *);
128 WhatNow (int argc
, char **argv
)
132 int use
= 0, atfile
= 1;
133 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
134 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
136 char **argp
, **arguments
;
138 char cwd
[PATH_MAX
+ 1]; /* current working directory */
139 char file
[PATH_MAX
+ 1]; /* file name buffer */
140 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
141 FILE *f
; /* read pointer for bgnd proc */
142 char *l
; /* set on -l to alist command */
143 int n
; /* set on -n to alist command */
145 /* Need this if called from what_now(). */
146 invo_name
= r1bindex (argv
[0], '/');
148 arguments
= getarguments (invo_name
, argc
, argv
, 1);
152 * Get the initial current working directory.
155 if (getcwd(cwd
, sizeof (cwd
)) == NULL
) {
156 adios("getcwd", "could not get working directory");
159 while ((cp
= *argp
++)) {
161 switch (smatch (++cp
, whatnowswitches
)) {
163 ambigsw (cp
, whatnowswitches
);
166 die("-%s unknown", cp
);
169 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
170 print_help (buf
, whatnowswitches
, 1);
173 print_version(invo_name
);
178 die("only one draft folder at a time!");
179 if (!(cp
= *argp
++) || *cp
== '-')
180 die("missing argument to %s", argp
[-2]);
181 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
182 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
186 die("only one draft message at a time!");
187 if (!(dmsg
= *argp
++) || *dmsg
== '-')
188 die("missing argument to %s", argp
[-2]);
196 if (!(ed
= *argp
++) || *ed
== '-')
197 die("missing argument to %s", argp
[-2]);
205 if (!(myprompt
= *argp
++) || *myprompt
== '-')
206 die("missing argument to %s", argp
[-2]);
211 die("only one draft at a time!");
215 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
216 drft
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
218 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? mh_xstrdup(cp
) : NULL
;
220 if ((cp
= getenv ("mhatfile")) && *cp
)
223 if ((cp
= getenv ("mhuse")) && *cp
)
226 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
231 /* start editing the draft, unless -noedit was given */
232 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
233 NULL
, 1, atfile
) < 0)
237 #ifdef READLINE_SUPPORT
238 if (!(argp
= read_switch_multiword_via_readline(myprompt
, aleqs
))) {
239 #else /* ! READLINE_SUPPORT */
240 if (!(argp
= read_switch_multiword(myprompt
, aleqs
))) {
241 #endif /* READLINE_SUPPORT */
242 (void) m_unlink (LINK
);
245 switch (smatch (*argp
, aleqs
)) {
247 /* display the message being replied to, or distributed */
249 showfile (++argp
, msgnam
);
251 inform("no alternate message to display");
255 /* Translate MIME composition file */
256 buildfile (++argp
, drft
);
260 /* Call an editor on the draft file */
263 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
264 NULL
, 1, atfile
) == NOTOK
)
269 /* display the draft file */
270 showfile (++argp
, drft
);
274 /* Check to whom the draft would be sent */
275 whomfile (++argp
, drft
);
279 /* Quit, and possibly delete the draft */
280 if (*++argp
&& (*argp
[0] == 'd' ||
281 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
284 if (stat (drft
, &st
) != NOTOK
)
285 inform("draft left on %s", drft
);
290 /* Delete draft and exit */
295 /* Send draft in background */
296 if (sendfile (++argp
, drft
, 1))
302 sendfile (++argp
, drft
, 0);
306 /* Refile the draft */
307 if (refile (++argp
, drft
) == 0)
312 /* Change the working directory for attachments
314 * Run the directory through the user's shell so that
315 * we can take advantage of any syntax that the user
316 * is accustomed to. Read back the absolute path.
319 if (*(argp
+1) == NULL
) {
320 strcpy(buf
, "$SHELL -c \"cd&&pwd\"");
323 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
325 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
326 if (fgets(cwd
, sizeof (cwd
), f
) == NULL
) {
327 advise (buf
, "fgets");
329 trim_suffix_c(cwd
, '\n');
333 advise("popen", "could not get directory");
339 /* Print the working directory for attachments */
344 /* List files in the current attachment working directory
346 * Use the user's shell so that we can take advantage of any
347 * syntax that the user is accustomed to.
349 writelscmd(buf
, sizeof(buf
), "", argp
);
350 (void)system_in_dir(cwd
, buf
);
355 * List attachments on current draft. Options are:
357 * -l long listing (full path names)
361 if (checkmimeheader(drft
))
367 while (*++argp
!= NULL
) {
368 if (strcmp(*argp
, "-l") == 0)
371 else if (strcmp(*argp
, "-n") == 0)
374 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
386 inform("usage is alist [-ln].");
389 annolist(drft
, ATTACH_FIELD
, l
, n
);
395 * Attach files to current draft.
398 bool verbose
= false;
401 if (checkmimeheader(drft
))
404 for (ap
= argp
+1; *ap
; ++ap
) {
405 if (strcmp(*ap
, "-v") == 0) {
408 } else if (*ap
[0] != '-') {
413 if (*(argp
+1) == NULL
) {
414 inform("attach command requires file argument(s).");
419 * Build a command line that causes the user's shell to list the file name
420 * arguments. This handles and wildcard expansion, tilde expansion, etc.
422 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
425 * Read back the response from the shell, which contains a number of lines
426 * with one file name per line. Remove off the newline. Determine whether
427 * we have an absolute or relative path name. Prepend the current working
428 * directory to relative path names. Add the attachment annotation to the
432 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
433 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
436 trim_suffix_c(shell
, '\n');
439 strncpy(file
, shell
, sizeof(file
));
440 file
[sizeof(file
) - 1] = '\0';
442 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
445 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
447 ctype
= mime_type(file
);
448 printf ("Attaching %s as a %s\n", file
, ctype
);
456 advise("popen", "could not get file from shell");
463 * Detach files from current draft.
467 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
468 * so this catches a -n anywhere in the argument list.
471 if (checkmimeheader(drft
))
474 for (n
= 0, arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
475 if (strcmp(*arguments
, "-n") == 0) {
482 * A -n was found so interpret the arguments as attachment numbers.
483 * Decrement any remaining argument number that is greater than the one
484 * just processed after processing each one so that the numbering stays
489 for (arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
490 if (strcmp(*arguments
, "-n") == 0)
493 if (**arguments
!= '\0') {
494 n
= atoi(*arguments
);
495 annotate(drft
, ATTACH_FIELD
, NULL
, 1, 0, n
, 1);
497 for (argp
= arguments
+ 1; *argp
!= NULL
; argp
++) {
498 if (atoi(*argp
) > n
) {
499 if (atoi(*argp
) == 1)
502 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
510 * The arguments are interpreted as file names. Run them through the
511 * user's shell for wildcard expansion and other goodies. Do this from
512 * the current working directory if the argument is not an absolute path
513 * name (does not begin with a /).
515 * We feed all the file names to the shell at once, otherwise you can't
516 * provide a file name with a space in it.
518 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
519 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
520 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
521 trim_suffix_c(shell
, '\n');
522 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
526 advise("popen", "could not get file from shell");
532 /* Unknown command */
542 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
544 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
547 /* Note that we do not quote -- the argp from the user
548 * is assumed to be quoted as they desire. (We can't treat
549 * it as pure literal as that would prevent them using ~,
550 * wildcards, etc.) The buffer produced by this function
551 * should be given to popen_in_dir() or system_in_dir() so
552 * that the current working directory is set correctly.
554 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
555 /* NB that some snprintf() return -1 on overflow rather than the
556 * new C99 mandated 'number of chars that would have been written'
558 /* length checks here and inside the loop allow for the
559 * trailing "&&", trailcmd, '"' and NUL
561 int trailln
= strlen(trailcmd
) + 4;
562 if (ln
< 0 || ln
+ trailln
> bufsz
)
563 die("arguments too long");
567 while (*argp
&& *++argp
) {
569 /* +1 for leading space */
570 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
571 die("arguments too long");
573 memcpy(cp
, *argp
, ln
+1);
577 *cp
++ = '&'; *cp
++ = '&';
578 strcpy(cp
, trailcmd
);
586 * Build a command line that causes the user's shell to list the file name
587 * arguments. This handles and wildcard expansion, tilde expansion, etc.
590 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
592 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
593 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
597 /* Like system(), but run the command in directory dir.
598 * This assumes the program is single-threaded!
601 system_in_dir(const char *dir
, const char *cmd
)
606 /* ensure that $SHELL exists, as the cmd was written relying on
607 a non-blank $SHELL... */
608 setenv("SHELL","/bin/sh",0); /* don't overwrite */
610 if (getcwd(olddir
, sizeof(olddir
)) == 0)
611 adios("getcwd", "could not get working directory");
613 adios("chdir", "could not change working directory");
615 if (chdir(olddir
) != 0)
616 adios("chdir", "could not change working directory");
620 /* ditto for popen() */
622 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
627 /* ensure that $SHELL exists, as the cmd was written relying on
628 a non-blank $SHELL... */
629 setenv("SHELL","/bin/sh",0); /* don't overwrite */
631 if (getcwd(olddir
, sizeof(olddir
)) == 0)
632 adios("getcwd", "could not get working directory");
634 adios("chdir", "could not change working directory");
635 f
= popen(cmd
, type
);
636 if (chdir(olddir
) != 0)
637 adios("chdir", "could not change working directory");
646 static bool reedit
; /* have we been here before? */
647 static char *edsave
= NULL
; /* the editor we used previously */
651 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
652 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
654 int pid
, status
, vecp
;
655 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
656 char *cp
, *prog
, **vec
;
659 bool slinked
= false;
661 /* Was there a previous edit session? */
662 if (reedit
&& (*ed
|| edsave
)) {
663 if (!*ed
) { /* no explicit editor */
664 *ed
= edsave
; /* so use the previous one */
665 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
668 /* unless we've specified it via "editor-next" */
669 cp
= concat (cp
, "-next", NULL
);
670 if ((cp
= context_find (cp
)) != NULL
)
674 /* set initial editor */
676 *ed
= get_default_editor();
680 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
681 strncpy (altpath
, altmsg
, sizeof(altpath
));
683 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
685 strncpy (linkpath
, LINK
, sizeof(linkpath
));
687 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
690 (void) m_unlink (linkpath
);
691 if (link (altpath
, linkpath
) == NOTOK
) {
692 if (symlink (altpath
, linkpath
) < 0) {
693 adios (linkpath
, "symlink");
702 context_save (); /* save the context file */
705 switch (pid
= fork()) {
707 advise ("fork", "unable to");
713 if (chdir (cwd
) < 0) {
714 advise (cwd
, "chdir");
719 setenv("mhfolder", mp
->foldpath
, 1);
720 setenv("editalt", altpath
, 1);
723 vec
= argsplit(*ed
, &prog
, &vecp
);
727 vec
[vecp
++] = *arg
++;
732 fprintf (stderr
, "unable to exec ");
737 if ((status
= pidwait (pid
, NOTOK
))) {
738 if (((status
& 0xff00) != 0xff00)
739 && (!reedit
|| (status
& 0x00ff))) {
740 if (!use
&& (status
& 0xff00) &&
741 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
742 inform("problems with edit--draft left in %s", cp
);
744 inform("problems with edit--%s preserved", file
);
747 status
= -2; /* maybe "reedit ? -2 : -1"? */
756 ? lstat (linkpath
, &st
) != NOTOK
757 && S_ISREG(st
.st_mode
)
758 && copyf (linkpath
, altpath
) == NOTOK
759 : stat (linkpath
, &st
) != NOTOK
761 && (m_unlink (altpath
) == NOTOK
762 || link (linkpath
, altpath
) == NOTOK
)))
763 advise (linkpath
, "unable to update %s from", altmsg
);
766 /* normally, we remember which editor we used */
768 edsave
= getcpy (*ed
);
771 if (altmsg
&& atfile
)
772 (void) m_unlink (linkpath
);
779 copyf (char *ifile
, char *ofile
)
784 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
786 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
787 admonish (ofile
, "unable to open and truncate");
792 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
793 if (write (out
, buffer
, i
) != i
) {
794 advise (ofile
, "may have damaged");
810 sendfile (char **arg
, char *file
, int pushsw
)
814 char *cp
, *sp
, **vec
, *program
;
817 * If the sendproc is the nmh command `send', then we call
818 * those routines directly rather than exec'ing the command.
820 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
822 sendit (invo_name
= sp
, arg
, file
, pushsw
);
827 context_save (); /* save the context file */
833 adios("fork", "failed:");
836 vec
= argsplit(sendproc
, &program
, &vecp
);
838 vec
[vecp
++] = "-push";
841 vec
[vecp
++] = *arg
++;
845 execvp (program
, vec
);
846 fprintf (stderr
, "unable to exec ");
851 if (pidwait(child_id
, OK
) == 0)
859 * Translate MIME composition file (call buildmimeproc)
863 buildfile (char **argp
, char *file
)
870 /* allocate space for arguments */
876 args
= mh_xmalloc((i
+ 2) * sizeof(char *));
879 * For backward compatibility, we need to add -build
880 * if we are using mhn as buildmimeproc
883 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
884 args
[i
++] = "-build";
886 /* copy any other arguments */
887 while (argp
&& *argp
)
891 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
894 return i
? NOTOK
: OK
;
899 # define SASLminc(a) (a)
900 #else /* CYRUS_SASL */
901 # define SASLminc(a) 0
902 #endif /* CYRUS_SASL */
905 # define TLSminc(a) (a)
906 #else /* TLS_SUPPORT */
907 # define TLSminc(a) 0
908 #endif /* TLS_SUPPORT */
910 #define SEND_SWITCHES \
911 X("alias aliasfile", 0, ALIASW) \
912 X("debug", -5, DEBUGSW) \
913 X("filter filterfile", 0, FILTSW) \
914 X("nofilter", 0, NFILTSW) \
915 X("format", 0, FRMTSW) \
916 X("noformat", 0, NFRMTSW) \
917 X("forward", 0, FORWSW) \
918 X("noforward", 0, NFORWSW) \
919 X("mime", 0, MIMESW) \
920 X("nomime", 0, NMIMESW) \
921 X("msgid", 0, MSGDSW) \
922 X("nomsgid", 0, NMSGDSW) \
923 X("push", 0, SPSHSW) \
924 X("nopush", 0, NSPSHSW) \
925 X("split seconds", 0, SPLITSW) \
926 X("unique", -6, UNIQSW) \
927 X("nounique", -8, NUNIQSW) \
928 X("verbose", 0, VERBSW) \
929 X("noverbose", 0, NVERBSW) \
930 X("watch", 0, WATCSW) \
931 X("nowatch", 0, NWATCSW) \
932 X("width columns", 0, WIDTHSW) \
933 X("version", 0, SVERSIONSW) \
934 X("help", 0, SHELPSW) \
935 X("dashstuffing", -12, BITSTUFFSW) \
936 X("nodashstuffing", -14, NBITSTUFFSW) \
937 X("client host", -6, CLIESW) \
938 X("server host", 6, SERVSW) \
939 X("snoop", -5, SNOOPSW) \
940 X("draftfolder +folder", 0, SDRFSW) \
941 X("draftmessage msg", 0, SDRMSW) \
942 X("nodraftfolder", 0, SNDRFSW) \
943 X("sasl", SASLminc(4), SASLSW) \
944 X("nosasl", SASLminc(6), NOSASLSW) \
945 X("saslmech", SASLminc(5), SASLMECHSW) \
946 X("authservice", SASLminc(0), AUTHSERVICESW) \
947 X("user username", SASLminc(4), USERSW) \
948 X("port server-port-name/number", 4, PORTSW) \
949 X("tls", TLSminc(-3), TLSSW) \
950 X("initialtls", TLSminc(-10), INITTLSSW) \
951 X("notls", TLSminc(-5), NTLSSW) \
952 X("certverify", TLSminc(-10), CERTVERSW) \
953 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
954 X("sendmail program", 0, MTSSM) \
955 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
956 X("messageid localname|random", 2, MESSAGEIDSW) \
958 #define X(sw, minchars, id) id,
959 DEFINE_SWITCH_ENUM(SEND
);
962 #define X(sw, minchars, id) { sw, minchars, id },
963 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
967 extern int debugsw
; /* from sendsbr.c */
975 extern char *altmsg
; /* .. */
976 extern char *annotext
;
977 extern char *distfile
;
981 sendit (char *sp
, char **arg
, char *file
, int pushed
)
984 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
985 char **arguments
, *savearg
[MAXARGS
], **vec
;
986 const char *user
= NULL
, *saslmech
= NULL
;
987 char *auth_svc
= NULL
;
991 * Make sure these are defined. In particular, we need
992 * savearg[1] to be NULL, in case "arg" is NULL below. It
993 * doesn't matter what is the value of savearg[0], but we
994 * set it to NULL, to help catch "off-by-one" errors.
1000 * Temporarily copy arg to savearg, since the brkstring() call in
1001 * getarguments() will wipe it out before it is merged in.
1002 * Also, we skip the first element of savearg, since getarguments()
1003 * skips it. Then we count the number of arguments
1004 * copied. The value of "n" will be one greater than
1005 * this in order to simulate the standard argc/argv.
1010 copyip (arg
, savearg
+1, MAXARGS
-1);
1017 * Merge any arguments from command line (now in savearg)
1018 * and arguments from profile.
1020 arguments
= getarguments (sp
, n
, savearg
, 1);
1033 * Get our initial arguments for postproc now
1036 vec
= argsplit(postproc
, &program
, &vecp
);
1038 vec
[vecp
++] = "-library";
1039 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
1041 if ((cp
= context_find ("fileproc"))) {
1042 vec
[vecp
++] = "-fileproc";
1046 if ((cp
= context_find ("mhlproc"))) {
1047 vec
[vecp
++] = "-mhlproc";
1051 if ((cp
= context_find ("credentials"))) {
1052 /* post doesn't read context so need to pass credentials. */
1053 vec
[vecp
++] = "-credentials";
1057 while ((cp
= *argp
++)) {
1059 switch (smatch (++cp
, sendswitches
)) {
1061 ambigsw (cp
, sendswitches
);
1064 inform("-%s unknown\n", cp
);
1068 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1069 print_help (buf
, sendswitches
, 1);
1072 print_version (invo_name
);
1083 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1084 inform("missing argument to %s", argp
[-2]);
1140 #ifdef OAUTH_SUPPORT
1141 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
1142 die("missing argument to %s", argp
[-2]);
1145 NMH_UNUSED (auth_svc
);
1146 die("not built with OAuth support");
1164 if (!(cp
= *argp
++) || *cp
== '-') {
1165 inform("missing argument to %s", argp
[-2]);
1174 if (!(cp
= *argp
++) || *cp
== '-') {
1175 inform("missing argument to %s", argp
[-2]);
1183 inform("usage: %s [switches]", sp
);
1187 /* allow Aliasfile: profile entry */
1188 if ((cp
= context_find ("Aliasfile"))) {
1191 dp
= mh_xstrdup(cp
);
1192 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1193 vec
[vecp
++] = "-alias";
1198 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1199 if ((cp
= context_find ("signature")) && *cp
)
1200 setenv("SIGNATURE", cp
, 1);
1202 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1204 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1206 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1207 inplace
= atoi (cp
);
1209 if ((cp
= getenv ("mhdist"))
1213 vec
[vecp
++] = "-dist";
1214 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1215 die("unable to create temporary file");
1217 distfile
= mh_xstrdup(cp
);
1218 (void) m_unlink(distfile
);
1219 if (link (altmsg
, distfile
) == NOTOK
)
1220 adios (distfile
, "unable to link %s to", altmsg
);
1225 #ifdef OAUTH_SUPPORT
1226 if (auth_svc
== NULL
) {
1227 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
1228 die("must specify -authservice with -saslmech xoauth2");
1232 die("must specify -user with -saslmech xoauth2");
1236 NMH_UNUSED(saslmech
);
1237 #endif /* OAUTH_SUPPORT */
1239 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1244 if ((pushsw
= pushed
))
1249 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, auth_svc
) == OK
)
1258 whomfile (char **arg
, char *file
)
1262 char **vec
, *program
;
1264 context_save (); /* save the context file */
1267 switch (pid
= fork()) {
1269 advise ("fork", "unable to");
1273 vec
= argsplit(whomproc
, &program
, &vecp
);
1276 vec
[vecp
++] = *arg
++;
1280 execvp (program
, vec
);
1281 fprintf (stderr
, "unable to exec ");
1283 _exit(1); /* NOTREACHED */
1286 return pidwait(pid
, NOTOK
) & 0377 ? 1 : 0;
1292 * Remove the draft file
1296 removefile (char *drft
)
1298 if (m_unlink (drft
) == NOTOK
)
1299 adios (drft
, "unable to unlink");
1306 * Return 1 if we already have a MIME-Version header, 0 otherwise.
1310 checkmimeheader (char *drft
)
1313 m_getfld_state_t gstate
;
1314 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1316 bool retval
= false;
1318 if ((f
= fopen(drft
, "r")) == NULL
) {
1319 admonish(drft
, "unable to read draft");
1323 gstate
= m_getfld_state_init(f
);
1325 int bufsz
= sizeof(buf
);
1326 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1329 if (strcasecmp(name
, VRSN_FIELD
) == 0) {
1330 inform("Cannot use attach commands with already-"
1331 "formatted MIME message \"%s\"", drft
);
1342 m_getfld_state_destroy(&gstate
);