]>
diplodocus.org Git - nmh/blob - uip/whatnowsbr.c
a2f872c45767c7d3dcf17403e93897cd85e57871
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/read_switch_multiword_via_readline.h"
42 #include "sbr/concat.h"
43 #include "sbr/showfile.h"
44 #include "sbr/smatch.h"
45 #include "sbr/r1bindex.h"
46 #include "sbr/refile.h"
47 #include "sbr/closefds.h"
48 #include "sbr/copyip.h"
49 #include "sbr/getcpy.h"
50 #include "sbr/m_draft.h"
51 #include "sbr/m_backup.h"
52 #include "sbr/geteditor.h"
53 #include "sbr/context_save.h"
54 #include "sbr/context_find.h"
55 #include "sbr/brkstring.h"
56 #include "sbr/ambigsw.h"
59 #include "sbr/print_version.h"
60 #include "sbr/print_help.h"
61 #include "sbr/arglist.h"
62 #include "sbr/error.h"
70 #include "sbr/m_maildir.h"
71 #include "sbr/m_mktemp.h"
72 #include "sbr/mime_type.h"
74 #define WHATNOW_SWITCHES \
75 X("draftfolder +folder", 0, DFOLDSW) \
76 X("draftmessage msg", 0, DMSGSW) \
77 X("nodraftfolder", 0, NDFLDSW) \
78 X("editor editor", 0, EDITRSW) \
79 X("noedit", 0, NEDITSW) \
80 X("prompt string", 4, PRMPTSW) \
81 X("version", 0, VERSIONSW) \
82 X("help", 0, HELPSW) \
85 #define X(sw, minchars, id) id,
86 DEFINE_SWITCH_ENUM(WHATNOW
);
89 #define X(sw, minchars, id) { sw, minchars, id },
90 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
94 * Options at the "whatnow" prompt
96 #define PROMPT_SWITCHES \
97 X("edit [<editor> <switches>]", 0, EDITSW) \
98 X("refile [<switches>] +folder", 0, REFILEOPT) \
99 X("mime [<switches>]", 0, BUILDMIMESW) \
100 X("display [<switches>]", 0, DISPSW) \
101 X("list [<switches>]", 0, LISTSW) \
102 X("send [<switches>]", 0, SENDSW) \
103 X("push [<switches>]", 0, PUSHSW) \
104 X("whom [<switches>]", 0, WHOMSW) \
105 X("quit [-delete]", 0, QUITSW) \
106 X("delete", 0, DELETESW) \
107 X("cd [directory]", 0, CDCMDSW) \
108 X("pwd", 0, PWDCMDSW) \
109 X("ls", 2, LSCMDSW) \
110 X("attach [-v]", 0, ATTACHCMDSW) \
111 X("detach [-n]", 0, DETACHCMDSW) \
112 X("alist [-ln] ", 2, ALISTCMDSW) \
114 #define X(sw, minchars, id) id,
115 DEFINE_SWITCH_ENUM(PROMPT
);
118 #define X(sw, minchars, id) { sw, minchars, id },
119 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
122 static char *myprompt
= "\nWhat now? ";
127 static int editfile (char **, char **, char *, int, struct msgs
*,
128 char *, char *, int, int);
129 static int sendfile (char **, char *, int);
130 static void sendit (char *, char **, char *, int);
131 static int buildfile (char **, char *);
132 static int whomfile (char **, char *);
133 static int removefile (char *);
134 static int checkmimeheader (char *);
135 static void writelscmd(char *, int, char *, char **);
136 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
137 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
138 static int system_in_dir(const char *dir
, const char *cmd
);
139 static int copyf (char *, char *);
143 WhatNow (int argc
, char **argv
)
147 int use
= 0, atfile
= 1;
148 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
149 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
151 char **argp
, **arguments
;
153 char cwd
[PATH_MAX
+ 1]; /* current working directory */
154 char file
[PATH_MAX
+ 1]; /* file name buffer */
155 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
156 FILE *f
; /* read pointer for bgnd proc */
157 char *l
; /* set on -l to alist command */
158 int n
; /* set on -n to alist command */
160 /* Need this if called from what_now(). */
161 invo_name
= r1bindex (argv
[0], '/');
163 arguments
= getarguments (invo_name
, argc
, argv
, 1);
167 * Get the initial current working directory.
170 if (getcwd(cwd
, sizeof (cwd
)) == NULL
) {
171 adios("getcwd", "could not get working directory");
174 while ((cp
= *argp
++)) {
176 switch (smatch (++cp
, whatnowswitches
)) {
178 ambigsw (cp
, whatnowswitches
);
181 die("-%s unknown", cp
);
184 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
185 print_help (buf
, whatnowswitches
, 1);
188 print_version(invo_name
);
193 die("only one draft folder at a time!");
194 if (!(cp
= *argp
++) || *cp
== '-')
195 die("missing argument to %s", argp
[-2]);
196 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
197 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
201 die("only one draft message at a time!");
202 if (!(dmsg
= *argp
++) || *dmsg
== '-')
203 die("missing argument to %s", argp
[-2]);
211 if (!(ed
= *argp
++) || *ed
== '-')
212 die("missing argument to %s", argp
[-2]);
220 if (!(myprompt
= *argp
++) || *myprompt
== '-')
221 die("missing argument to %s", argp
[-2]);
226 die("only one draft at a time!");
230 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
231 drft
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
233 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? mh_xstrdup(cp
) : NULL
;
235 if ((cp
= getenv ("mhatfile")) && *cp
)
238 if ((cp
= getenv ("mhuse")) && *cp
)
241 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
246 /* start editing the draft, unless -noedit was given */
247 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
248 NULL
, 1, atfile
) < 0)
252 #ifdef READLINE_SUPPORT
253 if (!(argp
= read_switch_multiword_via_readline(myprompt
, aleqs
))) {
254 #else /* ! READLINE_SUPPORT */
255 if (!(argp
= read_switch_multiword(myprompt
, aleqs
))) {
256 #endif /* READLINE_SUPPORT */
257 (void) m_unlink (LINK
);
260 switch (smatch (*argp
, aleqs
)) {
262 /* display the message being replied to, or distributed */
264 showfile (++argp
, msgnam
);
266 inform("no alternate message to display");
270 /* Translate MIME composition file */
271 buildfile (++argp
, drft
);
275 /* Call an editor on the draft file */
278 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
279 NULL
, 1, atfile
) == NOTOK
)
284 /* display the draft file */
285 showfile (++argp
, drft
);
289 /* Check to whom the draft would be sent */
290 whomfile (++argp
, drft
);
294 /* Quit, and possibly delete the draft */
295 if (*++argp
&& (*argp
[0] == 'd' ||
296 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
299 if (stat (drft
, &st
) != NOTOK
)
300 inform("draft left on %s", drft
);
305 /* Delete draft and exit */
310 /* Send draft in background */
311 if (sendfile (++argp
, drft
, 1))
317 sendfile (++argp
, drft
, 0);
321 /* Refile the draft */
322 if (refile (++argp
, drft
) == 0)
327 /* Change the working directory for attachments
329 * Run the directory through the user's shell so that
330 * we can take advantage of any syntax that the user
331 * is accustomed to. Read back the absolute path.
334 if (*(argp
+1) == NULL
) {
335 strcpy(buf
, "$SHELL -c \"cd&&pwd\"");
338 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
340 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
341 if (fgets(cwd
, sizeof (cwd
), f
) == NULL
) {
342 advise (buf
, "fgets");
344 trim_suffix_c(cwd
, '\n');
348 advise("popen", "could not get directory");
354 /* Print the working directory for attachments */
359 /* List files in the current attachment working directory
361 * Use the user's shell so that we can take advantage of any
362 * syntax that the user is accustomed to.
364 writelscmd(buf
, sizeof(buf
), "", argp
);
365 (void)system_in_dir(cwd
, buf
);
370 * List attachments on current draft. Options are:
372 * -l long listing (full path names)
376 if (checkmimeheader(drft
))
382 while (*++argp
!= NULL
) {
383 if (strcmp(*argp
, "-l") == 0)
386 else if (strcmp(*argp
, "-n") == 0)
389 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
401 inform("usage is alist [-ln].");
404 annolist(drft
, ATTACH_FIELD
, l
, n
);
410 * Attach files to current draft.
413 bool verbose
= false;
416 if (checkmimeheader(drft
))
419 for (ap
= argp
+1; *ap
; ++ap
) {
420 if (strcmp(*ap
, "-v") == 0) {
423 } else if (*ap
[0] != '-') {
428 if (*(argp
+1) == NULL
) {
429 inform("attach command requires file argument(s).");
434 * Build a command line that causes the user's shell to list the file name
435 * arguments. This handles and wildcard expansion, tilde expansion, etc.
437 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
440 * Read back the response from the shell, which contains a number of lines
441 * with one file name per line. Remove off the newline. Determine whether
442 * we have an absolute or relative path name. Prepend the current working
443 * directory to relative path names. Add the attachment annotation to the
447 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
448 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
451 trim_suffix_c(shell
, '\n');
454 strncpy(file
, shell
, sizeof(file
));
455 file
[sizeof(file
) - 1] = '\0';
457 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
460 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
462 ctype
= mime_type(file
);
463 printf ("Attaching %s as a %s\n", file
, ctype
);
471 advise("popen", "could not get file from shell");
478 * Detach files from current draft.
482 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
483 * so this catches a -n anywhere in the argument list.
486 if (checkmimeheader(drft
))
489 for (n
= 0, arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
490 if (strcmp(*arguments
, "-n") == 0) {
497 * A -n was found so interpret the arguments as attachment numbers.
498 * Decrement any remaining argument number that is greater than the one
499 * just processed after processing each one so that the numbering stays
504 for (arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
505 if (strcmp(*arguments
, "-n") == 0)
508 if (**arguments
!= '\0') {
509 n
= atoi(*arguments
);
510 annotate(drft
, ATTACH_FIELD
, NULL
, 1, 0, n
, 1);
512 for (argp
= arguments
+ 1; *argp
!= NULL
; argp
++) {
513 if (atoi(*argp
) > n
) {
514 if (atoi(*argp
) == 1)
517 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
525 * The arguments are interpreted as file names. Run them through the
526 * user's shell for wildcard expansion and other goodies. Do this from
527 * the current working directory if the argument is not an absolute path
528 * name (does not begin with a /).
530 * We feed all the file names to the shell at once, otherwise you can't
531 * provide a file name with a space in it.
533 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
534 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
535 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
536 trim_suffix_c(shell
, '\n');
537 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
541 advise("popen", "could not get file from shell");
547 /* Unknown command */
557 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
559 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
562 /* Note that we do not quote -- the argp from the user
563 * is assumed to be quoted as they desire. (We can't treat
564 * it as pure literal as that would prevent them using ~,
565 * wildcards, etc.) The buffer produced by this function
566 * should be given to popen_in_dir() or system_in_dir() so
567 * that the current working directory is set correctly.
569 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
570 /* NB that some snprintf() return -1 on overflow rather than the
571 * new C99 mandated 'number of chars that would have been written'
573 /* length checks here and inside the loop allow for the
574 * trailing "&&", trailcmd, '"' and NUL
576 int trailln
= strlen(trailcmd
) + 4;
577 if (ln
< 0 || ln
+ trailln
> bufsz
)
578 die("arguments too long");
582 while (*argp
&& *++argp
) {
584 /* +1 for leading space */
585 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
586 die("arguments too long");
588 memcpy(cp
, *argp
, ln
+1);
592 *cp
++ = '&'; *cp
++ = '&';
593 strcpy(cp
, trailcmd
);
601 * Build a command line that causes the user's shell to list the file name
602 * arguments. This handles and wildcard expansion, tilde expansion, etc.
605 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
607 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
608 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
612 /* Like system(), but run the command in directory dir.
613 * This assumes the program is single-threaded!
616 system_in_dir(const char *dir
, const char *cmd
)
621 /* ensure that $SHELL exists, as the cmd was written relying on
622 a non-blank $SHELL... */
623 setenv("SHELL","/bin/sh",0); /* don't overwrite */
625 if (getcwd(olddir
, sizeof(olddir
)) == 0)
626 adios("getcwd", "could not get working directory");
628 adios("chdir", "could not change working directory");
630 if (chdir(olddir
) != 0)
631 adios("chdir", "could not change working directory");
635 /* ditto for popen() */
637 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
642 /* ensure that $SHELL exists, as the cmd was written relying on
643 a non-blank $SHELL... */
644 setenv("SHELL","/bin/sh",0); /* don't overwrite */
646 if (getcwd(olddir
, sizeof(olddir
)) == 0)
647 adios("getcwd", "could not get working directory");
649 adios("chdir", "could not change working directory");
650 f
= popen(cmd
, type
);
651 if (chdir(olddir
) != 0)
652 adios("chdir", "could not change working directory");
661 static bool reedit
; /* have we been here before? */
662 static char *edsave
= NULL
; /* the editor we used previously */
666 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
667 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
669 int pid
, status
, vecp
;
670 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
671 char *cp
, *prog
, **vec
;
674 bool slinked
= false;
676 /* Was there a previous edit session? */
677 if (reedit
&& (*ed
|| edsave
)) {
678 if (!*ed
) { /* no explicit editor */
679 *ed
= edsave
; /* so use the previous one */
680 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
683 /* unless we've specified it via "editor-next" */
684 cp
= concat (cp
, "-next", NULL
);
685 if ((cp
= context_find (cp
)) != NULL
)
689 /* set initial editor */
691 *ed
= get_default_editor();
695 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
696 strncpy (altpath
, altmsg
, sizeof(altpath
));
698 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
700 strncpy (linkpath
, LINK
, sizeof(linkpath
));
702 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
705 (void) m_unlink (linkpath
);
706 if (link (altpath
, linkpath
) == NOTOK
) {
707 if (symlink (altpath
, linkpath
) < 0) {
708 adios (linkpath
, "symlink");
717 context_save (); /* save the context file */
720 switch (pid
= fork()) {
722 advise ("fork", "unable to");
728 if (chdir (cwd
) < 0) {
729 advise (cwd
, "chdir");
734 setenv("mhfolder", mp
->foldpath
, 1);
735 setenv("editalt", altpath
, 1);
738 vec
= argsplit(*ed
, &prog
, &vecp
);
742 vec
[vecp
++] = *arg
++;
747 fprintf (stderr
, "unable to exec ");
752 if ((status
= pidwait (pid
, NOTOK
))) {
753 if (((status
& 0xff00) != 0xff00)
754 && (!reedit
|| (status
& 0x00ff))) {
755 if (!use
&& (status
& 0xff00) &&
756 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
757 inform("problems with edit--draft left in %s", cp
);
759 inform("problems with edit--%s preserved", file
);
762 status
= -2; /* maybe "reedit ? -2 : -1"? */
771 ? lstat (linkpath
, &st
) != NOTOK
772 && S_ISREG(st
.st_mode
)
773 && copyf (linkpath
, altpath
) == NOTOK
774 : stat (linkpath
, &st
) != NOTOK
776 && (m_unlink (altpath
) == NOTOK
777 || link (linkpath
, altpath
) == NOTOK
)))
778 advise (linkpath
, "unable to update %s from", altmsg
);
781 /* normally, we remember which editor we used */
783 edsave
= getcpy (*ed
);
786 if (altmsg
&& atfile
)
787 (void) m_unlink (linkpath
);
794 copyf (char *ifile
, char *ofile
)
799 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
801 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
802 admonish (ofile
, "unable to open and truncate");
807 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
808 if (write (out
, buffer
, i
) != i
) {
809 advise (ofile
, "may have damaged");
825 sendfile (char **arg
, char *file
, int pushsw
)
829 char *cp
, *sp
, **vec
, *program
;
832 * If the sendproc is the nmh command `send', then we call
833 * those routines directly rather than exec'ing the command.
835 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
837 sendit (invo_name
= sp
, arg
, file
, pushsw
);
842 context_save (); /* save the context file */
848 adios("fork", "failed:");
851 vec
= argsplit(sendproc
, &program
, &vecp
);
853 vec
[vecp
++] = "-push";
856 vec
[vecp
++] = *arg
++;
860 execvp (program
, vec
);
861 fprintf (stderr
, "unable to exec ");
866 if (pidwait(child_id
, OK
) == 0)
874 * Translate MIME composition file (call buildmimeproc)
878 buildfile (char **argp
, char *file
)
885 /* allocate space for arguments */
891 args
= mh_xmalloc((i
+ 2) * sizeof(char *));
894 * For backward compatibility, we need to add -build
895 * if we are using mhn as buildmimeproc
898 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
899 args
[i
++] = "-build";
901 /* copy any other arguments */
902 while (argp
&& *argp
)
906 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
909 return i
? NOTOK
: OK
;
914 # define SASLminc(a) (a)
915 #else /* CYRUS_SASL */
916 # define SASLminc(a) 0
917 #endif /* CYRUS_SASL */
920 # define TLSminc(a) (a)
921 #else /* TLS_SUPPORT */
922 # define TLSminc(a) 0
923 #endif /* TLS_SUPPORT */
925 #define SEND_SWITCHES \
926 X("alias aliasfile", 0, ALIASW) \
927 X("debug", -5, DEBUGSW) \
928 X("filter filterfile", 0, FILTSW) \
929 X("nofilter", 0, NFILTSW) \
930 X("format", 0, FRMTSW) \
931 X("noformat", 0, NFRMTSW) \
932 X("forward", 0, FORWSW) \
933 X("noforward", 0, NFORWSW) \
934 X("mime", 0, MIMESW) \
935 X("nomime", 0, NMIMESW) \
936 X("msgid", 0, MSGDSW) \
937 X("nomsgid", 0, NMSGDSW) \
938 X("push", 0, SPSHSW) \
939 X("nopush", 0, NSPSHSW) \
940 X("split seconds", 0, SPLITSW) \
941 X("unique", -6, UNIQSW) \
942 X("nounique", -8, NUNIQSW) \
943 X("verbose", 0, VERBSW) \
944 X("noverbose", 0, NVERBSW) \
945 X("watch", 0, WATCSW) \
946 X("nowatch", 0, NWATCSW) \
947 X("width columns", 0, WIDTHSW) \
948 X("version", 0, SVERSIONSW) \
949 X("help", 0, SHELPSW) \
950 X("dashstuffing", -12, BITSTUFFSW) \
951 X("nodashstuffing", -14, NBITSTUFFSW) \
952 X("client host", -6, CLIESW) \
953 X("server host", 6, SERVSW) \
954 X("snoop", -5, SNOOPSW) \
955 X("draftfolder +folder", 0, SDRFSW) \
956 X("draftmessage msg", 0, SDRMSW) \
957 X("nodraftfolder", 0, SNDRFSW) \
958 X("sasl", SASLminc(4), SASLSW) \
959 X("nosasl", SASLminc(6), NOSASLSW) \
960 X("saslmech", SASLminc(5), SASLMECHSW) \
961 X("authservice", SASLminc(0), AUTHSERVICESW) \
962 X("user username", SASLminc(4), USERSW) \
963 X("port server-port-name/number", 4, PORTSW) \
964 X("tls", TLSminc(-3), TLSSW) \
965 X("initialtls", TLSminc(-10), INITTLSSW) \
966 X("notls", TLSminc(-5), NTLSSW) \
967 X("certverify", TLSminc(-10), CERTVERSW) \
968 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
969 X("sendmail program", 0, MTSSM) \
970 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
971 X("messageid localname|random", 2, MESSAGEIDSW) \
973 #define X(sw, minchars, id) id,
974 DEFINE_SWITCH_ENUM(SEND
);
977 #define X(sw, minchars, id) { sw, minchars, id },
978 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
982 extern int debugsw
; /* from sendsbr.c */
990 extern char *altmsg
; /* .. */
991 extern char *annotext
;
992 extern char *distfile
;
996 sendit (char *sp
, char **arg
, char *file
, int pushed
)
999 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
1000 char **arguments
, *savearg
[MAXARGS
], **vec
;
1001 const char *user
= NULL
, *saslmech
= NULL
;
1002 char *auth_svc
= NULL
;
1006 * Make sure these are defined. In particular, we need
1007 * savearg[1] to be NULL, in case "arg" is NULL below. It
1008 * doesn't matter what is the value of savearg[0], but we
1009 * set it to NULL, to help catch "off-by-one" errors.
1015 * Temporarily copy arg to savearg, since the brkstring() call in
1016 * getarguments() will wipe it out before it is merged in.
1017 * Also, we skip the first element of savearg, since getarguments()
1018 * skips it. Then we count the number of arguments
1019 * copied. The value of "n" will be one greater than
1020 * this in order to simulate the standard argc/argv.
1025 copyip (arg
, savearg
+1, MAXARGS
-1);
1032 * Merge any arguments from command line (now in savearg)
1033 * and arguments from profile.
1035 arguments
= getarguments (sp
, n
, savearg
, 1);
1048 * Get our initial arguments for postproc now
1051 vec
= argsplit(postproc
, &program
, &vecp
);
1053 vec
[vecp
++] = "-library";
1054 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
1056 if ((cp
= context_find ("fileproc"))) {
1057 vec
[vecp
++] = "-fileproc";
1061 if ((cp
= context_find ("mhlproc"))) {
1062 vec
[vecp
++] = "-mhlproc";
1066 if ((cp
= context_find ("credentials"))) {
1067 /* post doesn't read context so need to pass credentials. */
1068 vec
[vecp
++] = "-credentials";
1072 while ((cp
= *argp
++)) {
1074 switch (smatch (++cp
, sendswitches
)) {
1076 ambigsw (cp
, sendswitches
);
1079 inform("-%s unknown\n", cp
);
1083 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1084 print_help (buf
, sendswitches
, 1);
1087 print_version (invo_name
);
1098 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1099 inform("missing argument to %s", argp
[-2]);
1155 #ifdef OAUTH_SUPPORT
1156 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
1157 die("missing argument to %s", argp
[-2]);
1160 NMH_UNUSED (auth_svc
);
1161 die("not built with OAuth support");
1179 if (!(cp
= *argp
++) || *cp
== '-') {
1180 inform("missing argument to %s", argp
[-2]);
1189 if (!(cp
= *argp
++) || *cp
== '-') {
1190 inform("missing argument to %s", argp
[-2]);
1198 inform("usage: %s [switches]", sp
);
1202 /* allow Aliasfile: profile entry */
1203 if ((cp
= context_find ("Aliasfile"))) {
1206 dp
= mh_xstrdup(cp
);
1207 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1208 vec
[vecp
++] = "-alias";
1213 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1214 if ((cp
= context_find ("signature")) && *cp
)
1215 setenv("SIGNATURE", cp
, 1);
1217 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1219 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1221 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1222 inplace
= atoi (cp
);
1224 if ((cp
= getenv ("mhdist"))
1228 vec
[vecp
++] = "-dist";
1229 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1230 die("unable to create temporary file");
1232 distfile
= mh_xstrdup(cp
);
1233 (void) m_unlink(distfile
);
1234 if (link (altmsg
, distfile
) == NOTOK
)
1235 adios (distfile
, "unable to link %s to", altmsg
);
1240 #ifdef OAUTH_SUPPORT
1241 if (auth_svc
== NULL
) {
1242 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
1243 die("must specify -authservice with -saslmech xoauth2");
1247 die("must specify -user with -saslmech xoauth2");
1251 NMH_UNUSED(saslmech
);
1252 #endif /* OAUTH_SUPPORT */
1254 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1259 if ((pushsw
= pushed
))
1264 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, auth_svc
) == OK
)
1273 whomfile (char **arg
, char *file
)
1277 char **vec
, *program
;
1279 context_save (); /* save the context file */
1282 switch (pid
= fork()) {
1284 advise ("fork", "unable to");
1288 vec
= argsplit(whomproc
, &program
, &vecp
);
1291 vec
[vecp
++] = *arg
++;
1295 execvp (program
, vec
);
1296 fprintf (stderr
, "unable to exec ");
1298 _exit(1); /* NOTREACHED */
1301 return pidwait(pid
, NOTOK
) & 0377 ? 1 : 0;
1307 * Remove the draft file
1311 removefile (char *drft
)
1313 if (m_unlink (drft
) == NOTOK
)
1314 adios (drft
, "unable to unlink");
1321 * Return 1 if we already have a MIME-Version header, 0 otherwise.
1325 checkmimeheader (char *drft
)
1328 m_getfld_state_t gstate
;
1329 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1331 bool retval
= false;
1333 if ((f
= fopen(drft
, "r")) == NULL
) {
1334 admonish(drft
, "unable to read draft");
1338 gstate
= m_getfld_state_init(f
);
1340 int bufsz
= sizeof(buf
);
1341 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1344 if (strcasecmp(name
, VRSN_FIELD
) == 0) {
1345 inform("Cannot use attach commands with already-"
1346 "formatted MIME message \"%s\"", drft
);
1357 m_getfld_state_destroy(&gstate
);