]>
diplodocus.org Git - nmh/blob - uip/whatnowsbr.c
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.
43 #include "sbr/print_version.h"
44 #include "sbr/print_help.h"
45 #include "sbr/arglist.h"
46 #include "sbr/error.h"
54 #include "sbr/m_maildir.h"
55 #include "sbr/m_mktemp.h"
56 #include "sbr/mime_type.h"
58 #define WHATNOW_SWITCHES \
59 X("draftfolder +folder", 0, DFOLDSW) \
60 X("draftmessage msg", 0, DMSGSW) \
61 X("nodraftfolder", 0, NDFLDSW) \
62 X("editor editor", 0, EDITRSW) \
63 X("noedit", 0, NEDITSW) \
64 X("prompt string", 4, PRMPTSW) \
65 X("version", 0, VERSIONSW) \
66 X("help", 0, HELPSW) \
69 #define X(sw, minchars, id) id,
70 DEFINE_SWITCH_ENUM(WHATNOW
);
73 #define X(sw, minchars, id) { sw, minchars, id },
74 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
78 * Options at the "whatnow" prompt
80 #define PROMPT_SWITCHES \
81 X("edit [<editor> <switches>]", 0, EDITSW) \
82 X("refile [<switches>] +folder", 0, REFILEOPT) \
83 X("mime [<switches>]", 0, BUILDMIMESW) \
84 X("display [<switches>]", 0, DISPSW) \
85 X("list [<switches>]", 0, LISTSW) \
86 X("send [<switches>]", 0, SENDSW) \
87 X("push [<switches>]", 0, PUSHSW) \
88 X("whom [<switches>]", 0, WHOMSW) \
89 X("quit [-delete]", 0, QUITSW) \
90 X("delete", 0, DELETESW) \
91 X("cd [directory]", 0, CDCMDSW) \
92 X("pwd", 0, PWDCMDSW) \
94 X("attach [-v]", 0, ATTACHCMDSW) \
95 X("detach [-n]", 0, DETACHCMDSW) \
96 X("alist [-ln] ", 2, ALISTCMDSW) \
98 #define X(sw, minchars, id) id,
99 DEFINE_SWITCH_ENUM(PROMPT
);
102 #define X(sw, minchars, id) { sw, minchars, id },
103 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
106 static char *myprompt
= "\nWhat now? ";
111 static int editfile (char **, char **, char *, int, struct msgs
*,
112 char *, char *, int, int);
113 static int sendfile (char **, char *, int);
114 static void sendit (char *, char **, char *, int);
115 static int buildfile (char **, char *);
116 static int whomfile (char **, char *);
117 static int removefile (char *);
118 static int checkmimeheader (char *);
119 static void writelscmd(char *, int, char *, char **);
120 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
121 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
122 static int system_in_dir(const char *dir
, const char *cmd
);
123 static int copyf (char *, char *);
127 WhatNow (int argc
, char **argv
)
131 int use
= 0, atfile
= 1;
132 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
133 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
135 char **argp
, **arguments
;
137 char cwd
[PATH_MAX
+ 1]; /* current working directory */
138 char file
[PATH_MAX
+ 1]; /* file name buffer */
139 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
140 FILE *f
; /* read pointer for bgnd proc */
141 char *l
; /* set on -l to alist command */
142 int n
; /* set on -n to alist command */
144 /* Need this if called from what_now(). */
145 invo_name
= r1bindex (argv
[0], '/');
147 arguments
= getarguments (invo_name
, argc
, argv
, 1);
151 * Get the initial current working directory.
154 if (getcwd(cwd
, sizeof (cwd
)) == NULL
) {
155 adios("getcwd", "could not get working directory");
158 while ((cp
= *argp
++)) {
160 switch (smatch (++cp
, whatnowswitches
)) {
162 ambigsw (cp
, whatnowswitches
);
165 die("-%s unknown", cp
);
168 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
169 print_help (buf
, whatnowswitches
, 1);
172 print_version(invo_name
);
177 die("only one draft folder at a time!");
178 if (!(cp
= *argp
++) || *cp
== '-')
179 die("missing argument to %s", argp
[-2]);
180 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
181 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
185 die("only one draft message at a time!");
186 if (!(dmsg
= *argp
++) || *dmsg
== '-')
187 die("missing argument to %s", argp
[-2]);
195 if (!(ed
= *argp
++) || *ed
== '-')
196 die("missing argument to %s", argp
[-2]);
204 if (!(myprompt
= *argp
++) || *myprompt
== '-')
205 die("missing argument to %s", argp
[-2]);
210 die("only one draft at a time!");
214 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
215 drft
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
217 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? mh_xstrdup(cp
) : NULL
;
219 if ((cp
= getenv ("mhatfile")) && *cp
)
222 if ((cp
= getenv ("mhuse")) && *cp
)
225 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
230 /* start editing the draft, unless -noedit was given */
231 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
232 NULL
, 1, atfile
) < 0)
236 #ifdef READLINE_SUPPORT
237 if (!(argp
= read_switch_multiword_via_readline(myprompt
, aleqs
))) {
238 #else /* ! READLINE_SUPPORT */
239 if (!(argp
= read_switch_multiword(myprompt
, aleqs
))) {
240 #endif /* READLINE_SUPPORT */
241 (void) m_unlink (LINK
);
244 switch (smatch (*argp
, aleqs
)) {
246 /* display the message being replied to, or distributed */
248 showfile (++argp
, msgnam
);
250 inform("no alternate message to display");
254 /* Translate MIME composition file */
255 buildfile (++argp
, drft
);
259 /* Call an editor on the draft file */
262 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
263 NULL
, 1, atfile
) == NOTOK
)
268 /* display the draft file */
269 showfile (++argp
, drft
);
273 /* Check to whom the draft would be sent */
274 whomfile (++argp
, drft
);
278 /* Quit, and possibly delete the draft */
279 if (*++argp
&& (*argp
[0] == 'd' ||
280 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
283 if (stat (drft
, &st
) != NOTOK
)
284 inform("draft left on %s", drft
);
289 /* Delete draft and exit */
294 /* Send draft in background */
295 if (sendfile (++argp
, drft
, 1))
301 sendfile (++argp
, drft
, 0);
305 /* Refile the draft */
306 if (refile (++argp
, drft
) == 0)
311 /* Change the working directory for attachments
313 * Run the directory through the user's shell so that
314 * we can take advantage of any syntax that the user
315 * is accustomed to. Read back the absolute path.
318 if (*(argp
+1) == NULL
) {
319 strcpy(buf
, "$SHELL -c \"cd&&pwd\"");
322 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
324 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
325 if (fgets(cwd
, sizeof (cwd
), f
) == NULL
) {
326 advise (buf
, "fgets");
328 trim_suffix_c(cwd
, '\n');
332 advise("popen", "could not get directory");
338 /* Print the working directory for attachments */
343 /* List files in the current attachment working directory
345 * Use the user's shell so that we can take advantage of any
346 * syntax that the user is accustomed to.
348 writelscmd(buf
, sizeof(buf
), "", argp
);
349 (void)system_in_dir(cwd
, buf
);
354 * List attachments on current draft. Options are:
356 * -l long listing (full path names)
360 if (checkmimeheader(drft
))
366 while (*++argp
!= NULL
) {
367 if (strcmp(*argp
, "-l") == 0)
370 else if (strcmp(*argp
, "-n") == 0)
373 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
385 inform("usage is alist [-ln].");
388 annolist(drft
, ATTACH_FIELD
, l
, n
);
394 * Attach files to current draft.
397 bool verbose
= false;
400 if (checkmimeheader(drft
))
403 for (ap
= argp
+1; *ap
; ++ap
) {
404 if (strcmp(*ap
, "-v") == 0) {
407 } else if (*ap
[0] != '-') {
412 if (*(argp
+1) == NULL
) {
413 inform("attach command requires file argument(s).");
418 * Build a command line that causes the user's shell to list the file name
419 * arguments. This handles and wildcard expansion, tilde expansion, etc.
421 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
424 * Read back the response from the shell, which contains a number of lines
425 * with one file name per line. Remove off the newline. Determine whether
426 * we have an absolute or relative path name. Prepend the current working
427 * directory to relative path names. Add the attachment annotation to the
431 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
432 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
435 trim_suffix_c(shell
, '\n');
438 strncpy(file
, shell
, sizeof(file
));
439 file
[sizeof(file
) - 1] = '\0';
441 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
444 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
446 ctype
= mime_type(file
);
447 printf ("Attaching %s as a %s\n", file
, ctype
);
455 advise("popen", "could not get file from shell");
462 * Detach files from current draft.
466 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
467 * so this catches a -n anywhere in the argument list.
470 if (checkmimeheader(drft
))
473 for (n
= 0, arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
474 if (strcmp(*arguments
, "-n") == 0) {
481 * A -n was found so interpret the arguments as attachment numbers.
482 * Decrement any remaining argument number that is greater than the one
483 * just processed after processing each one so that the numbering stays
488 for (arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
489 if (strcmp(*arguments
, "-n") == 0)
492 if (**arguments
!= '\0') {
493 n
= atoi(*arguments
);
494 annotate(drft
, ATTACH_FIELD
, NULL
, 1, 0, n
, 1);
496 for (argp
= arguments
+ 1; *argp
!= NULL
; argp
++) {
497 if (atoi(*argp
) > n
) {
498 if (atoi(*argp
) == 1)
501 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
509 * The arguments are interpreted as file names. Run them through the
510 * user's shell for wildcard expansion and other goodies. Do this from
511 * the current working directory if the argument is not an absolute path
512 * name (does not begin with a /).
514 * We feed all the file names to the shell at once, otherwise you can't
515 * provide a file name with a space in it.
517 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
518 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
519 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
520 trim_suffix_c(shell
, '\n');
521 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
525 advise("popen", "could not get file from shell");
531 /* Unknown command */
541 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
543 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
546 /* Note that we do not quote -- the argp from the user
547 * is assumed to be quoted as they desire. (We can't treat
548 * it as pure literal as that would prevent them using ~,
549 * wildcards, etc.) The buffer produced by this function
550 * should be given to popen_in_dir() or system_in_dir() so
551 * that the current working directory is set correctly.
553 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
554 /* NB that some snprintf() return -1 on overflow rather than the
555 * new C99 mandated 'number of chars that would have been written'
557 /* length checks here and inside the loop allow for the
558 * trailing "&&", trailcmd, '"' and NUL
560 int trailln
= strlen(trailcmd
) + 4;
561 if (ln
< 0 || ln
+ trailln
> bufsz
)
562 die("arguments too long");
566 while (*argp
&& *++argp
) {
568 /* +1 for leading space */
569 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
570 die("arguments too long");
572 memcpy(cp
, *argp
, ln
+1);
576 *cp
++ = '&'; *cp
++ = '&';
577 strcpy(cp
, trailcmd
);
585 * Build a command line that causes the user's shell to list the file name
586 * arguments. This handles and wildcard expansion, tilde expansion, etc.
589 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
591 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
592 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
596 /* Like system(), but run the command in directory dir.
597 * This assumes the program is single-threaded!
600 system_in_dir(const char *dir
, const char *cmd
)
605 /* ensure that $SHELL exists, as the cmd was written relying on
606 a non-blank $SHELL... */
607 setenv("SHELL","/bin/sh",0); /* don't overwrite */
609 if (getcwd(olddir
, sizeof(olddir
)) == 0)
610 adios("getcwd", "could not get working directory");
612 adios("chdir", "could not change working directory");
614 if (chdir(olddir
) != 0)
615 adios("chdir", "could not change working directory");
619 /* ditto for popen() */
621 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
626 /* ensure that $SHELL exists, as the cmd was written relying on
627 a non-blank $SHELL... */
628 setenv("SHELL","/bin/sh",0); /* don't overwrite */
630 if (getcwd(olddir
, sizeof(olddir
)) == 0)
631 adios("getcwd", "could not get working directory");
633 adios("chdir", "could not change working directory");
634 f
= popen(cmd
, type
);
635 if (chdir(olddir
) != 0)
636 adios("chdir", "could not change working directory");
645 static bool reedit
; /* have we been here before? */
646 static char *edsave
= NULL
; /* the editor we used previously */
650 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
651 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
653 int pid
, status
, vecp
;
654 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
655 char *cp
, *prog
, **vec
;
658 bool slinked
= false;
660 /* Was there a previous edit session? */
661 if (reedit
&& (*ed
|| edsave
)) {
662 if (!*ed
) { /* no explicit editor */
663 *ed
= edsave
; /* so use the previous one */
664 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
667 /* unless we've specified it via "editor-next" */
668 cp
= concat (cp
, "-next", NULL
);
669 if ((cp
= context_find (cp
)) != NULL
)
673 /* set initial editor */
675 *ed
= get_default_editor();
679 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
680 strncpy (altpath
, altmsg
, sizeof(altpath
));
682 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
684 strncpy (linkpath
, LINK
, sizeof(linkpath
));
686 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
689 (void) m_unlink (linkpath
);
690 if (link (altpath
, linkpath
) == NOTOK
) {
691 if (symlink (altpath
, linkpath
) < 0) {
692 adios (linkpath
, "symlink");
701 context_save (); /* save the context file */
704 switch (pid
= fork()) {
706 advise ("fork", "unable to");
712 if (chdir (cwd
) < 0) {
713 advise (cwd
, "chdir");
718 setenv("mhfolder", mp
->foldpath
, 1);
719 setenv("editalt", altpath
, 1);
722 vec
= argsplit(*ed
, &prog
, &vecp
);
726 vec
[vecp
++] = *arg
++;
731 fprintf (stderr
, "unable to exec ");
736 if ((status
= pidwait (pid
, NOTOK
))) {
737 if (((status
& 0xff00) != 0xff00)
738 && (!reedit
|| (status
& 0x00ff))) {
739 if (!use
&& (status
& 0xff00) &&
740 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
741 inform("problems with edit--draft left in %s", cp
);
743 inform("problems with edit--%s preserved", file
);
746 status
= -2; /* maybe "reedit ? -2 : -1"? */
755 ? lstat (linkpath
, &st
) != NOTOK
756 && S_ISREG(st
.st_mode
)
757 && copyf (linkpath
, altpath
) == NOTOK
758 : stat (linkpath
, &st
) != NOTOK
760 && (m_unlink (altpath
) == NOTOK
761 || link (linkpath
, altpath
) == NOTOK
)))
762 advise (linkpath
, "unable to update %s from", altmsg
);
765 /* normally, we remember which editor we used */
767 edsave
= getcpy (*ed
);
770 if (altmsg
&& atfile
)
771 (void) m_unlink (linkpath
);
778 copyf (char *ifile
, char *ofile
)
783 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
785 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
786 admonish (ofile
, "unable to open and truncate");
791 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
792 if (write (out
, buffer
, i
) != i
) {
793 advise (ofile
, "may have damaged");
809 sendfile (char **arg
, char *file
, int pushsw
)
813 char *cp
, *sp
, **vec
, *program
;
816 * If the sendproc is the nmh command `send', then we call
817 * those routines directly rather than exec'ing the command.
819 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
821 sendit (invo_name
= sp
, arg
, file
, pushsw
);
826 context_save (); /* save the context file */
832 adios("fork", "failed:");
835 vec
= argsplit(sendproc
, &program
, &vecp
);
837 vec
[vecp
++] = "-push";
840 vec
[vecp
++] = *arg
++;
844 execvp (program
, vec
);
845 fprintf (stderr
, "unable to exec ");
850 if (pidwait(child_id
, OK
) == 0)
858 * Translate MIME composition file (call buildmimeproc)
862 buildfile (char **argp
, char *file
)
869 /* allocate space for arguments */
875 args
= mh_xmalloc((i
+ 2) * sizeof(char *));
878 * For backward compatibility, we need to add -build
879 * if we are using mhn as buildmimeproc
882 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
883 args
[i
++] = "-build";
885 /* copy any other arguments */
886 while (argp
&& *argp
)
890 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
893 return i
? NOTOK
: OK
;
898 # define SASLminc(a) (a)
899 #else /* CYRUS_SASL */
900 # define SASLminc(a) 0
901 #endif /* CYRUS_SASL */
904 # define TLSminc(a) (a)
905 #else /* TLS_SUPPORT */
906 # define TLSminc(a) 0
907 #endif /* TLS_SUPPORT */
909 #define SEND_SWITCHES \
910 X("alias aliasfile", 0, ALIASW) \
911 X("debug", -5, DEBUGSW) \
912 X("filter filterfile", 0, FILTSW) \
913 X("nofilter", 0, NFILTSW) \
914 X("format", 0, FRMTSW) \
915 X("noformat", 0, NFRMTSW) \
916 X("forward", 0, FORWSW) \
917 X("noforward", 0, NFORWSW) \
918 X("mime", 0, MIMESW) \
919 X("nomime", 0, NMIMESW) \
920 X("msgid", 0, MSGDSW) \
921 X("nomsgid", 0, NMSGDSW) \
922 X("push", 0, SPSHSW) \
923 X("nopush", 0, NSPSHSW) \
924 X("split seconds", 0, SPLITSW) \
925 X("unique", -6, UNIQSW) \
926 X("nounique", -8, NUNIQSW) \
927 X("verbose", 0, VERBSW) \
928 X("noverbose", 0, NVERBSW) \
929 X("watch", 0, WATCSW) \
930 X("nowatch", 0, NWATCSW) \
931 X("width columns", 0, WIDTHSW) \
932 X("version", 0, SVERSIONSW) \
933 X("help", 0, SHELPSW) \
934 X("dashstuffing", -12, BITSTUFFSW) \
935 X("nodashstuffing", -14, NBITSTUFFSW) \
936 X("client host", -6, CLIESW) \
937 X("server host", 6, SERVSW) \
938 X("snoop", -5, SNOOPSW) \
939 X("draftfolder +folder", 0, SDRFSW) \
940 X("draftmessage msg", 0, SDRMSW) \
941 X("nodraftfolder", 0, SNDRFSW) \
942 X("sasl", SASLminc(4), SASLSW) \
943 X("nosasl", SASLminc(6), NOSASLSW) \
944 X("saslmech", SASLminc(5), SASLMECHSW) \
945 X("authservice", SASLminc(0), AUTHSERVICESW) \
946 X("user username", SASLminc(4), USERSW) \
947 X("port server-port-name/number", 4, PORTSW) \
948 X("tls", TLSminc(-3), TLSSW) \
949 X("initialtls", TLSminc(-10), INITTLSSW) \
950 X("notls", TLSminc(-5), NTLSSW) \
951 X("certverify", TLSminc(-10), CERTVERSW) \
952 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
953 X("sendmail program", 0, MTSSM) \
954 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
955 X("messageid localname|random", 2, MESSAGEIDSW) \
957 #define X(sw, minchars, id) id,
958 DEFINE_SWITCH_ENUM(SEND
);
961 #define X(sw, minchars, id) { sw, minchars, id },
962 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
966 extern int debugsw
; /* from sendsbr.c */
974 extern char *altmsg
; /* .. */
975 extern char *annotext
;
976 extern char *distfile
;
980 sendit (char *sp
, char **arg
, char *file
, int pushed
)
983 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
984 char **arguments
, *savearg
[MAXARGS
], **vec
;
985 const char *user
= NULL
, *saslmech
= NULL
;
986 char *auth_svc
= NULL
;
990 * Make sure these are defined. In particular, we need
991 * savearg[1] to be NULL, in case "arg" is NULL below. It
992 * doesn't matter what is the value of savearg[0], but we
993 * set it to NULL, to help catch "off-by-one" errors.
999 * Temporarily copy arg to savearg, since the brkstring() call in
1000 * getarguments() will wipe it out before it is merged in.
1001 * Also, we skip the first element of savearg, since getarguments()
1002 * skips it. Then we count the number of arguments
1003 * copied. The value of "n" will be one greater than
1004 * this in order to simulate the standard argc/argv.
1009 copyip (arg
, savearg
+1, MAXARGS
-1);
1016 * Merge any arguments from command line (now in savearg)
1017 * and arguments from profile.
1019 arguments
= getarguments (sp
, n
, savearg
, 1);
1032 * Get our initial arguments for postproc now
1035 vec
= argsplit(postproc
, &program
, &vecp
);
1037 vec
[vecp
++] = "-library";
1038 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
1040 if ((cp
= context_find ("fileproc"))) {
1041 vec
[vecp
++] = "-fileproc";
1045 if ((cp
= context_find ("mhlproc"))) {
1046 vec
[vecp
++] = "-mhlproc";
1050 if ((cp
= context_find ("credentials"))) {
1051 /* post doesn't read context so need to pass credentials. */
1052 vec
[vecp
++] = "-credentials";
1056 while ((cp
= *argp
++)) {
1058 switch (smatch (++cp
, sendswitches
)) {
1060 ambigsw (cp
, sendswitches
);
1063 inform("-%s unknown\n", cp
);
1067 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1068 print_help (buf
, sendswitches
, 1);
1071 print_version (invo_name
);
1082 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1083 inform("missing argument to %s", argp
[-2]);
1139 #ifdef OAUTH_SUPPORT
1140 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
1141 die("missing argument to %s", argp
[-2]);
1144 NMH_UNUSED (auth_svc
);
1145 die("not built with OAuth support");
1163 if (!(cp
= *argp
++) || *cp
== '-') {
1164 inform("missing argument to %s", argp
[-2]);
1173 if (!(cp
= *argp
++) || *cp
== '-') {
1174 inform("missing argument to %s", argp
[-2]);
1182 inform("usage: %s [switches]", sp
);
1186 /* allow Aliasfile: profile entry */
1187 if ((cp
= context_find ("Aliasfile"))) {
1190 dp
= mh_xstrdup(cp
);
1191 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1192 vec
[vecp
++] = "-alias";
1197 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1198 if ((cp
= context_find ("signature")) && *cp
)
1199 setenv("SIGNATURE", cp
, 1);
1201 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1203 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1205 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1206 inplace
= atoi (cp
);
1208 if ((cp
= getenv ("mhdist"))
1212 vec
[vecp
++] = "-dist";
1213 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1214 die("unable to create temporary file");
1216 distfile
= mh_xstrdup(cp
);
1217 (void) m_unlink(distfile
);
1218 if (link (altmsg
, distfile
) == NOTOK
)
1219 adios (distfile
, "unable to link %s to", altmsg
);
1224 #ifdef OAUTH_SUPPORT
1225 if (auth_svc
== NULL
) {
1226 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
1227 die("must specify -authservice with -saslmech xoauth2");
1231 die("must specify -user with -saslmech xoauth2");
1235 NMH_UNUSED(saslmech
);
1236 #endif /* OAUTH_SUPPORT */
1238 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1243 if ((pushsw
= pushed
))
1248 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, auth_svc
) == OK
)
1257 whomfile (char **arg
, char *file
)
1261 char **vec
, *program
;
1263 context_save (); /* save the context file */
1266 switch (pid
= fork()) {
1268 advise ("fork", "unable to");
1272 vec
= argsplit(whomproc
, &program
, &vecp
);
1275 vec
[vecp
++] = *arg
++;
1279 execvp (program
, vec
);
1280 fprintf (stderr
, "unable to exec ");
1282 _exit(1); /* NOTREACHED */
1285 return pidwait(pid
, NOTOK
) & 0377 ? 1 : 0;
1291 * Remove the draft file
1295 removefile (char *drft
)
1297 if (m_unlink (drft
) == NOTOK
)
1298 adios (drft
, "unable to unlink");
1305 * Return 1 if we already have a MIME-Version header, 0 otherwise.
1309 checkmimeheader (char *drft
)
1312 m_getfld_state_t gstate
;
1313 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1315 bool retval
= false;
1317 if ((f
= fopen(drft
, "r")) == NULL
) {
1318 admonish(drft
, "unable to read draft");
1322 gstate
= m_getfld_state_init(f
);
1324 int bufsz
= sizeof(buf
);
1325 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1328 if (strcasecmp(name
, VRSN_FIELD
) == 0) {
1329 inform("Cannot use attach commands with already-"
1330 "formatted MIME message \"%s\"", drft
);
1341 m_getfld_state_destroy(&gstate
);