]>
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.
41 #include "sbr/context_save.h"
42 #include "sbr/context_find.h"
43 #include "sbr/brkstring.h"
44 #include "sbr/ambigsw.h"
47 #include "sbr/print_version.h"
48 #include "sbr/print_help.h"
49 #include "sbr/arglist.h"
50 #include "sbr/error.h"
58 #include "sbr/m_maildir.h"
59 #include "sbr/m_mktemp.h"
60 #include "sbr/mime_type.h"
62 #define WHATNOW_SWITCHES \
63 X("draftfolder +folder", 0, DFOLDSW) \
64 X("draftmessage msg", 0, DMSGSW) \
65 X("nodraftfolder", 0, NDFLDSW) \
66 X("editor editor", 0, EDITRSW) \
67 X("noedit", 0, NEDITSW) \
68 X("prompt string", 4, PRMPTSW) \
69 X("version", 0, VERSIONSW) \
70 X("help", 0, HELPSW) \
73 #define X(sw, minchars, id) id,
74 DEFINE_SWITCH_ENUM(WHATNOW
);
77 #define X(sw, minchars, id) { sw, minchars, id },
78 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
82 * Options at the "whatnow" prompt
84 #define PROMPT_SWITCHES \
85 X("edit [<editor> <switches>]", 0, EDITSW) \
86 X("refile [<switches>] +folder", 0, REFILEOPT) \
87 X("mime [<switches>]", 0, BUILDMIMESW) \
88 X("display [<switches>]", 0, DISPSW) \
89 X("list [<switches>]", 0, LISTSW) \
90 X("send [<switches>]", 0, SENDSW) \
91 X("push [<switches>]", 0, PUSHSW) \
92 X("whom [<switches>]", 0, WHOMSW) \
93 X("quit [-delete]", 0, QUITSW) \
94 X("delete", 0, DELETESW) \
95 X("cd [directory]", 0, CDCMDSW) \
96 X("pwd", 0, PWDCMDSW) \
98 X("attach [-v]", 0, ATTACHCMDSW) \
99 X("detach [-n]", 0, DETACHCMDSW) \
100 X("alist [-ln] ", 2, ALISTCMDSW) \
102 #define X(sw, minchars, id) id,
103 DEFINE_SWITCH_ENUM(PROMPT
);
106 #define X(sw, minchars, id) { sw, minchars, id },
107 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
110 static char *myprompt
= "\nWhat now? ";
115 static int editfile (char **, char **, char *, int, struct msgs
*,
116 char *, char *, int, int);
117 static int sendfile (char **, char *, int);
118 static void sendit (char *, char **, char *, int);
119 static int buildfile (char **, char *);
120 static int whomfile (char **, char *);
121 static int removefile (char *);
122 static int checkmimeheader (char *);
123 static void writelscmd(char *, int, char *, char **);
124 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
125 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
126 static int system_in_dir(const char *dir
, const char *cmd
);
127 static int copyf (char *, char *);
131 WhatNow (int argc
, char **argv
)
135 int use
= 0, atfile
= 1;
136 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
137 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
139 char **argp
, **arguments
;
141 char cwd
[PATH_MAX
+ 1]; /* current working directory */
142 char file
[PATH_MAX
+ 1]; /* file name buffer */
143 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
144 FILE *f
; /* read pointer for bgnd proc */
145 char *l
; /* set on -l to alist command */
146 int n
; /* set on -n to alist command */
148 /* Need this if called from what_now(). */
149 invo_name
= r1bindex (argv
[0], '/');
151 arguments
= getarguments (invo_name
, argc
, argv
, 1);
155 * Get the initial current working directory.
158 if (getcwd(cwd
, sizeof (cwd
)) == NULL
) {
159 adios("getcwd", "could not get working directory");
162 while ((cp
= *argp
++)) {
164 switch (smatch (++cp
, whatnowswitches
)) {
166 ambigsw (cp
, whatnowswitches
);
169 die("-%s unknown", cp
);
172 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
173 print_help (buf
, whatnowswitches
, 1);
176 print_version(invo_name
);
181 die("only one draft folder at a time!");
182 if (!(cp
= *argp
++) || *cp
== '-')
183 die("missing argument to %s", argp
[-2]);
184 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
185 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
189 die("only one draft message at a time!");
190 if (!(dmsg
= *argp
++) || *dmsg
== '-')
191 die("missing argument to %s", argp
[-2]);
199 if (!(ed
= *argp
++) || *ed
== '-')
200 die("missing argument to %s", argp
[-2]);
208 if (!(myprompt
= *argp
++) || *myprompt
== '-')
209 die("missing argument to %s", argp
[-2]);
214 die("only one draft at a time!");
218 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
219 drft
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
221 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? mh_xstrdup(cp
) : NULL
;
223 if ((cp
= getenv ("mhatfile")) && *cp
)
226 if ((cp
= getenv ("mhuse")) && *cp
)
229 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
234 /* start editing the draft, unless -noedit was given */
235 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
236 NULL
, 1, atfile
) < 0)
240 #ifdef READLINE_SUPPORT
241 if (!(argp
= read_switch_multiword_via_readline(myprompt
, aleqs
))) {
242 #else /* ! READLINE_SUPPORT */
243 if (!(argp
= read_switch_multiword(myprompt
, aleqs
))) {
244 #endif /* READLINE_SUPPORT */
245 (void) m_unlink (LINK
);
248 switch (smatch (*argp
, aleqs
)) {
250 /* display the message being replied to, or distributed */
252 showfile (++argp
, msgnam
);
254 inform("no alternate message to display");
258 /* Translate MIME composition file */
259 buildfile (++argp
, drft
);
263 /* Call an editor on the draft file */
266 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
267 NULL
, 1, atfile
) == NOTOK
)
272 /* display the draft file */
273 showfile (++argp
, drft
);
277 /* Check to whom the draft would be sent */
278 whomfile (++argp
, drft
);
282 /* Quit, and possibly delete the draft */
283 if (*++argp
&& (*argp
[0] == 'd' ||
284 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
287 if (stat (drft
, &st
) != NOTOK
)
288 inform("draft left on %s", drft
);
293 /* Delete draft and exit */
298 /* Send draft in background */
299 if (sendfile (++argp
, drft
, 1))
305 sendfile (++argp
, drft
, 0);
309 /* Refile the draft */
310 if (refile (++argp
, drft
) == 0)
315 /* Change the working directory for attachments
317 * Run the directory through the user's shell so that
318 * we can take advantage of any syntax that the user
319 * is accustomed to. Read back the absolute path.
322 if (*(argp
+1) == NULL
) {
323 strcpy(buf
, "$SHELL -c \"cd&&pwd\"");
326 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
328 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
329 if (fgets(cwd
, sizeof (cwd
), f
) == NULL
) {
330 advise (buf
, "fgets");
332 trim_suffix_c(cwd
, '\n');
336 advise("popen", "could not get directory");
342 /* Print the working directory for attachments */
347 /* List files in the current attachment working directory
349 * Use the user's shell so that we can take advantage of any
350 * syntax that the user is accustomed to.
352 writelscmd(buf
, sizeof(buf
), "", argp
);
353 (void)system_in_dir(cwd
, buf
);
358 * List attachments on current draft. Options are:
360 * -l long listing (full path names)
364 if (checkmimeheader(drft
))
370 while (*++argp
!= NULL
) {
371 if (strcmp(*argp
, "-l") == 0)
374 else if (strcmp(*argp
, "-n") == 0)
377 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
389 inform("usage is alist [-ln].");
392 annolist(drft
, ATTACH_FIELD
, l
, n
);
398 * Attach files to current draft.
401 bool verbose
= false;
404 if (checkmimeheader(drft
))
407 for (ap
= argp
+1; *ap
; ++ap
) {
408 if (strcmp(*ap
, "-v") == 0) {
411 } else if (*ap
[0] != '-') {
416 if (*(argp
+1) == NULL
) {
417 inform("attach command requires file argument(s).");
422 * Build a command line that causes the user's shell to list the file name
423 * arguments. This handles and wildcard expansion, tilde expansion, etc.
425 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
428 * Read back the response from the shell, which contains a number of lines
429 * with one file name per line. Remove off the newline. Determine whether
430 * we have an absolute or relative path name. Prepend the current working
431 * directory to relative path names. Add the attachment annotation to the
435 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
436 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
439 trim_suffix_c(shell
, '\n');
442 strncpy(file
, shell
, sizeof(file
));
443 file
[sizeof(file
) - 1] = '\0';
445 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
448 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
450 ctype
= mime_type(file
);
451 printf ("Attaching %s as a %s\n", file
, ctype
);
459 advise("popen", "could not get file from shell");
466 * Detach files from current draft.
470 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
471 * so this catches a -n anywhere in the argument list.
474 if (checkmimeheader(drft
))
477 for (n
= 0, arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
478 if (strcmp(*arguments
, "-n") == 0) {
485 * A -n was found so interpret the arguments as attachment numbers.
486 * Decrement any remaining argument number that is greater than the one
487 * just processed after processing each one so that the numbering stays
492 for (arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
493 if (strcmp(*arguments
, "-n") == 0)
496 if (**arguments
!= '\0') {
497 n
= atoi(*arguments
);
498 annotate(drft
, ATTACH_FIELD
, NULL
, 1, 0, n
, 1);
500 for (argp
= arguments
+ 1; *argp
!= NULL
; argp
++) {
501 if (atoi(*argp
) > n
) {
502 if (atoi(*argp
) == 1)
505 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
513 * The arguments are interpreted as file names. Run them through the
514 * user's shell for wildcard expansion and other goodies. Do this from
515 * the current working directory if the argument is not an absolute path
516 * name (does not begin with a /).
518 * We feed all the file names to the shell at once, otherwise you can't
519 * provide a file name with a space in it.
521 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
522 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
523 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
524 trim_suffix_c(shell
, '\n');
525 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
529 advise("popen", "could not get file from shell");
535 /* Unknown command */
545 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
547 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
550 /* Note that we do not quote -- the argp from the user
551 * is assumed to be quoted as they desire. (We can't treat
552 * it as pure literal as that would prevent them using ~,
553 * wildcards, etc.) The buffer produced by this function
554 * should be given to popen_in_dir() or system_in_dir() so
555 * that the current working directory is set correctly.
557 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
558 /* NB that some snprintf() return -1 on overflow rather than the
559 * new C99 mandated 'number of chars that would have been written'
561 /* length checks here and inside the loop allow for the
562 * trailing "&&", trailcmd, '"' and NUL
564 int trailln
= strlen(trailcmd
) + 4;
565 if (ln
< 0 || ln
+ trailln
> bufsz
)
566 die("arguments too long");
570 while (*argp
&& *++argp
) {
572 /* +1 for leading space */
573 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
574 die("arguments too long");
576 memcpy(cp
, *argp
, ln
+1);
580 *cp
++ = '&'; *cp
++ = '&';
581 strcpy(cp
, trailcmd
);
589 * Build a command line that causes the user's shell to list the file name
590 * arguments. This handles and wildcard expansion, tilde expansion, etc.
593 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
595 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
596 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
600 /* Like system(), but run the command in directory dir.
601 * This assumes the program is single-threaded!
604 system_in_dir(const char *dir
, const char *cmd
)
609 /* ensure that $SHELL exists, as the cmd was written relying on
610 a non-blank $SHELL... */
611 setenv("SHELL","/bin/sh",0); /* don't overwrite */
613 if (getcwd(olddir
, sizeof(olddir
)) == 0)
614 adios("getcwd", "could not get working directory");
616 adios("chdir", "could not change working directory");
618 if (chdir(olddir
) != 0)
619 adios("chdir", "could not change working directory");
623 /* ditto for popen() */
625 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
630 /* ensure that $SHELL exists, as the cmd was written relying on
631 a non-blank $SHELL... */
632 setenv("SHELL","/bin/sh",0); /* don't overwrite */
634 if (getcwd(olddir
, sizeof(olddir
)) == 0)
635 adios("getcwd", "could not get working directory");
637 adios("chdir", "could not change working directory");
638 f
= popen(cmd
, type
);
639 if (chdir(olddir
) != 0)
640 adios("chdir", "could not change working directory");
649 static bool reedit
; /* have we been here before? */
650 static char *edsave
= NULL
; /* the editor we used previously */
654 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
655 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
657 int pid
, status
, vecp
;
658 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
659 char *cp
, *prog
, **vec
;
662 bool slinked
= false;
664 /* Was there a previous edit session? */
665 if (reedit
&& (*ed
|| edsave
)) {
666 if (!*ed
) { /* no explicit editor */
667 *ed
= edsave
; /* so use the previous one */
668 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
671 /* unless we've specified it via "editor-next" */
672 cp
= concat (cp
, "-next", NULL
);
673 if ((cp
= context_find (cp
)) != NULL
)
677 /* set initial editor */
679 *ed
= get_default_editor();
683 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
684 strncpy (altpath
, altmsg
, sizeof(altpath
));
686 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
688 strncpy (linkpath
, LINK
, sizeof(linkpath
));
690 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
693 (void) m_unlink (linkpath
);
694 if (link (altpath
, linkpath
) == NOTOK
) {
695 if (symlink (altpath
, linkpath
) < 0) {
696 adios (linkpath
, "symlink");
705 context_save (); /* save the context file */
708 switch (pid
= fork()) {
710 advise ("fork", "unable to");
716 if (chdir (cwd
) < 0) {
717 advise (cwd
, "chdir");
722 setenv("mhfolder", mp
->foldpath
, 1);
723 setenv("editalt", altpath
, 1);
726 vec
= argsplit(*ed
, &prog
, &vecp
);
730 vec
[vecp
++] = *arg
++;
735 fprintf (stderr
, "unable to exec ");
740 if ((status
= pidwait (pid
, NOTOK
))) {
741 if (((status
& 0xff00) != 0xff00)
742 && (!reedit
|| (status
& 0x00ff))) {
743 if (!use
&& (status
& 0xff00) &&
744 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
745 inform("problems with edit--draft left in %s", cp
);
747 inform("problems with edit--%s preserved", file
);
750 status
= -2; /* maybe "reedit ? -2 : -1"? */
759 ? lstat (linkpath
, &st
) != NOTOK
760 && S_ISREG(st
.st_mode
)
761 && copyf (linkpath
, altpath
) == NOTOK
762 : stat (linkpath
, &st
) != NOTOK
764 && (m_unlink (altpath
) == NOTOK
765 || link (linkpath
, altpath
) == NOTOK
)))
766 advise (linkpath
, "unable to update %s from", altmsg
);
769 /* normally, we remember which editor we used */
771 edsave
= getcpy (*ed
);
774 if (altmsg
&& atfile
)
775 (void) m_unlink (linkpath
);
782 copyf (char *ifile
, char *ofile
)
787 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
789 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
790 admonish (ofile
, "unable to open and truncate");
795 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
796 if (write (out
, buffer
, i
) != i
) {
797 advise (ofile
, "may have damaged");
813 sendfile (char **arg
, char *file
, int pushsw
)
817 char *cp
, *sp
, **vec
, *program
;
820 * If the sendproc is the nmh command `send', then we call
821 * those routines directly rather than exec'ing the command.
823 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
825 sendit (invo_name
= sp
, arg
, file
, pushsw
);
830 context_save (); /* save the context file */
836 adios("fork", "failed:");
839 vec
= argsplit(sendproc
, &program
, &vecp
);
841 vec
[vecp
++] = "-push";
844 vec
[vecp
++] = *arg
++;
848 execvp (program
, vec
);
849 fprintf (stderr
, "unable to exec ");
854 if (pidwait(child_id
, OK
) == 0)
862 * Translate MIME composition file (call buildmimeproc)
866 buildfile (char **argp
, char *file
)
873 /* allocate space for arguments */
879 args
= mh_xmalloc((i
+ 2) * sizeof(char *));
882 * For backward compatibility, we need to add -build
883 * if we are using mhn as buildmimeproc
886 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
887 args
[i
++] = "-build";
889 /* copy any other arguments */
890 while (argp
&& *argp
)
894 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
897 return i
? NOTOK
: OK
;
902 # define SASLminc(a) (a)
903 #else /* CYRUS_SASL */
904 # define SASLminc(a) 0
905 #endif /* CYRUS_SASL */
908 # define TLSminc(a) (a)
909 #else /* TLS_SUPPORT */
910 # define TLSminc(a) 0
911 #endif /* TLS_SUPPORT */
913 #define SEND_SWITCHES \
914 X("alias aliasfile", 0, ALIASW) \
915 X("debug", -5, DEBUGSW) \
916 X("filter filterfile", 0, FILTSW) \
917 X("nofilter", 0, NFILTSW) \
918 X("format", 0, FRMTSW) \
919 X("noformat", 0, NFRMTSW) \
920 X("forward", 0, FORWSW) \
921 X("noforward", 0, NFORWSW) \
922 X("mime", 0, MIMESW) \
923 X("nomime", 0, NMIMESW) \
924 X("msgid", 0, MSGDSW) \
925 X("nomsgid", 0, NMSGDSW) \
926 X("push", 0, SPSHSW) \
927 X("nopush", 0, NSPSHSW) \
928 X("split seconds", 0, SPLITSW) \
929 X("unique", -6, UNIQSW) \
930 X("nounique", -8, NUNIQSW) \
931 X("verbose", 0, VERBSW) \
932 X("noverbose", 0, NVERBSW) \
933 X("watch", 0, WATCSW) \
934 X("nowatch", 0, NWATCSW) \
935 X("width columns", 0, WIDTHSW) \
936 X("version", 0, SVERSIONSW) \
937 X("help", 0, SHELPSW) \
938 X("dashstuffing", -12, BITSTUFFSW) \
939 X("nodashstuffing", -14, NBITSTUFFSW) \
940 X("client host", -6, CLIESW) \
941 X("server host", 6, SERVSW) \
942 X("snoop", -5, SNOOPSW) \
943 X("draftfolder +folder", 0, SDRFSW) \
944 X("draftmessage msg", 0, SDRMSW) \
945 X("nodraftfolder", 0, SNDRFSW) \
946 X("sasl", SASLminc(4), SASLSW) \
947 X("nosasl", SASLminc(6), NOSASLSW) \
948 X("saslmech", SASLminc(5), SASLMECHSW) \
949 X("authservice", SASLminc(0), AUTHSERVICESW) \
950 X("user username", SASLminc(4), USERSW) \
951 X("port server-port-name/number", 4, PORTSW) \
952 X("tls", TLSminc(-3), TLSSW) \
953 X("initialtls", TLSminc(-10), INITTLSSW) \
954 X("notls", TLSminc(-5), NTLSSW) \
955 X("certverify", TLSminc(-10), CERTVERSW) \
956 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
957 X("sendmail program", 0, MTSSM) \
958 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
959 X("messageid localname|random", 2, MESSAGEIDSW) \
961 #define X(sw, minchars, id) id,
962 DEFINE_SWITCH_ENUM(SEND
);
965 #define X(sw, minchars, id) { sw, minchars, id },
966 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
970 extern int debugsw
; /* from sendsbr.c */
978 extern char *altmsg
; /* .. */
979 extern char *annotext
;
980 extern char *distfile
;
984 sendit (char *sp
, char **arg
, char *file
, int pushed
)
987 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
988 char **arguments
, *savearg
[MAXARGS
], **vec
;
989 const char *user
= NULL
, *saslmech
= NULL
;
990 char *auth_svc
= NULL
;
994 * Make sure these are defined. In particular, we need
995 * savearg[1] to be NULL, in case "arg" is NULL below. It
996 * doesn't matter what is the value of savearg[0], but we
997 * set it to NULL, to help catch "off-by-one" errors.
1003 * Temporarily copy arg to savearg, since the brkstring() call in
1004 * getarguments() will wipe it out before it is merged in.
1005 * Also, we skip the first element of savearg, since getarguments()
1006 * skips it. Then we count the number of arguments
1007 * copied. The value of "n" will be one greater than
1008 * this in order to simulate the standard argc/argv.
1013 copyip (arg
, savearg
+1, MAXARGS
-1);
1020 * Merge any arguments from command line (now in savearg)
1021 * and arguments from profile.
1023 arguments
= getarguments (sp
, n
, savearg
, 1);
1036 * Get our initial arguments for postproc now
1039 vec
= argsplit(postproc
, &program
, &vecp
);
1041 vec
[vecp
++] = "-library";
1042 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
1044 if ((cp
= context_find ("fileproc"))) {
1045 vec
[vecp
++] = "-fileproc";
1049 if ((cp
= context_find ("mhlproc"))) {
1050 vec
[vecp
++] = "-mhlproc";
1054 if ((cp
= context_find ("credentials"))) {
1055 /* post doesn't read context so need to pass credentials. */
1056 vec
[vecp
++] = "-credentials";
1060 while ((cp
= *argp
++)) {
1062 switch (smatch (++cp
, sendswitches
)) {
1064 ambigsw (cp
, sendswitches
);
1067 inform("-%s unknown\n", cp
);
1071 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1072 print_help (buf
, sendswitches
, 1);
1075 print_version (invo_name
);
1086 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1087 inform("missing argument to %s", argp
[-2]);
1143 #ifdef OAUTH_SUPPORT
1144 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
1145 die("missing argument to %s", argp
[-2]);
1148 NMH_UNUSED (auth_svc
);
1149 die("not built with OAuth support");
1167 if (!(cp
= *argp
++) || *cp
== '-') {
1168 inform("missing argument to %s", argp
[-2]);
1177 if (!(cp
= *argp
++) || *cp
== '-') {
1178 inform("missing argument to %s", argp
[-2]);
1186 inform("usage: %s [switches]", sp
);
1190 /* allow Aliasfile: profile entry */
1191 if ((cp
= context_find ("Aliasfile"))) {
1194 dp
= mh_xstrdup(cp
);
1195 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1196 vec
[vecp
++] = "-alias";
1201 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1202 if ((cp
= context_find ("signature")) && *cp
)
1203 setenv("SIGNATURE", cp
, 1);
1205 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1207 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1209 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1210 inplace
= atoi (cp
);
1212 if ((cp
= getenv ("mhdist"))
1216 vec
[vecp
++] = "-dist";
1217 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1218 die("unable to create temporary file");
1220 distfile
= mh_xstrdup(cp
);
1221 (void) m_unlink(distfile
);
1222 if (link (altmsg
, distfile
) == NOTOK
)
1223 adios (distfile
, "unable to link %s to", altmsg
);
1228 #ifdef OAUTH_SUPPORT
1229 if (auth_svc
== NULL
) {
1230 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
1231 die("must specify -authservice with -saslmech xoauth2");
1235 die("must specify -user with -saslmech xoauth2");
1239 NMH_UNUSED(saslmech
);
1240 #endif /* OAUTH_SUPPORT */
1242 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1247 if ((pushsw
= pushed
))
1252 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, auth_svc
) == OK
)
1261 whomfile (char **arg
, char *file
)
1265 char **vec
, *program
;
1267 context_save (); /* save the context file */
1270 switch (pid
= fork()) {
1272 advise ("fork", "unable to");
1276 vec
= argsplit(whomproc
, &program
, &vecp
);
1279 vec
[vecp
++] = *arg
++;
1283 execvp (program
, vec
);
1284 fprintf (stderr
, "unable to exec ");
1286 _exit(1); /* NOTREACHED */
1289 return pidwait(pid
, NOTOK
) & 0377 ? 1 : 0;
1295 * Remove the draft file
1299 removefile (char *drft
)
1301 if (m_unlink (drft
) == NOTOK
)
1302 adios (drft
, "unable to unlink");
1309 * Return 1 if we already have a MIME-Version header, 0 otherwise.
1313 checkmimeheader (char *drft
)
1316 m_getfld_state_t gstate
;
1317 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1319 bool retval
= false;
1321 if ((f
= fopen(drft
, "r")) == NULL
) {
1322 admonish(drft
, "unable to read draft");
1326 gstate
= m_getfld_state_init(f
);
1328 int bufsz
= sizeof(buf
);
1329 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1332 if (strcasecmp(name
, VRSN_FIELD
) == 0) {
1333 inform("Cannot use attach commands with already-"
1334 "formatted MIME message \"%s\"", drft
);
1345 m_getfld_state_destroy(&gstate
);