]>
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.
41 #include "whatnowsbr.h"
44 #include "sbr/m_getfld.h"
45 #include "sbr/getarguments.h"
46 #include "sbr/read_switch_multiword_via_readline.h"
47 #include "sbr/concat.h"
48 #include "sbr/showfile.h"
49 #include "sbr/smatch.h"
50 #include "sbr/r1bindex.h"
51 #include "sbr/refile.h"
52 #include "sbr/closefds.h"
53 #include "sbr/copyip.h"
54 #include "sbr/getcpy.h"
55 #include "sbr/m_draft.h"
56 #include "sbr/m_backup.h"
57 #include "sbr/geteditor.h"
58 #include "sbr/context_save.h"
59 #include "sbr/context_find.h"
60 #include "sbr/brkstring.h"
61 #include "sbr/ambigsw.h"
64 #include "sbr/print_version.h"
65 #include "sbr/print_help.h"
66 #include "sbr/arglist.h"
67 #include "sbr/error.h"
75 #include "sbr/m_maildir.h"
76 #include "sbr/m_mktemp.h"
77 #include "sbr/mime_type.h"
79 #define WHATNOW_SWITCHES \
80 X("draftfolder +folder", 0, DFOLDSW) \
81 X("draftmessage msg", 0, DMSGSW) \
82 X("nodraftfolder", 0, NDFLDSW) \
83 X("editor editor", 0, EDITRSW) \
84 X("noedit", 0, NEDITSW) \
85 X("prompt string", 4, PRMPTSW) \
86 X("version", 0, VERSIONSW) \
87 X("help", 0, HELPSW) \
90 #define X(sw, minchars, id) id,
91 DEFINE_SWITCH_ENUM(WHATNOW
);
94 #define X(sw, minchars, id) { sw, minchars, id },
95 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
99 * Options at the "whatnow" prompt
101 #define PROMPT_SWITCHES \
102 X("edit [<editor> <switches>]", 0, EDITSW) \
103 X("refile [<switches>] +folder", 0, REFILEOPT) \
104 X("mime [<switches>]", 0, BUILDMIMESW) \
105 X("display [<switches>]", 0, DISPSW) \
106 X("list [<switches>]", 0, LISTSW) \
107 X("send [<switches>]", 0, SENDSW) \
108 X("push [<switches>]", 0, PUSHSW) \
109 X("whom [<switches>]", 0, WHOMSW) \
110 X("quit [-delete]", 0, QUITSW) \
111 X("delete", 0, DELETESW) \
112 X("cd [directory]", 0, CDCMDSW) \
113 X("pwd", 0, PWDCMDSW) \
114 X("ls", 2, LSCMDSW) \
115 X("attach [-v]", 0, ATTACHCMDSW) \
116 X("detach [-n]", 0, DETACHCMDSW) \
117 X("alist [-ln] ", 2, ALISTCMDSW) \
119 #define X(sw, minchars, id) id,
120 DEFINE_SWITCH_ENUM(PROMPT
);
123 #define X(sw, minchars, id) { sw, minchars, id },
124 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
127 static char *myprompt
= "\nWhat now? ";
132 static int editfile (char **, char **, char *, int, struct msgs
*,
133 char *, char *, int, int);
134 static int sendfile (char **, char *, int);
135 static void sendit (char *, char **, char *, int);
136 static int buildfile (char **, char *);
137 static int whomfile (char **, char *);
138 static int removefile (char *);
139 static int checkmimeheader (char *);
140 static void writelscmd(char *, int, char *, char **);
141 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
142 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
143 static int system_in_dir(const char *dir
, const char *cmd
);
144 static int copyf (char *, char *);
148 WhatNow (int argc
, char **argv
)
152 int use
= 0, atfile
= 1;
153 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
154 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
156 char **argp
, **arguments
;
158 char cwd
[PATH_MAX
+ 1]; /* current working directory */
159 char file
[PATH_MAX
+ 1]; /* file name buffer */
160 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
161 FILE *f
; /* read pointer for bgnd proc */
162 char *l
; /* set on -l to alist command */
163 int n
; /* set on -n to alist command */
165 /* Need this if called from what_now(). */
166 invo_name
= r1bindex (argv
[0], '/');
168 arguments
= getarguments (invo_name
, argc
, argv
, 1);
172 * Get the initial current working directory.
175 if (getcwd(cwd
, sizeof (cwd
)) == NULL
) {
176 adios("getcwd", "could not get working directory");
179 while ((cp
= *argp
++)) {
181 switch (smatch (++cp
, whatnowswitches
)) {
183 ambigsw (cp
, whatnowswitches
);
186 die("-%s unknown", cp
);
189 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
190 print_help (buf
, whatnowswitches
, 1);
193 print_version(invo_name
);
198 die("only one draft folder at a time!");
199 if (!(cp
= *argp
++) || *cp
== '-')
200 die("missing argument to %s", argp
[-2]);
201 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
202 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
206 die("only one draft message at a time!");
207 if (!(dmsg
= *argp
++) || *dmsg
== '-')
208 die("missing argument to %s", argp
[-2]);
216 if (!(ed
= *argp
++) || *ed
== '-')
217 die("missing argument to %s", argp
[-2]);
225 if (!(myprompt
= *argp
++) || *myprompt
== '-')
226 die("missing argument to %s", argp
[-2]);
231 die("only one draft at a time!");
235 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
236 drft
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
238 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? mh_xstrdup(cp
) : NULL
;
240 if ((cp
= getenv ("mhatfile")) && *cp
)
243 if ((cp
= getenv ("mhuse")) && *cp
)
246 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
251 /* start editing the draft, unless -noedit was given */
252 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
253 NULL
, 1, atfile
) < 0)
257 #ifdef READLINE_SUPPORT
258 if (!(argp
= read_switch_multiword_via_readline(myprompt
, aleqs
))) {
259 #else /* ! READLINE_SUPPORT */
260 if (!(argp
= read_switch_multiword(myprompt
, aleqs
))) {
261 #endif /* READLINE_SUPPORT */
262 (void) m_unlink (LINK
);
265 switch (smatch (*argp
, aleqs
)) {
267 /* display the message being replied to, or distributed */
269 showfile (++argp
, msgnam
);
271 inform("no alternate message to display");
275 /* Translate MIME composition file */
276 buildfile (++argp
, drft
);
280 /* Call an editor on the draft file */
283 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
284 NULL
, 1, atfile
) == NOTOK
)
289 /* display the draft file */
290 showfile (++argp
, drft
);
294 /* Check to whom the draft would be sent */
295 whomfile (++argp
, drft
);
299 /* Quit, and possibly delete the draft */
300 if (*++argp
&& (*argp
[0] == 'd' ||
301 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
304 if (stat (drft
, &st
) != NOTOK
)
305 inform("draft left on %s", drft
);
310 /* Delete draft and exit */
315 /* Send draft in background */
316 if (sendfile (++argp
, drft
, 1))
322 sendfile (++argp
, drft
, 0);
326 /* Refile the draft */
327 if (refile (++argp
, drft
) == 0)
332 /* Change the working directory for attachments
334 * Run the directory through the user's shell so that
335 * we can take advantage of any syntax that the user
336 * is accustomed to. Read back the absolute path.
339 if (*(argp
+1) == NULL
) {
340 strcpy(buf
, "$SHELL -c \"cd&&pwd\"");
343 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
345 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
346 if (fgets(cwd
, sizeof (cwd
), f
) == NULL
) {
347 advise (buf
, "fgets");
349 trim_suffix_c(cwd
, '\n');
353 advise("popen", "could not get directory");
359 /* Print the working directory for attachments */
364 /* List files in the current attachment working directory
366 * Use the user's shell so that we can take advantage of any
367 * syntax that the user is accustomed to.
369 writelscmd(buf
, sizeof(buf
), "", argp
);
370 (void)system_in_dir(cwd
, buf
);
375 * List attachments on current draft. Options are:
377 * -l long listing (full path names)
381 if (checkmimeheader(drft
))
387 while (*++argp
!= NULL
) {
388 if (strcmp(*argp
, "-l") == 0)
391 else if (strcmp(*argp
, "-n") == 0)
394 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
406 inform("usage is alist [-ln].");
409 annolist(drft
, ATTACH_FIELD
, l
, n
);
415 * Attach files to current draft.
418 bool verbose
= false;
421 if (checkmimeheader(drft
))
424 for (ap
= argp
+1; *ap
; ++ap
) {
425 if (strcmp(*ap
, "-v") == 0) {
428 } else if (*ap
[0] != '-') {
433 if (*(argp
+1) == NULL
) {
434 inform("attach command requires file argument(s).");
439 * Build a command line that causes the user's shell to list the file name
440 * arguments. This handles and wildcard expansion, tilde expansion, etc.
442 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
445 * Read back the response from the shell, which contains a number of lines
446 * with one file name per line. Remove off the newline. Determine whether
447 * we have an absolute or relative path name. Prepend the current working
448 * directory to relative path names. Add the attachment annotation to the
452 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
453 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
456 trim_suffix_c(shell
, '\n');
459 strncpy(file
, shell
, sizeof(file
));
460 file
[sizeof(file
) - 1] = '\0';
462 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
465 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
467 ctype
= mime_type(file
);
468 printf ("Attaching %s as a %s\n", file
, ctype
);
476 advise("popen", "could not get file from shell");
483 * Detach files from current draft.
487 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
488 * so this catches a -n anywhere in the argument list.
491 if (checkmimeheader(drft
))
494 for (n
= 0, arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
495 if (strcmp(*arguments
, "-n") == 0) {
502 * A -n was found so interpret the arguments as attachment numbers.
503 * Decrement any remaining argument number that is greater than the one
504 * just processed after processing each one so that the numbering stays
509 for (arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
510 if (strcmp(*arguments
, "-n") == 0)
513 if (**arguments
!= '\0') {
514 n
= atoi(*arguments
);
515 annotate(drft
, ATTACH_FIELD
, NULL
, 1, 0, n
, 1);
517 for (argp
= arguments
+ 1; *argp
!= NULL
; argp
++) {
518 if (atoi(*argp
) > n
) {
519 if (atoi(*argp
) == 1)
522 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
530 * The arguments are interpreted as file names. Run them through the
531 * user's shell for wildcard expansion and other goodies. Do this from
532 * the current working directory if the argument is not an absolute path
533 * name (does not begin with a /).
535 * We feed all the file names to the shell at once, otherwise you can't
536 * provide a file name with a space in it.
538 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
539 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
540 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
541 trim_suffix_c(shell
, '\n');
542 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
546 advise("popen", "could not get file from shell");
552 /* Unknown command */
562 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
564 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
567 /* Note that we do not quote -- the argp from the user
568 * is assumed to be quoted as they desire. (We can't treat
569 * it as pure literal as that would prevent them using ~,
570 * wildcards, etc.) The buffer produced by this function
571 * should be given to popen_in_dir() or system_in_dir() so
572 * that the current working directory is set correctly.
574 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
575 /* NB that some snprintf() return -1 on overflow rather than the
576 * new C99 mandated 'number of chars that would have been written'
578 /* length checks here and inside the loop allow for the
579 * trailing "&&", trailcmd, '"' and NUL
581 int trailln
= strlen(trailcmd
) + 4;
582 if (ln
< 0 || ln
+ trailln
> bufsz
)
583 die("arguments too long");
587 while (*argp
&& *++argp
) {
589 /* +1 for leading space */
590 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
591 die("arguments too long");
593 memcpy(cp
, *argp
, ln
+1);
597 *cp
++ = '&'; *cp
++ = '&';
598 strcpy(cp
, trailcmd
);
606 * Build a command line that causes the user's shell to list the file name
607 * arguments. This handles and wildcard expansion, tilde expansion, etc.
610 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
612 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
613 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
617 /* Like system(), but run the command in directory dir.
618 * This assumes the program is single-threaded!
621 system_in_dir(const char *dir
, const char *cmd
)
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");
635 if (chdir(olddir
) != 0)
636 adios("chdir", "could not change working directory");
640 /* ditto for popen() */
642 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
647 /* ensure that $SHELL exists, as the cmd was written relying on
648 a non-blank $SHELL... */
649 setenv("SHELL","/bin/sh",0); /* don't overwrite */
651 if (getcwd(olddir
, sizeof(olddir
)) == 0)
652 adios("getcwd", "could not get working directory");
654 adios("chdir", "could not change working directory");
655 f
= popen(cmd
, type
);
656 if (chdir(olddir
) != 0)
657 adios("chdir", "could not change working directory");
666 static bool reedit
; /* have we been here before? */
667 static char *edsave
= NULL
; /* the editor we used previously */
671 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
672 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
674 int pid
, status
, vecp
;
675 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
676 char *cp
, *prog
, **vec
;
679 bool slinked
= false;
681 /* Was there a previous edit session? */
682 if (reedit
&& (*ed
|| edsave
)) {
683 if (!*ed
) { /* no explicit editor */
684 *ed
= edsave
; /* so use the previous one */
685 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
688 /* unless we've specified it via "editor-next" */
689 cp
= concat (cp
, "-next", NULL
);
690 if ((cp
= context_find (cp
)) != NULL
)
694 /* set initial editor */
696 *ed
= get_default_editor();
700 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
701 strncpy (altpath
, altmsg
, sizeof(altpath
));
703 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
705 strncpy (linkpath
, LINK
, sizeof(linkpath
));
707 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
710 (void) m_unlink (linkpath
);
711 if (link (altpath
, linkpath
) == NOTOK
) {
712 if (symlink (altpath
, linkpath
) < 0) {
713 adios (linkpath
, "symlink");
722 context_save (); /* save the context file */
725 switch (pid
= fork()) {
727 advise ("fork", "unable to");
733 if (chdir (cwd
) < 0) {
734 advise (cwd
, "chdir");
739 setenv("mhfolder", mp
->foldpath
, 1);
740 setenv("editalt", altpath
, 1);
743 vec
= argsplit(*ed
, &prog
, &vecp
);
747 vec
[vecp
++] = *arg
++;
752 fprintf (stderr
, "unable to exec ");
757 if ((status
= pidwait (pid
, NOTOK
))) {
758 if (((status
& 0xff00) != 0xff00)
759 && (!reedit
|| (status
& 0x00ff))) {
760 if (!use
&& (status
& 0xff00) &&
761 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
762 inform("problems with edit--draft left in %s", cp
);
764 inform("problems with edit--%s preserved", file
);
767 status
= -2; /* maybe "reedit ? -2 : -1"? */
776 ? lstat (linkpath
, &st
) != NOTOK
777 && S_ISREG(st
.st_mode
)
778 && copyf (linkpath
, altpath
) == NOTOK
779 : stat (linkpath
, &st
) != NOTOK
781 && (m_unlink (altpath
) == NOTOK
782 || link (linkpath
, altpath
) == NOTOK
)))
783 advise (linkpath
, "unable to update %s from", altmsg
);
786 /* normally, we remember which editor we used */
788 edsave
= getcpy (*ed
);
791 if (altmsg
&& atfile
)
792 (void) m_unlink (linkpath
);
799 copyf (char *ifile
, char *ofile
)
804 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
806 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
807 admonish (ofile
, "unable to open and truncate");
812 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
813 if (write (out
, buffer
, i
) != i
) {
814 advise (ofile
, "may have damaged");
830 sendfile (char **arg
, char *file
, int pushsw
)
834 char *cp
, *sp
, **vec
, *program
;
837 * If the sendproc is the nmh command `send', then we call
838 * those routines directly rather than exec'ing the command.
840 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
842 sendit (invo_name
= sp
, arg
, file
, pushsw
);
847 context_save (); /* save the context file */
853 adios("fork", "failed:");
856 vec
= argsplit(sendproc
, &program
, &vecp
);
858 vec
[vecp
++] = "-push";
861 vec
[vecp
++] = *arg
++;
865 execvp (program
, vec
);
866 fprintf (stderr
, "unable to exec ");
871 if (pidwait(child_id
, OK
) == 0)
879 * Translate MIME composition file (call buildmimeproc)
883 buildfile (char **argp
, char *file
)
890 /* allocate space for arguments */
896 args
= mh_xmalloc((i
+ 2) * sizeof(char *));
899 * For backward compatibility, we need to add -build
900 * if we are using mhn as buildmimeproc
903 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
904 args
[i
++] = "-build";
906 /* copy any other arguments */
907 while (argp
&& *argp
)
911 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
914 return i
? NOTOK
: OK
;
919 # define SASLminc(a) (a)
920 #else /* CYRUS_SASL */
921 # define SASLminc(a) 0
922 #endif /* CYRUS_SASL */
925 # define TLSminc(a) (a)
926 #else /* TLS_SUPPORT */
927 # define TLSminc(a) 0
928 #endif /* TLS_SUPPORT */
930 #define SEND_SWITCHES \
931 X("alias aliasfile", 0, ALIASW) \
932 X("debug", -5, DEBUGSW) \
933 X("filter filterfile", 0, FILTSW) \
934 X("nofilter", 0, NFILTSW) \
935 X("format", 0, FRMTSW) \
936 X("noformat", 0, NFRMTSW) \
937 X("forward", 0, FORWSW) \
938 X("noforward", 0, NFORWSW) \
939 X("mime", 0, MIMESW) \
940 X("nomime", 0, NMIMESW) \
941 X("msgid", 0, MSGDSW) \
942 X("nomsgid", 0, NMSGDSW) \
943 X("push", 0, SPSHSW) \
944 X("nopush", 0, NSPSHSW) \
945 X("split seconds", 0, SPLITSW) \
946 X("unique", -6, UNIQSW) \
947 X("nounique", -8, NUNIQSW) \
948 X("verbose", 0, VERBSW) \
949 X("noverbose", 0, NVERBSW) \
950 X("watch", 0, WATCSW) \
951 X("nowatch", 0, NWATCSW) \
952 X("width columns", 0, WIDTHSW) \
953 X("version", 0, SVERSIONSW) \
954 X("help", 0, SHELPSW) \
955 X("dashstuffing", -12, BITSTUFFSW) \
956 X("nodashstuffing", -14, NBITSTUFFSW) \
957 X("client host", -6, CLIESW) \
958 X("server host", 6, SERVSW) \
959 X("snoop", -5, SNOOPSW) \
960 X("draftfolder +folder", 0, SDRFSW) \
961 X("draftmessage msg", 0, SDRMSW) \
962 X("nodraftfolder", 0, SNDRFSW) \
963 X("sasl", SASLminc(4), SASLSW) \
964 X("nosasl", SASLminc(6), NOSASLSW) \
965 X("saslmech", SASLminc(5), SASLMECHSW) \
966 X("authservice", SASLminc(0), AUTHSERVICESW) \
967 X("user username", SASLminc(4), USERSW) \
968 X("port server-port-name/number", 4, PORTSW) \
969 X("tls", TLSminc(-3), TLSSW) \
970 X("initialtls", TLSminc(-10), INITTLSSW) \
971 X("notls", TLSminc(-5), NTLSSW) \
972 X("certverify", TLSminc(-10), CERTVERSW) \
973 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
974 X("sendmail program", 0, MTSSM) \
975 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
976 X("messageid localname|random", 2, MESSAGEIDSW) \
978 #define X(sw, minchars, id) id,
979 DEFINE_SWITCH_ENUM(SEND
);
982 #define X(sw, minchars, id) { sw, minchars, id },
983 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
987 extern int debugsw
; /* from sendsbr.c */
995 extern char *altmsg
; /* .. */
996 extern char *annotext
;
997 extern char *distfile
;
1001 sendit (char *sp
, char **arg
, char *file
, int pushed
)
1004 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
1005 char **arguments
, *savearg
[MAXARGS
], **vec
;
1006 const char *user
= NULL
, *saslmech
= NULL
;
1007 char *auth_svc
= NULL
;
1011 * Make sure these are defined. In particular, we need
1012 * savearg[1] to be NULL, in case "arg" is NULL below. It
1013 * doesn't matter what is the value of savearg[0], but we
1014 * set it to NULL, to help catch "off-by-one" errors.
1020 * Temporarily copy arg to savearg, since the brkstring() call in
1021 * getarguments() will wipe it out before it is merged in.
1022 * Also, we skip the first element of savearg, since getarguments()
1023 * skips it. Then we count the number of arguments
1024 * copied. The value of "n" will be one greater than
1025 * this in order to simulate the standard argc/argv.
1030 copyip (arg
, savearg
+1, MAXARGS
-1);
1037 * Merge any arguments from command line (now in savearg)
1038 * and arguments from profile.
1040 arguments
= getarguments (sp
, n
, savearg
, 1);
1053 * Get our initial arguments for postproc now
1056 vec
= argsplit(postproc
, &program
, &vecp
);
1058 vec
[vecp
++] = "-library";
1059 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
1061 if ((cp
= context_find ("fileproc"))) {
1062 vec
[vecp
++] = "-fileproc";
1066 if ((cp
= context_find ("mhlproc"))) {
1067 vec
[vecp
++] = "-mhlproc";
1071 if ((cp
= context_find ("credentials"))) {
1072 /* post doesn't read context so need to pass credentials. */
1073 vec
[vecp
++] = "-credentials";
1077 while ((cp
= *argp
++)) {
1079 switch (smatch (++cp
, sendswitches
)) {
1081 ambigsw (cp
, sendswitches
);
1084 inform("-%s unknown\n", cp
);
1088 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1089 print_help (buf
, sendswitches
, 1);
1092 print_version (invo_name
);
1103 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1104 inform("missing argument to %s", argp
[-2]);
1160 #ifdef OAUTH_SUPPORT
1161 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
1162 die("missing argument to %s", argp
[-2]);
1165 NMH_UNUSED (auth_svc
);
1166 die("not built with OAuth support");
1184 if (!(cp
= *argp
++) || *cp
== '-') {
1185 inform("missing argument to %s", argp
[-2]);
1194 if (!(cp
= *argp
++) || *cp
== '-') {
1195 inform("missing argument to %s", argp
[-2]);
1203 inform("usage: %s [switches]", sp
);
1207 /* allow Aliasfile: profile entry */
1208 if ((cp
= context_find ("Aliasfile"))) {
1211 dp
= mh_xstrdup(cp
);
1212 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1213 vec
[vecp
++] = "-alias";
1218 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1219 if ((cp
= context_find ("signature")) && *cp
)
1220 setenv("SIGNATURE", cp
, 1);
1222 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1224 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1226 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1227 inplace
= atoi (cp
);
1229 if ((cp
= getenv ("mhdist"))
1233 vec
[vecp
++] = "-dist";
1234 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1235 die("unable to create temporary file");
1237 distfile
= mh_xstrdup(cp
);
1238 (void) m_unlink(distfile
);
1239 if (link (altmsg
, distfile
) == NOTOK
)
1240 adios (distfile
, "unable to link %s to", altmsg
);
1245 #ifdef OAUTH_SUPPORT
1246 if (auth_svc
== NULL
) {
1247 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
1248 die("must specify -authservice with -saslmech xoauth2");
1252 die("must specify -user with -saslmech xoauth2");
1256 NMH_UNUSED(saslmech
);
1257 #endif /* OAUTH_SUPPORT */
1259 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1264 if ((pushsw
= pushed
))
1269 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, auth_svc
) == OK
)
1278 whomfile (char **arg
, char *file
)
1282 char **vec
, *program
;
1284 context_save (); /* save the context file */
1287 switch (pid
= fork()) {
1289 advise ("fork", "unable to");
1293 vec
= argsplit(whomproc
, &program
, &vecp
);
1296 vec
[vecp
++] = *arg
++;
1300 execvp (program
, vec
);
1301 fprintf (stderr
, "unable to exec ");
1303 _exit(1); /* NOTREACHED */
1306 return pidwait(pid
, NOTOK
) & 0377 ? 1 : 0;
1312 * Remove the draft file
1316 removefile (char *drft
)
1318 if (m_unlink (drft
) == NOTOK
)
1319 adios (drft
, "unable to unlink");
1326 * Return 1 if we already have a MIME-Version header, 0 otherwise.
1330 checkmimeheader (char *drft
)
1333 m_getfld_state_t gstate
;
1334 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1336 bool retval
= false;
1338 if ((f
= fopen(drft
, "r")) == NULL
) {
1339 admonish(drft
, "unable to read draft");
1343 gstate
= m_getfld_state_init(f
);
1345 int bufsz
= sizeof(buf
);
1346 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1349 if (strcasecmp(name
, VRSN_FIELD
) == 0) {
1350 inform("Cannot use attach commands with already-"
1351 "formatted MIME message \"%s\"", drft
);
1362 m_getfld_state_destroy(&gstate
);