]>
diplodocus.org Git - nmh/blob - uip/whatnowsbr.c
1 /* whatnowsbr.c -- the WhatNow shell
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
7 * Several options have been added to ease the inclusion of attachments
8 * using the header field name mechanism added to anno and send. The
9 * -attach option is used to specify the header field name for attachments.
11 * Several commands have been added at the whatnow prompt:
13 * cd [ directory ] This option works just like the shell's
14 * cd command and lets the user change the
15 * directory from which attachments are
16 * taken so that long path names are not
17 * needed with every file.
19 * ls [ ls-options ] This option works just like the normal
20 * ls command and exists to allow the user
21 * to verify file names in the directory.
23 * pwd This option works just like the normal
24 * pwd command and exists to allow the user
25 * to verify the directory.
27 * attach [-v] files This option attaches the named files to
28 * the draft. -v displays the mhbuild
29 * directive that send(1) will use.
31 * alist [-ln] This option lists the attachments on the
32 * draft. -l gets long listings, -n gets
35 * detach files This option removes attachments from the
36 * detach -n numbers draft. This can be done by file name or
37 * by attachment number.
48 #include "sbr/m_maildir.h"
49 #include "sbr/m_mktemp.h"
50 #include "sbr/mime_type.h"
52 #define WHATNOW_SWITCHES \
53 X("draftfolder +folder", 0, DFOLDSW) \
54 X("draftmessage msg", 0, DMSGSW) \
55 X("nodraftfolder", 0, NDFLDSW) \
56 X("editor editor", 0, EDITRSW) \
57 X("noedit", 0, NEDITSW) \
58 X("prompt string", 4, PRMPTSW) \
59 X("version", 0, VERSIONSW) \
60 X("help", 0, HELPSW) \
63 #define X(sw, minchars, id) id,
64 DEFINE_SWITCH_ENUM(WHATNOW
);
67 #define X(sw, minchars, id) { sw, minchars, id },
68 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
72 * Options at the "whatnow" prompt
74 #define PROMPT_SWITCHES \
75 X("edit [<editor> <switches>]", 0, EDITSW) \
76 X("refile [<switches>] +folder", 0, REFILEOPT) \
77 X("mime [<switches>]", 0, BUILDMIMESW) \
78 X("display [<switches>]", 0, DISPSW) \
79 X("list [<switches>]", 0, LISTSW) \
80 X("send [<switches>]", 0, SENDSW) \
81 X("push [<switches>]", 0, PUSHSW) \
82 X("whom [<switches>]", 0, WHOMSW) \
83 X("quit [-delete]", 0, QUITSW) \
84 X("delete", 0, DELETESW) \
85 X("cd [directory]", 0, CDCMDSW) \
86 X("pwd", 0, PWDCMDSW) \
88 X("attach [-v]", 0, ATTACHCMDSW) \
89 X("detach [-n]", 0, DETACHCMDSW) \
90 X("alist [-ln] ", 2, ALISTCMDSW) \
92 #define X(sw, minchars, id) id,
93 DEFINE_SWITCH_ENUM(PROMPT
);
96 #define X(sw, minchars, id) { sw, minchars, id },
97 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
100 static char *myprompt
= "\nWhat now? ";
105 static int editfile (char **, char **, char *, int, struct msgs
*,
106 char *, char *, int, int);
107 static int sendfile (char **, char *, int);
108 static void sendit (char *, char **, char *, int);
109 static int buildfile (char **, char *);
110 static int whomfile (char **, char *);
111 static int removefile (char *);
112 static int checkmimeheader (char *);
113 static void writelscmd(char *, int, char *, char **);
114 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
115 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
116 static int system_in_dir(const char *dir
, const char *cmd
);
117 static int copyf (char *, char *);
121 WhatNow (int argc
, char **argv
)
125 int use
= 0, atfile
= 1;
126 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
127 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
129 char **argp
, **arguments
;
131 char cwd
[PATH_MAX
+ 1]; /* current working directory */
132 char file
[PATH_MAX
+ 1]; /* file name buffer */
133 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
134 FILE *f
; /* read pointer for bgnd proc */
135 char *l
; /* set on -l to alist command */
136 int n
; /* set on -n to alist command */
138 /* Need this if called from what_now(). */
139 invo_name
= r1bindex (argv
[0], '/');
141 arguments
= getarguments (invo_name
, argc
, argv
, 1);
145 * Get the initial current working directory.
148 if (getcwd(cwd
, sizeof (cwd
)) == NULL
) {
149 adios("getcwd", "could not get working directory");
152 while ((cp
= *argp
++)) {
154 switch (smatch (++cp
, whatnowswitches
)) {
156 ambigsw (cp
, whatnowswitches
);
159 die("-%s unknown", cp
);
162 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
163 print_help (buf
, whatnowswitches
, 1);
166 print_version(invo_name
);
171 die("only one draft folder at a time!");
172 if (!(cp
= *argp
++) || *cp
== '-')
173 die("missing argument to %s", argp
[-2]);
174 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
175 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
179 die("only one draft message at a time!");
180 if (!(dmsg
= *argp
++) || *dmsg
== '-')
181 die("missing argument to %s", argp
[-2]);
189 if (!(ed
= *argp
++) || *ed
== '-')
190 die("missing argument to %s", argp
[-2]);
198 if (!(myprompt
= *argp
++) || *myprompt
== '-')
199 die("missing argument to %s", argp
[-2]);
204 die("only one draft at a time!");
208 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
209 drft
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
211 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? mh_xstrdup(cp
) : NULL
;
213 if ((cp
= getenv ("mhatfile")) && *cp
)
216 if ((cp
= getenv ("mhuse")) && *cp
)
219 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
224 /* start editing the draft, unless -noedit was given */
225 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
226 NULL
, 1, atfile
) < 0)
230 #ifdef READLINE_SUPPORT
231 if (!(argp
= read_switch_multiword_via_readline(myprompt
, aleqs
))) {
232 #else /* ! READLINE_SUPPORT */
233 if (!(argp
= read_switch_multiword(myprompt
, aleqs
))) {
234 #endif /* READLINE_SUPPORT */
235 (void) m_unlink (LINK
);
238 switch (smatch (*argp
, aleqs
)) {
240 /* display the message being replied to, or distributed */
242 showfile (++argp
, msgnam
);
244 inform("no alternate message to display");
248 /* Translate MIME composition file */
249 buildfile (++argp
, drft
);
253 /* Call an editor on the draft file */
256 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
257 NULL
, 1, atfile
) == NOTOK
)
262 /* display the draft file */
263 showfile (++argp
, drft
);
267 /* Check to whom the draft would be sent */
268 whomfile (++argp
, drft
);
272 /* Quit, and possibly delete the draft */
273 if (*++argp
&& (*argp
[0] == 'd' ||
274 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
277 if (stat (drft
, &st
) != NOTOK
)
278 inform("draft left on %s", drft
);
283 /* Delete draft and exit */
288 /* Send draft in background */
289 if (sendfile (++argp
, drft
, 1))
295 sendfile (++argp
, drft
, 0);
299 /* Refile the draft */
300 if (refile (++argp
, drft
) == 0)
305 /* Change the working directory for attachments
307 * Run the directory through the user's shell so that
308 * we can take advantage of any syntax that the user
309 * is accustomed to. Read back the absolute path.
312 if (*(argp
+1) == NULL
) {
313 strcpy(buf
, "$SHELL -c \"cd&&pwd\"");
316 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
318 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
319 if (fgets(cwd
, sizeof (cwd
), f
) == NULL
) {
320 advise (buf
, "fgets");
322 trim_suffix_c(cwd
, '\n');
326 advise("popen", "could not get directory");
332 /* Print the working directory for attachments */
337 /* List files in the current attachment working directory
339 * Use the user's shell so that we can take advantage of any
340 * syntax that the user is accustomed to.
342 writelscmd(buf
, sizeof(buf
), "", argp
);
343 (void)system_in_dir(cwd
, buf
);
348 * List attachments on current draft. Options are:
350 * -l long listing (full path names)
354 if (checkmimeheader(drft
))
360 while (*++argp
!= NULL
) {
361 if (strcmp(*argp
, "-l") == 0)
364 else if (strcmp(*argp
, "-n") == 0)
367 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
379 inform("usage is alist [-ln].");
382 annolist(drft
, ATTACH_FIELD
, l
, n
);
388 * Attach files to current draft.
391 bool verbose
= false;
394 if (checkmimeheader(drft
))
397 for (ap
= argp
+1; *ap
; ++ap
) {
398 if (strcmp(*ap
, "-v") == 0) {
401 } else if (*ap
[0] != '-') {
406 if (*(argp
+1) == NULL
) {
407 inform("attach command requires file argument(s).");
412 * Build a command line that causes the user's shell to list the file name
413 * arguments. This handles and wildcard expansion, tilde expansion, etc.
415 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
418 * Read back the response from the shell, which contains a number of lines
419 * with one file name per line. Remove off the newline. Determine whether
420 * we have an absolute or relative path name. Prepend the current working
421 * directory to relative path names. Add the attachment annotation to the
425 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
426 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
429 trim_suffix_c(shell
, '\n');
432 strncpy(file
, shell
, sizeof(file
));
433 file
[sizeof(file
) - 1] = '\0';
435 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
438 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
440 ctype
= mime_type(file
);
441 printf ("Attaching %s as a %s\n", file
, ctype
);
449 advise("popen", "could not get file from shell");
456 * Detach files from current draft.
460 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
461 * so this catches a -n anywhere in the argument list.
464 if (checkmimeheader(drft
))
467 for (n
= 0, arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
468 if (strcmp(*arguments
, "-n") == 0) {
475 * A -n was found so interpret the arguments as attachment numbers.
476 * Decrement any remaining argument number that is greater than the one
477 * just processed after processing each one so that the numbering stays
482 for (arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
483 if (strcmp(*arguments
, "-n") == 0)
486 if (**arguments
!= '\0') {
487 n
= atoi(*arguments
);
488 annotate(drft
, ATTACH_FIELD
, NULL
, 1, 0, n
, 1);
490 for (argp
= arguments
+ 1; *argp
!= NULL
; argp
++) {
491 if (atoi(*argp
) > n
) {
492 if (atoi(*argp
) == 1)
495 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
503 * The arguments are interpreted as file names. Run them through the
504 * user's shell for wildcard expansion and other goodies. Do this from
505 * the current working directory if the argument is not an absolute path
506 * name (does not begin with a /).
508 * We feed all the file names to the shell at once, otherwise you can't
509 * provide a file name with a space in it.
511 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
512 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
513 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
514 trim_suffix_c(shell
, '\n');
515 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
519 advise("popen", "could not get file from shell");
525 /* Unknown command */
535 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
537 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
540 /* Note that we do not quote -- the argp from the user
541 * is assumed to be quoted as they desire. (We can't treat
542 * it as pure literal as that would prevent them using ~,
543 * wildcards, etc.) The buffer produced by this function
544 * should be given to popen_in_dir() or system_in_dir() so
545 * that the current working directory is set correctly.
547 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
548 /* NB that some snprintf() return -1 on overflow rather than the
549 * new C99 mandated 'number of chars that would have been written'
551 /* length checks here and inside the loop allow for the
552 * trailing "&&", trailcmd, '"' and NUL
554 int trailln
= strlen(trailcmd
) + 4;
555 if (ln
< 0 || ln
+ trailln
> bufsz
)
556 die("arguments too long");
560 while (*argp
&& *++argp
) {
562 /* +1 for leading space */
563 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
564 die("arguments too long");
566 memcpy(cp
, *argp
, ln
+1);
570 *cp
++ = '&'; *cp
++ = '&';
571 strcpy(cp
, trailcmd
);
579 * Build a command line that causes the user's shell to list the file name
580 * arguments. This handles and wildcard expansion, tilde expansion, etc.
583 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
585 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
586 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
590 /* Like system(), but run the command in directory dir.
591 * This assumes the program is single-threaded!
594 system_in_dir(const char *dir
, const char *cmd
)
599 /* ensure that $SHELL exists, as the cmd was written relying on
600 a non-blank $SHELL... */
601 setenv("SHELL","/bin/sh",0); /* don't overwrite */
603 if (getcwd(olddir
, sizeof(olddir
)) == 0)
604 adios("getcwd", "could not get working directory");
606 adios("chdir", "could not change working directory");
608 if (chdir(olddir
) != 0)
609 adios("chdir", "could not change working directory");
613 /* ditto for popen() */
615 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
620 /* ensure that $SHELL exists, as the cmd was written relying on
621 a non-blank $SHELL... */
622 setenv("SHELL","/bin/sh",0); /* don't overwrite */
624 if (getcwd(olddir
, sizeof(olddir
)) == 0)
625 adios("getcwd", "could not get working directory");
627 adios("chdir", "could not change working directory");
628 f
= popen(cmd
, type
);
629 if (chdir(olddir
) != 0)
630 adios("chdir", "could not change working directory");
639 static bool reedit
; /* have we been here before? */
640 static char *edsave
= NULL
; /* the editor we used previously */
644 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
645 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
647 int pid
, status
, vecp
;
648 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
649 char *cp
, *prog
, **vec
;
652 bool slinked
= false;
654 /* Was there a previous edit session? */
655 if (reedit
&& (*ed
|| edsave
)) {
656 if (!*ed
) { /* no explicit editor */
657 *ed
= edsave
; /* so use the previous one */
658 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
661 /* unless we've specified it via "editor-next" */
662 cp
= concat (cp
, "-next", NULL
);
663 if ((cp
= context_find (cp
)) != NULL
)
667 /* set initial editor */
669 *ed
= get_default_editor();
673 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
674 strncpy (altpath
, altmsg
, sizeof(altpath
));
676 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
678 strncpy (linkpath
, LINK
, sizeof(linkpath
));
680 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
683 (void) m_unlink (linkpath
);
684 if (link (altpath
, linkpath
) == NOTOK
) {
685 if (symlink (altpath
, linkpath
) < 0) {
686 adios (linkpath
, "symlink");
695 context_save (); /* save the context file */
698 switch (pid
= fork()) {
700 advise ("fork", "unable to");
706 if (chdir (cwd
) < 0) {
707 advise (cwd
, "chdir");
712 setenv("mhfolder", mp
->foldpath
, 1);
713 setenv("editalt", altpath
, 1);
716 vec
= argsplit(*ed
, &prog
, &vecp
);
720 vec
[vecp
++] = *arg
++;
725 fprintf (stderr
, "unable to exec ");
730 if ((status
= pidwait (pid
, NOTOK
))) {
731 if (((status
& 0xff00) != 0xff00)
732 && (!reedit
|| (status
& 0x00ff))) {
733 if (!use
&& (status
& 0xff00) &&
734 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
735 inform("problems with edit--draft left in %s", cp
);
737 inform("problems with edit--%s preserved", file
);
740 status
= -2; /* maybe "reedit ? -2 : -1"? */
749 ? lstat (linkpath
, &st
) != NOTOK
750 && S_ISREG(st
.st_mode
)
751 && copyf (linkpath
, altpath
) == NOTOK
752 : stat (linkpath
, &st
) != NOTOK
754 && (m_unlink (altpath
) == NOTOK
755 || link (linkpath
, altpath
) == NOTOK
)))
756 advise (linkpath
, "unable to update %s from", altmsg
);
759 /* normally, we remember which editor we used */
761 edsave
= getcpy (*ed
);
764 if (altmsg
&& atfile
)
765 (void) m_unlink (linkpath
);
772 copyf (char *ifile
, char *ofile
)
777 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
779 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
780 admonish (ofile
, "unable to open and truncate");
785 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
786 if (write (out
, buffer
, i
) != i
) {
787 advise (ofile
, "may have damaged");
803 sendfile (char **arg
, char *file
, int pushsw
)
807 char *cp
, *sp
, **vec
, *program
;
810 * If the sendproc is the nmh command `send', then we call
811 * those routines directly rather than exec'ing the command.
813 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
815 sendit (invo_name
= sp
, arg
, file
, pushsw
);
820 context_save (); /* save the context file */
826 adios("fork", "failed:");
829 vec
= argsplit(sendproc
, &program
, &vecp
);
831 vec
[vecp
++] = "-push";
834 vec
[vecp
++] = *arg
++;
838 execvp (program
, vec
);
839 fprintf (stderr
, "unable to exec ");
844 if (pidwait(child_id
, OK
) == 0)
852 * Translate MIME composition file (call buildmimeproc)
856 buildfile (char **argp
, char *file
)
863 /* allocate space for arguments */
869 args
= mh_xmalloc((i
+ 2) * sizeof(char *));
872 * For backward compatibility, we need to add -build
873 * if we are using mhn as buildmimeproc
876 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
877 args
[i
++] = "-build";
879 /* copy any other arguments */
880 while (argp
&& *argp
)
884 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
887 return i
? NOTOK
: OK
;
892 # define SASLminc(a) (a)
893 #else /* CYRUS_SASL */
894 # define SASLminc(a) 0
895 #endif /* CYRUS_SASL */
898 # define TLSminc(a) (a)
899 #else /* TLS_SUPPORT */
900 # define TLSminc(a) 0
901 #endif /* TLS_SUPPORT */
903 #define SEND_SWITCHES \
904 X("alias aliasfile", 0, ALIASW) \
905 X("debug", -5, DEBUGSW) \
906 X("filter filterfile", 0, FILTSW) \
907 X("nofilter", 0, NFILTSW) \
908 X("format", 0, FRMTSW) \
909 X("noformat", 0, NFRMTSW) \
910 X("forward", 0, FORWSW) \
911 X("noforward", 0, NFORWSW) \
912 X("mime", 0, MIMESW) \
913 X("nomime", 0, NMIMESW) \
914 X("msgid", 0, MSGDSW) \
915 X("nomsgid", 0, NMSGDSW) \
916 X("push", 0, SPSHSW) \
917 X("nopush", 0, NSPSHSW) \
918 X("split seconds", 0, SPLITSW) \
919 X("unique", -6, UNIQSW) \
920 X("nounique", -8, NUNIQSW) \
921 X("verbose", 0, VERBSW) \
922 X("noverbose", 0, NVERBSW) \
923 X("watch", 0, WATCSW) \
924 X("nowatch", 0, NWATCSW) \
925 X("width columns", 0, WIDTHSW) \
926 X("version", 0, SVERSIONSW) \
927 X("help", 0, SHELPSW) \
928 X("dashstuffing", -12, BITSTUFFSW) \
929 X("nodashstuffing", -14, NBITSTUFFSW) \
930 X("client host", -6, CLIESW) \
931 X("server host", 6, SERVSW) \
932 X("snoop", -5, SNOOPSW) \
933 X("draftfolder +folder", 0, SDRFSW) \
934 X("draftmessage msg", 0, SDRMSW) \
935 X("nodraftfolder", 0, SNDRFSW) \
936 X("sasl", SASLminc(4), SASLSW) \
937 X("nosasl", SASLminc(6), NOSASLSW) \
938 X("saslmech", SASLminc(5), SASLMECHSW) \
939 X("authservice", SASLminc(0), AUTHSERVICESW) \
940 X("user username", SASLminc(4), USERSW) \
941 X("port server-port-name/number", 4, PORTSW) \
942 X("tls", TLSminc(-3), TLSSW) \
943 X("initialtls", TLSminc(-10), INITTLSSW) \
944 X("notls", TLSminc(-5), NTLSSW) \
945 X("certverify", TLSminc(-10), CERTVERSW) \
946 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
947 X("sendmail program", 0, MTSSM) \
948 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
949 X("messageid localname|random", 2, MESSAGEIDSW) \
951 #define X(sw, minchars, id) id,
952 DEFINE_SWITCH_ENUM(SEND
);
955 #define X(sw, minchars, id) { sw, minchars, id },
956 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
960 extern int debugsw
; /* from sendsbr.c */
968 extern char *altmsg
; /* .. */
969 extern char *annotext
;
970 extern char *distfile
;
974 sendit (char *sp
, char **arg
, char *file
, int pushed
)
977 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
978 char **arguments
, *savearg
[MAXARGS
], **vec
;
979 const char *user
= NULL
, *saslmech
= NULL
;
980 char *auth_svc
= NULL
;
984 * Make sure these are defined. In particular, we need
985 * savearg[1] to be NULL, in case "arg" is NULL below. It
986 * doesn't matter what is the value of savearg[0], but we
987 * set it to NULL, to help catch "off-by-one" errors.
993 * Temporarily copy arg to savearg, since the brkstring() call in
994 * getarguments() will wipe it out before it is merged in.
995 * Also, we skip the first element of savearg, since getarguments()
996 * skips it. Then we count the number of arguments
997 * copied. The value of "n" will be one greater than
998 * this in order to simulate the standard argc/argv.
1003 copyip (arg
, savearg
+1, MAXARGS
-1);
1010 * Merge any arguments from command line (now in savearg)
1011 * and arguments from profile.
1013 arguments
= getarguments (sp
, n
, savearg
, 1);
1026 * Get our initial arguments for postproc now
1029 vec
= argsplit(postproc
, &program
, &vecp
);
1031 vec
[vecp
++] = "-library";
1032 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
1034 if ((cp
= context_find ("fileproc"))) {
1035 vec
[vecp
++] = "-fileproc";
1039 if ((cp
= context_find ("mhlproc"))) {
1040 vec
[vecp
++] = "-mhlproc";
1044 if ((cp
= context_find ("credentials"))) {
1045 /* post doesn't read context so need to pass credentials. */
1046 vec
[vecp
++] = "-credentials";
1050 while ((cp
= *argp
++)) {
1052 switch (smatch (++cp
, sendswitches
)) {
1054 ambigsw (cp
, sendswitches
);
1057 inform("-%s unknown\n", cp
);
1061 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1062 print_help (buf
, sendswitches
, 1);
1065 print_version (invo_name
);
1076 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1077 inform("missing argument to %s", argp
[-2]);
1133 #ifdef OAUTH_SUPPORT
1134 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
1135 die("missing argument to %s", argp
[-2]);
1138 NMH_UNUSED (auth_svc
);
1139 die("not built with OAuth support");
1157 if (!(cp
= *argp
++) || *cp
== '-') {
1158 inform("missing argument to %s", argp
[-2]);
1167 if (!(cp
= *argp
++) || *cp
== '-') {
1168 inform("missing argument to %s", argp
[-2]);
1176 inform("usage: %s [switches]", sp
);
1180 /* allow Aliasfile: profile entry */
1181 if ((cp
= context_find ("Aliasfile"))) {
1184 dp
= mh_xstrdup(cp
);
1185 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1186 vec
[vecp
++] = "-alias";
1191 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1192 if ((cp
= context_find ("signature")) && *cp
)
1193 setenv("SIGNATURE", cp
, 1);
1195 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1197 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1199 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1200 inplace
= atoi (cp
);
1202 if ((cp
= getenv ("mhdist"))
1206 vec
[vecp
++] = "-dist";
1207 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1208 die("unable to create temporary file");
1210 distfile
= mh_xstrdup(cp
);
1211 (void) m_unlink(distfile
);
1212 if (link (altmsg
, distfile
) == NOTOK
)
1213 adios (distfile
, "unable to link %s to", altmsg
);
1218 #ifdef OAUTH_SUPPORT
1219 if (auth_svc
== NULL
) {
1220 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
1221 die("must specify -authservice with -saslmech xoauth2");
1225 die("must specify -user with -saslmech xoauth2");
1229 NMH_UNUSED(saslmech
);
1230 #endif /* OAUTH_SUPPORT */
1232 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1237 if ((pushsw
= pushed
))
1242 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, auth_svc
) == OK
)
1251 whomfile (char **arg
, char *file
)
1255 char **vec
, *program
;
1257 context_save (); /* save the context file */
1260 switch (pid
= fork()) {
1262 advise ("fork", "unable to");
1266 vec
= argsplit(whomproc
, &program
, &vecp
);
1269 vec
[vecp
++] = *arg
++;
1273 execvp (program
, vec
);
1274 fprintf (stderr
, "unable to exec ");
1276 _exit(1); /* NOTREACHED */
1279 return pidwait(pid
, NOTOK
) & 0377 ? 1 : 0;
1285 * Remove the draft file
1289 removefile (char *drft
)
1291 if (m_unlink (drft
) == NOTOK
)
1292 adios (drft
, "unable to unlink");
1299 * Return 1 if we already have a MIME-Version header, 0 otherwise.
1303 checkmimeheader (char *drft
)
1306 m_getfld_state_t gstate
;
1307 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1309 bool retval
= false;
1311 if ((f
= fopen(drft
, "r")) == NULL
) {
1312 admonish(drft
, "unable to read draft");
1316 gstate
= m_getfld_state_init(f
);
1318 int bufsz
= sizeof(buf
);
1319 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1322 if (strcasecmp(name
, VRSN_FIELD
) == 0) {
1323 inform("Cannot use attach commands with already-"
1324 "formatted MIME message \"%s\"", drft
);
1335 m_getfld_state_destroy(&gstate
);