]>
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.
51 #define WHATNOW_SWITCHES \
52 X("draftfolder +folder", 0, DFOLDSW) \
53 X("draftmessage msg", 0, DMSGSW) \
54 X("nodraftfolder", 0, NDFLDSW) \
55 X("editor editor", 0, EDITRSW) \
56 X("noedit", 0, NEDITSW) \
57 X("prompt string", 4, PRMPTSW) \
58 X("version", 0, VERSIONSW) \
59 X("help", 0, HELPSW) \
60 X("attach header-field-name", -6, ATTACHSW) \
61 X("noattach", -8, NOATTACHSW) \
64 #define X(sw, minchars, id) id,
65 DEFINE_SWITCH_ENUM(WHATNOW
);
68 #define X(sw, minchars, id) { sw, minchars, id },
69 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
73 * Options at the "whatnow" prompt
75 #define PROMPT_SWITCHES \
76 X("edit [<editor> <switches>]", 0, EDITSW) \
77 X("refile [<switches>] +folder", 0, REFILEOPT) \
78 X("mime [<switches>]", 0, BUILDMIMESW) \
79 X("display [<switches>]", 0, DISPSW) \
80 X("list [<switches>]", 0, LISTSW) \
81 X("send [<switches>]", 0, SENDSW) \
82 X("push [<switches>]", 0, PUSHSW) \
83 X("whom [<switches>]", 0, WHOMSW) \
84 X("quit [-delete]", 0, QUITSW) \
85 X("delete", 0, DELETESW) \
86 X("cd [directory]", 0, CDCMDSW) \
87 X("pwd", 0, PWDCMDSW) \
89 X("attach [-v]", 0, ATTACHCMDSW) \
90 X("detach [-n]", 0, DETACHCMDSW) \
91 X("alist [-ln] ", 2, ALISTCMDSW) \
93 #define X(sw, minchars, id) id,
94 DEFINE_SWITCH_ENUM(PROMPT
);
97 #define X(sw, minchars, id) { sw, minchars, id },
98 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
101 static char *myprompt
= "\nWhat now? ";
106 static int editfile (char **, char **, char *, int, struct msgs
*,
107 char *, char *, int, int);
108 static int sendfile (char **, char *, int);
109 static void sendit (char *, char **, char *, int);
110 static int buildfile (char **, char *);
111 static int whomfile (char **, char *);
112 static int removefile (char *);
113 static int checkmimeheader (char *);
114 static void writelscmd(char *, int, char *, char **);
115 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
116 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
117 static int system_in_dir(const char *dir
, const char *cmd
);
118 static int copyf (char *, char *);
122 WhatNow (int argc
, char **argv
)
124 int isdf
= 0, nedit
= 0, use
= 0, atfile
= 1;
125 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
126 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
127 char buf
[BUFSIZ
], prompt
[BUFSIZ
];
128 char **argp
, **arguments
;
130 char cwd
[PATH_MAX
+ 1]; /* current working directory */
131 char file
[PATH_MAX
+ 1]; /* file name buffer */
132 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
133 FILE *f
; /* read pointer for bgnd proc */
134 char *l
; /* set on -l to alist command */
135 int n
; /* set on -n to alist command */
137 /* Need this if called from what_now(). */
138 invo_name
= r1bindex (argv
[0], '/');
140 arguments
= getarguments (invo_name
, argc
, argv
, 1);
144 * Get the initial current working directory.
147 if (getcwd(cwd
, sizeof (cwd
)) == NULL
) {
148 adios("getcwd", "could not get working directory");
151 while ((cp
= *argp
++)) {
153 switch (smatch (++cp
, whatnowswitches
)) {
155 ambigsw (cp
, whatnowswitches
);
158 adios (NULL
, "-%s unknown", cp
);
161 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
162 print_help (buf
, whatnowswitches
, 1);
165 print_version(invo_name
);
170 adios (NULL
, "only one draft folder at a time!");
171 if (!(cp
= *argp
++) || *cp
== '-')
172 adios (NULL
, "missing argument to %s", argp
[-2]);
173 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
174 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
178 adios (NULL
, "only one draft message at a time!");
179 if (!(dmsg
= *argp
++) || *dmsg
== '-')
180 adios (NULL
, "missing argument to %s", argp
[-2]);
188 if (!(ed
= *argp
++) || *ed
== '-')
189 adios (NULL
, "missing argument to %s", argp
[-2]);
197 if (!(myprompt
= *argp
++) || *myprompt
== '-')
198 adios (NULL
, "missing argument to %s", argp
[-2]);
202 advise(NULL
, "The -attach switch is deprecated");
206 advise(NULL
, "The -noattach switch is deprecated");
211 adios (NULL
, "only one draft at a time!");
216 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
217 drft
= getcpy (m_draft (dfolder
, dmsg
, 1, &isdf
));
219 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? mh_xstrdup(cp
) : NULL
;
221 if ((cp
= getenv ("mhatfile")) && *cp
)
224 if ((cp
= getenv ("mhuse")) && *cp
)
227 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
232 /* start editing the draft, unless -noedit was given */
233 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
234 NULL
, 1, atfile
) < 0)
237 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
239 #ifdef READLINE_SUPPORT
240 if (!(argp
= read_switch_multiword_via_readline (prompt
, aleqs
))) {
241 #else /* ! READLINE_SUPPORT */
242 if (!(argp
= read_switch_multiword (prompt
, aleqs
))) {
243 #endif /* READLINE_SUPPORT */
244 (void) m_unlink (LINK
);
247 switch (smatch (*argp
, aleqs
)) {
249 /* display the message being replied to, or distributed */
251 showfile (++argp
, msgnam
);
253 advise (NULL
, "no alternate message to display");
257 /* Translate MIME composition file */
258 buildfile (++argp
, drft
);
262 /* Call an editor on the draft file */
265 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
266 NULL
, 1, atfile
) == NOTOK
)
271 /* display the draft file */
272 showfile (++argp
, drft
);
276 /* Check to whom the draft would be sent */
277 whomfile (++argp
, drft
);
281 /* Quit, and possibly delete the draft */
282 if (*++argp
&& (*argp
[0] == 'd' ||
283 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
286 if (stat (drft
, &st
) != NOTOK
)
287 advise (NULL
, "draft left on %s", drft
);
292 /* Delete draft and exit */
297 /* Send draft in background */
298 if (sendfile (++argp
, drft
, 1))
304 sendfile (++argp
, drft
, 0);
308 /* Refile the draft */
309 if (refile (++argp
, drft
) == 0)
314 /* Change the working directory for attachments
316 * Run the directory through the user's shell so that
317 * we can take advantage of any syntax that the user
318 * is accustomed to. Read back the absolute path.
321 if (*(argp
+1) == NULL
) {
322 strcpy(buf
, "$SHELL -c \"cd&&pwd\"");
325 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
327 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
328 if (fgets(cwd
, sizeof (cwd
), f
) == NULL
) {
329 advise (buf
, "fgets");
331 TrimSuffixC(cwd
, '\n');
335 advise("popen", "could not get directory");
341 /* Print the working directory for attachments */
346 /* List files in the current attachment working directory
348 * Use the user's shell so that we can take advantage of any
349 * syntax that the user is accustomed to.
351 writelscmd(buf
, sizeof(buf
), "", argp
);
352 (void)system_in_dir(cwd
, buf
);
357 * List attachments on current draft. Options are:
359 * -l long listing (full path names)
363 if (checkmimeheader(drft
))
369 while (*++argp
!= NULL
) {
370 if (strcmp(*argp
, "-l") == 0)
373 else if (strcmp(*argp
, "-n") == 0)
376 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
388 advise(NULL
, "usage is alist [-ln].");
391 annolist(drft
, ATTACH_FIELD
, l
, n
);
397 * Attach files to current draft.
403 if (checkmimeheader(drft
))
406 for (ap
= argp
+1; *ap
; ++ap
) {
407 if (strcmp(*ap
, "-v") == 0) {
410 } else if (*ap
[0] != '-') {
415 if (*(argp
+1) == NULL
) {
416 advise(NULL
, "attach command requires file argument(s).");
421 * Build a command line that causes the user's shell to list the file name
422 * arguments. This handles and wildcard expansion, tilde expansion, etc.
424 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
427 * Read back the response from the shell, which contains a number of lines
428 * with one file name per line. Remove off the newline. Determine whether
429 * we have an absolute or relative path name. Prepend the current working
430 * directory to relative path names. Add the attachment annotation to the
434 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
435 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
438 TrimSuffixC(shell
, '\n');
441 strncpy(file
, shell
, sizeof(file
));
442 file
[sizeof(file
) - 1] = '\0';
444 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
447 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
449 ctype
= mime_type(file
);
450 printf ("Attaching %s as a %s\n", file
, ctype
);
458 advise("popen", "could not get file from shell");
465 * Detach files from current draft.
469 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
470 * so this catches a -n anywhere in the argument list.
473 if (checkmimeheader(drft
))
476 for (n
= 0, arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
477 if (strcmp(*arguments
, "-n") == 0) {
484 * A -n was found so interpret the arguments as attachment numbers.
485 * Decrement any remaining argument number that is greater than the one
486 * just processed after processing each one so that the numbering stays
491 for (arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
492 if (strcmp(*arguments
, "-n") == 0)
495 if (**arguments
!= '\0') {
496 n
= atoi(*arguments
);
497 annotate(drft
, ATTACH_FIELD
, NULL
, 1, 0, n
, 1);
499 for (argp
= arguments
+ 1; *argp
!= NULL
; argp
++) {
500 if (atoi(*argp
) > n
) {
501 if (atoi(*argp
) == 1)
504 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
512 * The arguments are interpreted as file names. Run them through the
513 * user's shell for wildcard expansion and other goodies. Do this from
514 * the current working directory if the argument is not an absolute path
515 * name (does not begin with a /).
517 * We feed all the file names to the shell at once, otherwise you can't
518 * provide a file name with a space in it.
520 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
521 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
522 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
523 TrimSuffixC(shell
, '\n');
524 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
528 advise("popen", "could not get file from shell");
534 /* Unknown command */
535 advise (NULL
, "say what?");
544 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
546 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
549 /* Note that we do not quote -- the argp from the user
550 * is assumed to be quoted as they desire. (We can't treat
551 * it as pure literal as that would prevent them using ~,
552 * wildcards, etc.) The buffer produced by this function
553 * should be given to popen_in_dir() or system_in_dir() so
554 * that the current working directory is set correctly.
556 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
557 /* NB that some snprintf() return -1 on overflow rather than the
558 * new C99 mandated 'number of chars that would have been written'
560 /* length checks here and inside the loop allow for the
561 * trailing "&&", trailcmd, '"' and NUL
563 int trailln
= strlen(trailcmd
) + 4;
564 if (ln
< 0 || ln
+ trailln
> bufsz
)
565 adios(NULL
, "arguments too long");
569 while (*argp
&& *++argp
) {
571 /* +1 for leading space */
572 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
573 adios(NULL
, "arguments too long");
575 memcpy(cp
, *argp
, ln
+1);
579 *cp
++ = '&'; *cp
++ = '&';
580 strcpy(cp
, trailcmd
);
588 * Build a command line that causes the user's shell to list the file name
589 * arguments. This handles and wildcard expansion, tilde expansion, etc.
592 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
594 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
595 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
599 /* Like system(), but run the command in directory dir.
600 * This assumes the program is single-threaded!
603 system_in_dir(const char *dir
, const char *cmd
)
608 /* ensure that $SHELL exists, as the cmd was written relying on
609 a non-blank $SHELL... */
610 setenv("SHELL","/bin/sh",0); /* don't overwrite */
612 if (getcwd(olddir
, sizeof(olddir
)) == 0)
613 adios("getcwd", "could not get working directory");
615 adios("chdir", "could not change working directory");
617 if (chdir(olddir
) != 0)
618 adios("chdir", "could not change working directory");
622 /* ditto for popen() */
624 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
629 /* ensure that $SHELL exists, as the cmd was written relying on
630 a non-blank $SHELL... */
631 setenv("SHELL","/bin/sh",0); /* don't overwrite */
633 if (getcwd(olddir
, sizeof(olddir
)) == 0)
634 adios("getcwd", "could not get working directory");
636 adios("chdir", "could not change working directory");
637 f
= popen(cmd
, type
);
638 if (chdir(olddir
) != 0)
639 adios("chdir", "could not change working directory");
648 static int reedit
= 0; /* have we been here before? */
649 static char *edsave
= NULL
; /* the editor we used previously */
653 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
654 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
656 int pid
, status
, vecp
;
657 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
658 char *cp
, *prog
, **vec
;
663 /* Was there a previous edit session? */
664 if (reedit
&& (*ed
|| edsave
)) {
665 if (!*ed
) { /* no explicit editor */
666 *ed
= edsave
; /* so use the previous one */
667 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
670 /* unless we've specified it via "editor-next" */
671 cp
= concat (cp
, "-next", NULL
);
672 if ((cp
= context_find (cp
)) != NULL
)
676 /* set initial editor */
678 *ed
= get_default_editor();
682 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
683 strncpy (altpath
, altmsg
, sizeof(altpath
));
685 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
687 strncpy (linkpath
, LINK
, sizeof(linkpath
));
689 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
692 (void) m_unlink (linkpath
);
693 if (link (altpath
, linkpath
) == NOTOK
) {
694 if (symlink (altpath
, linkpath
) < 0) {
695 adios (linkpath
, "symlink");
704 context_save (); /* save the context file */
707 switch (pid
= fork()) {
709 advise ("fork", "unable to");
715 if (chdir (cwd
) < 0) {
716 advise (cwd
, "chdir");
721 m_putenv ("mhfolder", mp
->foldpath
);
722 m_putenv ("editalt", altpath
);
725 vec
= argsplit(*ed
, &prog
, &vecp
);
729 vec
[vecp
++] = *arg
++;
734 fprintf (stderr
, "unable to exec ");
739 if ((status
= pidwait (pid
, NOTOK
))) {
740 if (((status
& 0xff00) != 0xff00)
741 && (!reedit
|| (status
& 0x00ff))) {
742 if (!use
&& (status
& 0xff00) &&
743 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
744 advise (NULL
, "problems with edit--draft left in %s", cp
);
746 advise (NULL
, "problems with edit--%s preserved", file
);
749 status
= -2; /* maybe "reedit ? -2 : -1"? */
758 ? lstat (linkpath
, &st
) != NOTOK
759 && S_ISREG(st
.st_mode
)
760 && copyf (linkpath
, altpath
) == NOTOK
761 : stat (linkpath
, &st
) != NOTOK
763 && (m_unlink (altpath
) == NOTOK
764 || link (linkpath
, altpath
) == NOTOK
)))
765 advise (linkpath
, "unable to update %s from", altmsg
);
768 /* normally, we remember which editor we used */
770 edsave
= getcpy (*ed
);
773 if (altmsg
&& atfile
)
774 (void) m_unlink (linkpath
);
781 copyf (char *ifile
, char *ofile
)
786 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
788 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
789 admonish (ofile
, "unable to open and truncate");
794 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
795 if (write (out
, buffer
, i
) != i
) {
796 advise (ofile
, "may have damaged");
812 sendfile (char **arg
, char *file
, int pushsw
)
816 char *cp
, *sp
, **vec
, *program
;
819 * If the sendproc is the nmh command `send', then we call
820 * those routines directly rather than exec'ing the command.
822 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
824 sendit (invo_name
= sp
, arg
, file
, pushsw
);
829 context_save (); /* save the context file */
832 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
836 advise (NULL
, "unable to fork, so sending directly...");
838 vec
= argsplit(sendproc
, &program
, &vecp
);
840 vec
[vecp
++] = "-push";
843 vec
[vecp
++] = *arg
++;
847 execvp (program
, vec
);
848 fprintf (stderr
, "unable to exec ");
853 if (pidwait(child_id
, OK
) == 0)
861 * Translate MIME composition file (call buildmimeproc)
865 buildfile (char **argp
, char *file
)
872 /* allocate space for arguments */
878 args
= (char **) mh_xmalloc((i
+ 2) * sizeof(char *));
881 * For backward compatibility, we need to add -build
882 * if we are using mhn as buildmimeproc
885 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
886 args
[i
++] = "-build";
888 /* copy any other arguments */
889 while (argp
&& *argp
)
893 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
896 return (i
? NOTOK
: OK
);
901 # define SASLminc(a) (a)
902 #else /* CYRUS_SASL */
903 # define SASLminc(a) 0
904 #endif /* CYRUS_SASL */
907 # define TLSminc(a) (a)
908 #else /* TLS_SUPPORT */
909 # define TLSminc(a) 0
910 #endif /* TLS_SUPPORT */
912 #define SEND_SWITCHES \
913 X("alias aliasfile", 0, ALIASW) \
914 X("debug", -5, DEBUGSW) \
915 X("filter filterfile", 0, FILTSW) \
916 X("nofilter", 0, NFILTSW) \
917 X("format", 0, FRMTSW) \
918 X("noformat", 0, NFRMTSW) \
919 X("forward", 0, FORWSW) \
920 X("noforward", 0, NFORWSW) \
921 X("mime", 0, MIMESW) \
922 X("nomime", 0, NMIMESW) \
923 X("msgid", 0, MSGDSW) \
924 X("nomsgid", 0, NMSGDSW) \
925 X("push", 0, SPSHSW) \
926 X("nopush", 0, NSPSHSW) \
927 X("split seconds", 0, SPLITSW) \
928 X("unique", -6, UNIQSW) \
929 X("nounique", -8, NUNIQSW) \
930 X("verbose", 0, VERBSW) \
931 X("noverbose", 0, NVERBSW) \
932 X("watch", 0, WATCSW) \
933 X("nowatch", 0, NWATCSW) \
934 X("width columns", 0, WIDTHSW) \
935 X("version", 0, SVERSIONSW) \
936 X("help", 0, SHELPSW) \
937 X("dashstuffing", -12, BITSTUFFSW) \
938 X("nodashstuffing", -14, NBITSTUFFSW) \
939 X("client host", -6, CLIESW) \
940 X("server host", 6, SERVSW) \
941 X("snoop", -5, SNOOPSW) \
942 X("draftfolder +folder", 0, SDRFSW) \
943 X("draftmessage msg", 0, SDRMSW) \
944 X("nodraftfolder", 0, SNDRFSW) \
945 X("sasl", SASLminc(4), SASLSW) \
946 X("nosasl", SASLminc(6), NOSASLSW) \
947 X("saslmech", SASLminc(5), SASLMECHSW) \
948 X("authservice", SASLminc(0), AUTHSERVICESW) \
949 X("user username", SASLminc(4), USERSW) \
950 X("attach fieldname", 6, SNDATTACHSW) \
951 X("noattach", 0, SNDNOATTACHSW) \
952 X("attachformat", 7, SNDATTACHFORMAT) \
953 X("port server-port-name/number", 4, PORTSW) \
954 X("tls", TLSminc(-3), TLSSW) \
955 X("initialtls", TLSminc(-10), INITTLSSW) \
956 X("notls", TLSminc(-5), NTLSSW) \
957 X("sendmail program", 0, MTSSM) \
958 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
959 X("messageid localname|random", 2, MESSAGEIDSW) \
961 #define X(sw, minchars, id) id,
962 DEFINE_SWITCH_ENUM(SEND
);
965 #define X(sw, minchars, id) { sw, minchars, id },
966 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
970 extern int debugsw
; /* from sendsbr.c */
978 extern char *altmsg
; /* .. */
979 extern char *annotext
;
980 extern char *distfile
;
984 sendit (char *sp
, char **arg
, char *file
, int pushed
)
987 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
988 char **arguments
, *savearg
[MAXARGS
], **vec
;
989 const char *user
= NULL
, *saslmech
= NULL
;
990 char *auth_svc
= NULL
;
999 * Make sure these are defined. In particular, we need
1000 * savearg[1] to be NULL, in case "arg" is NULL below. It
1001 * doesn't matter what is the value of savearg[0], but we
1002 * set it to NULL, to help catch "off-by-one" errors.
1008 * Temporarily copy arg to savearg, since the brkstring() call in
1009 * getarguments() will wipe it out before it is merged in.
1010 * Also, we skip the first element of savearg, since getarguments()
1011 * skips it. Then we count the number of arguments
1012 * copied. The value of "n" will be one greater than
1013 * this in order to simulate the standard argc/argv.
1018 copyip (arg
, savearg
+1, MAXARGS
-1);
1025 * Merge any arguments from command line (now in savearg)
1026 * and arguments from profile.
1028 arguments
= getarguments (sp
, n
, savearg
, 1);
1041 * Get our initial arguments for postproc now
1044 vec
= argsplit(postproc
, &program
, &vecp
);
1046 vec
[vecp
++] = "-library";
1047 vec
[vecp
++] = getcpy (m_maildir (""));
1049 if ((cp
= context_find ("fileproc"))) {
1050 vec
[vecp
++] = "-fileproc";
1054 if ((cp
= context_find ("mhlproc"))) {
1055 vec
[vecp
++] = "-mhlproc";
1059 if ((cp
= context_find ("credentials"))) {
1060 /* post doesn't read context so need to pass credentials. */
1061 vec
[vecp
++] = "-credentials";
1065 while ((cp
= *argp
++)) {
1067 switch (smatch (++cp
, sendswitches
)) {
1069 ambigsw (cp
, sendswitches
);
1072 advise (NULL
, "-%s unknown\n", cp
);
1076 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1077 print_help (buf
, sendswitches
, 1);
1080 print_version (invo_name
);
1091 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1092 advise (NULL
, "missing argument to %s", argp
[-2]);
1120 debugsw
++; /* fall */
1146 #ifdef OAUTH_SUPPORT
1147 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
1148 adios (NULL
, "missing argument to %s", argp
[-2]);
1151 NMH_UNUSED (auth_svc
);
1152 adios (NULL
, "not built with OAuth support");
1170 if (!(cp
= *argp
++) || *cp
== '-') {
1171 advise (NULL
, "missing argument to %s", argp
[-2]);
1180 if (!(cp
= *argp
++) || *cp
== '-') {
1181 advise (NULL
, "missing argument to %s", argp
[-2]);
1188 advise(NULL
, "The -attach switch is deprecated");
1191 advise(NULL
, "The -noattach switch is deprecated");
1194 case SNDATTACHFORMAT
:
1195 advise(NULL
, "The -attachformat switch is deprecated");
1199 advise (NULL
, "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 m_putenv ("SIGNATURE", cp
);
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"))
1228 && (distsw
= atoi (cp
))
1229 #endif /* not lint */
1231 vec
[vecp
++] = "-dist";
1232 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1233 adios(NULL
, "unable to create temporary file in %s",
1236 distfile
= mh_xstrdup(cp
);
1237 (void) m_unlink(distfile
);
1238 if (link (altmsg
, distfile
) == NOTOK
)
1239 adios (distfile
, "unable to link %s to", altmsg
);
1244 #ifdef OAUTH_SUPPORT
1245 if (auth_svc
== NULL
) {
1246 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
1247 adios (NULL
, "must specify -authservice with -saslmech xoauth2");
1251 adios (NULL
, "must specify -user with -saslmech xoauth2");
1255 NMH_UNUSED(saslmech
);
1256 #endif /* OAUTH_SUPPORT */
1258 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1263 if ((pushsw
= pushed
))
1268 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, auth_svc
) == OK
)
1277 whomfile (char **arg
, char *file
)
1281 char **vec
, *program
;
1283 context_save (); /* save the context file */
1286 switch (pid
= fork()) {
1288 advise ("fork", "unable to");
1292 vec
= argsplit(whomproc
, &program
, &vecp
);
1295 vec
[vecp
++] = *arg
++;
1299 execvp (program
, vec
);
1300 fprintf (stderr
, "unable to exec ");
1302 _exit (-1); /* NOTREACHED */
1305 return (pidwait (pid
, NOTOK
) & 0377 ? 1 : 0);
1311 * Remove the draft file
1315 removefile (char *drft
)
1317 if (m_unlink (drft
) == NOTOK
)
1318 adios (drft
, "unable to unlink");
1325 * Return 1 if we already have a MIME-Version header, 0 otherwise.
1329 checkmimeheader (char *drft
)
1332 m_getfld_state_t gstate
= 0;
1333 char buf
[BUFSIZ
], name
[NAMESZ
];
1334 int state
, retval
= 0;
1336 if ((f
= fopen(drft
, "r")) == NULL
) {
1337 admonish(drft
, "unable to read draft");
1342 int bufsz
= sizeof(buf
);
1343 switch (state
= m_getfld(&gstate
, name
, buf
, &bufsz
, f
)) {
1346 if (strcasecmp(name
, VRSN_FIELD
) == 0) {
1347 advise(NULL
, "Cannot use attach commands with already-"
1348 "formatted MIME message \"%s\"", drft
);
1359 m_getfld_state_destroy(&gstate
);