]>
diplodocus.org Git - nmh/blob - uip/whatnowsbr.c
7f28f34a18580b9698e1d9272d13a8fffcca9a01
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 #ifdef READLINE_SUPPORT
47 #include "sbr/read_switch_multiword_via_readline.h"
48 #else /* ! READLINE_SUPPORT */
49 #include "sbr/read_switch_multiword.h"
50 #endif /* READLINE_SUPPORT */
51 #include "sbr/concat.h"
52 #include "sbr/showfile.h"
53 #include "sbr/smatch.h"
54 #include "sbr/r1bindex.h"
55 #include "sbr/refile.h"
56 #include "sbr/copyip.h"
57 #include "sbr/getcpy.h"
58 #include "sbr/m_draft.h"
59 #include "sbr/m_backup.h"
60 #include "sbr/geteditor.h"
61 #include "sbr/context_save.h"
62 #include "sbr/context_find.h"
63 #include "sbr/brkstring.h"
64 #include "sbr/ambigsw.h"
67 #include "sbr/print_version.h"
68 #include "sbr/print_help.h"
69 #include "sbr/arglist.h"
70 #include "sbr/error.h"
78 #include "sbr/m_maildir.h"
79 #include "sbr/m_mktemp.h"
80 #include "sbr/mime_type.h"
82 #define WHATNOW_SWITCHES \
83 X("draftfolder +folder", 0, DFOLDSW) \
84 X("draftmessage msg", 0, DMSGSW) \
85 X("nodraftfolder", 0, NDFLDSW) \
86 X("editor editor", 0, EDITRSW) \
87 X("noedit", 0, NEDITSW) \
88 X("prompt string", 4, PRMPTSW) \
89 X("version", 0, VERSIONSW) \
90 X("help", 0, HELPSW) \
93 #define X(sw, minchars, id) id,
94 DEFINE_SWITCH_ENUM(WHATNOW
);
97 #define X(sw, minchars, id) { sw, minchars, id },
98 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
102 * Options at the "whatnow" prompt
104 #define PROMPT_SWITCHES \
105 X("edit [<editor> <switches>]", 0, EDITSW) \
106 X("refile [<switches>] +folder", 0, REFILEOPT) \
107 X("mime [<switches>]", 0, BUILDMIMESW) \
108 X("display [<switches>]", 0, DISPSW) \
109 X("list [<switches>]", 0, LISTSW) \
110 X("send [<switches>]", 0, SENDSW) \
111 X("push [<switches>]", 0, PUSHSW) \
112 X("whom [<switches>]", 0, WHOMSW) \
113 X("quit [-delete]", 0, QUITSW) \
114 X("delete", 0, DELETESW) \
115 X("cd [directory]", 0, CDCMDSW) \
116 X("pwd", 0, PWDCMDSW) \
117 X("ls", 2, LSCMDSW) \
118 X("attach [-v]", 0, ATTACHCMDSW) \
119 X("detach [-n]", 0, DETACHCMDSW) \
120 X("alist [-ln] ", 2, ALISTCMDSW) \
122 #define X(sw, minchars, id) id,
123 DEFINE_SWITCH_ENUM(PROMPT
);
126 #define X(sw, minchars, id) { sw, minchars, id },
127 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
130 static char *myprompt
= "\nWhat now? ";
135 static int editfile (char **, char **, char *, int, struct msgs
*,
136 char *, char *, int, int);
137 static int sendfile (char **, char *, int);
138 static void sendit (char *, char **, char *, int);
139 static int buildfile (char **, char *);
140 static int whomfile (char **, char *);
141 static int removefile (char *);
142 static int checkmimeheader (char *);
143 static void writelscmd(char *, int, char *, char **);
144 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
145 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
146 static int system_in_dir(const char *dir
, const char *cmd
);
147 static int copyf (char *, char *);
151 WhatNow (int argc
, char **argv
)
155 int use
= 0, atfile
= 1;
156 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
157 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
159 char **argp
, **arguments
;
161 char cwd
[PATH_MAX
+ 1]; /* current working directory */
162 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
163 FILE *f
; /* read pointer for bgnd proc */
164 char *l
; /* set on -l to alist command */
165 int n
; /* set on -n to alist command */
167 /* Need this if called from what_now(). */
168 invo_name
= r1bindex (argv
[0], '/');
170 arguments
= getarguments (invo_name
, argc
, argv
, 1);
174 * Get the initial current working directory.
177 if (getcwd(cwd
, sizeof (cwd
)) == NULL
) {
178 adios("getcwd", "could not get working directory");
181 while ((cp
= *argp
++)) {
183 switch (smatch (++cp
, whatnowswitches
)) {
185 ambigsw (cp
, whatnowswitches
);
188 die("-%s unknown", cp
);
191 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
192 print_help (buf
, whatnowswitches
, 1);
195 print_version(invo_name
);
200 die("only one draft folder at a time!");
201 if (!(cp
= *argp
++) || *cp
== '-')
202 die("missing argument to %s", argp
[-2]);
203 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
204 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
208 die("only one draft message at a time!");
209 if (!(dmsg
= *argp
++) || *dmsg
== '-')
210 die("missing argument to %s", argp
[-2]);
218 if (!(ed
= *argp
++) || *ed
== '-')
219 die("missing argument to %s", argp
[-2]);
227 if (!(myprompt
= *argp
++) || *myprompt
== '-')
228 die("missing argument to %s", argp
[-2]);
233 die("only one draft at a time!");
237 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
238 drft
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
240 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? mh_xstrdup(cp
) : NULL
;
242 if ((cp
= getenv ("mhatfile")) && *cp
)
245 if ((cp
= getenv ("mhuse")) && *cp
)
248 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
253 /* start editing the draft, unless -noedit was given */
254 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
255 NULL
, 1, atfile
) < 0)
259 #ifdef READLINE_SUPPORT
260 if (!(argp
= read_switch_multiword_via_readline(myprompt
, aleqs
))) {
261 #else /* ! READLINE_SUPPORT */
262 if (!(argp
= read_switch_multiword(myprompt
, aleqs
))) {
263 #endif /* READLINE_SUPPORT */
264 (void) m_unlink (LINK
);
267 switch (smatch (*argp
, aleqs
)) {
269 /* display the message being replied to, or distributed */
271 showfile (++argp
, msgnam
);
273 inform("no alternate message to display");
277 /* Translate MIME composition file */
278 buildfile (++argp
, drft
);
282 /* Call an editor on the draft file */
285 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
286 NULL
, 1, atfile
) == NOTOK
)
291 /* display the draft file */
292 showfile (++argp
, drft
);
296 /* Check to whom the draft would be sent */
297 whomfile (++argp
, drft
);
301 /* Quit, and possibly delete the draft */
302 if (*++argp
&& (*argp
[0] == 'd' ||
303 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
306 if (stat (drft
, &st
) != NOTOK
)
307 inform("draft left on %s", drft
);
312 /* Delete draft and exit */
317 /* Send draft in background */
318 if (sendfile (++argp
, drft
, 1))
324 sendfile (++argp
, drft
, 0);
328 /* Refile the draft */
329 if (refile (++argp
, drft
) == 0)
334 /* Change the working directory for attachments
336 * Run the directory through the user's shell so that
337 * we can take advantage of any syntax that the user
338 * is accustomed to. Read back the absolute path.
341 if (*(argp
+1) == NULL
) {
342 strcpy(buf
, "$SHELL -c \"cd&&pwd\"");
345 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
347 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
348 if (fgets(cwd
, sizeof (cwd
), f
) == NULL
) {
349 advise (buf
, "fgets");
351 trim_suffix_c(cwd
, '\n');
355 advise("popen", "could not get directory");
361 /* Print the working directory for attachments */
366 /* List files in the current attachment working directory
368 * Use the user's shell so that we can take advantage of any
369 * syntax that the user is accustomed to.
371 writelscmd(buf
, sizeof(buf
), "", argp
);
372 (void)system_in_dir(cwd
, buf
);
377 * List attachments on current draft. Options are:
379 * -l long listing (full path names)
383 if (checkmimeheader(drft
))
389 while (*++argp
!= NULL
) {
390 if (strcmp(*argp
, "-l") == 0)
393 else if (strcmp(*argp
, "-n") == 0)
396 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
408 inform("usage is alist [-ln].");
411 annolist(drft
, ATTACH_FIELD
, l
, n
);
417 * Attach files to current draft.
420 bool verbose
= false;
423 if (checkmimeheader(drft
))
426 for (ap
= argp
+1; *ap
; ++ap
) {
427 if (strcmp(*ap
, "-v") == 0) {
430 } else if (*ap
[0] != '-') {
435 if (*(argp
+1) == NULL
) {
436 inform("attach command requires file argument(s).");
441 * Build a command line that causes the user's shell to list the file name
442 * arguments. This handles and wildcard expansion, tilde expansion, etc.
444 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
447 * Read back the response from the shell, which contains a number of lines
448 * with one file name per line. Remove off the newline. Determine whether
449 * we have an absolute or relative path name. Prepend the current working
450 * directory to relative path names. Add the attachment annotation to the
454 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
455 char file
[2 * PATH_MAX
+ 2]; /* file name buffer */
457 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
460 trim_suffix_c(shell
, '\n');
463 strncpy(file
, shell
, sizeof(file
));
464 file
[sizeof(file
) - 1] = '\0';
466 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
469 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
471 ctype
= mime_type(file
);
472 printf ("Attaching %s as a %s\n", file
, ctype
);
480 advise("popen", "could not get file from shell");
487 * Detach files from current draft.
491 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
492 * so this catches a -n anywhere in the argument list.
495 if (checkmimeheader(drft
))
498 for (n
= 0, arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
499 if (strcmp(*arguments
, "-n") == 0) {
506 * A -n was found so interpret the arguments as attachment numbers.
507 * Decrement any remaining argument number that is greater than the one
508 * just processed after processing each one so that the numbering stays
513 for (arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
514 if (strcmp(*arguments
, "-n") == 0)
517 if (**arguments
!= '\0') {
518 n
= atoi(*arguments
);
519 annotate(drft
, ATTACH_FIELD
, NULL
, 1, 0, n
, 1);
521 for (argp
= arguments
+ 1; *argp
!= NULL
; argp
++) {
522 if (atoi(*argp
) > n
) {
523 if (atoi(*argp
) == 1)
526 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
534 * The arguments are interpreted as file names. Run them through the
535 * user's shell for wildcard expansion and other goodies. Do this from
536 * the current working directory if the argument is not an absolute path
537 * name (does not begin with a /).
539 * We feed all the file names to the shell at once, otherwise you can't
540 * provide a file name with a space in it.
542 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
543 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
544 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
545 trim_suffix_c(shell
, '\n');
546 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
550 advise("popen", "could not get file from shell");
556 /* Unknown command */
566 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
568 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
571 /* Note that we do not quote -- the argp from the user
572 * is assumed to be quoted as they desire. (We can't treat
573 * it as pure literal as that would prevent them using ~,
574 * wildcards, etc.) The buffer produced by this function
575 * should be given to popen_in_dir() or system_in_dir() so
576 * that the current working directory is set correctly.
578 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
579 /* NB that some snprintf() return -1 on overflow rather than the
580 * new C99 mandated 'number of chars that would have been written'
582 /* length checks here and inside the loop allow for the
583 * trailing "&&", trailcmd, '"' and NUL
585 int trailln
= strlen(trailcmd
) + 4;
586 if (ln
< 0 || ln
+ trailln
> bufsz
)
587 die("arguments too long");
591 while (*argp
&& *++argp
) {
593 /* +1 for leading space */
594 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
595 die("arguments too long");
597 memcpy(cp
, *argp
, ln
+1);
601 *cp
++ = '&'; *cp
++ = '&';
602 strcpy(cp
, trailcmd
);
610 * Build a command line that causes the user's shell to list the file name
611 * arguments. This handles and wildcard expansion, tilde expansion, etc.
614 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
616 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
617 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
621 /* Like system(), but run the command in directory dir.
622 * This assumes the program is single-threaded!
625 system_in_dir(const char *dir
, const char *cmd
)
630 /* ensure that $SHELL exists, as the cmd was written relying on
631 a non-blank $SHELL... */
632 setenv("SHELL","/bin/sh",0); /* don't overwrite */
634 if (getcwd(olddir
, sizeof(olddir
)) == 0)
635 adios("getcwd", "could not get working directory");
637 adios("chdir", "could not change working directory");
639 if (chdir(olddir
) != 0)
640 adios("chdir", "could not change working directory");
644 /* ditto for popen() */
646 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
651 /* ensure that $SHELL exists, as the cmd was written relying on
652 a non-blank $SHELL... */
653 setenv("SHELL","/bin/sh",0); /* don't overwrite */
655 if (getcwd(olddir
, sizeof(olddir
)) == 0)
656 adios("getcwd", "could not get working directory");
658 adios("chdir", "could not change working directory");
659 f
= popen(cmd
, type
);
660 if (chdir(olddir
) != 0)
661 adios("chdir", "could not change working directory");
670 static bool reedit
; /* have we been here before? */
671 static char *edsave
= NULL
; /* the editor we used previously */
675 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
676 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
678 int pid
, status
, vecp
;
679 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
680 char *cp
, *prog
, **vec
;
683 bool slinked
= false;
685 /* Was there a previous edit session? */
686 if (reedit
&& (*ed
|| edsave
)) {
687 if (!*ed
) { /* no explicit editor */
688 *ed
= edsave
; /* so use the previous one */
689 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
692 /* unless we've specified it via "editor-next" */
693 cp
= concat (cp
, "-next", NULL
);
694 if ((cp
= context_find (cp
)) != NULL
)
698 /* set initial editor */
700 *ed
= get_default_editor();
704 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
705 strncpy (altpath
, altmsg
, sizeof(altpath
) - 1);
707 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
709 strncpy (linkpath
, LINK
, sizeof(linkpath
));
711 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
714 (void) m_unlink (linkpath
);
715 if (link (altpath
, linkpath
) == NOTOK
) {
716 if (symlink (altpath
, linkpath
) < 0) {
717 adios (linkpath
, "symlink");
726 context_save (); /* save the context file */
729 switch (pid
= fork()) {
731 advise ("fork", "unable to");
737 if (chdir (cwd
) < 0) {
738 advise (cwd
, "chdir");
743 setenv("mhfolder", mp
->foldpath
, 1);
744 setenv("editalt", altpath
, 1);
747 vec
= argsplit(*ed
, &prog
, &vecp
);
751 vec
[vecp
++] = *arg
++;
756 fprintf (stderr
, "unable to exec ");
761 if ((status
= pidwait (pid
, NOTOK
))) {
762 if (((status
& 0xff00) != 0xff00)
763 && (!reedit
|| (status
& 0x00ff))) {
764 if (!use
&& (status
& 0xff00) &&
765 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
766 inform("problems with edit--draft left in %s", cp
);
768 inform("problems with edit--%s preserved", file
);
771 status
= -2; /* maybe "reedit ? -2 : -1"? */
780 ? lstat (linkpath
, &st
) != NOTOK
781 && S_ISREG(st
.st_mode
)
782 && copyf (linkpath
, altpath
) == NOTOK
783 : stat (linkpath
, &st
) != NOTOK
785 && (m_unlink (altpath
) == NOTOK
786 || link (linkpath
, altpath
) == NOTOK
)))
787 advise (linkpath
, "unable to update %s from", altmsg
);
790 /* normally, we remember which editor we used */
792 edsave
= getcpy (*ed
);
795 if (altmsg
&& atfile
)
796 (void) m_unlink (linkpath
);
803 copyf (char *ifile
, char *ofile
)
808 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
810 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
811 admonish (ofile
, "unable to open and truncate");
816 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
817 if (write (out
, buffer
, i
) != i
) {
818 advise (ofile
, "may have damaged");
834 sendfile (char **arg
, char *file
, int pushsw
)
838 char *cp
, *sp
, **vec
, *program
;
841 * If the sendproc is the nmh command `send', then we call
842 * those routines directly rather than exec'ing the command.
844 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
846 sendit (invo_name
= sp
, arg
, file
, pushsw
);
851 context_save (); /* save the context file */
857 adios("fork", "failed:");
860 vec
= argsplit(sendproc
, &program
, &vecp
);
862 vec
[vecp
++] = "-push";
865 vec
[vecp
++] = *arg
++;
869 execvp (program
, vec
);
870 fprintf (stderr
, "unable to exec ");
875 if (pidwait(child_id
, OK
) == 0)
883 * Translate MIME composition file (call buildmimeproc)
887 buildfile (char **argp
, char *file
)
894 /* allocate space for arguments */
900 args
= mh_xmalloc((i
+ 2) * sizeof(char *));
903 * For backward compatibility, we need to add -build
904 * if we are using mhn as buildmimeproc
907 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
908 args
[i
++] = "-build";
910 /* copy any other arguments */
911 while (argp
&& *argp
)
915 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
918 return i
? NOTOK
: OK
;
923 # define SASLminc(a) (a)
924 #else /* CYRUS_SASL */
925 # define SASLminc(a) 0
926 #endif /* CYRUS_SASL */
929 # define TLSminc(a) (a)
930 #else /* TLS_SUPPORT */
931 # define TLSminc(a) 0
932 #endif /* TLS_SUPPORT */
934 #define SEND_SWITCHES \
935 X("alias aliasfile", 0, ALIASW) \
936 X("debug", -5, DEBUGSW) \
937 X("filter filterfile", 0, FILTSW) \
938 X("nofilter", 0, NFILTSW) \
939 X("format", 0, FRMTSW) \
940 X("noformat", 0, NFRMTSW) \
941 X("forward", 0, FORWSW) \
942 X("noforward", 0, NFORWSW) \
943 X("mime", 0, MIMESW) \
944 X("nomime", 0, NMIMESW) \
945 X("msgid", 0, MSGDSW) \
946 X("nomsgid", 0, NMSGDSW) \
947 X("push", 0, SPSHSW) \
948 X("nopush", 0, NSPSHSW) \
949 X("split seconds", 0, SPLITSW) \
950 X("unique", -6, UNIQSW) \
951 X("nounique", -8, NUNIQSW) \
952 X("verbose", 0, VERBSW) \
953 X("noverbose", 0, NVERBSW) \
954 X("watch", 0, WATCSW) \
955 X("nowatch", 0, NWATCSW) \
956 X("width columns", 0, WIDTHSW) \
957 X("version", 0, SVERSIONSW) \
958 X("help", 0, SHELPSW) \
959 X("dashstuffing", -12, BITSTUFFSW) \
960 X("nodashstuffing", -14, NBITSTUFFSW) \
961 X("client host", -6, CLIESW) \
962 X("server host", 6, SERVSW) \
963 X("snoop", -5, SNOOPSW) \
964 X("draftfolder +folder", 0, SDRFSW) \
965 X("draftmessage msg", 0, SDRMSW) \
966 X("nodraftfolder", 0, SNDRFSW) \
967 X("sasl", SASLminc(4), SASLSW) \
968 X("nosasl", SASLminc(6), NOSASLSW) \
969 X("saslmech", SASLminc(5), SASLMECHSW) \
970 X("authservice", SASLminc(0), AUTHSERVICESW) \
971 X("user username", SASLminc(4), USERSW) \
972 X("port server-port-name/number", 4, PORTSW) \
973 X("tls", TLSminc(-3), TLSSW) \
974 X("initialtls", TLSminc(-10), INITTLSSW) \
975 X("notls", TLSminc(-5), NTLSSW) \
976 X("certverify", TLSminc(-10), CERTVERSW) \
977 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
978 X("sendmail program", 0, MTSSM) \
979 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
980 X("messageid localname|random", 2, MESSAGEIDSW) \
982 #define X(sw, minchars, id) id,
983 DEFINE_SWITCH_ENUM(SEND
);
986 #define X(sw, minchars, id) { sw, minchars, id },
987 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
991 extern int debugsw
; /* from sendsbr.c */
998 extern char *altmsg
; /* .. */
999 extern char *annotext
;
1000 extern char *distfile
;
1004 sendit (char *sp
, char **arg
, char *file
, int pushed
)
1007 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
1008 char **arguments
, *savearg
[MAXARGS
], **vec
;
1009 const char *user
= NULL
, *saslmech
= NULL
;
1010 char *auth_svc
= NULL
;
1014 * Make sure these are defined. In particular, we need
1015 * savearg[1] to be NULL, in case "arg" is NULL below. It
1016 * doesn't matter what is the value of savearg[0], but we
1017 * set it to NULL, to help catch "off-by-one" errors.
1023 * Temporarily copy arg to savearg, since the brkstring() call in
1024 * getarguments() will wipe it out before it is merged in.
1025 * Also, we skip the first element of savearg, since getarguments()
1026 * skips it. Then we count the number of arguments
1027 * copied. The value of "n" will be one greater than
1028 * this in order to simulate the standard argc/argv.
1033 copyip (arg
, savearg
+1, MAXARGS
-1);
1040 * Merge any arguments from command line (now in savearg)
1041 * and arguments from profile.
1043 arguments
= getarguments (sp
, n
, savearg
, 1);
1056 * Get our initial arguments for postproc now
1059 vec
= argsplit(postproc
, &program
, &vecp
);
1061 vec
[vecp
++] = "-library";
1062 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
1064 if ((cp
= context_find ("fileproc"))) {
1065 vec
[vecp
++] = "-fileproc";
1069 if ((cp
= context_find ("mhlproc"))) {
1070 vec
[vecp
++] = "-mhlproc";
1074 if ((cp
= context_find ("credentials"))) {
1075 /* post doesn't read context so need to pass credentials. */
1076 vec
[vecp
++] = "-credentials";
1080 while ((cp
= *argp
++)) {
1082 switch (smatch (++cp
, sendswitches
)) {
1084 ambigsw (cp
, sendswitches
);
1087 inform("-%s unknown\n", cp
);
1091 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1092 print_help (buf
, sendswitches
, 1);
1095 print_version (invo_name
);
1156 #ifdef OAUTH_SUPPORT
1157 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
1158 die("missing argument to %s", argp
[-2]);
1161 NMH_UNUSED (auth_svc
);
1162 die("not built with OAuth support");
1180 if (!(cp
= *argp
++) || *cp
== '-') {
1181 inform("missing argument to %s", argp
[-2]);
1190 if (!(cp
= *argp
++) || *cp
== '-') {
1191 inform("missing argument to %s", argp
[-2]);
1199 inform("usage: %s [switches]", sp
);
1203 /* allow Aliasfile: profile entry */
1204 if ((cp
= context_find ("Aliasfile"))) {
1207 dp
= mh_xstrdup(cp
);
1208 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1209 vec
[vecp
++] = "-alias";
1214 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1215 if ((cp
= context_find ("signature")) && *cp
)
1216 setenv("SIGNATURE", cp
, 1);
1218 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1220 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1222 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1223 inplace
= atoi (cp
);
1225 if ((cp
= getenv ("mhdist"))
1229 vec
[vecp
++] = "-dist";
1230 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1231 die("unable to create temporary file");
1233 distfile
= mh_xstrdup(cp
);
1234 (void) m_unlink(distfile
);
1235 if (link (altmsg
, distfile
) == NOTOK
)
1236 adios (distfile
, "unable to link %s to", altmsg
);
1241 #ifdef OAUTH_SUPPORT
1242 if (auth_svc
== NULL
) {
1243 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
1244 die("must specify -authservice with -saslmech xoauth2");
1248 die("must specify -user with -saslmech xoauth2");
1252 NMH_UNUSED(saslmech
);
1253 #endif /* OAUTH_SUPPORT */
1255 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1260 if ((pushsw
= pushed
))
1263 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, auth_svc
) == OK
)
1272 whomfile (char **arg
, char *file
)
1276 char **vec
, *program
;
1278 context_save (); /* save the context file */
1281 switch (pid
= fork()) {
1283 advise ("fork", "unable to");
1287 vec
= argsplit(whomproc
, &program
, &vecp
);
1290 vec
[vecp
++] = *arg
++;
1294 execvp (program
, vec
);
1295 fprintf (stderr
, "unable to exec ");
1297 _exit(1); /* NOTREACHED */
1300 return pidwait(pid
, NOTOK
) & 0377 ? 1 : 0;
1306 * Remove the draft file
1310 removefile (char *drft
)
1312 if (m_unlink (drft
) == NOTOK
)
1313 adios (drft
, "unable to unlink");
1320 * Return 1 if we already have a MIME-Version header, 0 otherwise.
1324 checkmimeheader (char *drft
)
1327 m_getfld_state_t gstate
;
1328 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1330 bool retval
= false;
1332 if ((f
= fopen(drft
, "r")) == NULL
) {
1333 admonish(drft
, "unable to read draft");
1337 gstate
= m_getfld_state_init(f
);
1339 int bufsz
= sizeof(buf
);
1340 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1343 if (strcasecmp(name
, VRSN_FIELD
) == 0) {
1344 inform("Cannot use attach commands with already-"
1345 "formatted MIME message \"%s\"", drft
);
1356 m_getfld_state_destroy(&gstate
);