]>
diplodocus.org Git - nmh/blob - uip/whatnowsbr.c
c4549f1825a7a24bb4085e49d5a82c6be94db407
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/m_draft.h"
42 #include "sbr/m_backup.h"
43 #include "sbr/geteditor.h"
44 #include "sbr/context_save.h"
45 #include "sbr/context_find.h"
46 #include "sbr/brkstring.h"
47 #include "sbr/ambigsw.h"
50 #include "sbr/print_version.h"
51 #include "sbr/print_help.h"
52 #include "sbr/arglist.h"
53 #include "sbr/error.h"
61 #include "sbr/m_maildir.h"
62 #include "sbr/m_mktemp.h"
63 #include "sbr/mime_type.h"
65 #define WHATNOW_SWITCHES \
66 X("draftfolder +folder", 0, DFOLDSW) \
67 X("draftmessage msg", 0, DMSGSW) \
68 X("nodraftfolder", 0, NDFLDSW) \
69 X("editor editor", 0, EDITRSW) \
70 X("noedit", 0, NEDITSW) \
71 X("prompt string", 4, PRMPTSW) \
72 X("version", 0, VERSIONSW) \
73 X("help", 0, HELPSW) \
76 #define X(sw, minchars, id) id,
77 DEFINE_SWITCH_ENUM(WHATNOW
);
80 #define X(sw, minchars, id) { sw, minchars, id },
81 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
85 * Options at the "whatnow" prompt
87 #define PROMPT_SWITCHES \
88 X("edit [<editor> <switches>]", 0, EDITSW) \
89 X("refile [<switches>] +folder", 0, REFILEOPT) \
90 X("mime [<switches>]", 0, BUILDMIMESW) \
91 X("display [<switches>]", 0, DISPSW) \
92 X("list [<switches>]", 0, LISTSW) \
93 X("send [<switches>]", 0, SENDSW) \
94 X("push [<switches>]", 0, PUSHSW) \
95 X("whom [<switches>]", 0, WHOMSW) \
96 X("quit [-delete]", 0, QUITSW) \
97 X("delete", 0, DELETESW) \
98 X("cd [directory]", 0, CDCMDSW) \
99 X("pwd", 0, PWDCMDSW) \
100 X("ls", 2, LSCMDSW) \
101 X("attach [-v]", 0, ATTACHCMDSW) \
102 X("detach [-n]", 0, DETACHCMDSW) \
103 X("alist [-ln] ", 2, ALISTCMDSW) \
105 #define X(sw, minchars, id) id,
106 DEFINE_SWITCH_ENUM(PROMPT
);
109 #define X(sw, minchars, id) { sw, minchars, id },
110 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
113 static char *myprompt
= "\nWhat now? ";
118 static int editfile (char **, char **, char *, int, struct msgs
*,
119 char *, char *, int, int);
120 static int sendfile (char **, char *, int);
121 static void sendit (char *, char **, char *, int);
122 static int buildfile (char **, char *);
123 static int whomfile (char **, char *);
124 static int removefile (char *);
125 static int checkmimeheader (char *);
126 static void writelscmd(char *, int, char *, char **);
127 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
128 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
129 static int system_in_dir(const char *dir
, const char *cmd
);
130 static int copyf (char *, char *);
134 WhatNow (int argc
, char **argv
)
138 int use
= 0, atfile
= 1;
139 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
140 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
142 char **argp
, **arguments
;
144 char cwd
[PATH_MAX
+ 1]; /* current working directory */
145 char file
[PATH_MAX
+ 1]; /* file name buffer */
146 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
147 FILE *f
; /* read pointer for bgnd proc */
148 char *l
; /* set on -l to alist command */
149 int n
; /* set on -n to alist command */
151 /* Need this if called from what_now(). */
152 invo_name
= r1bindex (argv
[0], '/');
154 arguments
= getarguments (invo_name
, argc
, argv
, 1);
158 * Get the initial current working directory.
161 if (getcwd(cwd
, sizeof (cwd
)) == NULL
) {
162 adios("getcwd", "could not get working directory");
165 while ((cp
= *argp
++)) {
167 switch (smatch (++cp
, whatnowswitches
)) {
169 ambigsw (cp
, whatnowswitches
);
172 die("-%s unknown", cp
);
175 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
176 print_help (buf
, whatnowswitches
, 1);
179 print_version(invo_name
);
184 die("only one draft folder at a time!");
185 if (!(cp
= *argp
++) || *cp
== '-')
186 die("missing argument to %s", argp
[-2]);
187 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
188 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
192 die("only one draft message at a time!");
193 if (!(dmsg
= *argp
++) || *dmsg
== '-')
194 die("missing argument to %s", argp
[-2]);
202 if (!(ed
= *argp
++) || *ed
== '-')
203 die("missing argument to %s", argp
[-2]);
211 if (!(myprompt
= *argp
++) || *myprompt
== '-')
212 die("missing argument to %s", argp
[-2]);
217 die("only one draft at a time!");
221 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
222 drft
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
224 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? mh_xstrdup(cp
) : NULL
;
226 if ((cp
= getenv ("mhatfile")) && *cp
)
229 if ((cp
= getenv ("mhuse")) && *cp
)
232 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
237 /* start editing the draft, unless -noedit was given */
238 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
239 NULL
, 1, atfile
) < 0)
243 #ifdef READLINE_SUPPORT
244 if (!(argp
= read_switch_multiword_via_readline(myprompt
, aleqs
))) {
245 #else /* ! READLINE_SUPPORT */
246 if (!(argp
= read_switch_multiword(myprompt
, aleqs
))) {
247 #endif /* READLINE_SUPPORT */
248 (void) m_unlink (LINK
);
251 switch (smatch (*argp
, aleqs
)) {
253 /* display the message being replied to, or distributed */
255 showfile (++argp
, msgnam
);
257 inform("no alternate message to display");
261 /* Translate MIME composition file */
262 buildfile (++argp
, drft
);
266 /* Call an editor on the draft file */
269 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
270 NULL
, 1, atfile
) == NOTOK
)
275 /* display the draft file */
276 showfile (++argp
, drft
);
280 /* Check to whom the draft would be sent */
281 whomfile (++argp
, drft
);
285 /* Quit, and possibly delete the draft */
286 if (*++argp
&& (*argp
[0] == 'd' ||
287 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
290 if (stat (drft
, &st
) != NOTOK
)
291 inform("draft left on %s", drft
);
296 /* Delete draft and exit */
301 /* Send draft in background */
302 if (sendfile (++argp
, drft
, 1))
308 sendfile (++argp
, drft
, 0);
312 /* Refile the draft */
313 if (refile (++argp
, drft
) == 0)
318 /* Change the working directory for attachments
320 * Run the directory through the user's shell so that
321 * we can take advantage of any syntax that the user
322 * is accustomed to. Read back the absolute path.
325 if (*(argp
+1) == NULL
) {
326 strcpy(buf
, "$SHELL -c \"cd&&pwd\"");
329 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
331 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
332 if (fgets(cwd
, sizeof (cwd
), f
) == NULL
) {
333 advise (buf
, "fgets");
335 trim_suffix_c(cwd
, '\n');
339 advise("popen", "could not get directory");
345 /* Print the working directory for attachments */
350 /* List files in the current attachment working directory
352 * Use the user's shell so that we can take advantage of any
353 * syntax that the user is accustomed to.
355 writelscmd(buf
, sizeof(buf
), "", argp
);
356 (void)system_in_dir(cwd
, buf
);
361 * List attachments on current draft. Options are:
363 * -l long listing (full path names)
367 if (checkmimeheader(drft
))
373 while (*++argp
!= NULL
) {
374 if (strcmp(*argp
, "-l") == 0)
377 else if (strcmp(*argp
, "-n") == 0)
380 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
392 inform("usage is alist [-ln].");
395 annolist(drft
, ATTACH_FIELD
, l
, n
);
401 * Attach files to current draft.
404 bool verbose
= false;
407 if (checkmimeheader(drft
))
410 for (ap
= argp
+1; *ap
; ++ap
) {
411 if (strcmp(*ap
, "-v") == 0) {
414 } else if (*ap
[0] != '-') {
419 if (*(argp
+1) == NULL
) {
420 inform("attach command requires file argument(s).");
425 * Build a command line that causes the user's shell to list the file name
426 * arguments. This handles and wildcard expansion, tilde expansion, etc.
428 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
431 * Read back the response from the shell, which contains a number of lines
432 * with one file name per line. Remove off the newline. Determine whether
433 * we have an absolute or relative path name. Prepend the current working
434 * directory to relative path names. Add the attachment annotation to the
438 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
439 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
442 trim_suffix_c(shell
, '\n');
445 strncpy(file
, shell
, sizeof(file
));
446 file
[sizeof(file
) - 1] = '\0';
448 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
451 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
453 ctype
= mime_type(file
);
454 printf ("Attaching %s as a %s\n", file
, ctype
);
462 advise("popen", "could not get file from shell");
469 * Detach files from current draft.
473 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
474 * so this catches a -n anywhere in the argument list.
477 if (checkmimeheader(drft
))
480 for (n
= 0, arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
481 if (strcmp(*arguments
, "-n") == 0) {
488 * A -n was found so interpret the arguments as attachment numbers.
489 * Decrement any remaining argument number that is greater than the one
490 * just processed after processing each one so that the numbering stays
495 for (arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
496 if (strcmp(*arguments
, "-n") == 0)
499 if (**arguments
!= '\0') {
500 n
= atoi(*arguments
);
501 annotate(drft
, ATTACH_FIELD
, NULL
, 1, 0, n
, 1);
503 for (argp
= arguments
+ 1; *argp
!= NULL
; argp
++) {
504 if (atoi(*argp
) > n
) {
505 if (atoi(*argp
) == 1)
508 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
516 * The arguments are interpreted as file names. Run them through the
517 * user's shell for wildcard expansion and other goodies. Do this from
518 * the current working directory if the argument is not an absolute path
519 * name (does not begin with a /).
521 * We feed all the file names to the shell at once, otherwise you can't
522 * provide a file name with a space in it.
524 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
525 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
526 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
527 trim_suffix_c(shell
, '\n');
528 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
532 advise("popen", "could not get file from shell");
538 /* Unknown command */
548 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
550 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
553 /* Note that we do not quote -- the argp from the user
554 * is assumed to be quoted as they desire. (We can't treat
555 * it as pure literal as that would prevent them using ~,
556 * wildcards, etc.) The buffer produced by this function
557 * should be given to popen_in_dir() or system_in_dir() so
558 * that the current working directory is set correctly.
560 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
561 /* NB that some snprintf() return -1 on overflow rather than the
562 * new C99 mandated 'number of chars that would have been written'
564 /* length checks here and inside the loop allow for the
565 * trailing "&&", trailcmd, '"' and NUL
567 int trailln
= strlen(trailcmd
) + 4;
568 if (ln
< 0 || ln
+ trailln
> bufsz
)
569 die("arguments too long");
573 while (*argp
&& *++argp
) {
575 /* +1 for leading space */
576 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
577 die("arguments too long");
579 memcpy(cp
, *argp
, ln
+1);
583 *cp
++ = '&'; *cp
++ = '&';
584 strcpy(cp
, trailcmd
);
592 * Build a command line that causes the user's shell to list the file name
593 * arguments. This handles and wildcard expansion, tilde expansion, etc.
596 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
598 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
599 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
603 /* Like system(), but run the command in directory dir.
604 * This assumes the program is single-threaded!
607 system_in_dir(const char *dir
, const char *cmd
)
612 /* ensure that $SHELL exists, as the cmd was written relying on
613 a non-blank $SHELL... */
614 setenv("SHELL","/bin/sh",0); /* don't overwrite */
616 if (getcwd(olddir
, sizeof(olddir
)) == 0)
617 adios("getcwd", "could not get working directory");
619 adios("chdir", "could not change working directory");
621 if (chdir(olddir
) != 0)
622 adios("chdir", "could not change working directory");
626 /* ditto for popen() */
628 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
633 /* ensure that $SHELL exists, as the cmd was written relying on
634 a non-blank $SHELL... */
635 setenv("SHELL","/bin/sh",0); /* don't overwrite */
637 if (getcwd(olddir
, sizeof(olddir
)) == 0)
638 adios("getcwd", "could not get working directory");
640 adios("chdir", "could not change working directory");
641 f
= popen(cmd
, type
);
642 if (chdir(olddir
) != 0)
643 adios("chdir", "could not change working directory");
652 static bool reedit
; /* have we been here before? */
653 static char *edsave
= NULL
; /* the editor we used previously */
657 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
658 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
660 int pid
, status
, vecp
;
661 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
662 char *cp
, *prog
, **vec
;
665 bool slinked
= false;
667 /* Was there a previous edit session? */
668 if (reedit
&& (*ed
|| edsave
)) {
669 if (!*ed
) { /* no explicit editor */
670 *ed
= edsave
; /* so use the previous one */
671 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
674 /* unless we've specified it via "editor-next" */
675 cp
= concat (cp
, "-next", NULL
);
676 if ((cp
= context_find (cp
)) != NULL
)
680 /* set initial editor */
682 *ed
= get_default_editor();
686 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
687 strncpy (altpath
, altmsg
, sizeof(altpath
));
689 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
691 strncpy (linkpath
, LINK
, sizeof(linkpath
));
693 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
696 (void) m_unlink (linkpath
);
697 if (link (altpath
, linkpath
) == NOTOK
) {
698 if (symlink (altpath
, linkpath
) < 0) {
699 adios (linkpath
, "symlink");
708 context_save (); /* save the context file */
711 switch (pid
= fork()) {
713 advise ("fork", "unable to");
719 if (chdir (cwd
) < 0) {
720 advise (cwd
, "chdir");
725 setenv("mhfolder", mp
->foldpath
, 1);
726 setenv("editalt", altpath
, 1);
729 vec
= argsplit(*ed
, &prog
, &vecp
);
733 vec
[vecp
++] = *arg
++;
738 fprintf (stderr
, "unable to exec ");
743 if ((status
= pidwait (pid
, NOTOK
))) {
744 if (((status
& 0xff00) != 0xff00)
745 && (!reedit
|| (status
& 0x00ff))) {
746 if (!use
&& (status
& 0xff00) &&
747 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
748 inform("problems with edit--draft left in %s", cp
);
750 inform("problems with edit--%s preserved", file
);
753 status
= -2; /* maybe "reedit ? -2 : -1"? */
762 ? lstat (linkpath
, &st
) != NOTOK
763 && S_ISREG(st
.st_mode
)
764 && copyf (linkpath
, altpath
) == NOTOK
765 : stat (linkpath
, &st
) != NOTOK
767 && (m_unlink (altpath
) == NOTOK
768 || link (linkpath
, altpath
) == NOTOK
)))
769 advise (linkpath
, "unable to update %s from", altmsg
);
772 /* normally, we remember which editor we used */
774 edsave
= getcpy (*ed
);
777 if (altmsg
&& atfile
)
778 (void) m_unlink (linkpath
);
785 copyf (char *ifile
, char *ofile
)
790 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
792 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
793 admonish (ofile
, "unable to open and truncate");
798 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
799 if (write (out
, buffer
, i
) != i
) {
800 advise (ofile
, "may have damaged");
816 sendfile (char **arg
, char *file
, int pushsw
)
820 char *cp
, *sp
, **vec
, *program
;
823 * If the sendproc is the nmh command `send', then we call
824 * those routines directly rather than exec'ing the command.
826 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
828 sendit (invo_name
= sp
, arg
, file
, pushsw
);
833 context_save (); /* save the context file */
839 adios("fork", "failed:");
842 vec
= argsplit(sendproc
, &program
, &vecp
);
844 vec
[vecp
++] = "-push";
847 vec
[vecp
++] = *arg
++;
851 execvp (program
, vec
);
852 fprintf (stderr
, "unable to exec ");
857 if (pidwait(child_id
, OK
) == 0)
865 * Translate MIME composition file (call buildmimeproc)
869 buildfile (char **argp
, char *file
)
876 /* allocate space for arguments */
882 args
= mh_xmalloc((i
+ 2) * sizeof(char *));
885 * For backward compatibility, we need to add -build
886 * if we are using mhn as buildmimeproc
889 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
890 args
[i
++] = "-build";
892 /* copy any other arguments */
893 while (argp
&& *argp
)
897 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
900 return i
? NOTOK
: OK
;
905 # define SASLminc(a) (a)
906 #else /* CYRUS_SASL */
907 # define SASLminc(a) 0
908 #endif /* CYRUS_SASL */
911 # define TLSminc(a) (a)
912 #else /* TLS_SUPPORT */
913 # define TLSminc(a) 0
914 #endif /* TLS_SUPPORT */
916 #define SEND_SWITCHES \
917 X("alias aliasfile", 0, ALIASW) \
918 X("debug", -5, DEBUGSW) \
919 X("filter filterfile", 0, FILTSW) \
920 X("nofilter", 0, NFILTSW) \
921 X("format", 0, FRMTSW) \
922 X("noformat", 0, NFRMTSW) \
923 X("forward", 0, FORWSW) \
924 X("noforward", 0, NFORWSW) \
925 X("mime", 0, MIMESW) \
926 X("nomime", 0, NMIMESW) \
927 X("msgid", 0, MSGDSW) \
928 X("nomsgid", 0, NMSGDSW) \
929 X("push", 0, SPSHSW) \
930 X("nopush", 0, NSPSHSW) \
931 X("split seconds", 0, SPLITSW) \
932 X("unique", -6, UNIQSW) \
933 X("nounique", -8, NUNIQSW) \
934 X("verbose", 0, VERBSW) \
935 X("noverbose", 0, NVERBSW) \
936 X("watch", 0, WATCSW) \
937 X("nowatch", 0, NWATCSW) \
938 X("width columns", 0, WIDTHSW) \
939 X("version", 0, SVERSIONSW) \
940 X("help", 0, SHELPSW) \
941 X("dashstuffing", -12, BITSTUFFSW) \
942 X("nodashstuffing", -14, NBITSTUFFSW) \
943 X("client host", -6, CLIESW) \
944 X("server host", 6, SERVSW) \
945 X("snoop", -5, SNOOPSW) \
946 X("draftfolder +folder", 0, SDRFSW) \
947 X("draftmessage msg", 0, SDRMSW) \
948 X("nodraftfolder", 0, SNDRFSW) \
949 X("sasl", SASLminc(4), SASLSW) \
950 X("nosasl", SASLminc(6), NOSASLSW) \
951 X("saslmech", SASLminc(5), SASLMECHSW) \
952 X("authservice", SASLminc(0), AUTHSERVICESW) \
953 X("user username", SASLminc(4), USERSW) \
954 X("port server-port-name/number", 4, PORTSW) \
955 X("tls", TLSminc(-3), TLSSW) \
956 X("initialtls", TLSminc(-10), INITTLSSW) \
957 X("notls", TLSminc(-5), NTLSSW) \
958 X("certverify", TLSminc(-10), CERTVERSW) \
959 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
960 X("sendmail program", 0, MTSSM) \
961 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
962 X("messageid localname|random", 2, MESSAGEIDSW) \
964 #define X(sw, minchars, id) id,
965 DEFINE_SWITCH_ENUM(SEND
);
968 #define X(sw, minchars, id) { sw, minchars, id },
969 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
973 extern int debugsw
; /* from sendsbr.c */
981 extern char *altmsg
; /* .. */
982 extern char *annotext
;
983 extern char *distfile
;
987 sendit (char *sp
, char **arg
, char *file
, int pushed
)
990 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
991 char **arguments
, *savearg
[MAXARGS
], **vec
;
992 const char *user
= NULL
, *saslmech
= NULL
;
993 char *auth_svc
= NULL
;
997 * Make sure these are defined. In particular, we need
998 * savearg[1] to be NULL, in case "arg" is NULL below. It
999 * doesn't matter what is the value of savearg[0], but we
1000 * set it to NULL, to help catch "off-by-one" errors.
1006 * Temporarily copy arg to savearg, since the brkstring() call in
1007 * getarguments() will wipe it out before it is merged in.
1008 * Also, we skip the first element of savearg, since getarguments()
1009 * skips it. Then we count the number of arguments
1010 * copied. The value of "n" will be one greater than
1011 * this in order to simulate the standard argc/argv.
1016 copyip (arg
, savearg
+1, MAXARGS
-1);
1023 * Merge any arguments from command line (now in savearg)
1024 * and arguments from profile.
1026 arguments
= getarguments (sp
, n
, savearg
, 1);
1039 * Get our initial arguments for postproc now
1042 vec
= argsplit(postproc
, &program
, &vecp
);
1044 vec
[vecp
++] = "-library";
1045 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
1047 if ((cp
= context_find ("fileproc"))) {
1048 vec
[vecp
++] = "-fileproc";
1052 if ((cp
= context_find ("mhlproc"))) {
1053 vec
[vecp
++] = "-mhlproc";
1057 if ((cp
= context_find ("credentials"))) {
1058 /* post doesn't read context so need to pass credentials. */
1059 vec
[vecp
++] = "-credentials";
1063 while ((cp
= *argp
++)) {
1065 switch (smatch (++cp
, sendswitches
)) {
1067 ambigsw (cp
, sendswitches
);
1070 inform("-%s unknown\n", cp
);
1074 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1075 print_help (buf
, sendswitches
, 1);
1078 print_version (invo_name
);
1089 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1090 inform("missing argument to %s", argp
[-2]);
1146 #ifdef OAUTH_SUPPORT
1147 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
1148 die("missing argument to %s", argp
[-2]);
1151 NMH_UNUSED (auth_svc
);
1152 die("not built with OAuth support");
1170 if (!(cp
= *argp
++) || *cp
== '-') {
1171 inform("missing argument to %s", argp
[-2]);
1180 if (!(cp
= *argp
++) || *cp
== '-') {
1181 inform("missing argument to %s", argp
[-2]);
1189 inform("usage: %s [switches]", sp
);
1193 /* allow Aliasfile: profile entry */
1194 if ((cp
= context_find ("Aliasfile"))) {
1197 dp
= mh_xstrdup(cp
);
1198 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1199 vec
[vecp
++] = "-alias";
1204 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1205 if ((cp
= context_find ("signature")) && *cp
)
1206 setenv("SIGNATURE", cp
, 1);
1208 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1210 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1212 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1213 inplace
= atoi (cp
);
1215 if ((cp
= getenv ("mhdist"))
1219 vec
[vecp
++] = "-dist";
1220 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1221 die("unable to create temporary file");
1223 distfile
= mh_xstrdup(cp
);
1224 (void) m_unlink(distfile
);
1225 if (link (altmsg
, distfile
) == NOTOK
)
1226 adios (distfile
, "unable to link %s to", altmsg
);
1231 #ifdef OAUTH_SUPPORT
1232 if (auth_svc
== NULL
) {
1233 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
1234 die("must specify -authservice with -saslmech xoauth2");
1238 die("must specify -user with -saslmech xoauth2");
1242 NMH_UNUSED(saslmech
);
1243 #endif /* OAUTH_SUPPORT */
1245 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1250 if ((pushsw
= pushed
))
1255 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, auth_svc
) == OK
)
1264 whomfile (char **arg
, char *file
)
1268 char **vec
, *program
;
1270 context_save (); /* save the context file */
1273 switch (pid
= fork()) {
1275 advise ("fork", "unable to");
1279 vec
= argsplit(whomproc
, &program
, &vecp
);
1282 vec
[vecp
++] = *arg
++;
1286 execvp (program
, vec
);
1287 fprintf (stderr
, "unable to exec ");
1289 _exit(1); /* NOTREACHED */
1292 return pidwait(pid
, NOTOK
) & 0377 ? 1 : 0;
1298 * Remove the draft file
1302 removefile (char *drft
)
1304 if (m_unlink (drft
) == NOTOK
)
1305 adios (drft
, "unable to unlink");
1312 * Return 1 if we already have a MIME-Version header, 0 otherwise.
1316 checkmimeheader (char *drft
)
1319 m_getfld_state_t gstate
;
1320 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1322 bool retval
= false;
1324 if ((f
= fopen(drft
, "r")) == NULL
) {
1325 admonish(drft
, "unable to read draft");
1329 gstate
= m_getfld_state_init(f
);
1331 int bufsz
= sizeof(buf
);
1332 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1335 if (strcasecmp(name
, VRSN_FIELD
) == 0) {
1336 inform("Cannot use attach commands with already-"
1337 "formatted MIME message \"%s\"", drft
);
1348 m_getfld_state_destroy(&gstate
);