]>
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.
47 #include "../sbr/m_maildir.h"
48 #include "../sbr/m_mktemp.h"
49 #include "../sbr/mime_type.h"
51 #define WHATNOW_SWITCHES \
52 X("draftfolder +folder", 0, DFOLDSW) \
53 X("draftmessage msg", 0, DMSGSW) \
54 X("nodraftfolder", 0, NDFLDSW) \
55 X("editor editor", 0, EDITRSW) \
56 X("noedit", 0, NEDITSW) \
57 X("prompt string", 4, PRMPTSW) \
58 X("version", 0, VERSIONSW) \
59 X("help", 0, HELPSW) \
62 #define X(sw, minchars, id) id,
63 DEFINE_SWITCH_ENUM(WHATNOW
);
66 #define X(sw, minchars, id) { sw, minchars, id },
67 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
71 * Options at the "whatnow" prompt
73 #define PROMPT_SWITCHES \
74 X("edit [<editor> <switches>]", 0, EDITSW) \
75 X("refile [<switches>] +folder", 0, REFILEOPT) \
76 X("mime [<switches>]", 0, BUILDMIMESW) \
77 X("display [<switches>]", 0, DISPSW) \
78 X("list [<switches>]", 0, LISTSW) \
79 X("send [<switches>]", 0, SENDSW) \
80 X("push [<switches>]", 0, PUSHSW) \
81 X("whom [<switches>]", 0, WHOMSW) \
82 X("quit [-delete]", 0, QUITSW) \
83 X("delete", 0, DELETESW) \
84 X("cd [directory]", 0, CDCMDSW) \
85 X("pwd", 0, PWDCMDSW) \
87 X("attach [-v]", 0, ATTACHCMDSW) \
88 X("detach [-n]", 0, DETACHCMDSW) \
89 X("alist [-ln] ", 2, ALISTCMDSW) \
91 #define X(sw, minchars, id) id,
92 DEFINE_SWITCH_ENUM(PROMPT
);
95 #define X(sw, minchars, id) { sw, minchars, id },
96 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
99 static char *myprompt
= "\nWhat now? ";
104 static int editfile (char **, char **, char *, int, struct msgs
*,
105 char *, char *, int, int);
106 static int sendfile (char **, char *, int);
107 static void sendit (char *, char **, char *, int);
108 static int buildfile (char **, char *);
109 static int whomfile (char **, char *);
110 static int removefile (char *);
111 static int checkmimeheader (char *);
112 static void writelscmd(char *, int, char *, char **);
113 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
114 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
115 static int system_in_dir(const char *dir
, const char *cmd
);
116 static int copyf (char *, char *);
120 WhatNow (int argc
, char **argv
)
122 int isdf
= 0, nedit
= 0, use
= 0, atfile
= 1;
123 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
124 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
125 char buf
[BUFSIZ
], prompt
[BUFSIZ
];
126 char **argp
, **arguments
;
128 char cwd
[PATH_MAX
+ 1]; /* current working directory */
129 char file
[PATH_MAX
+ 1]; /* file name buffer */
130 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
131 FILE *f
; /* read pointer for bgnd proc */
132 char *l
; /* set on -l to alist command */
133 int n
; /* set on -n to alist command */
135 /* Need this if called from what_now(). */
136 invo_name
= r1bindex (argv
[0], '/');
138 arguments
= getarguments (invo_name
, argc
, argv
, 1);
142 * Get the initial current working directory.
145 if (getcwd(cwd
, sizeof (cwd
)) == NULL
) {
146 adios("getcwd", "could not get working directory");
149 while ((cp
= *argp
++)) {
151 switch (smatch (++cp
, whatnowswitches
)) {
153 ambigsw (cp
, whatnowswitches
);
156 adios (NULL
, "-%s unknown", cp
);
159 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
160 print_help (buf
, whatnowswitches
, 1);
163 print_version(invo_name
);
168 adios (NULL
, "only one draft folder at a time!");
169 if (!(cp
= *argp
++) || *cp
== '-')
170 adios (NULL
, "missing argument to %s", argp
[-2]);
171 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
172 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
176 adios (NULL
, "only one draft message at a time!");
177 if (!(dmsg
= *argp
++) || *dmsg
== '-')
178 adios (NULL
, "missing argument to %s", argp
[-2]);
186 if (!(ed
= *argp
++) || *ed
== '-')
187 adios (NULL
, "missing argument to %s", argp
[-2]);
195 if (!(myprompt
= *argp
++) || *myprompt
== '-')
196 adios (NULL
, "missing argument to %s", argp
[-2]);
201 adios (NULL
, "only one draft at a time!");
206 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
207 drft
= getcpy (m_draft (dfolder
, dmsg
, 1, &isdf
));
209 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? mh_xstrdup(cp
) : NULL
;
211 if ((cp
= getenv ("mhatfile")) && *cp
)
214 if ((cp
= getenv ("mhuse")) && *cp
)
217 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
222 /* start editing the draft, unless -noedit was given */
223 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
224 NULL
, 1, atfile
) < 0)
227 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
229 #ifdef READLINE_SUPPORT
230 if (!(argp
= read_switch_multiword_via_readline (prompt
, aleqs
))) {
231 #else /* ! READLINE_SUPPORT */
232 if (!(argp
= read_switch_multiword (prompt
, aleqs
))) {
233 #endif /* READLINE_SUPPORT */
234 (void) m_unlink (LINK
);
237 switch (smatch (*argp
, aleqs
)) {
239 /* display the message being replied to, or distributed */
241 showfile (++argp
, msgnam
);
243 inform("no alternate message to display");
247 /* Translate MIME composition file */
248 buildfile (++argp
, drft
);
252 /* Call an editor on the draft file */
255 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
256 NULL
, 1, atfile
) == NOTOK
)
261 /* display the draft file */
262 showfile (++argp
, drft
);
266 /* Check to whom the draft would be sent */
267 whomfile (++argp
, drft
);
271 /* Quit, and possibly delete the draft */
272 if (*++argp
&& (*argp
[0] == 'd' ||
273 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
276 if (stat (drft
, &st
) != NOTOK
)
277 inform("draft left on %s", drft
);
282 /* Delete draft and exit */
287 /* Send draft in background */
288 if (sendfile (++argp
, drft
, 1))
294 sendfile (++argp
, drft
, 0);
298 /* Refile the draft */
299 if (refile (++argp
, drft
) == 0)
304 /* Change the working directory for attachments
306 * Run the directory through the user's shell so that
307 * we can take advantage of any syntax that the user
308 * is accustomed to. Read back the absolute path.
311 if (*(argp
+1) == NULL
) {
312 strcpy(buf
, "$SHELL -c \"cd&&pwd\"");
315 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
317 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
318 if (fgets(cwd
, sizeof (cwd
), f
) == NULL
) {
319 advise (buf
, "fgets");
321 trim_suffix_c(cwd
, '\n');
325 advise("popen", "could not get directory");
331 /* Print the working directory for attachments */
336 /* List files in the current attachment working directory
338 * Use the user's shell so that we can take advantage of any
339 * syntax that the user is accustomed to.
341 writelscmd(buf
, sizeof(buf
), "", argp
);
342 (void)system_in_dir(cwd
, buf
);
347 * List attachments on current draft. Options are:
349 * -l long listing (full path names)
353 if (checkmimeheader(drft
))
359 while (*++argp
!= NULL
) {
360 if (strcmp(*argp
, "-l") == 0)
363 else if (strcmp(*argp
, "-n") == 0)
366 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
378 inform("usage is alist [-ln].");
381 annolist(drft
, ATTACH_FIELD
, l
, n
);
387 * Attach files to current draft.
393 if (checkmimeheader(drft
))
396 for (ap
= argp
+1; *ap
; ++ap
) {
397 if (strcmp(*ap
, "-v") == 0) {
400 } else if (*ap
[0] != '-') {
405 if (*(argp
+1) == NULL
) {
406 inform("attach command requires file argument(s).");
411 * Build a command line that causes the user's shell to list the file name
412 * arguments. This handles and wildcard expansion, tilde expansion, etc.
414 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
417 * Read back the response from the shell, which contains a number of lines
418 * with one file name per line. Remove off the newline. Determine whether
419 * we have an absolute or relative path name. Prepend the current working
420 * directory to relative path names. Add the attachment annotation to the
424 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
425 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
428 trim_suffix_c(shell
, '\n');
431 strncpy(file
, shell
, sizeof(file
));
432 file
[sizeof(file
) - 1] = '\0';
434 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
437 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
439 ctype
= mime_type(file
);
440 printf ("Attaching %s as a %s\n", file
, ctype
);
448 advise("popen", "could not get file from shell");
455 * Detach files from current draft.
459 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
460 * so this catches a -n anywhere in the argument list.
463 if (checkmimeheader(drft
))
466 for (n
= 0, arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
467 if (strcmp(*arguments
, "-n") == 0) {
474 * A -n was found so interpret the arguments as attachment numbers.
475 * Decrement any remaining argument number that is greater than the one
476 * just processed after processing each one so that the numbering stays
481 for (arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
482 if (strcmp(*arguments
, "-n") == 0)
485 if (**arguments
!= '\0') {
486 n
= atoi(*arguments
);
487 annotate(drft
, ATTACH_FIELD
, NULL
, 1, 0, n
, 1);
489 for (argp
= arguments
+ 1; *argp
!= NULL
; argp
++) {
490 if (atoi(*argp
) > n
) {
491 if (atoi(*argp
) == 1)
494 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
502 * The arguments are interpreted as file names. Run them through the
503 * user's shell for wildcard expansion and other goodies. Do this from
504 * the current working directory if the argument is not an absolute path
505 * name (does not begin with a /).
507 * We feed all the file names to the shell at once, otherwise you can't
508 * provide a file name with a space in it.
510 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
511 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
512 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
513 trim_suffix_c(shell
, '\n');
514 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
518 advise("popen", "could not get file from shell");
524 /* Unknown command */
534 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
536 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
539 /* Note that we do not quote -- the argp from the user
540 * is assumed to be quoted as they desire. (We can't treat
541 * it as pure literal as that would prevent them using ~,
542 * wildcards, etc.) The buffer produced by this function
543 * should be given to popen_in_dir() or system_in_dir() so
544 * that the current working directory is set correctly.
546 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
547 /* NB that some snprintf() return -1 on overflow rather than the
548 * new C99 mandated 'number of chars that would have been written'
550 /* length checks here and inside the loop allow for the
551 * trailing "&&", trailcmd, '"' and NUL
553 int trailln
= strlen(trailcmd
) + 4;
554 if (ln
< 0 || ln
+ trailln
> bufsz
)
555 adios(NULL
, "arguments too long");
559 while (*argp
&& *++argp
) {
561 /* +1 for leading space */
562 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
563 adios(NULL
, "arguments too long");
565 memcpy(cp
, *argp
, ln
+1);
569 *cp
++ = '&'; *cp
++ = '&';
570 strcpy(cp
, trailcmd
);
578 * Build a command line that causes the user's shell to list the file name
579 * arguments. This handles and wildcard expansion, tilde expansion, etc.
582 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
584 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
585 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
589 /* Like system(), but run the command in directory dir.
590 * This assumes the program is single-threaded!
593 system_in_dir(const char *dir
, const char *cmd
)
598 /* ensure that $SHELL exists, as the cmd was written relying on
599 a non-blank $SHELL... */
600 setenv("SHELL","/bin/sh",0); /* don't overwrite */
602 if (getcwd(olddir
, sizeof(olddir
)) == 0)
603 adios("getcwd", "could not get working directory");
605 adios("chdir", "could not change working directory");
607 if (chdir(olddir
) != 0)
608 adios("chdir", "could not change working directory");
612 /* ditto for popen() */
614 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
619 /* ensure that $SHELL exists, as the cmd was written relying on
620 a non-blank $SHELL... */
621 setenv("SHELL","/bin/sh",0); /* don't overwrite */
623 if (getcwd(olddir
, sizeof(olddir
)) == 0)
624 adios("getcwd", "could not get working directory");
626 adios("chdir", "could not change working directory");
627 f
= popen(cmd
, type
);
628 if (chdir(olddir
) != 0)
629 adios("chdir", "could not change working directory");
638 static int reedit
= 0; /* have we been here before? */
639 static char *edsave
= NULL
; /* the editor we used previously */
643 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
644 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
646 int pid
, status
, vecp
;
647 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
648 char *cp
, *prog
, **vec
;
653 /* Was there a previous edit session? */
654 if (reedit
&& (*ed
|| edsave
)) {
655 if (!*ed
) { /* no explicit editor */
656 *ed
= edsave
; /* so use the previous one */
657 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
660 /* unless we've specified it via "editor-next" */
661 cp
= concat (cp
, "-next", NULL
);
662 if ((cp
= context_find (cp
)) != NULL
)
666 /* set initial editor */
668 *ed
= get_default_editor();
672 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
673 strncpy (altpath
, altmsg
, sizeof(altpath
));
675 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
677 strncpy (linkpath
, LINK
, sizeof(linkpath
));
679 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
682 (void) m_unlink (linkpath
);
683 if (link (altpath
, linkpath
) == NOTOK
) {
684 if (symlink (altpath
, linkpath
) < 0) {
685 adios (linkpath
, "symlink");
694 context_save (); /* save the context file */
697 switch (pid
= fork()) {
699 advise ("fork", "unable to");
705 if (chdir (cwd
) < 0) {
706 advise (cwd
, "chdir");
711 setenv("mhfolder", mp
->foldpath
, 1);
712 setenv("editalt", altpath
, 1);
715 vec
= argsplit(*ed
, &prog
, &vecp
);
719 vec
[vecp
++] = *arg
++;
724 fprintf (stderr
, "unable to exec ");
729 if ((status
= pidwait (pid
, NOTOK
))) {
730 if (((status
& 0xff00) != 0xff00)
731 && (!reedit
|| (status
& 0x00ff))) {
732 if (!use
&& (status
& 0xff00) &&
733 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
734 inform("problems with edit--draft left in %s", cp
);
736 inform("problems with edit--%s preserved", file
);
739 status
= -2; /* maybe "reedit ? -2 : -1"? */
748 ? lstat (linkpath
, &st
) != NOTOK
749 && S_ISREG(st
.st_mode
)
750 && copyf (linkpath
, altpath
) == NOTOK
751 : stat (linkpath
, &st
) != NOTOK
753 && (m_unlink (altpath
) == NOTOK
754 || link (linkpath
, altpath
) == NOTOK
)))
755 advise (linkpath
, "unable to update %s from", altmsg
);
758 /* normally, we remember which editor we used */
760 edsave
= getcpy (*ed
);
763 if (altmsg
&& atfile
)
764 (void) m_unlink (linkpath
);
771 copyf (char *ifile
, char *ofile
)
776 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
778 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
779 admonish (ofile
, "unable to open and truncate");
784 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
785 if (write (out
, buffer
, i
) != i
) {
786 advise (ofile
, "may have damaged");
802 sendfile (char **arg
, char *file
, int pushsw
)
806 char *cp
, *sp
, **vec
, *program
;
809 * If the sendproc is the nmh command `send', then we call
810 * those routines directly rather than exec'ing the command.
812 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
814 sendit (invo_name
= sp
, arg
, file
, pushsw
);
819 context_save (); /* save the context file */
822 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
826 inform("unable to fork, so sending directly...");
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
= (char **) 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
;
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
++] = getcpy (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]);
1139 #ifdef OAUTH_SUPPORT
1140 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
1141 adios (NULL
, "missing argument to %s", argp
[-2]);
1144 NMH_UNUSED (auth_svc
);
1145 adios (NULL
, "not built with OAuth support");
1163 if (!(cp
= *argp
++) || *cp
== '-') {
1164 inform("missing argument to %s", argp
[-2]);
1173 if (!(cp
= *argp
++) || *cp
== '-') {
1174 inform("missing argument to %s", argp
[-2]);
1182 inform("usage: %s [switches]", sp
);
1186 /* allow Aliasfile: profile entry */
1187 if ((cp
= context_find ("Aliasfile"))) {
1190 dp
= mh_xstrdup(cp
);
1191 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1192 vec
[vecp
++] = "-alias";
1197 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1198 if ((cp
= context_find ("signature")) && *cp
)
1199 setenv("SIGNATURE", cp
, 1);
1201 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1203 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1205 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1206 inplace
= atoi (cp
);
1208 if ((cp
= getenv ("mhdist"))
1211 && (distsw
= atoi (cp
))
1212 #endif /* not lint */
1214 vec
[vecp
++] = "-dist";
1215 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1216 adios(NULL
, "unable to create temporary file in %s",
1219 distfile
= mh_xstrdup(cp
);
1220 (void) m_unlink(distfile
);
1221 if (link (altmsg
, distfile
) == NOTOK
)
1222 adios (distfile
, "unable to link %s to", altmsg
);
1227 #ifdef OAUTH_SUPPORT
1228 if (auth_svc
== NULL
) {
1229 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
1230 adios (NULL
, "must specify -authservice with -saslmech xoauth2");
1234 adios (NULL
, "must specify -user with -saslmech xoauth2");
1238 NMH_UNUSED(saslmech
);
1239 #endif /* OAUTH_SUPPORT */
1241 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1246 if ((pushsw
= pushed
))
1251 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, auth_svc
) == OK
)
1260 whomfile (char **arg
, char *file
)
1264 char **vec
, *program
;
1266 context_save (); /* save the context file */
1269 switch (pid
= fork()) {
1271 advise ("fork", "unable to");
1275 vec
= argsplit(whomproc
, &program
, &vecp
);
1278 vec
[vecp
++] = *arg
++;
1282 execvp (program
, vec
);
1283 fprintf (stderr
, "unable to exec ");
1285 _exit (-1); /* NOTREACHED */
1288 return (pidwait (pid
, NOTOK
) & 0377 ? 1 : 0);
1294 * Remove the draft file
1298 removefile (char *drft
)
1300 if (m_unlink (drft
) == NOTOK
)
1301 adios (drft
, "unable to unlink");
1308 * Return 1 if we already have a MIME-Version header, 0 otherwise.
1312 checkmimeheader (char *drft
)
1315 m_getfld_state_t gstate
= 0;
1316 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1317 int state
, retval
= 0;
1319 if ((f
= fopen(drft
, "r")) == NULL
) {
1320 admonish(drft
, "unable to read draft");
1325 int bufsz
= sizeof(buf
);
1326 switch (state
= m_getfld(&gstate
, name
, buf
, &bufsz
, f
)) {
1329 if (strcasecmp(name
, VRSN_FIELD
) == 0) {
1330 inform("Cannot use attach commands with already-"
1331 "formatted MIME message \"%s\"", drft
);
1342 m_getfld_state_destroy(&gstate
);