]>
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 "sbr/showfile.h"
42 #include "sbr/smatch.h"
43 #include "sbr/r1bindex.h"
44 #include "sbr/refile.h"
45 #include "sbr/closefds.h"
46 #include "sbr/copyip.h"
47 #include "sbr/getcpy.h"
48 #include "sbr/m_draft.h"
49 #include "sbr/m_backup.h"
50 #include "sbr/geteditor.h"
51 #include "sbr/context_save.h"
52 #include "sbr/context_find.h"
53 #include "sbr/brkstring.h"
54 #include "sbr/ambigsw.h"
57 #include "sbr/print_version.h"
58 #include "sbr/print_help.h"
59 #include "sbr/arglist.h"
60 #include "sbr/error.h"
68 #include "sbr/m_maildir.h"
69 #include "sbr/m_mktemp.h"
70 #include "sbr/mime_type.h"
72 #define WHATNOW_SWITCHES \
73 X("draftfolder +folder", 0, DFOLDSW) \
74 X("draftmessage msg", 0, DMSGSW) \
75 X("nodraftfolder", 0, NDFLDSW) \
76 X("editor editor", 0, EDITRSW) \
77 X("noedit", 0, NEDITSW) \
78 X("prompt string", 4, PRMPTSW) \
79 X("version", 0, VERSIONSW) \
80 X("help", 0, HELPSW) \
83 #define X(sw, minchars, id) id,
84 DEFINE_SWITCH_ENUM(WHATNOW
);
87 #define X(sw, minchars, id) { sw, minchars, id },
88 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
92 * Options at the "whatnow" prompt
94 #define PROMPT_SWITCHES \
95 X("edit [<editor> <switches>]", 0, EDITSW) \
96 X("refile [<switches>] +folder", 0, REFILEOPT) \
97 X("mime [<switches>]", 0, BUILDMIMESW) \
98 X("display [<switches>]", 0, DISPSW) \
99 X("list [<switches>]", 0, LISTSW) \
100 X("send [<switches>]", 0, SENDSW) \
101 X("push [<switches>]", 0, PUSHSW) \
102 X("whom [<switches>]", 0, WHOMSW) \
103 X("quit [-delete]", 0, QUITSW) \
104 X("delete", 0, DELETESW) \
105 X("cd [directory]", 0, CDCMDSW) \
106 X("pwd", 0, PWDCMDSW) \
107 X("ls", 2, LSCMDSW) \
108 X("attach [-v]", 0, ATTACHCMDSW) \
109 X("detach [-n]", 0, DETACHCMDSW) \
110 X("alist [-ln] ", 2, ALISTCMDSW) \
112 #define X(sw, minchars, id) id,
113 DEFINE_SWITCH_ENUM(PROMPT
);
116 #define X(sw, minchars, id) { sw, minchars, id },
117 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
120 static char *myprompt
= "\nWhat now? ";
125 static int editfile (char **, char **, char *, int, struct msgs
*,
126 char *, char *, int, int);
127 static int sendfile (char **, char *, int);
128 static void sendit (char *, char **, char *, int);
129 static int buildfile (char **, char *);
130 static int whomfile (char **, char *);
131 static int removefile (char *);
132 static int checkmimeheader (char *);
133 static void writelscmd(char *, int, char *, char **);
134 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
135 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
136 static int system_in_dir(const char *dir
, const char *cmd
);
137 static int copyf (char *, char *);
141 WhatNow (int argc
, char **argv
)
145 int use
= 0, atfile
= 1;
146 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
147 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
149 char **argp
, **arguments
;
151 char cwd
[PATH_MAX
+ 1]; /* current working directory */
152 char file
[PATH_MAX
+ 1]; /* file name buffer */
153 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
154 FILE *f
; /* read pointer for bgnd proc */
155 char *l
; /* set on -l to alist command */
156 int n
; /* set on -n to alist command */
158 /* Need this if called from what_now(). */
159 invo_name
= r1bindex (argv
[0], '/');
161 arguments
= getarguments (invo_name
, argc
, argv
, 1);
165 * Get the initial current working directory.
168 if (getcwd(cwd
, sizeof (cwd
)) == NULL
) {
169 adios("getcwd", "could not get working directory");
172 while ((cp
= *argp
++)) {
174 switch (smatch (++cp
, whatnowswitches
)) {
176 ambigsw (cp
, whatnowswitches
);
179 die("-%s unknown", cp
);
182 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
183 print_help (buf
, whatnowswitches
, 1);
186 print_version(invo_name
);
191 die("only one draft folder at a time!");
192 if (!(cp
= *argp
++) || *cp
== '-')
193 die("missing argument to %s", argp
[-2]);
194 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
195 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
199 die("only one draft message at a time!");
200 if (!(dmsg
= *argp
++) || *dmsg
== '-')
201 die("missing argument to %s", argp
[-2]);
209 if (!(ed
= *argp
++) || *ed
== '-')
210 die("missing argument to %s", argp
[-2]);
218 if (!(myprompt
= *argp
++) || *myprompt
== '-')
219 die("missing argument to %s", argp
[-2]);
224 die("only one draft at a time!");
228 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
229 drft
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
231 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? mh_xstrdup(cp
) : NULL
;
233 if ((cp
= getenv ("mhatfile")) && *cp
)
236 if ((cp
= getenv ("mhuse")) && *cp
)
239 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
244 /* start editing the draft, unless -noedit was given */
245 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
246 NULL
, 1, atfile
) < 0)
250 #ifdef READLINE_SUPPORT
251 if (!(argp
= read_switch_multiword_via_readline(myprompt
, aleqs
))) {
252 #else /* ! READLINE_SUPPORT */
253 if (!(argp
= read_switch_multiword(myprompt
, aleqs
))) {
254 #endif /* READLINE_SUPPORT */
255 (void) m_unlink (LINK
);
258 switch (smatch (*argp
, aleqs
)) {
260 /* display the message being replied to, or distributed */
262 showfile (++argp
, msgnam
);
264 inform("no alternate message to display");
268 /* Translate MIME composition file */
269 buildfile (++argp
, drft
);
273 /* Call an editor on the draft file */
276 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
277 NULL
, 1, atfile
) == NOTOK
)
282 /* display the draft file */
283 showfile (++argp
, drft
);
287 /* Check to whom the draft would be sent */
288 whomfile (++argp
, drft
);
292 /* Quit, and possibly delete the draft */
293 if (*++argp
&& (*argp
[0] == 'd' ||
294 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
297 if (stat (drft
, &st
) != NOTOK
)
298 inform("draft left on %s", drft
);
303 /* Delete draft and exit */
308 /* Send draft in background */
309 if (sendfile (++argp
, drft
, 1))
315 sendfile (++argp
, drft
, 0);
319 /* Refile the draft */
320 if (refile (++argp
, drft
) == 0)
325 /* Change the working directory for attachments
327 * Run the directory through the user's shell so that
328 * we can take advantage of any syntax that the user
329 * is accustomed to. Read back the absolute path.
332 if (*(argp
+1) == NULL
) {
333 strcpy(buf
, "$SHELL -c \"cd&&pwd\"");
336 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
338 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
339 if (fgets(cwd
, sizeof (cwd
), f
) == NULL
) {
340 advise (buf
, "fgets");
342 trim_suffix_c(cwd
, '\n');
346 advise("popen", "could not get directory");
352 /* Print the working directory for attachments */
357 /* List files in the current attachment working directory
359 * Use the user's shell so that we can take advantage of any
360 * syntax that the user is accustomed to.
362 writelscmd(buf
, sizeof(buf
), "", argp
);
363 (void)system_in_dir(cwd
, buf
);
368 * List attachments on current draft. Options are:
370 * -l long listing (full path names)
374 if (checkmimeheader(drft
))
380 while (*++argp
!= NULL
) {
381 if (strcmp(*argp
, "-l") == 0)
384 else if (strcmp(*argp
, "-n") == 0)
387 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
399 inform("usage is alist [-ln].");
402 annolist(drft
, ATTACH_FIELD
, l
, n
);
408 * Attach files to current draft.
411 bool verbose
= false;
414 if (checkmimeheader(drft
))
417 for (ap
= argp
+1; *ap
; ++ap
) {
418 if (strcmp(*ap
, "-v") == 0) {
421 } else if (*ap
[0] != '-') {
426 if (*(argp
+1) == NULL
) {
427 inform("attach command requires file argument(s).");
432 * Build a command line that causes the user's shell to list the file name
433 * arguments. This handles and wildcard expansion, tilde expansion, etc.
435 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
438 * Read back the response from the shell, which contains a number of lines
439 * with one file name per line. Remove off the newline. Determine whether
440 * we have an absolute or relative path name. Prepend the current working
441 * directory to relative path names. Add the attachment annotation to the
445 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
446 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
449 trim_suffix_c(shell
, '\n');
452 strncpy(file
, shell
, sizeof(file
));
453 file
[sizeof(file
) - 1] = '\0';
455 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
458 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
460 ctype
= mime_type(file
);
461 printf ("Attaching %s as a %s\n", file
, ctype
);
469 advise("popen", "could not get file from shell");
476 * Detach files from current draft.
480 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
481 * so this catches a -n anywhere in the argument list.
484 if (checkmimeheader(drft
))
487 for (n
= 0, arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
488 if (strcmp(*arguments
, "-n") == 0) {
495 * A -n was found so interpret the arguments as attachment numbers.
496 * Decrement any remaining argument number that is greater than the one
497 * just processed after processing each one so that the numbering stays
502 for (arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
503 if (strcmp(*arguments
, "-n") == 0)
506 if (**arguments
!= '\0') {
507 n
= atoi(*arguments
);
508 annotate(drft
, ATTACH_FIELD
, NULL
, 1, 0, n
, 1);
510 for (argp
= arguments
+ 1; *argp
!= NULL
; argp
++) {
511 if (atoi(*argp
) > n
) {
512 if (atoi(*argp
) == 1)
515 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
523 * The arguments are interpreted as file names. Run them through the
524 * user's shell for wildcard expansion and other goodies. Do this from
525 * the current working directory if the argument is not an absolute path
526 * name (does not begin with a /).
528 * We feed all the file names to the shell at once, otherwise you can't
529 * provide a file name with a space in it.
531 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
532 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
533 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
534 trim_suffix_c(shell
, '\n');
535 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
539 advise("popen", "could not get file from shell");
545 /* Unknown command */
555 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
557 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
560 /* Note that we do not quote -- the argp from the user
561 * is assumed to be quoted as they desire. (We can't treat
562 * it as pure literal as that would prevent them using ~,
563 * wildcards, etc.) The buffer produced by this function
564 * should be given to popen_in_dir() or system_in_dir() so
565 * that the current working directory is set correctly.
567 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
568 /* NB that some snprintf() return -1 on overflow rather than the
569 * new C99 mandated 'number of chars that would have been written'
571 /* length checks here and inside the loop allow for the
572 * trailing "&&", trailcmd, '"' and NUL
574 int trailln
= strlen(trailcmd
) + 4;
575 if (ln
< 0 || ln
+ trailln
> bufsz
)
576 die("arguments too long");
580 while (*argp
&& *++argp
) {
582 /* +1 for leading space */
583 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
584 die("arguments too long");
586 memcpy(cp
, *argp
, ln
+1);
590 *cp
++ = '&'; *cp
++ = '&';
591 strcpy(cp
, trailcmd
);
599 * Build a command line that causes the user's shell to list the file name
600 * arguments. This handles and wildcard expansion, tilde expansion, etc.
603 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
605 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
606 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
610 /* Like system(), but run the command in directory dir.
611 * This assumes the program is single-threaded!
614 system_in_dir(const char *dir
, const char *cmd
)
619 /* ensure that $SHELL exists, as the cmd was written relying on
620 a non-blank $SHELL... */
621 setenv("SHELL","/bin/sh",0); /* don't overwrite */
623 if (getcwd(olddir
, sizeof(olddir
)) == 0)
624 adios("getcwd", "could not get working directory");
626 adios("chdir", "could not change working directory");
628 if (chdir(olddir
) != 0)
629 adios("chdir", "could not change working directory");
633 /* ditto for popen() */
635 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
640 /* ensure that $SHELL exists, as the cmd was written relying on
641 a non-blank $SHELL... */
642 setenv("SHELL","/bin/sh",0); /* don't overwrite */
644 if (getcwd(olddir
, sizeof(olddir
)) == 0)
645 adios("getcwd", "could not get working directory");
647 adios("chdir", "could not change working directory");
648 f
= popen(cmd
, type
);
649 if (chdir(olddir
) != 0)
650 adios("chdir", "could not change working directory");
659 static bool reedit
; /* have we been here before? */
660 static char *edsave
= NULL
; /* the editor we used previously */
664 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
665 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
667 int pid
, status
, vecp
;
668 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
669 char *cp
, *prog
, **vec
;
672 bool slinked
= false;
674 /* Was there a previous edit session? */
675 if (reedit
&& (*ed
|| edsave
)) {
676 if (!*ed
) { /* no explicit editor */
677 *ed
= edsave
; /* so use the previous one */
678 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
681 /* unless we've specified it via "editor-next" */
682 cp
= concat (cp
, "-next", NULL
);
683 if ((cp
= context_find (cp
)) != NULL
)
687 /* set initial editor */
689 *ed
= get_default_editor();
693 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
694 strncpy (altpath
, altmsg
, sizeof(altpath
));
696 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
698 strncpy (linkpath
, LINK
, sizeof(linkpath
));
700 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
703 (void) m_unlink (linkpath
);
704 if (link (altpath
, linkpath
) == NOTOK
) {
705 if (symlink (altpath
, linkpath
) < 0) {
706 adios (linkpath
, "symlink");
715 context_save (); /* save the context file */
718 switch (pid
= fork()) {
720 advise ("fork", "unable to");
726 if (chdir (cwd
) < 0) {
727 advise (cwd
, "chdir");
732 setenv("mhfolder", mp
->foldpath
, 1);
733 setenv("editalt", altpath
, 1);
736 vec
= argsplit(*ed
, &prog
, &vecp
);
740 vec
[vecp
++] = *arg
++;
745 fprintf (stderr
, "unable to exec ");
750 if ((status
= pidwait (pid
, NOTOK
))) {
751 if (((status
& 0xff00) != 0xff00)
752 && (!reedit
|| (status
& 0x00ff))) {
753 if (!use
&& (status
& 0xff00) &&
754 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
755 inform("problems with edit--draft left in %s", cp
);
757 inform("problems with edit--%s preserved", file
);
760 status
= -2; /* maybe "reedit ? -2 : -1"? */
769 ? lstat (linkpath
, &st
) != NOTOK
770 && S_ISREG(st
.st_mode
)
771 && copyf (linkpath
, altpath
) == NOTOK
772 : stat (linkpath
, &st
) != NOTOK
774 && (m_unlink (altpath
) == NOTOK
775 || link (linkpath
, altpath
) == NOTOK
)))
776 advise (linkpath
, "unable to update %s from", altmsg
);
779 /* normally, we remember which editor we used */
781 edsave
= getcpy (*ed
);
784 if (altmsg
&& atfile
)
785 (void) m_unlink (linkpath
);
792 copyf (char *ifile
, char *ofile
)
797 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
799 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
800 admonish (ofile
, "unable to open and truncate");
805 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
806 if (write (out
, buffer
, i
) != i
) {
807 advise (ofile
, "may have damaged");
823 sendfile (char **arg
, char *file
, int pushsw
)
827 char *cp
, *sp
, **vec
, *program
;
830 * If the sendproc is the nmh command `send', then we call
831 * those routines directly rather than exec'ing the command.
833 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
835 sendit (invo_name
= sp
, arg
, file
, pushsw
);
840 context_save (); /* save the context file */
846 adios("fork", "failed:");
849 vec
= argsplit(sendproc
, &program
, &vecp
);
851 vec
[vecp
++] = "-push";
854 vec
[vecp
++] = *arg
++;
858 execvp (program
, vec
);
859 fprintf (stderr
, "unable to exec ");
864 if (pidwait(child_id
, OK
) == 0)
872 * Translate MIME composition file (call buildmimeproc)
876 buildfile (char **argp
, char *file
)
883 /* allocate space for arguments */
889 args
= mh_xmalloc((i
+ 2) * sizeof(char *));
892 * For backward compatibility, we need to add -build
893 * if we are using mhn as buildmimeproc
896 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
897 args
[i
++] = "-build";
899 /* copy any other arguments */
900 while (argp
&& *argp
)
904 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
907 return i
? NOTOK
: OK
;
912 # define SASLminc(a) (a)
913 #else /* CYRUS_SASL */
914 # define SASLminc(a) 0
915 #endif /* CYRUS_SASL */
918 # define TLSminc(a) (a)
919 #else /* TLS_SUPPORT */
920 # define TLSminc(a) 0
921 #endif /* TLS_SUPPORT */
923 #define SEND_SWITCHES \
924 X("alias aliasfile", 0, ALIASW) \
925 X("debug", -5, DEBUGSW) \
926 X("filter filterfile", 0, FILTSW) \
927 X("nofilter", 0, NFILTSW) \
928 X("format", 0, FRMTSW) \
929 X("noformat", 0, NFRMTSW) \
930 X("forward", 0, FORWSW) \
931 X("noforward", 0, NFORWSW) \
932 X("mime", 0, MIMESW) \
933 X("nomime", 0, NMIMESW) \
934 X("msgid", 0, MSGDSW) \
935 X("nomsgid", 0, NMSGDSW) \
936 X("push", 0, SPSHSW) \
937 X("nopush", 0, NSPSHSW) \
938 X("split seconds", 0, SPLITSW) \
939 X("unique", -6, UNIQSW) \
940 X("nounique", -8, NUNIQSW) \
941 X("verbose", 0, VERBSW) \
942 X("noverbose", 0, NVERBSW) \
943 X("watch", 0, WATCSW) \
944 X("nowatch", 0, NWATCSW) \
945 X("width columns", 0, WIDTHSW) \
946 X("version", 0, SVERSIONSW) \
947 X("help", 0, SHELPSW) \
948 X("dashstuffing", -12, BITSTUFFSW) \
949 X("nodashstuffing", -14, NBITSTUFFSW) \
950 X("client host", -6, CLIESW) \
951 X("server host", 6, SERVSW) \
952 X("snoop", -5, SNOOPSW) \
953 X("draftfolder +folder", 0, SDRFSW) \
954 X("draftmessage msg", 0, SDRMSW) \
955 X("nodraftfolder", 0, SNDRFSW) \
956 X("sasl", SASLminc(4), SASLSW) \
957 X("nosasl", SASLminc(6), NOSASLSW) \
958 X("saslmech", SASLminc(5), SASLMECHSW) \
959 X("authservice", SASLminc(0), AUTHSERVICESW) \
960 X("user username", SASLminc(4), USERSW) \
961 X("port server-port-name/number", 4, PORTSW) \
962 X("tls", TLSminc(-3), TLSSW) \
963 X("initialtls", TLSminc(-10), INITTLSSW) \
964 X("notls", TLSminc(-5), NTLSSW) \
965 X("certverify", TLSminc(-10), CERTVERSW) \
966 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
967 X("sendmail program", 0, MTSSM) \
968 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
969 X("messageid localname|random", 2, MESSAGEIDSW) \
971 #define X(sw, minchars, id) id,
972 DEFINE_SWITCH_ENUM(SEND
);
975 #define X(sw, minchars, id) { sw, minchars, id },
976 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
980 extern int debugsw
; /* from sendsbr.c */
988 extern char *altmsg
; /* .. */
989 extern char *annotext
;
990 extern char *distfile
;
994 sendit (char *sp
, char **arg
, char *file
, int pushed
)
997 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
998 char **arguments
, *savearg
[MAXARGS
], **vec
;
999 const char *user
= NULL
, *saslmech
= NULL
;
1000 char *auth_svc
= NULL
;
1004 * Make sure these are defined. In particular, we need
1005 * savearg[1] to be NULL, in case "arg" is NULL below. It
1006 * doesn't matter what is the value of savearg[0], but we
1007 * set it to NULL, to help catch "off-by-one" errors.
1013 * Temporarily copy arg to savearg, since the brkstring() call in
1014 * getarguments() will wipe it out before it is merged in.
1015 * Also, we skip the first element of savearg, since getarguments()
1016 * skips it. Then we count the number of arguments
1017 * copied. The value of "n" will be one greater than
1018 * this in order to simulate the standard argc/argv.
1023 copyip (arg
, savearg
+1, MAXARGS
-1);
1030 * Merge any arguments from command line (now in savearg)
1031 * and arguments from profile.
1033 arguments
= getarguments (sp
, n
, savearg
, 1);
1046 * Get our initial arguments for postproc now
1049 vec
= argsplit(postproc
, &program
, &vecp
);
1051 vec
[vecp
++] = "-library";
1052 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
1054 if ((cp
= context_find ("fileproc"))) {
1055 vec
[vecp
++] = "-fileproc";
1059 if ((cp
= context_find ("mhlproc"))) {
1060 vec
[vecp
++] = "-mhlproc";
1064 if ((cp
= context_find ("credentials"))) {
1065 /* post doesn't read context so need to pass credentials. */
1066 vec
[vecp
++] = "-credentials";
1070 while ((cp
= *argp
++)) {
1072 switch (smatch (++cp
, sendswitches
)) {
1074 ambigsw (cp
, sendswitches
);
1077 inform("-%s unknown\n", cp
);
1081 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1082 print_help (buf
, sendswitches
, 1);
1085 print_version (invo_name
);
1096 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1097 inform("missing argument to %s", argp
[-2]);
1153 #ifdef OAUTH_SUPPORT
1154 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
1155 die("missing argument to %s", argp
[-2]);
1158 NMH_UNUSED (auth_svc
);
1159 die("not built with OAuth support");
1177 if (!(cp
= *argp
++) || *cp
== '-') {
1178 inform("missing argument to %s", argp
[-2]);
1187 if (!(cp
= *argp
++) || *cp
== '-') {
1188 inform("missing argument to %s", argp
[-2]);
1196 inform("usage: %s [switches]", sp
);
1200 /* allow Aliasfile: profile entry */
1201 if ((cp
= context_find ("Aliasfile"))) {
1204 dp
= mh_xstrdup(cp
);
1205 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1206 vec
[vecp
++] = "-alias";
1211 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1212 if ((cp
= context_find ("signature")) && *cp
)
1213 setenv("SIGNATURE", cp
, 1);
1215 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1217 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1219 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1220 inplace
= atoi (cp
);
1222 if ((cp
= getenv ("mhdist"))
1226 vec
[vecp
++] = "-dist";
1227 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1228 die("unable to create temporary file");
1230 distfile
= mh_xstrdup(cp
);
1231 (void) m_unlink(distfile
);
1232 if (link (altmsg
, distfile
) == NOTOK
)
1233 adios (distfile
, "unable to link %s to", altmsg
);
1238 #ifdef OAUTH_SUPPORT
1239 if (auth_svc
== NULL
) {
1240 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
1241 die("must specify -authservice with -saslmech xoauth2");
1245 die("must specify -user with -saslmech xoauth2");
1249 NMH_UNUSED(saslmech
);
1250 #endif /* OAUTH_SUPPORT */
1252 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1257 if ((pushsw
= pushed
))
1262 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, auth_svc
) == OK
)
1271 whomfile (char **arg
, char *file
)
1275 char **vec
, *program
;
1277 context_save (); /* save the context file */
1280 switch (pid
= fork()) {
1282 advise ("fork", "unable to");
1286 vec
= argsplit(whomproc
, &program
, &vecp
);
1289 vec
[vecp
++] = *arg
++;
1293 execvp (program
, vec
);
1294 fprintf (stderr
, "unable to exec ");
1296 _exit(1); /* NOTREACHED */
1299 return pidwait(pid
, NOTOK
) & 0377 ? 1 : 0;
1305 * Remove the draft file
1309 removefile (char *drft
)
1311 if (m_unlink (drft
) == NOTOK
)
1312 adios (drft
, "unable to unlink");
1319 * Return 1 if we already have a MIME-Version header, 0 otherwise.
1323 checkmimeheader (char *drft
)
1326 m_getfld_state_t gstate
;
1327 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1329 bool retval
= false;
1331 if ((f
= fopen(drft
, "r")) == NULL
) {
1332 admonish(drft
, "unable to read draft");
1336 gstate
= m_getfld_state_init(f
);
1338 int bufsz
= sizeof(buf
);
1339 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1342 if (strcasecmp(name
, VRSN_FIELD
) == 0) {
1343 inform("Cannot use attach commands with already-"
1344 "formatted MIME message \"%s\"", drft
);
1355 m_getfld_state_destroy(&gstate
);