]>
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!");
205 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
206 drft
= getcpy (m_draft (dfolder
, dmsg
, 1, &isdf
));
208 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? mh_xstrdup(cp
) : NULL
;
210 if ((cp
= getenv ("mhatfile")) && *cp
)
213 if ((cp
= getenv ("mhuse")) && *cp
)
216 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
221 /* start editing the draft, unless -noedit was given */
222 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
223 NULL
, 1, atfile
) < 0)
226 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
228 #ifdef READLINE_SUPPORT
229 if (!(argp
= read_switch_multiword_via_readline (prompt
, aleqs
))) {
230 #else /* ! READLINE_SUPPORT */
231 if (!(argp
= read_switch_multiword (prompt
, aleqs
))) {
232 #endif /* READLINE_SUPPORT */
233 (void) m_unlink (LINK
);
236 switch (smatch (*argp
, aleqs
)) {
238 /* display the message being replied to, or distributed */
240 showfile (++argp
, msgnam
);
242 inform("no alternate message to display");
246 /* Translate MIME composition file */
247 buildfile (++argp
, drft
);
251 /* Call an editor on the draft file */
254 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
255 NULL
, 1, atfile
) == NOTOK
)
260 /* display the draft file */
261 showfile (++argp
, drft
);
265 /* Check to whom the draft would be sent */
266 whomfile (++argp
, drft
);
270 /* Quit, and possibly delete the draft */
271 if (*++argp
&& (*argp
[0] == 'd' ||
272 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
275 if (stat (drft
, &st
) != NOTOK
)
276 inform("draft left on %s", drft
);
281 /* Delete draft and exit */
286 /* Send draft in background */
287 if (sendfile (++argp
, drft
, 1))
293 sendfile (++argp
, drft
, 0);
297 /* Refile the draft */
298 if (refile (++argp
, drft
) == 0)
303 /* Change the working directory for attachments
305 * Run the directory through the user's shell so that
306 * we can take advantage of any syntax that the user
307 * is accustomed to. Read back the absolute path.
310 if (*(argp
+1) == NULL
) {
311 strcpy(buf
, "$SHELL -c \"cd&&pwd\"");
314 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
316 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
317 if (fgets(cwd
, sizeof (cwd
), f
) == NULL
) {
318 advise (buf
, "fgets");
320 trim_suffix_c(cwd
, '\n');
324 advise("popen", "could not get directory");
330 /* Print the working directory for attachments */
335 /* List files in the current attachment working directory
337 * Use the user's shell so that we can take advantage of any
338 * syntax that the user is accustomed to.
340 writelscmd(buf
, sizeof(buf
), "", argp
);
341 (void)system_in_dir(cwd
, buf
);
346 * List attachments on current draft. Options are:
348 * -l long listing (full path names)
352 if (checkmimeheader(drft
))
358 while (*++argp
!= NULL
) {
359 if (strcmp(*argp
, "-l") == 0)
362 else if (strcmp(*argp
, "-n") == 0)
365 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
377 inform("usage is alist [-ln].");
380 annolist(drft
, ATTACH_FIELD
, l
, n
);
386 * Attach files to current draft.
392 if (checkmimeheader(drft
))
395 for (ap
= argp
+1; *ap
; ++ap
) {
396 if (strcmp(*ap
, "-v") == 0) {
399 } else if (*ap
[0] != '-') {
404 if (*(argp
+1) == NULL
) {
405 inform("attach command requires file argument(s).");
410 * Build a command line that causes the user's shell to list the file name
411 * arguments. This handles and wildcard expansion, tilde expansion, etc.
413 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
416 * Read back the response from the shell, which contains a number of lines
417 * with one file name per line. Remove off the newline. Determine whether
418 * we have an absolute or relative path name. Prepend the current working
419 * directory to relative path names. Add the attachment annotation to the
423 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
424 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
427 trim_suffix_c(shell
, '\n');
430 strncpy(file
, shell
, sizeof(file
));
431 file
[sizeof(file
) - 1] = '\0';
433 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
436 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
438 ctype
= mime_type(file
);
439 printf ("Attaching %s as a %s\n", file
, ctype
);
447 advise("popen", "could not get file from shell");
454 * Detach files from current draft.
458 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
459 * so this catches a -n anywhere in the argument list.
462 if (checkmimeheader(drft
))
465 for (n
= 0, arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
466 if (strcmp(*arguments
, "-n") == 0) {
473 * A -n was found so interpret the arguments as attachment numbers.
474 * Decrement any remaining argument number that is greater than the one
475 * just processed after processing each one so that the numbering stays
480 for (arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
481 if (strcmp(*arguments
, "-n") == 0)
484 if (**arguments
!= '\0') {
485 n
= atoi(*arguments
);
486 annotate(drft
, ATTACH_FIELD
, NULL
, 1, 0, n
, 1);
488 for (argp
= arguments
+ 1; *argp
!= NULL
; argp
++) {
489 if (atoi(*argp
) > n
) {
490 if (atoi(*argp
) == 1)
493 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
501 * The arguments are interpreted as file names. Run them through the
502 * user's shell for wildcard expansion and other goodies. Do this from
503 * the current working directory if the argument is not an absolute path
504 * name (does not begin with a /).
506 * We feed all the file names to the shell at once, otherwise you can't
507 * provide a file name with a space in it.
509 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
510 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
511 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
512 trim_suffix_c(shell
, '\n');
513 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
517 advise("popen", "could not get file from shell");
523 /* Unknown command */
533 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
535 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
538 /* Note that we do not quote -- the argp from the user
539 * is assumed to be quoted as they desire. (We can't treat
540 * it as pure literal as that would prevent them using ~,
541 * wildcards, etc.) The buffer produced by this function
542 * should be given to popen_in_dir() or system_in_dir() so
543 * that the current working directory is set correctly.
545 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
546 /* NB that some snprintf() return -1 on overflow rather than the
547 * new C99 mandated 'number of chars that would have been written'
549 /* length checks here and inside the loop allow for the
550 * trailing "&&", trailcmd, '"' and NUL
552 int trailln
= strlen(trailcmd
) + 4;
553 if (ln
< 0 || ln
+ trailln
> bufsz
)
554 adios(NULL
, "arguments too long");
558 while (*argp
&& *++argp
) {
560 /* +1 for leading space */
561 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
562 adios(NULL
, "arguments too long");
564 memcpy(cp
, *argp
, ln
+1);
568 *cp
++ = '&'; *cp
++ = '&';
569 strcpy(cp
, trailcmd
);
577 * Build a command line that causes the user's shell to list the file name
578 * arguments. This handles and wildcard expansion, tilde expansion, etc.
581 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
583 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
584 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
588 /* Like system(), but run the command in directory dir.
589 * This assumes the program is single-threaded!
592 system_in_dir(const char *dir
, const char *cmd
)
597 /* ensure that $SHELL exists, as the cmd was written relying on
598 a non-blank $SHELL... */
599 setenv("SHELL","/bin/sh",0); /* don't overwrite */
601 if (getcwd(olddir
, sizeof(olddir
)) == 0)
602 adios("getcwd", "could not get working directory");
604 adios("chdir", "could not change working directory");
606 if (chdir(olddir
) != 0)
607 adios("chdir", "could not change working directory");
611 /* ditto for popen() */
613 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
618 /* ensure that $SHELL exists, as the cmd was written relying on
619 a non-blank $SHELL... */
620 setenv("SHELL","/bin/sh",0); /* don't overwrite */
622 if (getcwd(olddir
, sizeof(olddir
)) == 0)
623 adios("getcwd", "could not get working directory");
625 adios("chdir", "could not change working directory");
626 f
= popen(cmd
, type
);
627 if (chdir(olddir
) != 0)
628 adios("chdir", "could not change working directory");
637 static int reedit
= 0; /* have we been here before? */
638 static char *edsave
= NULL
; /* the editor we used previously */
642 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
643 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
645 int pid
, status
, vecp
;
646 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
647 char *cp
, *prog
, **vec
;
652 /* Was there a previous edit session? */
653 if (reedit
&& (*ed
|| edsave
)) {
654 if (!*ed
) { /* no explicit editor */
655 *ed
= edsave
; /* so use the previous one */
656 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
659 /* unless we've specified it via "editor-next" */
660 cp
= concat (cp
, "-next", NULL
);
661 if ((cp
= context_find (cp
)) != NULL
)
665 /* set initial editor */
667 *ed
= get_default_editor();
671 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
672 strncpy (altpath
, altmsg
, sizeof(altpath
));
674 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
676 strncpy (linkpath
, LINK
, sizeof(linkpath
));
678 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
681 (void) m_unlink (linkpath
);
682 if (link (altpath
, linkpath
) == NOTOK
) {
683 if (symlink (altpath
, linkpath
) < 0) {
684 adios (linkpath
, "symlink");
693 context_save (); /* save the context file */
696 switch (pid
= fork()) {
698 advise ("fork", "unable to");
704 if (chdir (cwd
) < 0) {
705 advise (cwd
, "chdir");
710 setenv("mhfolder", mp
->foldpath
, 1);
711 setenv("editalt", altpath
, 1);
714 vec
= argsplit(*ed
, &prog
, &vecp
);
718 vec
[vecp
++] = *arg
++;
723 fprintf (stderr
, "unable to exec ");
728 if ((status
= pidwait (pid
, NOTOK
))) {
729 if (((status
& 0xff00) != 0xff00)
730 && (!reedit
|| (status
& 0x00ff))) {
731 if (!use
&& (status
& 0xff00) &&
732 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
733 inform("problems with edit--draft left in %s", cp
);
735 inform("problems with edit--%s preserved", file
);
738 status
= -2; /* maybe "reedit ? -2 : -1"? */
747 ? lstat (linkpath
, &st
) != NOTOK
748 && S_ISREG(st
.st_mode
)
749 && copyf (linkpath
, altpath
) == NOTOK
750 : stat (linkpath
, &st
) != NOTOK
752 && (m_unlink (altpath
) == NOTOK
753 || link (linkpath
, altpath
) == NOTOK
)))
754 advise (linkpath
, "unable to update %s from", altmsg
);
757 /* normally, we remember which editor we used */
759 edsave
= getcpy (*ed
);
762 if (altmsg
&& atfile
)
763 (void) m_unlink (linkpath
);
770 copyf (char *ifile
, char *ofile
)
775 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
777 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
778 admonish (ofile
, "unable to open and truncate");
783 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
784 if (write (out
, buffer
, i
) != i
) {
785 advise (ofile
, "may have damaged");
801 sendfile (char **arg
, char *file
, int pushsw
)
805 char *cp
, *sp
, **vec
, *program
;
808 * If the sendproc is the nmh command `send', then we call
809 * those routines directly rather than exec'ing the command.
811 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
813 sendit (invo_name
= sp
, arg
, file
, pushsw
);
818 context_save (); /* save the context file */
824 inform("unable to fork, so sending directly...");
827 vec
= argsplit(sendproc
, &program
, &vecp
);
829 vec
[vecp
++] = "-push";
832 vec
[vecp
++] = *arg
++;
836 execvp (program
, vec
);
837 fprintf (stderr
, "unable to exec ");
842 if (pidwait(child_id
, OK
) == 0)
850 * Translate MIME composition file (call buildmimeproc)
854 buildfile (char **argp
, char *file
)
861 /* allocate space for arguments */
867 args
= (char **) mh_xmalloc((i
+ 2) * sizeof(char *));
870 * For backward compatibility, we need to add -build
871 * if we are using mhn as buildmimeproc
874 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
875 args
[i
++] = "-build";
877 /* copy any other arguments */
878 while (argp
&& *argp
)
882 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
885 return (i
? NOTOK
: OK
);
890 # define SASLminc(a) (a)
891 #else /* CYRUS_SASL */
892 # define SASLminc(a) 0
893 #endif /* CYRUS_SASL */
896 # define TLSminc(a) (a)
897 #else /* TLS_SUPPORT */
898 # define TLSminc(a) 0
899 #endif /* TLS_SUPPORT */
901 #define SEND_SWITCHES \
902 X("alias aliasfile", 0, ALIASW) \
903 X("debug", -5, DEBUGSW) \
904 X("filter filterfile", 0, FILTSW) \
905 X("nofilter", 0, NFILTSW) \
906 X("format", 0, FRMTSW) \
907 X("noformat", 0, NFRMTSW) \
908 X("forward", 0, FORWSW) \
909 X("noforward", 0, NFORWSW) \
910 X("mime", 0, MIMESW) \
911 X("nomime", 0, NMIMESW) \
912 X("msgid", 0, MSGDSW) \
913 X("nomsgid", 0, NMSGDSW) \
914 X("push", 0, SPSHSW) \
915 X("nopush", 0, NSPSHSW) \
916 X("split seconds", 0, SPLITSW) \
917 X("unique", -6, UNIQSW) \
918 X("nounique", -8, NUNIQSW) \
919 X("verbose", 0, VERBSW) \
920 X("noverbose", 0, NVERBSW) \
921 X("watch", 0, WATCSW) \
922 X("nowatch", 0, NWATCSW) \
923 X("width columns", 0, WIDTHSW) \
924 X("version", 0, SVERSIONSW) \
925 X("help", 0, SHELPSW) \
926 X("dashstuffing", -12, BITSTUFFSW) \
927 X("nodashstuffing", -14, NBITSTUFFSW) \
928 X("client host", -6, CLIESW) \
929 X("server host", 6, SERVSW) \
930 X("snoop", -5, SNOOPSW) \
931 X("draftfolder +folder", 0, SDRFSW) \
932 X("draftmessage msg", 0, SDRMSW) \
933 X("nodraftfolder", 0, SNDRFSW) \
934 X("sasl", SASLminc(4), SASLSW) \
935 X("nosasl", SASLminc(6), NOSASLSW) \
936 X("saslmech", SASLminc(5), SASLMECHSW) \
937 X("authservice", SASLminc(0), AUTHSERVICESW) \
938 X("user username", SASLminc(4), USERSW) \
939 X("port server-port-name/number", 4, PORTSW) \
940 X("tls", TLSminc(-3), TLSSW) \
941 X("initialtls", TLSminc(-10), INITTLSSW) \
942 X("notls", TLSminc(-5), NTLSSW) \
943 X("certverify", TLSminc(-10), CERTVERSW) \
944 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
945 X("sendmail program", 0, MTSSM) \
946 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
947 X("messageid localname|random", 2, MESSAGEIDSW) \
949 #define X(sw, minchars, id) id,
950 DEFINE_SWITCH_ENUM(SEND
);
953 #define X(sw, minchars, id) { sw, minchars, id },
954 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
958 extern int debugsw
; /* from sendsbr.c */
966 extern char *altmsg
; /* .. */
967 extern char *annotext
;
968 extern char *distfile
;
972 sendit (char *sp
, char **arg
, char *file
, int pushed
)
975 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
976 char **arguments
, *savearg
[MAXARGS
], **vec
;
977 const char *user
= NULL
, *saslmech
= NULL
;
978 char *auth_svc
= NULL
;
987 * Make sure these are defined. In particular, we need
988 * savearg[1] to be NULL, in case "arg" is NULL below. It
989 * doesn't matter what is the value of savearg[0], but we
990 * set it to NULL, to help catch "off-by-one" errors.
996 * Temporarily copy arg to savearg, since the brkstring() call in
997 * getarguments() will wipe it out before it is merged in.
998 * Also, we skip the first element of savearg, since getarguments()
999 * skips it. Then we count the number of arguments
1000 * copied. The value of "n" will be one greater than
1001 * this in order to simulate the standard argc/argv.
1006 copyip (arg
, savearg
+1, MAXARGS
-1);
1013 * Merge any arguments from command line (now in savearg)
1014 * and arguments from profile.
1016 arguments
= getarguments (sp
, n
, savearg
, 1);
1029 * Get our initial arguments for postproc now
1032 vec
= argsplit(postproc
, &program
, &vecp
);
1034 vec
[vecp
++] = "-library";
1035 vec
[vecp
++] = getcpy (m_maildir (""));
1037 if ((cp
= context_find ("fileproc"))) {
1038 vec
[vecp
++] = "-fileproc";
1042 if ((cp
= context_find ("mhlproc"))) {
1043 vec
[vecp
++] = "-mhlproc";
1047 if ((cp
= context_find ("credentials"))) {
1048 /* post doesn't read context so need to pass credentials. */
1049 vec
[vecp
++] = "-credentials";
1053 while ((cp
= *argp
++)) {
1055 switch (smatch (++cp
, sendswitches
)) {
1057 ambigsw (cp
, sendswitches
);
1060 inform("-%s unknown\n", cp
);
1064 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1065 print_help (buf
, sendswitches
, 1);
1068 print_version (invo_name
);
1079 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1080 inform("missing argument to %s", argp
[-2]);
1137 #ifdef OAUTH_SUPPORT
1138 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
1139 adios (NULL
, "missing argument to %s", argp
[-2]);
1142 NMH_UNUSED (auth_svc
);
1143 adios (NULL
, "not built with OAuth support");
1161 if (!(cp
= *argp
++) || *cp
== '-') {
1162 inform("missing argument to %s", argp
[-2]);
1171 if (!(cp
= *argp
++) || *cp
== '-') {
1172 inform("missing argument to %s", argp
[-2]);
1180 inform("usage: %s [switches]", sp
);
1184 /* allow Aliasfile: profile entry */
1185 if ((cp
= context_find ("Aliasfile"))) {
1188 dp
= mh_xstrdup(cp
);
1189 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1190 vec
[vecp
++] = "-alias";
1195 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1196 if ((cp
= context_find ("signature")) && *cp
)
1197 setenv("SIGNATURE", cp
, 1);
1199 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1201 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1203 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1204 inplace
= atoi (cp
);
1206 if ((cp
= getenv ("mhdist"))
1209 && (distsw
= atoi (cp
))
1210 #endif /* not lint */
1212 vec
[vecp
++] = "-dist";
1213 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1214 adios(NULL
, "unable to create temporary file in %s",
1217 distfile
= mh_xstrdup(cp
);
1218 (void) m_unlink(distfile
);
1219 if (link (altmsg
, distfile
) == NOTOK
)
1220 adios (distfile
, "unable to link %s to", altmsg
);
1225 #ifdef OAUTH_SUPPORT
1226 if (auth_svc
== NULL
) {
1227 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
1228 adios (NULL
, "must specify -authservice with -saslmech xoauth2");
1232 adios (NULL
, "must specify -user with -saslmech xoauth2");
1236 NMH_UNUSED(saslmech
);
1237 #endif /* OAUTH_SUPPORT */
1239 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1244 if ((pushsw
= pushed
))
1249 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, auth_svc
) == OK
)
1258 whomfile (char **arg
, char *file
)
1262 char **vec
, *program
;
1264 context_save (); /* save the context file */
1267 switch (pid
= fork()) {
1269 advise ("fork", "unable to");
1273 vec
= argsplit(whomproc
, &program
, &vecp
);
1276 vec
[vecp
++] = *arg
++;
1280 execvp (program
, vec
);
1281 fprintf (stderr
, "unable to exec ");
1283 _exit (-1); /* NOTREACHED */
1286 return (pidwait (pid
, NOTOK
) & 0377 ? 1 : 0);
1292 * Remove the draft file
1296 removefile (char *drft
)
1298 if (m_unlink (drft
) == NOTOK
)
1299 adios (drft
, "unable to unlink");
1306 * Return 1 if we already have a MIME-Version header, 0 otherwise.
1310 checkmimeheader (char *drft
)
1313 m_getfld_state_t gstate
;
1314 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1315 int state
, retval
= 0;
1317 if ((f
= fopen(drft
, "r")) == NULL
) {
1318 admonish(drft
, "unable to read draft");
1322 gstate
= m_getfld_state_init(f
);
1324 int bufsz
= sizeof(buf
);
1325 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1328 if (strcasecmp(name
, VRSN_FIELD
) == 0) {
1329 inform("Cannot use attach commands with already-"
1330 "formatted MIME message \"%s\"", drft
);
1341 m_getfld_state_destroy(&gstate
);