]>
diplodocus.org Git - nmh/blob - uip/whatnowsbr.c
857d179282a9e0c26dcaf42c181efecd94cdf622
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.
42 #include "sbr/print_version.h"
43 #include "sbr/print_help.h"
44 #include "sbr/arglist.h"
45 #include "sbr/error.h"
53 #include "sbr/m_maildir.h"
54 #include "sbr/m_mktemp.h"
55 #include "sbr/mime_type.h"
57 #define WHATNOW_SWITCHES \
58 X("draftfolder +folder", 0, DFOLDSW) \
59 X("draftmessage msg", 0, DMSGSW) \
60 X("nodraftfolder", 0, NDFLDSW) \
61 X("editor editor", 0, EDITRSW) \
62 X("noedit", 0, NEDITSW) \
63 X("prompt string", 4, PRMPTSW) \
64 X("version", 0, VERSIONSW) \
65 X("help", 0, HELPSW) \
68 #define X(sw, minchars, id) id,
69 DEFINE_SWITCH_ENUM(WHATNOW
);
72 #define X(sw, minchars, id) { sw, minchars, id },
73 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
77 * Options at the "whatnow" prompt
79 #define PROMPT_SWITCHES \
80 X("edit [<editor> <switches>]", 0, EDITSW) \
81 X("refile [<switches>] +folder", 0, REFILEOPT) \
82 X("mime [<switches>]", 0, BUILDMIMESW) \
83 X("display [<switches>]", 0, DISPSW) \
84 X("list [<switches>]", 0, LISTSW) \
85 X("send [<switches>]", 0, SENDSW) \
86 X("push [<switches>]", 0, PUSHSW) \
87 X("whom [<switches>]", 0, WHOMSW) \
88 X("quit [-delete]", 0, QUITSW) \
89 X("delete", 0, DELETESW) \
90 X("cd [directory]", 0, CDCMDSW) \
91 X("pwd", 0, PWDCMDSW) \
93 X("attach [-v]", 0, ATTACHCMDSW) \
94 X("detach [-n]", 0, DETACHCMDSW) \
95 X("alist [-ln] ", 2, ALISTCMDSW) \
97 #define X(sw, minchars, id) id,
98 DEFINE_SWITCH_ENUM(PROMPT
);
101 #define X(sw, minchars, id) { sw, minchars, id },
102 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
105 static char *myprompt
= "\nWhat now? ";
110 static int editfile (char **, char **, char *, int, struct msgs
*,
111 char *, char *, int, int);
112 static int sendfile (char **, char *, int);
113 static void sendit (char *, char **, char *, int);
114 static int buildfile (char **, char *);
115 static int whomfile (char **, char *);
116 static int removefile (char *);
117 static int checkmimeheader (char *);
118 static void writelscmd(char *, int, char *, char **);
119 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
120 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
121 static int system_in_dir(const char *dir
, const char *cmd
);
122 static int copyf (char *, char *);
126 WhatNow (int argc
, char **argv
)
130 int use
= 0, atfile
= 1;
131 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
132 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
134 char **argp
, **arguments
;
136 char cwd
[PATH_MAX
+ 1]; /* current working directory */
137 char file
[PATH_MAX
+ 1]; /* file name buffer */
138 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
139 FILE *f
; /* read pointer for bgnd proc */
140 char *l
; /* set on -l to alist command */
141 int n
; /* set on -n to alist command */
143 /* Need this if called from what_now(). */
144 invo_name
= r1bindex (argv
[0], '/');
146 arguments
= getarguments (invo_name
, argc
, argv
, 1);
150 * Get the initial current working directory.
153 if (getcwd(cwd
, sizeof (cwd
)) == NULL
) {
154 adios("getcwd", "could not get working directory");
157 while ((cp
= *argp
++)) {
159 switch (smatch (++cp
, whatnowswitches
)) {
161 ambigsw (cp
, whatnowswitches
);
164 die("-%s unknown", cp
);
167 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
168 print_help (buf
, whatnowswitches
, 1);
171 print_version(invo_name
);
176 die("only one draft folder at a time!");
177 if (!(cp
= *argp
++) || *cp
== '-')
178 die("missing argument to %s", argp
[-2]);
179 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
180 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
184 die("only one draft message at a time!");
185 if (!(dmsg
= *argp
++) || *dmsg
== '-')
186 die("missing argument to %s", argp
[-2]);
194 if (!(ed
= *argp
++) || *ed
== '-')
195 die("missing argument to %s", argp
[-2]);
203 if (!(myprompt
= *argp
++) || *myprompt
== '-')
204 die("missing argument to %s", argp
[-2]);
209 die("only one draft at a time!");
213 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
214 drft
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
216 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? mh_xstrdup(cp
) : NULL
;
218 if ((cp
= getenv ("mhatfile")) && *cp
)
221 if ((cp
= getenv ("mhuse")) && *cp
)
224 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
229 /* start editing the draft, unless -noedit was given */
230 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
231 NULL
, 1, atfile
) < 0)
235 #ifdef READLINE_SUPPORT
236 if (!(argp
= read_switch_multiword_via_readline(myprompt
, aleqs
))) {
237 #else /* ! READLINE_SUPPORT */
238 if (!(argp
= read_switch_multiword(myprompt
, aleqs
))) {
239 #endif /* READLINE_SUPPORT */
240 (void) m_unlink (LINK
);
243 switch (smatch (*argp
, aleqs
)) {
245 /* display the message being replied to, or distributed */
247 showfile (++argp
, msgnam
);
249 inform("no alternate message to display");
253 /* Translate MIME composition file */
254 buildfile (++argp
, drft
);
258 /* Call an editor on the draft file */
261 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
262 NULL
, 1, atfile
) == NOTOK
)
267 /* display the draft file */
268 showfile (++argp
, drft
);
272 /* Check to whom the draft would be sent */
273 whomfile (++argp
, drft
);
277 /* Quit, and possibly delete the draft */
278 if (*++argp
&& (*argp
[0] == 'd' ||
279 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
282 if (stat (drft
, &st
) != NOTOK
)
283 inform("draft left on %s", drft
);
288 /* Delete draft and exit */
293 /* Send draft in background */
294 if (sendfile (++argp
, drft
, 1))
300 sendfile (++argp
, drft
, 0);
304 /* Refile the draft */
305 if (refile (++argp
, drft
) == 0)
310 /* Change the working directory for attachments
312 * Run the directory through the user's shell so that
313 * we can take advantage of any syntax that the user
314 * is accustomed to. Read back the absolute path.
317 if (*(argp
+1) == NULL
) {
318 strcpy(buf
, "$SHELL -c \"cd&&pwd\"");
321 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
323 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
324 if (fgets(cwd
, sizeof (cwd
), f
) == NULL
) {
325 advise (buf
, "fgets");
327 trim_suffix_c(cwd
, '\n');
331 advise("popen", "could not get directory");
337 /* Print the working directory for attachments */
342 /* List files in the current attachment working directory
344 * Use the user's shell so that we can take advantage of any
345 * syntax that the user is accustomed to.
347 writelscmd(buf
, sizeof(buf
), "", argp
);
348 (void)system_in_dir(cwd
, buf
);
353 * List attachments on current draft. Options are:
355 * -l long listing (full path names)
359 if (checkmimeheader(drft
))
365 while (*++argp
!= NULL
) {
366 if (strcmp(*argp
, "-l") == 0)
369 else if (strcmp(*argp
, "-n") == 0)
372 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
384 inform("usage is alist [-ln].");
387 annolist(drft
, ATTACH_FIELD
, l
, n
);
393 * Attach files to current draft.
396 bool verbose
= false;
399 if (checkmimeheader(drft
))
402 for (ap
= argp
+1; *ap
; ++ap
) {
403 if (strcmp(*ap
, "-v") == 0) {
406 } else if (*ap
[0] != '-') {
411 if (*(argp
+1) == NULL
) {
412 inform("attach command requires file argument(s).");
417 * Build a command line that causes the user's shell to list the file name
418 * arguments. This handles and wildcard expansion, tilde expansion, etc.
420 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
423 * Read back the response from the shell, which contains a number of lines
424 * with one file name per line. Remove off the newline. Determine whether
425 * we have an absolute or relative path name. Prepend the current working
426 * directory to relative path names. Add the attachment annotation to the
430 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
431 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
434 trim_suffix_c(shell
, '\n');
437 strncpy(file
, shell
, sizeof(file
));
438 file
[sizeof(file
) - 1] = '\0';
440 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
443 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
445 ctype
= mime_type(file
);
446 printf ("Attaching %s as a %s\n", file
, ctype
);
454 advise("popen", "could not get file from shell");
461 * Detach files from current draft.
465 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
466 * so this catches a -n anywhere in the argument list.
469 if (checkmimeheader(drft
))
472 for (n
= 0, arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
473 if (strcmp(*arguments
, "-n") == 0) {
480 * A -n was found so interpret the arguments as attachment numbers.
481 * Decrement any remaining argument number that is greater than the one
482 * just processed after processing each one so that the numbering stays
487 for (arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
488 if (strcmp(*arguments
, "-n") == 0)
491 if (**arguments
!= '\0') {
492 n
= atoi(*arguments
);
493 annotate(drft
, ATTACH_FIELD
, NULL
, 1, 0, n
, 1);
495 for (argp
= arguments
+ 1; *argp
!= NULL
; argp
++) {
496 if (atoi(*argp
) > n
) {
497 if (atoi(*argp
) == 1)
500 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
508 * The arguments are interpreted as file names. Run them through the
509 * user's shell for wildcard expansion and other goodies. Do this from
510 * the current working directory if the argument is not an absolute path
511 * name (does not begin with a /).
513 * We feed all the file names to the shell at once, otherwise you can't
514 * provide a file name with a space in it.
516 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
517 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
518 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
519 trim_suffix_c(shell
, '\n');
520 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
524 advise("popen", "could not get file from shell");
530 /* Unknown command */
540 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
542 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
545 /* Note that we do not quote -- the argp from the user
546 * is assumed to be quoted as they desire. (We can't treat
547 * it as pure literal as that would prevent them using ~,
548 * wildcards, etc.) The buffer produced by this function
549 * should be given to popen_in_dir() or system_in_dir() so
550 * that the current working directory is set correctly.
552 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
553 /* NB that some snprintf() return -1 on overflow rather than the
554 * new C99 mandated 'number of chars that would have been written'
556 /* length checks here and inside the loop allow for the
557 * trailing "&&", trailcmd, '"' and NUL
559 int trailln
= strlen(trailcmd
) + 4;
560 if (ln
< 0 || ln
+ trailln
> bufsz
)
561 die("arguments too long");
565 while (*argp
&& *++argp
) {
567 /* +1 for leading space */
568 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
569 die("arguments too long");
571 memcpy(cp
, *argp
, ln
+1);
575 *cp
++ = '&'; *cp
++ = '&';
576 strcpy(cp
, trailcmd
);
584 * Build a command line that causes the user's shell to list the file name
585 * arguments. This handles and wildcard expansion, tilde expansion, etc.
588 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
590 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
591 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
595 /* Like system(), but run the command in directory dir.
596 * This assumes the program is single-threaded!
599 system_in_dir(const char *dir
, const char *cmd
)
604 /* ensure that $SHELL exists, as the cmd was written relying on
605 a non-blank $SHELL... */
606 setenv("SHELL","/bin/sh",0); /* don't overwrite */
608 if (getcwd(olddir
, sizeof(olddir
)) == 0)
609 adios("getcwd", "could not get working directory");
611 adios("chdir", "could not change working directory");
613 if (chdir(olddir
) != 0)
614 adios("chdir", "could not change working directory");
618 /* ditto for popen() */
620 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
625 /* ensure that $SHELL exists, as the cmd was written relying on
626 a non-blank $SHELL... */
627 setenv("SHELL","/bin/sh",0); /* don't overwrite */
629 if (getcwd(olddir
, sizeof(olddir
)) == 0)
630 adios("getcwd", "could not get working directory");
632 adios("chdir", "could not change working directory");
633 f
= popen(cmd
, type
);
634 if (chdir(olddir
) != 0)
635 adios("chdir", "could not change working directory");
644 static bool reedit
; /* have we been here before? */
645 static char *edsave
= NULL
; /* the editor we used previously */
649 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
650 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
652 int pid
, status
, vecp
;
653 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
654 char *cp
, *prog
, **vec
;
657 bool slinked
= false;
659 /* Was there a previous edit session? */
660 if (reedit
&& (*ed
|| edsave
)) {
661 if (!*ed
) { /* no explicit editor */
662 *ed
= edsave
; /* so use the previous one */
663 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
666 /* unless we've specified it via "editor-next" */
667 cp
= concat (cp
, "-next", NULL
);
668 if ((cp
= context_find (cp
)) != NULL
)
672 /* set initial editor */
674 *ed
= get_default_editor();
678 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
679 strncpy (altpath
, altmsg
, sizeof(altpath
));
681 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
683 strncpy (linkpath
, LINK
, sizeof(linkpath
));
685 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
688 (void) m_unlink (linkpath
);
689 if (link (altpath
, linkpath
) == NOTOK
) {
690 if (symlink (altpath
, linkpath
) < 0) {
691 adios (linkpath
, "symlink");
700 context_save (); /* save the context file */
703 switch (pid
= fork()) {
705 advise ("fork", "unable to");
711 if (chdir (cwd
) < 0) {
712 advise (cwd
, "chdir");
717 setenv("mhfolder", mp
->foldpath
, 1);
718 setenv("editalt", altpath
, 1);
721 vec
= argsplit(*ed
, &prog
, &vecp
);
725 vec
[vecp
++] = *arg
++;
730 fprintf (stderr
, "unable to exec ");
735 if ((status
= pidwait (pid
, NOTOK
))) {
736 if (((status
& 0xff00) != 0xff00)
737 && (!reedit
|| (status
& 0x00ff))) {
738 if (!use
&& (status
& 0xff00) &&
739 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
740 inform("problems with edit--draft left in %s", cp
);
742 inform("problems with edit--%s preserved", file
);
745 status
= -2; /* maybe "reedit ? -2 : -1"? */
754 ? lstat (linkpath
, &st
) != NOTOK
755 && S_ISREG(st
.st_mode
)
756 && copyf (linkpath
, altpath
) == NOTOK
757 : stat (linkpath
, &st
) != NOTOK
759 && (m_unlink (altpath
) == NOTOK
760 || link (linkpath
, altpath
) == NOTOK
)))
761 advise (linkpath
, "unable to update %s from", altmsg
);
764 /* normally, we remember which editor we used */
766 edsave
= getcpy (*ed
);
769 if (altmsg
&& atfile
)
770 (void) m_unlink (linkpath
);
777 copyf (char *ifile
, char *ofile
)
782 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
784 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
785 admonish (ofile
, "unable to open and truncate");
790 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
791 if (write (out
, buffer
, i
) != i
) {
792 advise (ofile
, "may have damaged");
808 sendfile (char **arg
, char *file
, int pushsw
)
812 char *cp
, *sp
, **vec
, *program
;
815 * If the sendproc is the nmh command `send', then we call
816 * those routines directly rather than exec'ing the command.
818 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
820 sendit (invo_name
= sp
, arg
, file
, pushsw
);
825 context_save (); /* save the context file */
831 adios("fork", "failed:");
834 vec
= argsplit(sendproc
, &program
, &vecp
);
836 vec
[vecp
++] = "-push";
839 vec
[vecp
++] = *arg
++;
843 execvp (program
, vec
);
844 fprintf (stderr
, "unable to exec ");
849 if (pidwait(child_id
, OK
) == 0)
857 * Translate MIME composition file (call buildmimeproc)
861 buildfile (char **argp
, char *file
)
868 /* allocate space for arguments */
874 args
= mh_xmalloc((i
+ 2) * sizeof(char *));
877 * For backward compatibility, we need to add -build
878 * if we are using mhn as buildmimeproc
881 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
882 args
[i
++] = "-build";
884 /* copy any other arguments */
885 while (argp
&& *argp
)
889 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
892 return i
? NOTOK
: OK
;
897 # define SASLminc(a) (a)
898 #else /* CYRUS_SASL */
899 # define SASLminc(a) 0
900 #endif /* CYRUS_SASL */
903 # define TLSminc(a) (a)
904 #else /* TLS_SUPPORT */
905 # define TLSminc(a) 0
906 #endif /* TLS_SUPPORT */
908 #define SEND_SWITCHES \
909 X("alias aliasfile", 0, ALIASW) \
910 X("debug", -5, DEBUGSW) \
911 X("filter filterfile", 0, FILTSW) \
912 X("nofilter", 0, NFILTSW) \
913 X("format", 0, FRMTSW) \
914 X("noformat", 0, NFRMTSW) \
915 X("forward", 0, FORWSW) \
916 X("noforward", 0, NFORWSW) \
917 X("mime", 0, MIMESW) \
918 X("nomime", 0, NMIMESW) \
919 X("msgid", 0, MSGDSW) \
920 X("nomsgid", 0, NMSGDSW) \
921 X("push", 0, SPSHSW) \
922 X("nopush", 0, NSPSHSW) \
923 X("split seconds", 0, SPLITSW) \
924 X("unique", -6, UNIQSW) \
925 X("nounique", -8, NUNIQSW) \
926 X("verbose", 0, VERBSW) \
927 X("noverbose", 0, NVERBSW) \
928 X("watch", 0, WATCSW) \
929 X("nowatch", 0, NWATCSW) \
930 X("width columns", 0, WIDTHSW) \
931 X("version", 0, SVERSIONSW) \
932 X("help", 0, SHELPSW) \
933 X("dashstuffing", -12, BITSTUFFSW) \
934 X("nodashstuffing", -14, NBITSTUFFSW) \
935 X("client host", -6, CLIESW) \
936 X("server host", 6, SERVSW) \
937 X("snoop", -5, SNOOPSW) \
938 X("draftfolder +folder", 0, SDRFSW) \
939 X("draftmessage msg", 0, SDRMSW) \
940 X("nodraftfolder", 0, SNDRFSW) \
941 X("sasl", SASLminc(4), SASLSW) \
942 X("nosasl", SASLminc(6), NOSASLSW) \
943 X("saslmech", SASLminc(5), SASLMECHSW) \
944 X("authservice", SASLminc(0), AUTHSERVICESW) \
945 X("user username", SASLminc(4), USERSW) \
946 X("port server-port-name/number", 4, PORTSW) \
947 X("tls", TLSminc(-3), TLSSW) \
948 X("initialtls", TLSminc(-10), INITTLSSW) \
949 X("notls", TLSminc(-5), NTLSSW) \
950 X("certverify", TLSminc(-10), CERTVERSW) \
951 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
952 X("sendmail program", 0, MTSSM) \
953 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
954 X("messageid localname|random", 2, MESSAGEIDSW) \
956 #define X(sw, minchars, id) id,
957 DEFINE_SWITCH_ENUM(SEND
);
960 #define X(sw, minchars, id) { sw, minchars, id },
961 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
965 extern int debugsw
; /* from sendsbr.c */
973 extern char *altmsg
; /* .. */
974 extern char *annotext
;
975 extern char *distfile
;
979 sendit (char *sp
, char **arg
, char *file
, int pushed
)
982 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
983 char **arguments
, *savearg
[MAXARGS
], **vec
;
984 const char *user
= NULL
, *saslmech
= NULL
;
985 char *auth_svc
= NULL
;
989 * Make sure these are defined. In particular, we need
990 * savearg[1] to be NULL, in case "arg" is NULL below. It
991 * doesn't matter what is the value of savearg[0], but we
992 * set it to NULL, to help catch "off-by-one" errors.
998 * Temporarily copy arg to savearg, since the brkstring() call in
999 * getarguments() will wipe it out before it is merged in.
1000 * Also, we skip the first element of savearg, since getarguments()
1001 * skips it. Then we count the number of arguments
1002 * copied. The value of "n" will be one greater than
1003 * this in order to simulate the standard argc/argv.
1008 copyip (arg
, savearg
+1, MAXARGS
-1);
1015 * Merge any arguments from command line (now in savearg)
1016 * and arguments from profile.
1018 arguments
= getarguments (sp
, n
, savearg
, 1);
1031 * Get our initial arguments for postproc now
1034 vec
= argsplit(postproc
, &program
, &vecp
);
1036 vec
[vecp
++] = "-library";
1037 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
1039 if ((cp
= context_find ("fileproc"))) {
1040 vec
[vecp
++] = "-fileproc";
1044 if ((cp
= context_find ("mhlproc"))) {
1045 vec
[vecp
++] = "-mhlproc";
1049 if ((cp
= context_find ("credentials"))) {
1050 /* post doesn't read context so need to pass credentials. */
1051 vec
[vecp
++] = "-credentials";
1055 while ((cp
= *argp
++)) {
1057 switch (smatch (++cp
, sendswitches
)) {
1059 ambigsw (cp
, sendswitches
);
1062 inform("-%s unknown\n", cp
);
1066 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1067 print_help (buf
, sendswitches
, 1);
1070 print_version (invo_name
);
1081 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1082 inform("missing argument to %s", argp
[-2]);
1138 #ifdef OAUTH_SUPPORT
1139 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
1140 die("missing argument to %s", argp
[-2]);
1143 NMH_UNUSED (auth_svc
);
1144 die("not built with OAuth support");
1162 if (!(cp
= *argp
++) || *cp
== '-') {
1163 inform("missing argument to %s", argp
[-2]);
1172 if (!(cp
= *argp
++) || *cp
== '-') {
1173 inform("missing argument to %s", argp
[-2]);
1181 inform("usage: %s [switches]", sp
);
1185 /* allow Aliasfile: profile entry */
1186 if ((cp
= context_find ("Aliasfile"))) {
1189 dp
= mh_xstrdup(cp
);
1190 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1191 vec
[vecp
++] = "-alias";
1196 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1197 if ((cp
= context_find ("signature")) && *cp
)
1198 setenv("SIGNATURE", cp
, 1);
1200 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1202 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1204 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1205 inplace
= atoi (cp
);
1207 if ((cp
= getenv ("mhdist"))
1211 vec
[vecp
++] = "-dist";
1212 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1213 die("unable to create temporary file");
1215 distfile
= mh_xstrdup(cp
);
1216 (void) m_unlink(distfile
);
1217 if (link (altmsg
, distfile
) == NOTOK
)
1218 adios (distfile
, "unable to link %s to", altmsg
);
1223 #ifdef OAUTH_SUPPORT
1224 if (auth_svc
== NULL
) {
1225 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
1226 die("must specify -authservice with -saslmech xoauth2");
1230 die("must specify -user with -saslmech xoauth2");
1234 NMH_UNUSED(saslmech
);
1235 #endif /* OAUTH_SUPPORT */
1237 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1242 if ((pushsw
= pushed
))
1247 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, auth_svc
) == OK
)
1256 whomfile (char **arg
, char *file
)
1260 char **vec
, *program
;
1262 context_save (); /* save the context file */
1265 switch (pid
= fork()) {
1267 advise ("fork", "unable to");
1271 vec
= argsplit(whomproc
, &program
, &vecp
);
1274 vec
[vecp
++] = *arg
++;
1278 execvp (program
, vec
);
1279 fprintf (stderr
, "unable to exec ");
1281 _exit(1); /* NOTREACHED */
1284 return pidwait(pid
, NOTOK
) & 0377 ? 1 : 0;
1290 * Remove the draft file
1294 removefile (char *drft
)
1296 if (m_unlink (drft
) == NOTOK
)
1297 adios (drft
, "unable to unlink");
1304 * Return 1 if we already have a MIME-Version header, 0 otherwise.
1308 checkmimeheader (char *drft
)
1311 m_getfld_state_t gstate
;
1312 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1314 bool retval
= false;
1316 if ((f
= fopen(drft
, "r")) == NULL
) {
1317 admonish(drft
, "unable to read draft");
1321 gstate
= m_getfld_state_init(f
);
1323 int bufsz
= sizeof(buf
);
1324 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1327 if (strcasecmp(name
, VRSN_FIELD
) == 0) {
1328 inform("Cannot use attach commands with already-"
1329 "formatted MIME message \"%s\"", drft
);
1340 m_getfld_state_destroy(&gstate
);