]>
diplodocus.org Git - nmh/blob - uip/whatnowsbr.c
f405721119cead75c16b11768110fbee8017807a
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/getcpy.h"
42 #include "sbr/m_draft.h"
43 #include "sbr/m_backup.h"
44 #include "sbr/geteditor.h"
45 #include "sbr/context_save.h"
46 #include "sbr/context_find.h"
47 #include "sbr/brkstring.h"
48 #include "sbr/ambigsw.h"
51 #include "sbr/print_version.h"
52 #include "sbr/print_help.h"
53 #include "sbr/arglist.h"
54 #include "sbr/error.h"
62 #include "sbr/m_maildir.h"
63 #include "sbr/m_mktemp.h"
64 #include "sbr/mime_type.h"
66 #define WHATNOW_SWITCHES \
67 X("draftfolder +folder", 0, DFOLDSW) \
68 X("draftmessage msg", 0, DMSGSW) \
69 X("nodraftfolder", 0, NDFLDSW) \
70 X("editor editor", 0, EDITRSW) \
71 X("noedit", 0, NEDITSW) \
72 X("prompt string", 4, PRMPTSW) \
73 X("version", 0, VERSIONSW) \
74 X("help", 0, HELPSW) \
77 #define X(sw, minchars, id) id,
78 DEFINE_SWITCH_ENUM(WHATNOW
);
81 #define X(sw, minchars, id) { sw, minchars, id },
82 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
86 * Options at the "whatnow" prompt
88 #define PROMPT_SWITCHES \
89 X("edit [<editor> <switches>]", 0, EDITSW) \
90 X("refile [<switches>] +folder", 0, REFILEOPT) \
91 X("mime [<switches>]", 0, BUILDMIMESW) \
92 X("display [<switches>]", 0, DISPSW) \
93 X("list [<switches>]", 0, LISTSW) \
94 X("send [<switches>]", 0, SENDSW) \
95 X("push [<switches>]", 0, PUSHSW) \
96 X("whom [<switches>]", 0, WHOMSW) \
97 X("quit [-delete]", 0, QUITSW) \
98 X("delete", 0, DELETESW) \
99 X("cd [directory]", 0, CDCMDSW) \
100 X("pwd", 0, PWDCMDSW) \
101 X("ls", 2, LSCMDSW) \
102 X("attach [-v]", 0, ATTACHCMDSW) \
103 X("detach [-n]", 0, DETACHCMDSW) \
104 X("alist [-ln] ", 2, ALISTCMDSW) \
106 #define X(sw, minchars, id) id,
107 DEFINE_SWITCH_ENUM(PROMPT
);
110 #define X(sw, minchars, id) { sw, minchars, id },
111 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
114 static char *myprompt
= "\nWhat now? ";
119 static int editfile (char **, char **, char *, int, struct msgs
*,
120 char *, char *, int, int);
121 static int sendfile (char **, char *, int);
122 static void sendit (char *, char **, char *, int);
123 static int buildfile (char **, char *);
124 static int whomfile (char **, char *);
125 static int removefile (char *);
126 static int checkmimeheader (char *);
127 static void writelscmd(char *, int, char *, char **);
128 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
129 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
130 static int system_in_dir(const char *dir
, const char *cmd
);
131 static int copyf (char *, char *);
135 WhatNow (int argc
, char **argv
)
139 int use
= 0, atfile
= 1;
140 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
141 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
143 char **argp
, **arguments
;
145 char cwd
[PATH_MAX
+ 1]; /* current working directory */
146 char file
[PATH_MAX
+ 1]; /* file name buffer */
147 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
148 FILE *f
; /* read pointer for bgnd proc */
149 char *l
; /* set on -l to alist command */
150 int n
; /* set on -n to alist command */
152 /* Need this if called from what_now(). */
153 invo_name
= r1bindex (argv
[0], '/');
155 arguments
= getarguments (invo_name
, argc
, argv
, 1);
159 * Get the initial current working directory.
162 if (getcwd(cwd
, sizeof (cwd
)) == NULL
) {
163 adios("getcwd", "could not get working directory");
166 while ((cp
= *argp
++)) {
168 switch (smatch (++cp
, whatnowswitches
)) {
170 ambigsw (cp
, whatnowswitches
);
173 die("-%s unknown", cp
);
176 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
177 print_help (buf
, whatnowswitches
, 1);
180 print_version(invo_name
);
185 die("only one draft folder at a time!");
186 if (!(cp
= *argp
++) || *cp
== '-')
187 die("missing argument to %s", argp
[-2]);
188 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
189 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
193 die("only one draft message at a time!");
194 if (!(dmsg
= *argp
++) || *dmsg
== '-')
195 die("missing argument to %s", argp
[-2]);
203 if (!(ed
= *argp
++) || *ed
== '-')
204 die("missing argument to %s", argp
[-2]);
212 if (!(myprompt
= *argp
++) || *myprompt
== '-')
213 die("missing argument to %s", argp
[-2]);
218 die("only one draft at a time!");
222 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
223 drft
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
225 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? mh_xstrdup(cp
) : NULL
;
227 if ((cp
= getenv ("mhatfile")) && *cp
)
230 if ((cp
= getenv ("mhuse")) && *cp
)
233 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
238 /* start editing the draft, unless -noedit was given */
239 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
240 NULL
, 1, atfile
) < 0)
244 #ifdef READLINE_SUPPORT
245 if (!(argp
= read_switch_multiword_via_readline(myprompt
, aleqs
))) {
246 #else /* ! READLINE_SUPPORT */
247 if (!(argp
= read_switch_multiword(myprompt
, aleqs
))) {
248 #endif /* READLINE_SUPPORT */
249 (void) m_unlink (LINK
);
252 switch (smatch (*argp
, aleqs
)) {
254 /* display the message being replied to, or distributed */
256 showfile (++argp
, msgnam
);
258 inform("no alternate message to display");
262 /* Translate MIME composition file */
263 buildfile (++argp
, drft
);
267 /* Call an editor on the draft file */
270 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
271 NULL
, 1, atfile
) == NOTOK
)
276 /* display the draft file */
277 showfile (++argp
, drft
);
281 /* Check to whom the draft would be sent */
282 whomfile (++argp
, drft
);
286 /* Quit, and possibly delete the draft */
287 if (*++argp
&& (*argp
[0] == 'd' ||
288 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
291 if (stat (drft
, &st
) != NOTOK
)
292 inform("draft left on %s", drft
);
297 /* Delete draft and exit */
302 /* Send draft in background */
303 if (sendfile (++argp
, drft
, 1))
309 sendfile (++argp
, drft
, 0);
313 /* Refile the draft */
314 if (refile (++argp
, drft
) == 0)
319 /* Change the working directory for attachments
321 * Run the directory through the user's shell so that
322 * we can take advantage of any syntax that the user
323 * is accustomed to. Read back the absolute path.
326 if (*(argp
+1) == NULL
) {
327 strcpy(buf
, "$SHELL -c \"cd&&pwd\"");
330 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
332 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
333 if (fgets(cwd
, sizeof (cwd
), f
) == NULL
) {
334 advise (buf
, "fgets");
336 trim_suffix_c(cwd
, '\n');
340 advise("popen", "could not get directory");
346 /* Print the working directory for attachments */
351 /* List files in the current attachment working directory
353 * Use the user's shell so that we can take advantage of any
354 * syntax that the user is accustomed to.
356 writelscmd(buf
, sizeof(buf
), "", argp
);
357 (void)system_in_dir(cwd
, buf
);
362 * List attachments on current draft. Options are:
364 * -l long listing (full path names)
368 if (checkmimeheader(drft
))
374 while (*++argp
!= NULL
) {
375 if (strcmp(*argp
, "-l") == 0)
378 else if (strcmp(*argp
, "-n") == 0)
381 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
393 inform("usage is alist [-ln].");
396 annolist(drft
, ATTACH_FIELD
, l
, n
);
402 * Attach files to current draft.
405 bool verbose
= false;
408 if (checkmimeheader(drft
))
411 for (ap
= argp
+1; *ap
; ++ap
) {
412 if (strcmp(*ap
, "-v") == 0) {
415 } else if (*ap
[0] != '-') {
420 if (*(argp
+1) == NULL
) {
421 inform("attach command requires file argument(s).");
426 * Build a command line that causes the user's shell to list the file name
427 * arguments. This handles and wildcard expansion, tilde expansion, etc.
429 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
432 * Read back the response from the shell, which contains a number of lines
433 * with one file name per line. Remove off the newline. Determine whether
434 * we have an absolute or relative path name. Prepend the current working
435 * directory to relative path names. Add the attachment annotation to the
439 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
440 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
443 trim_suffix_c(shell
, '\n');
446 strncpy(file
, shell
, sizeof(file
));
447 file
[sizeof(file
) - 1] = '\0';
449 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
452 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
454 ctype
= mime_type(file
);
455 printf ("Attaching %s as a %s\n", file
, ctype
);
463 advise("popen", "could not get file from shell");
470 * Detach files from current draft.
474 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
475 * so this catches a -n anywhere in the argument list.
478 if (checkmimeheader(drft
))
481 for (n
= 0, arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
482 if (strcmp(*arguments
, "-n") == 0) {
489 * A -n was found so interpret the arguments as attachment numbers.
490 * Decrement any remaining argument number that is greater than the one
491 * just processed after processing each one so that the numbering stays
496 for (arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
497 if (strcmp(*arguments
, "-n") == 0)
500 if (**arguments
!= '\0') {
501 n
= atoi(*arguments
);
502 annotate(drft
, ATTACH_FIELD
, NULL
, 1, 0, n
, 1);
504 for (argp
= arguments
+ 1; *argp
!= NULL
; argp
++) {
505 if (atoi(*argp
) > n
) {
506 if (atoi(*argp
) == 1)
509 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
517 * The arguments are interpreted as file names. Run them through the
518 * user's shell for wildcard expansion and other goodies. Do this from
519 * the current working directory if the argument is not an absolute path
520 * name (does not begin with a /).
522 * We feed all the file names to the shell at once, otherwise you can't
523 * provide a file name with a space in it.
525 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
526 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
527 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
528 trim_suffix_c(shell
, '\n');
529 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
533 advise("popen", "could not get file from shell");
539 /* Unknown command */
549 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
551 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
554 /* Note that we do not quote -- the argp from the user
555 * is assumed to be quoted as they desire. (We can't treat
556 * it as pure literal as that would prevent them using ~,
557 * wildcards, etc.) The buffer produced by this function
558 * should be given to popen_in_dir() or system_in_dir() so
559 * that the current working directory is set correctly.
561 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
562 /* NB that some snprintf() return -1 on overflow rather than the
563 * new C99 mandated 'number of chars that would have been written'
565 /* length checks here and inside the loop allow for the
566 * trailing "&&", trailcmd, '"' and NUL
568 int trailln
= strlen(trailcmd
) + 4;
569 if (ln
< 0 || ln
+ trailln
> bufsz
)
570 die("arguments too long");
574 while (*argp
&& *++argp
) {
576 /* +1 for leading space */
577 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
578 die("arguments too long");
580 memcpy(cp
, *argp
, ln
+1);
584 *cp
++ = '&'; *cp
++ = '&';
585 strcpy(cp
, trailcmd
);
593 * Build a command line that causes the user's shell to list the file name
594 * arguments. This handles and wildcard expansion, tilde expansion, etc.
597 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
599 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
600 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
604 /* Like system(), but run the command in directory dir.
605 * This assumes the program is single-threaded!
608 system_in_dir(const char *dir
, const char *cmd
)
613 /* ensure that $SHELL exists, as the cmd was written relying on
614 a non-blank $SHELL... */
615 setenv("SHELL","/bin/sh",0); /* don't overwrite */
617 if (getcwd(olddir
, sizeof(olddir
)) == 0)
618 adios("getcwd", "could not get working directory");
620 adios("chdir", "could not change working directory");
622 if (chdir(olddir
) != 0)
623 adios("chdir", "could not change working directory");
627 /* ditto for popen() */
629 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
634 /* ensure that $SHELL exists, as the cmd was written relying on
635 a non-blank $SHELL... */
636 setenv("SHELL","/bin/sh",0); /* don't overwrite */
638 if (getcwd(olddir
, sizeof(olddir
)) == 0)
639 adios("getcwd", "could not get working directory");
641 adios("chdir", "could not change working directory");
642 f
= popen(cmd
, type
);
643 if (chdir(olddir
) != 0)
644 adios("chdir", "could not change working directory");
653 static bool reedit
; /* have we been here before? */
654 static char *edsave
= NULL
; /* the editor we used previously */
658 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
659 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
661 int pid
, status
, vecp
;
662 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
663 char *cp
, *prog
, **vec
;
666 bool slinked
= false;
668 /* Was there a previous edit session? */
669 if (reedit
&& (*ed
|| edsave
)) {
670 if (!*ed
) { /* no explicit editor */
671 *ed
= edsave
; /* so use the previous one */
672 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
675 /* unless we've specified it via "editor-next" */
676 cp
= concat (cp
, "-next", NULL
);
677 if ((cp
= context_find (cp
)) != NULL
)
681 /* set initial editor */
683 *ed
= get_default_editor();
687 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
688 strncpy (altpath
, altmsg
, sizeof(altpath
));
690 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
692 strncpy (linkpath
, LINK
, sizeof(linkpath
));
694 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
697 (void) m_unlink (linkpath
);
698 if (link (altpath
, linkpath
) == NOTOK
) {
699 if (symlink (altpath
, linkpath
) < 0) {
700 adios (linkpath
, "symlink");
709 context_save (); /* save the context file */
712 switch (pid
= fork()) {
714 advise ("fork", "unable to");
720 if (chdir (cwd
) < 0) {
721 advise (cwd
, "chdir");
726 setenv("mhfolder", mp
->foldpath
, 1);
727 setenv("editalt", altpath
, 1);
730 vec
= argsplit(*ed
, &prog
, &vecp
);
734 vec
[vecp
++] = *arg
++;
739 fprintf (stderr
, "unable to exec ");
744 if ((status
= pidwait (pid
, NOTOK
))) {
745 if (((status
& 0xff00) != 0xff00)
746 && (!reedit
|| (status
& 0x00ff))) {
747 if (!use
&& (status
& 0xff00) &&
748 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
749 inform("problems with edit--draft left in %s", cp
);
751 inform("problems with edit--%s preserved", file
);
754 status
= -2; /* maybe "reedit ? -2 : -1"? */
763 ? lstat (linkpath
, &st
) != NOTOK
764 && S_ISREG(st
.st_mode
)
765 && copyf (linkpath
, altpath
) == NOTOK
766 : stat (linkpath
, &st
) != NOTOK
768 && (m_unlink (altpath
) == NOTOK
769 || link (linkpath
, altpath
) == NOTOK
)))
770 advise (linkpath
, "unable to update %s from", altmsg
);
773 /* normally, we remember which editor we used */
775 edsave
= getcpy (*ed
);
778 if (altmsg
&& atfile
)
779 (void) m_unlink (linkpath
);
786 copyf (char *ifile
, char *ofile
)
791 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
793 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
794 admonish (ofile
, "unable to open and truncate");
799 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
800 if (write (out
, buffer
, i
) != i
) {
801 advise (ofile
, "may have damaged");
817 sendfile (char **arg
, char *file
, int pushsw
)
821 char *cp
, *sp
, **vec
, *program
;
824 * If the sendproc is the nmh command `send', then we call
825 * those routines directly rather than exec'ing the command.
827 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
829 sendit (invo_name
= sp
, arg
, file
, pushsw
);
834 context_save (); /* save the context file */
840 adios("fork", "failed:");
843 vec
= argsplit(sendproc
, &program
, &vecp
);
845 vec
[vecp
++] = "-push";
848 vec
[vecp
++] = *arg
++;
852 execvp (program
, vec
);
853 fprintf (stderr
, "unable to exec ");
858 if (pidwait(child_id
, OK
) == 0)
866 * Translate MIME composition file (call buildmimeproc)
870 buildfile (char **argp
, char *file
)
877 /* allocate space for arguments */
883 args
= mh_xmalloc((i
+ 2) * sizeof(char *));
886 * For backward compatibility, we need to add -build
887 * if we are using mhn as buildmimeproc
890 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
891 args
[i
++] = "-build";
893 /* copy any other arguments */
894 while (argp
&& *argp
)
898 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
901 return i
? NOTOK
: OK
;
906 # define SASLminc(a) (a)
907 #else /* CYRUS_SASL */
908 # define SASLminc(a) 0
909 #endif /* CYRUS_SASL */
912 # define TLSminc(a) (a)
913 #else /* TLS_SUPPORT */
914 # define TLSminc(a) 0
915 #endif /* TLS_SUPPORT */
917 #define SEND_SWITCHES \
918 X("alias aliasfile", 0, ALIASW) \
919 X("debug", -5, DEBUGSW) \
920 X("filter filterfile", 0, FILTSW) \
921 X("nofilter", 0, NFILTSW) \
922 X("format", 0, FRMTSW) \
923 X("noformat", 0, NFRMTSW) \
924 X("forward", 0, FORWSW) \
925 X("noforward", 0, NFORWSW) \
926 X("mime", 0, MIMESW) \
927 X("nomime", 0, NMIMESW) \
928 X("msgid", 0, MSGDSW) \
929 X("nomsgid", 0, NMSGDSW) \
930 X("push", 0, SPSHSW) \
931 X("nopush", 0, NSPSHSW) \
932 X("split seconds", 0, SPLITSW) \
933 X("unique", -6, UNIQSW) \
934 X("nounique", -8, NUNIQSW) \
935 X("verbose", 0, VERBSW) \
936 X("noverbose", 0, NVERBSW) \
937 X("watch", 0, WATCSW) \
938 X("nowatch", 0, NWATCSW) \
939 X("width columns", 0, WIDTHSW) \
940 X("version", 0, SVERSIONSW) \
941 X("help", 0, SHELPSW) \
942 X("dashstuffing", -12, BITSTUFFSW) \
943 X("nodashstuffing", -14, NBITSTUFFSW) \
944 X("client host", -6, CLIESW) \
945 X("server host", 6, SERVSW) \
946 X("snoop", -5, SNOOPSW) \
947 X("draftfolder +folder", 0, SDRFSW) \
948 X("draftmessage msg", 0, SDRMSW) \
949 X("nodraftfolder", 0, SNDRFSW) \
950 X("sasl", SASLminc(4), SASLSW) \
951 X("nosasl", SASLminc(6), NOSASLSW) \
952 X("saslmech", SASLminc(5), SASLMECHSW) \
953 X("authservice", SASLminc(0), AUTHSERVICESW) \
954 X("user username", SASLminc(4), USERSW) \
955 X("port server-port-name/number", 4, PORTSW) \
956 X("tls", TLSminc(-3), TLSSW) \
957 X("initialtls", TLSminc(-10), INITTLSSW) \
958 X("notls", TLSminc(-5), NTLSSW) \
959 X("certverify", TLSminc(-10), CERTVERSW) \
960 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
961 X("sendmail program", 0, MTSSM) \
962 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
963 X("messageid localname|random", 2, MESSAGEIDSW) \
965 #define X(sw, minchars, id) id,
966 DEFINE_SWITCH_ENUM(SEND
);
969 #define X(sw, minchars, id) { sw, minchars, id },
970 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
974 extern int debugsw
; /* from sendsbr.c */
982 extern char *altmsg
; /* .. */
983 extern char *annotext
;
984 extern char *distfile
;
988 sendit (char *sp
, char **arg
, char *file
, int pushed
)
991 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
992 char **arguments
, *savearg
[MAXARGS
], **vec
;
993 const char *user
= NULL
, *saslmech
= NULL
;
994 char *auth_svc
= NULL
;
998 * Make sure these are defined. In particular, we need
999 * savearg[1] to be NULL, in case "arg" is NULL below. It
1000 * doesn't matter what is the value of savearg[0], but we
1001 * set it to NULL, to help catch "off-by-one" errors.
1007 * Temporarily copy arg to savearg, since the brkstring() call in
1008 * getarguments() will wipe it out before it is merged in.
1009 * Also, we skip the first element of savearg, since getarguments()
1010 * skips it. Then we count the number of arguments
1011 * copied. The value of "n" will be one greater than
1012 * this in order to simulate the standard argc/argv.
1017 copyip (arg
, savearg
+1, MAXARGS
-1);
1024 * Merge any arguments from command line (now in savearg)
1025 * and arguments from profile.
1027 arguments
= getarguments (sp
, n
, savearg
, 1);
1040 * Get our initial arguments for postproc now
1043 vec
= argsplit(postproc
, &program
, &vecp
);
1045 vec
[vecp
++] = "-library";
1046 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
1048 if ((cp
= context_find ("fileproc"))) {
1049 vec
[vecp
++] = "-fileproc";
1053 if ((cp
= context_find ("mhlproc"))) {
1054 vec
[vecp
++] = "-mhlproc";
1058 if ((cp
= context_find ("credentials"))) {
1059 /* post doesn't read context so need to pass credentials. */
1060 vec
[vecp
++] = "-credentials";
1064 while ((cp
= *argp
++)) {
1066 switch (smatch (++cp
, sendswitches
)) {
1068 ambigsw (cp
, sendswitches
);
1071 inform("-%s unknown\n", cp
);
1075 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1076 print_help (buf
, sendswitches
, 1);
1079 print_version (invo_name
);
1090 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1091 inform("missing argument to %s", argp
[-2]);
1147 #ifdef OAUTH_SUPPORT
1148 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
1149 die("missing argument to %s", argp
[-2]);
1152 NMH_UNUSED (auth_svc
);
1153 die("not built with OAuth support");
1171 if (!(cp
= *argp
++) || *cp
== '-') {
1172 inform("missing argument to %s", argp
[-2]);
1181 if (!(cp
= *argp
++) || *cp
== '-') {
1182 inform("missing argument to %s", argp
[-2]);
1190 inform("usage: %s [switches]", sp
);
1194 /* allow Aliasfile: profile entry */
1195 if ((cp
= context_find ("Aliasfile"))) {
1198 dp
= mh_xstrdup(cp
);
1199 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1200 vec
[vecp
++] = "-alias";
1205 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1206 if ((cp
= context_find ("signature")) && *cp
)
1207 setenv("SIGNATURE", cp
, 1);
1209 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1211 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1213 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1214 inplace
= atoi (cp
);
1216 if ((cp
= getenv ("mhdist"))
1220 vec
[vecp
++] = "-dist";
1221 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1222 die("unable to create temporary file");
1224 distfile
= mh_xstrdup(cp
);
1225 (void) m_unlink(distfile
);
1226 if (link (altmsg
, distfile
) == NOTOK
)
1227 adios (distfile
, "unable to link %s to", altmsg
);
1232 #ifdef OAUTH_SUPPORT
1233 if (auth_svc
== NULL
) {
1234 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
1235 die("must specify -authservice with -saslmech xoauth2");
1239 die("must specify -user with -saslmech xoauth2");
1243 NMH_UNUSED(saslmech
);
1244 #endif /* OAUTH_SUPPORT */
1246 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1251 if ((pushsw
= pushed
))
1256 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, auth_svc
) == OK
)
1265 whomfile (char **arg
, char *file
)
1269 char **vec
, *program
;
1271 context_save (); /* save the context file */
1274 switch (pid
= fork()) {
1276 advise ("fork", "unable to");
1280 vec
= argsplit(whomproc
, &program
, &vecp
);
1283 vec
[vecp
++] = *arg
++;
1287 execvp (program
, vec
);
1288 fprintf (stderr
, "unable to exec ");
1290 _exit(1); /* NOTREACHED */
1293 return pidwait(pid
, NOTOK
) & 0377 ? 1 : 0;
1299 * Remove the draft file
1303 removefile (char *drft
)
1305 if (m_unlink (drft
) == NOTOK
)
1306 adios (drft
, "unable to unlink");
1313 * Return 1 if we already have a MIME-Version header, 0 otherwise.
1317 checkmimeheader (char *drft
)
1320 m_getfld_state_t gstate
;
1321 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1323 bool retval
= false;
1325 if ((f
= fopen(drft
, "r")) == NULL
) {
1326 admonish(drft
, "unable to read draft");
1330 gstate
= m_getfld_state_init(f
);
1332 int bufsz
= sizeof(buf
);
1333 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1336 if (strcasecmp(name
, VRSN_FIELD
) == 0) {
1337 inform("Cannot use attach commands with already-"
1338 "formatted MIME message \"%s\"", drft
);
1349 m_getfld_state_destroy(&gstate
);