]>
diplodocus.org Git - nmh/blob - uip/whatnowsbr.c
3 * whatnowsbr.c -- the WhatNow shell
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
9 * Several options have been added to ease the inclusion of attachments
10 * using the header field name mechanism added to anno and send. The
11 * -attach option is used to specify the header field name for attachments.
13 * Several commands have been added at the whatnow prompt:
15 * cd [ directory ] This option works just like the shell's
16 * cd command and lets the user change the
17 * directory from which attachments are
18 * taken so that long path names are not
19 * needed with every file.
21 * ls [ ls-options ] This option works just like the normal
22 * ls command and exists to allow the user
23 * to verify file names in the directory.
25 * pwd This option works just like the normal
26 * pwd command and exists to allow the user
27 * to verify the directory.
29 * attach [-v] files This option attaches the named files to
30 * the draft. -v displays the mhbuild
31 * directive that send(1) will use.
33 * alist [-ln] This option lists the attachments on the
34 * draft. -l gets long listings, -n gets
37 * detach files This option removes attachments from the
38 * detach -n numbers draft. This can be done by file name or
39 * by attachment number.
47 #define WHATNOW_SWITCHES \
48 X("draftfolder +folder", 0, DFOLDSW) \
49 X("draftmessage msg", 0, DMSGSW) \
50 X("nodraftfolder", 0, NDFLDSW) \
51 X("editor editor", 0, EDITRSW) \
52 X("noedit", 0, NEDITSW) \
53 X("prompt string", 4, PRMPTSW) \
54 X("version", 0, VERSIONSW) \
55 X("help", 0, HELPSW) \
56 X("attach header-field-name", -6, ATTACHSW) \
57 X("noattach", -8, NOATTACHSW) \
60 #define X(sw, minchars, id) id,
61 DEFINE_SWITCH_ENUM(WHATNOW
);
64 #define X(sw, minchars, id) { sw, minchars, id },
65 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
69 * Options at the "whatnow" prompt
71 #define PROMPT_SWITCHES \
72 X("edit [<editor> <switches>]", 0, EDITSW) \
73 X("refile [<switches>] +folder", 0, REFILEOPT) \
74 X("mime [<switches>]", 0, BUILDMIMESW) \
75 X("display [<switches>]", 0, DISPSW) \
76 X("list [<switches>]", 0, LISTSW) \
77 X("send [<switches>]", 0, SENDSW) \
78 X("push [<switches>]", 0, PUSHSW) \
79 X("whom [<switches>]", 0, WHOMSW) \
80 X("quit [-delete]", 0, QUITSW) \
81 X("delete", 0, DELETESW) \
82 X("cd [directory]", 0, CDCMDSW) \
83 X("pwd", 0, PWDCMDSW) \
85 X("attach [-v]", 0, ATTACHCMDSW) \
86 X("detach [-n]", 0, DETACHCMDSW) \
87 X("alist [-ln] ", 2, ALISTCMDSW) \
89 #define X(sw, minchars, id) id,
90 DEFINE_SWITCH_ENUM(PROMPT
);
93 #define X(sw, minchars, id) { sw, minchars, id },
94 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
97 static char *myprompt
= "\nWhat now? ";
102 static int editfile (char **, char **, char *, int, struct msgs
*,
103 char *, char *, int, int);
104 static int sendfile (char **, char *, int);
105 static void sendit (char *, char **, char *, int);
106 static int buildfile (char **, char *);
107 static int whomfile (char **, char *);
108 static int removefile (char *);
109 static int checkmimeheader (char *);
110 static void writelscmd(char *, int, char *, char **);
111 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
112 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
113 static int system_in_dir(const char *dir
, const char *cmd
);
114 static int copyf (char *, char *);
118 WhatNow (int argc
, char **argv
)
120 int isdf
= 0, nedit
= 0, use
= 0, atfile
= 1;
121 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
122 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
123 char buf
[BUFSIZ
], prompt
[BUFSIZ
];
124 char **argp
, **arguments
;
126 char cwd
[PATH_MAX
+ 1]; /* current working directory */
127 char file
[PATH_MAX
+ 1]; /* file name buffer */
128 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
129 FILE *f
; /* read pointer for bgnd proc */
130 char *l
; /* set on -l to alist command */
131 int n
; /* set on -n to alist command */
133 /* Need this if called from what_now(). */
134 invo_name
= r1bindex (argv
[0], '/');
136 arguments
= getarguments (invo_name
, argc
, argv
, 1);
140 * Get the initial current working directory.
143 if (getcwd(cwd
, sizeof (cwd
)) == (char *)0) {
144 adios("getcwd", "could not get working directory");
147 while ((cp
= *argp
++)) {
149 switch (smatch (++cp
, whatnowswitches
)) {
151 ambigsw (cp
, whatnowswitches
);
154 adios (NULL
, "-%s unknown", cp
);
157 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
158 print_help (buf
, whatnowswitches
, 1);
161 print_version(invo_name
);
166 adios (NULL
, "only one draft folder at a time!");
167 if (!(cp
= *argp
++) || *cp
== '-')
168 adios (NULL
, "missing argument to %s", argp
[-2]);
169 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
170 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
174 adios (NULL
, "only one draft message at a time!");
175 if (!(dmsg
= *argp
++) || *dmsg
== '-')
176 adios (NULL
, "missing argument to %s", argp
[-2]);
184 if (!(ed
= *argp
++) || *ed
== '-')
185 adios (NULL
, "missing argument to %s", argp
[-2]);
193 if (!(myprompt
= *argp
++) || *myprompt
== '-')
194 adios (NULL
, "missing argument to %s", argp
[-2]);
198 advise(NULL
, "The -attach switch is deprecated");
202 advise(NULL
, "The -noattach switch is deprecated");
207 adios (NULL
, "only one draft at a time!");
212 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
213 drft
= getcpy (m_draft (dfolder
, dmsg
, 1, &isdf
));
215 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? getcpy (cp
) : NULL
;
217 if ((cp
= getenv ("mhatfile")) && *cp
)
220 if ((cp
= getenv ("mhuse")) && *cp
)
223 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
228 /* start editing the draft, unless -noedit was given */
229 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
230 NULL
, 1, atfile
) < 0)
233 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
235 #ifdef READLINE_SUPPORT
236 if (!(argp
= read_switch_multiword_via_readline (prompt
, aleqs
))) {
237 #else /* ! READLINE_SUPPORT */
238 if (!(argp
= read_switch_multiword (prompt
, aleqs
))) {
239 #endif /* READLINE_SUPPORT */
240 (void) m_unlink (LINK
);
243 switch (smatch (*argp
, aleqs
)) {
245 /* display the message being replied to, or distributed */
247 showfile (++argp
, msgnam
);
249 advise (NULL
, "no alternate message to display");
253 /* Translate MIME composition file */
254 buildfile (++argp
, drft
);
258 /* Call an editor on the draft file */
261 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
262 NULL
, 1, atfile
) == NOTOK
)
267 /* display the draft file */
268 showfile (++argp
, drft
);
272 /* Check to whom the draft would be sent */
273 whomfile (++argp
, drft
);
277 /* Quit, and possibly delete the draft */
278 if (*++argp
&& (*argp
[0] == 'd' ||
279 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
282 if (stat (drft
, &st
) != NOTOK
)
283 advise (NULL
, "draft left on %s", drft
);
288 /* Delete draft and exit */
293 /* Send draft in background */
294 if (sendfile (++argp
, drft
, 1))
300 sendfile (++argp
, drft
, 0);
304 /* Refile the draft */
305 if (refile (++argp
, drft
) == 0)
310 /* Change the working directory for attachments
312 * Run the directory through the user's shell so that
313 * we can take advantage of any syntax that the user
314 * is accustomed to. Read back the absolute path.
317 if (*(argp
+1) == (char *)0) {
318 (void)sprintf(buf
, "$SHELL -c \"cd&&pwd\"");
321 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
323 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
324 if (fgets(cwd
, sizeof (cwd
), f
) == NULL
) {
325 advise (buf
, "fgets");
328 if (strchr(cwd
, '\n') != (char *)0)
329 *strchr(cwd
, '\n') = '\0';
334 advise("popen", "could not get directory");
340 /* Print the working directory for attachments */
345 /* List files in the current attachment working directory
347 * Use the user's shell so that we can take advantage of any
348 * syntax that the user is accustomed to.
350 writelscmd(buf
, sizeof(buf
), "", argp
);
351 (void)system_in_dir(cwd
, buf
);
356 * List attachments on current draft. Options are:
358 * -l long listing (full path names)
362 if (checkmimeheader(drft
))
368 while (*++argp
!= (char *)0) {
369 if (strcmp(*argp
, "-l") == 0)
372 else if (strcmp(*argp
, "-n") == 0)
375 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
387 advise((char *)0, "usage is alist [-ln].");
390 annolist(drft
, ATTACH_FIELD
, l
, n
);
396 * Attach files to current draft.
402 if (checkmimeheader(drft
))
405 for (ap
= argp
+1; *ap
; ++ap
) {
406 if (strcmp(*ap
, "-v") == 0) {
409 } else if (*ap
[0] != '-') {
414 if (*(argp
+1) == (char *)0) {
415 advise(NULL
, "attach command requires file argument(s).");
420 * Build a command line that causes the user's shell to list the file name
421 * arguments. This handles and wildcard expansion, tilde expansion, etc.
423 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
426 * Read back the response from the shell, which contains a number of lines
427 * with one file name per line. Remove off the newline. Determine whether
428 * we have an absolute or relative path name. Prepend the current working
429 * directory to relative path names. Add the attachment annotation to the
433 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
434 while (fgets(shell
, sizeof (shell
), f
) != (char *)0) {
437 *(strchr(shell
, '\n')) = '\0';
440 strncpy(file
, shell
, sizeof(file
));
441 file
[sizeof(file
) - 1] = '\0';
443 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
446 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
448 ctype
= mime_type(file
);
449 printf ("Attaching %s as a %s\n", file
, ctype
);
457 advise("popen", "could not get file from shell");
464 * Detach files from current draft.
468 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
469 * so this catches a -n anywhere in the argument list.
472 if (checkmimeheader(drft
))
475 for (n
= 0, arguments
= argp
+ 1; *arguments
!= (char *)0; arguments
++) {
476 if (strcmp(*arguments
, "-n") == 0) {
483 * A -n was found so interpret the arguments as attachment numbers.
484 * Decrement any remaining argument number that is greater than the one
485 * just processed after processing each one so that the numbering stays
490 for (arguments
= argp
+ 1; *arguments
!= (char *)0; arguments
++) {
491 if (strcmp(*arguments
, "-n") == 0)
494 if (**arguments
!= '\0') {
495 n
= atoi(*arguments
);
496 annotate(drft
, ATTACH_FIELD
, (char *)0, 1, 0, n
, 1);
498 for (argp
= arguments
+ 1; *argp
!= (char *)0; argp
++) {
499 if (atoi(*argp
) > n
) {
500 if (atoi(*argp
) == 1)
503 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
511 * The arguments are interpreted as file names. Run them through the
512 * user's shell for wildcard expansion and other goodies. Do this from
513 * the current working directory if the argument is not an absolute path
514 * name (does not begin with a /).
516 * We feed all the file names to the shell at once, otherwise you can't
517 * provide a file name with a space in it.
519 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
520 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
521 while (fgets(shell
, sizeof (shell
), f
) != (char *)0) {
522 *(strchr(shell
, '\n')) = '\0';
523 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
527 advise("popen", "could not get file from shell");
533 /* Unknown command */
534 advise (NULL
, "say what?");
543 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
545 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
548 /* Note that we do not quote -- the argp from the user
549 * is assumed to be quoted as they desire. (We can't treat
550 * it as pure literal as that would prevent them using ~,
551 * wildcards, etc.) The buffer produced by this function
552 * should be given to popen_in_dir() or system_in_dir() so
553 * that the current working directory is set correctly.
555 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
556 /* NB that some snprintf() return -1 on overflow rather than the
557 * new C99 mandated 'number of chars that would have been written'
559 /* length checks here and inside the loop allow for the
560 * trailing "&&", trailcmd, '"' and NUL
562 int trailln
= strlen(trailcmd
) + 4;
563 if (ln
< 0 || ln
+ trailln
> bufsz
)
564 adios((char *)0, "arguments too long");
568 while (*argp
&& *++argp
) {
570 /* +1 for leading space */
571 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
572 adios((char *)0, "arguments too long");
574 memcpy(cp
, *argp
, ln
+1);
578 *cp
++ = '&'; *cp
++ = '&';
579 strcpy(cp
, trailcmd
);
587 * Build a command line that causes the user's shell to list the file name
588 * arguments. This handles and wildcard expansion, tilde expansion, etc.
591 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
593 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
594 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
598 /* Like system(), but run the command in directory dir.
599 * This assumes the program is single-threaded!
602 system_in_dir(const char *dir
, const char *cmd
)
607 /* ensure that $SHELL exists, as the cmd was written relying on
608 a non-blank $SHELL... */
609 setenv("SHELL","/bin/sh",0); /* don't overwrite */
611 if (getcwd(olddir
, sizeof(olddir
)) == 0)
612 adios("getcwd", "could not get working directory");
614 adios("chdir", "could not change working directory");
616 if (chdir(olddir
) != 0)
617 adios("chdir", "could not change working directory");
621 /* ditto for popen() */
623 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
628 /* ensure that $SHELL exists, as the cmd was written relying on
629 a non-blank $SHELL... */
630 setenv("SHELL","/bin/sh",0); /* don't overwrite */
632 if (getcwd(olddir
, sizeof(olddir
)) == 0)
633 adios("getcwd", "could not get working directory");
635 adios("chdir", "could not change working directory");
636 f
= popen(cmd
, type
);
637 if (chdir(olddir
) != 0)
638 adios("chdir", "could not change working directory");
647 static int reedit
= 0; /* have we been here before? */
648 static char *edsave
= NULL
; /* the editor we used previously */
652 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
653 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
655 int pid
, status
, vecp
;
656 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
657 char *cp
, *prog
, **vec
;
662 /* Was there a previous edit session? */
663 if (reedit
&& (*ed
|| edsave
)) {
664 if (!*ed
) { /* no explicit editor */
665 *ed
= edsave
; /* so use the previous one */
666 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
669 /* unless we've specified it via "editor-next" */
670 cp
= concat (cp
, "-next", NULL
);
671 if ((cp
= context_find (cp
)) != NULL
)
675 /* set initial editor */
677 *ed
= get_default_editor();
681 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
682 strncpy (altpath
, altmsg
, sizeof(altpath
));
684 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
686 strncpy (linkpath
, LINK
, sizeof(linkpath
));
688 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
691 (void) m_unlink (linkpath
);
692 if (link (altpath
, linkpath
) == NOTOK
) {
693 if (symlink (altpath
, linkpath
) < 0) {
694 adios (linkpath
, "symlink");
703 context_save (); /* save the context file */
706 switch (pid
= fork()) {
708 advise ("fork", "unable to");
714 if (chdir (cwd
) < 0) {
715 advise (cwd
, "chdir");
720 m_putenv ("mhfolder", mp
->foldpath
);
721 m_putenv ("editalt", altpath
);
724 vec
= argsplit(*ed
, &prog
, &vecp
);
728 vec
[vecp
++] = *arg
++;
733 fprintf (stderr
, "unable to exec ");
738 if ((status
= pidwait (pid
, NOTOK
))) {
739 if (((status
& 0xff00) != 0xff00)
740 && (!reedit
|| (status
& 0x00ff))) {
741 if (!use
&& (status
& 0xff00) &&
742 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
743 advise (NULL
, "problems with edit--draft left in %s", cp
);
745 advise (NULL
, "problems with edit--%s preserved", file
);
748 status
= -2; /* maybe "reedit ? -2 : -1"? */
757 ? lstat (linkpath
, &st
) != NOTOK
758 && S_ISREG(st
.st_mode
)
759 && copyf (linkpath
, altpath
) == NOTOK
760 : stat (linkpath
, &st
) != NOTOK
762 && (m_unlink (altpath
) == NOTOK
763 || link (linkpath
, altpath
) == NOTOK
)))
764 advise (linkpath
, "unable to update %s from", altmsg
);
767 /* normally, we remember which editor we used */
769 edsave
= getcpy (*ed
);
772 if (altmsg
&& atfile
)
773 (void) m_unlink (linkpath
);
780 copyf (char *ifile
, char *ofile
)
785 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
787 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
788 admonish (ofile
, "unable to open and truncate");
793 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
794 if (write (out
, buffer
, i
) != i
) {
795 advise (ofile
, "may have damaged");
811 sendfile (char **arg
, char *file
, int pushsw
)
815 char *cp
, *sp
, **vec
, *program
;
818 * If the sendproc is the nmh command `send', then we call
819 * those routines directly rather than exec'ing the command.
821 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
823 sendit (invo_name
= sp
, arg
, file
, pushsw
);
828 context_save (); /* save the context file */
831 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
835 advise (NULL
, "unable to fork, so sending directly...");
837 vec
= argsplit(sendproc
, &program
, &vecp
);
839 vec
[vecp
++] = "-push";
842 vec
[vecp
++] = *arg
++;
846 execvp (program
, vec
);
847 fprintf (stderr
, "unable to exec ");
852 if (pidwait(child_id
, OK
) == 0)
860 * Translate MIME composition file (call buildmimeproc)
864 buildfile (char **argp
, char *file
)
871 /* allocate space for arguments */
877 args
= (char **) mh_xmalloc((i
+ 2) * sizeof(char *));
880 * For backward compatibility, we need to add -build
881 * if we are using mhn as buildmimeproc
884 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
885 args
[i
++] = "-build";
887 /* copy any other arguments */
888 while (argp
&& *argp
)
892 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
895 return (i
? NOTOK
: OK
);
900 # define SASLminc(a) (a)
901 #else /* CYRUS_SASL */
902 # define SASLminc(a) 0
903 #endif /* CYRUS_SASL */
906 # define TLSminc(a) (a)
907 #else /* TLS_SUPPORT */
908 # define TLSminc(a) 0
909 #endif /* TLS_SUPPORT */
911 #define SEND_SWITCHES \
912 X("alias aliasfile", 0, ALIASW) \
913 X("debug", -5, DEBUGSW) \
914 X("filter filterfile", 0, FILTSW) \
915 X("nofilter", 0, NFILTSW) \
916 X("format", 0, FRMTSW) \
917 X("noformat", 0, NFRMTSW) \
918 X("forward", 0, FORWSW) \
919 X("noforward", 0, NFORWSW) \
920 X("mime", 0, MIMESW) \
921 X("nomime", 0, NMIMESW) \
922 X("msgid", 0, MSGDSW) \
923 X("nomsgid", 0, NMSGDSW) \
924 X("push", 0, SPSHSW) \
925 X("nopush", 0, NSPSHSW) \
926 X("split seconds", 0, SPLITSW) \
927 X("unique", -6, UNIQSW) \
928 X("nounique", -8, NUNIQSW) \
929 X("verbose", 0, VERBSW) \
930 X("noverbose", 0, NVERBSW) \
931 X("watch", 0, WATCSW) \
932 X("nowatch", 0, NWATCSW) \
933 X("width columns", 0, WIDTHSW) \
934 X("version", 0, SVERSIONSW) \
935 X("help", 0, SHELPSW) \
936 X("dashstuffing", -12, BITSTUFFSW) \
937 X("nodashstuffing", -14, NBITSTUFFSW) \
938 X("client host", -6, CLIESW) \
939 X("server host", 6, SERVSW) \
940 X("snoop", -5, SNOOPSW) \
941 X("draftfolder +folder", -6, SDRFSW) \
942 X("draftmessage msg", -6, SDRMSW) \
943 X("nodraftfolder", -3, SNDRFSW) \
944 X("sasl", SASLminc(-4), SASLSW) \
945 X("nosasl", SASLminc(-6), NOSASLSW) \
946 X("saslmaxssf", SASLminc(-10), SASLMXSSFSW) \
947 X("saslmech", SASLminc(-5), SASLMECHSW) \
948 X("user", SASLminc(-4), USERSW) \
949 X("attach fieldname", 6, SNDATTACHSW) \
950 X("noattach", 0, SNDNOATTACHSW) \
951 X("attachformat", 7, SNDATTACHFORMAT) \
952 X("port server-port-name/number", 4, PORTSW) \
953 X("tls", TLSminc(-3), TLSSW) \
954 X("initialtls", TLSminc(-10), INITTLSSW) \
955 X("notls", TLSminc(-5), NTLSSW) \
956 X("sendmail program", 0, MTSSM) \
957 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
958 X("messageid localname|random", 2, MESSAGEIDSW) \
960 #define X(sw, minchars, id) id,
961 DEFINE_SWITCH_ENUM(SEND
);
964 #define X(sw, minchars, id) { sw, minchars, id },
965 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
969 extern int debugsw
; /* from sendsbr.c */
977 extern char *altmsg
; /* .. */
978 extern char *annotext
;
979 extern char *distfile
;
983 sendit (char *sp
, char **arg
, char *file
, int pushed
)
986 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
987 char **arguments
, *savearg
[MAXARGS
], **vec
;
995 * Make sure these are defined. In particular, we need
996 * savearg[1] to be NULL, in case "arg" is NULL below. It
997 * doesn't matter what is the value of savearg[0], but we
998 * set it to NULL, to help catch "off-by-one" errors.
1004 * Temporarily copy arg to savearg, since the brkstring() call in
1005 * getarguments() will wipe it out before it is merged in.
1006 * Also, we skip the first element of savearg, since getarguments()
1007 * skips it. Then we count the number of arguments
1008 * copied. The value of "n" will be one greater than
1009 * this in order to simulate the standard argc/argv.
1014 copyip (arg
, savearg
+1, MAXARGS
-1);
1021 * Merge any arguments from command line (now in savearg)
1022 * and arguments from profile.
1024 arguments
= getarguments (sp
, n
, savearg
, 1);
1037 * Get our initial arguments for postproc now
1040 vec
= argsplit(postproc
, &program
, &vecp
);
1042 vec
[vecp
++] = "-library";
1043 vec
[vecp
++] = getcpy (m_maildir (""));
1045 if ((cp
= context_find ("fileproc"))) {
1046 vec
[vecp
++] = "-fileproc";
1050 if ((cp
= context_find ("mhlproc"))) {
1051 vec
[vecp
++] = "-mhlproc";
1055 if ((cp
= context_find ("credentials"))) {
1056 /* post doesn't read context so need to pass credentials. */
1057 vec
[vecp
++] = "-credentials";
1061 while ((cp
= *argp
++)) {
1063 switch (smatch (++cp
, sendswitches
)) {
1065 ambigsw (cp
, sendswitches
);
1068 advise (NULL
, "-%s unknown\n", cp
);
1072 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1073 print_help (buf
, sendswitches
, 1);
1076 print_version (invo_name
);
1087 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1088 advise (NULL
, "missing argument to %s", argp
[-2]);
1116 debugsw
++; /* fall */
1150 if (!(cp
= *argp
++) || *cp
== '-') {
1151 advise (NULL
, "missing argument to %s", argp
[-2]);
1159 if (!(cp
= *argp
++) || *cp
== '-') {
1160 advise (NULL
, "missing argument to %s", argp
[-2]);
1167 advise(NULL
, "The -attach switch is deprecated");
1170 advise(NULL
, "The -noattach switch is deprecated");
1173 case SNDATTACHFORMAT
:
1174 advise(NULL
, "The -attachformat switch is deprecated");
1178 advise (NULL
, "usage: %s [switches]", sp
);
1182 /* allow Aliasfile: profile entry */
1183 if ((cp
= context_find ("Aliasfile"))) {
1187 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1188 vec
[vecp
++] = "-alias";
1193 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1194 if ((cp
= context_find ("signature")) && *cp
)
1195 m_putenv ("SIGNATURE", cp
);
1197 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1199 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1201 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1202 inplace
= atoi (cp
);
1204 if ((cp
= getenv ("mhdist"))
1207 && (distsw
= atoi (cp
))
1208 #endif /* not lint */
1210 vec
[vecp
++] = "-dist";
1211 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1212 adios(NULL
, "unable to create temporary file in %s",
1215 distfile
= getcpy (cp
);
1216 (void) m_unlink(distfile
);
1217 if (link (altmsg
, distfile
) == NOTOK
)
1218 adios (distfile
, "unable to link %s to", altmsg
);
1223 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1228 if ((pushsw
= pushed
))
1233 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1) == OK
)
1242 whomfile (char **arg
, char *file
)
1246 char **vec
, *program
;
1248 context_save (); /* save the context file */
1251 switch (pid
= fork()) {
1253 advise ("fork", "unable to");
1257 vec
= argsplit(whomproc
, &program
, &vecp
);
1260 vec
[vecp
++] = *arg
++;
1264 execvp (program
, vec
);
1265 fprintf (stderr
, "unable to exec ");
1267 _exit (-1); /* NOTREACHED */
1270 return (pidwait (pid
, NOTOK
) & 0377 ? 1 : 0);
1276 * Remove the draft file
1280 removefile (char *drft
)
1282 if (m_unlink (drft
) == NOTOK
)
1283 adios (drft
, "unable to unlink");
1290 * Return 1 if we already have a MIME-Version header, 0 otherwise.
1294 checkmimeheader (char *drft
)
1297 m_getfld_state_t gstate
= 0;
1298 char buf
[BUFSIZ
], name
[NAMESZ
];
1299 int state
, retval
= 0;
1301 if ((f
= fopen(drft
, "r")) == NULL
) {
1302 admonish(drft
, "unable to read draft");
1307 int bufsz
= sizeof(buf
);
1308 switch (state
= m_getfld(&gstate
, name
, buf
, &bufsz
, f
)) {
1311 if (strcasecmp(name
, VRSN_FIELD
) == 0) {
1312 advise(NULL
, "Cannot use attach commands with already-"
1313 "formatted MIME message \"%s\"", drft
);
1324 m_getfld_state_destroy(&gstate
);