]>
diplodocus.org Git - nmh/blob - uip/whatnowsbr.c
ffbcfbf008393d73bc7b9118558a123a09f2f5f9
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/geteditor.h"
42 #include "sbr/context_save.h"
43 #include "sbr/context_find.h"
44 #include "sbr/brkstring.h"
45 #include "sbr/ambigsw.h"
48 #include "sbr/print_version.h"
49 #include "sbr/print_help.h"
50 #include "sbr/arglist.h"
51 #include "sbr/error.h"
59 #include "sbr/m_maildir.h"
60 #include "sbr/m_mktemp.h"
61 #include "sbr/mime_type.h"
63 #define WHATNOW_SWITCHES \
64 X("draftfolder +folder", 0, DFOLDSW) \
65 X("draftmessage msg", 0, DMSGSW) \
66 X("nodraftfolder", 0, NDFLDSW) \
67 X("editor editor", 0, EDITRSW) \
68 X("noedit", 0, NEDITSW) \
69 X("prompt string", 4, PRMPTSW) \
70 X("version", 0, VERSIONSW) \
71 X("help", 0, HELPSW) \
74 #define X(sw, minchars, id) id,
75 DEFINE_SWITCH_ENUM(WHATNOW
);
78 #define X(sw, minchars, id) { sw, minchars, id },
79 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
83 * Options at the "whatnow" prompt
85 #define PROMPT_SWITCHES \
86 X("edit [<editor> <switches>]", 0, EDITSW) \
87 X("refile [<switches>] +folder", 0, REFILEOPT) \
88 X("mime [<switches>]", 0, BUILDMIMESW) \
89 X("display [<switches>]", 0, DISPSW) \
90 X("list [<switches>]", 0, LISTSW) \
91 X("send [<switches>]", 0, SENDSW) \
92 X("push [<switches>]", 0, PUSHSW) \
93 X("whom [<switches>]", 0, WHOMSW) \
94 X("quit [-delete]", 0, QUITSW) \
95 X("delete", 0, DELETESW) \
96 X("cd [directory]", 0, CDCMDSW) \
97 X("pwd", 0, PWDCMDSW) \
99 X("attach [-v]", 0, ATTACHCMDSW) \
100 X("detach [-n]", 0, DETACHCMDSW) \
101 X("alist [-ln] ", 2, ALISTCMDSW) \
103 #define X(sw, minchars, id) id,
104 DEFINE_SWITCH_ENUM(PROMPT
);
107 #define X(sw, minchars, id) { sw, minchars, id },
108 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
111 static char *myprompt
= "\nWhat now? ";
116 static int editfile (char **, char **, char *, int, struct msgs
*,
117 char *, char *, int, int);
118 static int sendfile (char **, char *, int);
119 static void sendit (char *, char **, char *, int);
120 static int buildfile (char **, char *);
121 static int whomfile (char **, char *);
122 static int removefile (char *);
123 static int checkmimeheader (char *);
124 static void writelscmd(char *, int, char *, char **);
125 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
126 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
127 static int system_in_dir(const char *dir
, const char *cmd
);
128 static int copyf (char *, char *);
132 WhatNow (int argc
, char **argv
)
136 int use
= 0, atfile
= 1;
137 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
138 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
140 char **argp
, **arguments
;
142 char cwd
[PATH_MAX
+ 1]; /* current working directory */
143 char file
[PATH_MAX
+ 1]; /* file name buffer */
144 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
145 FILE *f
; /* read pointer for bgnd proc */
146 char *l
; /* set on -l to alist command */
147 int n
; /* set on -n to alist command */
149 /* Need this if called from what_now(). */
150 invo_name
= r1bindex (argv
[0], '/');
152 arguments
= getarguments (invo_name
, argc
, argv
, 1);
156 * Get the initial current working directory.
159 if (getcwd(cwd
, sizeof (cwd
)) == NULL
) {
160 adios("getcwd", "could not get working directory");
163 while ((cp
= *argp
++)) {
165 switch (smatch (++cp
, whatnowswitches
)) {
167 ambigsw (cp
, whatnowswitches
);
170 die("-%s unknown", cp
);
173 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
174 print_help (buf
, whatnowswitches
, 1);
177 print_version(invo_name
);
182 die("only one draft folder at a time!");
183 if (!(cp
= *argp
++) || *cp
== '-')
184 die("missing argument to %s", argp
[-2]);
185 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
186 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
190 die("only one draft message at a time!");
191 if (!(dmsg
= *argp
++) || *dmsg
== '-')
192 die("missing argument to %s", argp
[-2]);
200 if (!(ed
= *argp
++) || *ed
== '-')
201 die("missing argument to %s", argp
[-2]);
209 if (!(myprompt
= *argp
++) || *myprompt
== '-')
210 die("missing argument to %s", argp
[-2]);
215 die("only one draft at a time!");
219 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
220 drft
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
222 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? mh_xstrdup(cp
) : NULL
;
224 if ((cp
= getenv ("mhatfile")) && *cp
)
227 if ((cp
= getenv ("mhuse")) && *cp
)
230 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
235 /* start editing the draft, unless -noedit was given */
236 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
237 NULL
, 1, atfile
) < 0)
241 #ifdef READLINE_SUPPORT
242 if (!(argp
= read_switch_multiword_via_readline(myprompt
, aleqs
))) {
243 #else /* ! READLINE_SUPPORT */
244 if (!(argp
= read_switch_multiword(myprompt
, aleqs
))) {
245 #endif /* READLINE_SUPPORT */
246 (void) m_unlink (LINK
);
249 switch (smatch (*argp
, aleqs
)) {
251 /* display the message being replied to, or distributed */
253 showfile (++argp
, msgnam
);
255 inform("no alternate message to display");
259 /* Translate MIME composition file */
260 buildfile (++argp
, drft
);
264 /* Call an editor on the draft file */
267 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
268 NULL
, 1, atfile
) == NOTOK
)
273 /* display the draft file */
274 showfile (++argp
, drft
);
278 /* Check to whom the draft would be sent */
279 whomfile (++argp
, drft
);
283 /* Quit, and possibly delete the draft */
284 if (*++argp
&& (*argp
[0] == 'd' ||
285 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
288 if (stat (drft
, &st
) != NOTOK
)
289 inform("draft left on %s", drft
);
294 /* Delete draft and exit */
299 /* Send draft in background */
300 if (sendfile (++argp
, drft
, 1))
306 sendfile (++argp
, drft
, 0);
310 /* Refile the draft */
311 if (refile (++argp
, drft
) == 0)
316 /* Change the working directory for attachments
318 * Run the directory through the user's shell so that
319 * we can take advantage of any syntax that the user
320 * is accustomed to. Read back the absolute path.
323 if (*(argp
+1) == NULL
) {
324 strcpy(buf
, "$SHELL -c \"cd&&pwd\"");
327 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
329 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
330 if (fgets(cwd
, sizeof (cwd
), f
) == NULL
) {
331 advise (buf
, "fgets");
333 trim_suffix_c(cwd
, '\n');
337 advise("popen", "could not get directory");
343 /* Print the working directory for attachments */
348 /* List files in the current attachment working directory
350 * Use the user's shell so that we can take advantage of any
351 * syntax that the user is accustomed to.
353 writelscmd(buf
, sizeof(buf
), "", argp
);
354 (void)system_in_dir(cwd
, buf
);
359 * List attachments on current draft. Options are:
361 * -l long listing (full path names)
365 if (checkmimeheader(drft
))
371 while (*++argp
!= NULL
) {
372 if (strcmp(*argp
, "-l") == 0)
375 else if (strcmp(*argp
, "-n") == 0)
378 else if (strcmp(*argp
, "-ln") == 0 || strcmp(*argp
, "-nl") == 0) {
390 inform("usage is alist [-ln].");
393 annolist(drft
, ATTACH_FIELD
, l
, n
);
399 * Attach files to current draft.
402 bool verbose
= false;
405 if (checkmimeheader(drft
))
408 for (ap
= argp
+1; *ap
; ++ap
) {
409 if (strcmp(*ap
, "-v") == 0) {
412 } else if (*ap
[0] != '-') {
417 if (*(argp
+1) == NULL
) {
418 inform("attach command requires file argument(s).");
423 * Build a command line that causes the user's shell to list the file name
424 * arguments. This handles and wildcard expansion, tilde expansion, etc.
426 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
429 * Read back the response from the shell, which contains a number of lines
430 * with one file name per line. Remove off the newline. Determine whether
431 * we have an absolute or relative path name. Prepend the current working
432 * directory to relative path names. Add the attachment annotation to the
436 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
437 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
440 trim_suffix_c(shell
, '\n');
443 strncpy(file
, shell
, sizeof(file
));
444 file
[sizeof(file
) - 1] = '\0';
446 snprintf(file
, sizeof(file
), "%s/%s", cwd
, shell
);
449 annotate(drft
, ATTACH_FIELD
, file
, 1, 0, -2, 1);
451 ctype
= mime_type(file
);
452 printf ("Attaching %s as a %s\n", file
, ctype
);
460 advise("popen", "could not get file from shell");
467 * Detach files from current draft.
471 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
472 * so this catches a -n anywhere in the argument list.
475 if (checkmimeheader(drft
))
478 for (n
= 0, arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
479 if (strcmp(*arguments
, "-n") == 0) {
486 * A -n was found so interpret the arguments as attachment numbers.
487 * Decrement any remaining argument number that is greater than the one
488 * just processed after processing each one so that the numbering stays
493 for (arguments
= argp
+ 1; *arguments
!= NULL
; arguments
++) {
494 if (strcmp(*arguments
, "-n") == 0)
497 if (**arguments
!= '\0') {
498 n
= atoi(*arguments
);
499 annotate(drft
, ATTACH_FIELD
, NULL
, 1, 0, n
, 1);
501 for (argp
= arguments
+ 1; *argp
!= NULL
; argp
++) {
502 if (atoi(*argp
) > n
) {
503 if (atoi(*argp
) == 1)
506 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
514 * The arguments are interpreted as file names. Run them through the
515 * user's shell for wildcard expansion and other goodies. Do this from
516 * the current working directory if the argument is not an absolute path
517 * name (does not begin with a /).
519 * We feed all the file names to the shell at once, otherwise you can't
520 * provide a file name with a space in it.
522 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
523 if ((f
= popen_in_dir(cwd
, buf
, "r")) != NULL
) {
524 while (fgets(shell
, sizeof (shell
), f
) != NULL
) {
525 trim_suffix_c(shell
, '\n');
526 annotate(drft
, ATTACH_FIELD
, shell
, 1, 0, 0, 1);
530 advise("popen", "could not get file from shell");
536 /* Unknown command */
546 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
548 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
551 /* Note that we do not quote -- the argp from the user
552 * is assumed to be quoted as they desire. (We can't treat
553 * it as pure literal as that would prevent them using ~,
554 * wildcards, etc.) The buffer produced by this function
555 * should be given to popen_in_dir() or system_in_dir() so
556 * that the current working directory is set correctly.
558 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
559 /* NB that some snprintf() return -1 on overflow rather than the
560 * new C99 mandated 'number of chars that would have been written'
562 /* length checks here and inside the loop allow for the
563 * trailing "&&", trailcmd, '"' and NUL
565 int trailln
= strlen(trailcmd
) + 4;
566 if (ln
< 0 || ln
+ trailln
> bufsz
)
567 die("arguments too long");
571 while (*argp
&& *++argp
) {
573 /* +1 for leading space */
574 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
575 die("arguments too long");
577 memcpy(cp
, *argp
, ln
+1);
581 *cp
++ = '&'; *cp
++ = '&';
582 strcpy(cp
, trailcmd
);
590 * Build a command line that causes the user's shell to list the file name
591 * arguments. This handles and wildcard expansion, tilde expansion, etc.
594 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
596 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
597 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
601 /* Like system(), but run the command in directory dir.
602 * This assumes the program is single-threaded!
605 system_in_dir(const char *dir
, const char *cmd
)
610 /* ensure that $SHELL exists, as the cmd was written relying on
611 a non-blank $SHELL... */
612 setenv("SHELL","/bin/sh",0); /* don't overwrite */
614 if (getcwd(olddir
, sizeof(olddir
)) == 0)
615 adios("getcwd", "could not get working directory");
617 adios("chdir", "could not change working directory");
619 if (chdir(olddir
) != 0)
620 adios("chdir", "could not change working directory");
624 /* ditto for popen() */
626 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
631 /* ensure that $SHELL exists, as the cmd was written relying on
632 a non-blank $SHELL... */
633 setenv("SHELL","/bin/sh",0); /* don't overwrite */
635 if (getcwd(olddir
, sizeof(olddir
)) == 0)
636 adios("getcwd", "could not get working directory");
638 adios("chdir", "could not change working directory");
639 f
= popen(cmd
, type
);
640 if (chdir(olddir
) != 0)
641 adios("chdir", "could not change working directory");
650 static bool reedit
; /* have we been here before? */
651 static char *edsave
= NULL
; /* the editor we used previously */
655 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
656 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
658 int pid
, status
, vecp
;
659 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
660 char *cp
, *prog
, **vec
;
663 bool slinked
= false;
665 /* Was there a previous edit session? */
666 if (reedit
&& (*ed
|| edsave
)) {
667 if (!*ed
) { /* no explicit editor */
668 *ed
= edsave
; /* so use the previous one */
669 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
672 /* unless we've specified it via "editor-next" */
673 cp
= concat (cp
, "-next", NULL
);
674 if ((cp
= context_find (cp
)) != NULL
)
678 /* set initial editor */
680 *ed
= get_default_editor();
684 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
685 strncpy (altpath
, altmsg
, sizeof(altpath
));
687 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
689 strncpy (linkpath
, LINK
, sizeof(linkpath
));
691 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
694 (void) m_unlink (linkpath
);
695 if (link (altpath
, linkpath
) == NOTOK
) {
696 if (symlink (altpath
, linkpath
) < 0) {
697 adios (linkpath
, "symlink");
706 context_save (); /* save the context file */
709 switch (pid
= fork()) {
711 advise ("fork", "unable to");
717 if (chdir (cwd
) < 0) {
718 advise (cwd
, "chdir");
723 setenv("mhfolder", mp
->foldpath
, 1);
724 setenv("editalt", altpath
, 1);
727 vec
= argsplit(*ed
, &prog
, &vecp
);
731 vec
[vecp
++] = *arg
++;
736 fprintf (stderr
, "unable to exec ");
741 if ((status
= pidwait (pid
, NOTOK
))) {
742 if (((status
& 0xff00) != 0xff00)
743 && (!reedit
|| (status
& 0x00ff))) {
744 if (!use
&& (status
& 0xff00) &&
745 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
746 inform("problems with edit--draft left in %s", cp
);
748 inform("problems with edit--%s preserved", file
);
751 status
= -2; /* maybe "reedit ? -2 : -1"? */
760 ? lstat (linkpath
, &st
) != NOTOK
761 && S_ISREG(st
.st_mode
)
762 && copyf (linkpath
, altpath
) == NOTOK
763 : stat (linkpath
, &st
) != NOTOK
765 && (m_unlink (altpath
) == NOTOK
766 || link (linkpath
, altpath
) == NOTOK
)))
767 advise (linkpath
, "unable to update %s from", altmsg
);
770 /* normally, we remember which editor we used */
772 edsave
= getcpy (*ed
);
775 if (altmsg
&& atfile
)
776 (void) m_unlink (linkpath
);
783 copyf (char *ifile
, char *ofile
)
788 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
790 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
791 admonish (ofile
, "unable to open and truncate");
796 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
797 if (write (out
, buffer
, i
) != i
) {
798 advise (ofile
, "may have damaged");
814 sendfile (char **arg
, char *file
, int pushsw
)
818 char *cp
, *sp
, **vec
, *program
;
821 * If the sendproc is the nmh command `send', then we call
822 * those routines directly rather than exec'ing the command.
824 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
826 sendit (invo_name
= sp
, arg
, file
, pushsw
);
831 context_save (); /* save the context file */
837 adios("fork", "failed:");
840 vec
= argsplit(sendproc
, &program
, &vecp
);
842 vec
[vecp
++] = "-push";
845 vec
[vecp
++] = *arg
++;
849 execvp (program
, vec
);
850 fprintf (stderr
, "unable to exec ");
855 if (pidwait(child_id
, OK
) == 0)
863 * Translate MIME composition file (call buildmimeproc)
867 buildfile (char **argp
, char *file
)
874 /* allocate space for arguments */
880 args
= mh_xmalloc((i
+ 2) * sizeof(char *));
883 * For backward compatibility, we need to add -build
884 * if we are using mhn as buildmimeproc
887 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
888 args
[i
++] = "-build";
890 /* copy any other arguments */
891 while (argp
&& *argp
)
895 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
898 return i
? NOTOK
: OK
;
903 # define SASLminc(a) (a)
904 #else /* CYRUS_SASL */
905 # define SASLminc(a) 0
906 #endif /* CYRUS_SASL */
909 # define TLSminc(a) (a)
910 #else /* TLS_SUPPORT */
911 # define TLSminc(a) 0
912 #endif /* TLS_SUPPORT */
914 #define SEND_SWITCHES \
915 X("alias aliasfile", 0, ALIASW) \
916 X("debug", -5, DEBUGSW) \
917 X("filter filterfile", 0, FILTSW) \
918 X("nofilter", 0, NFILTSW) \
919 X("format", 0, FRMTSW) \
920 X("noformat", 0, NFRMTSW) \
921 X("forward", 0, FORWSW) \
922 X("noforward", 0, NFORWSW) \
923 X("mime", 0, MIMESW) \
924 X("nomime", 0, NMIMESW) \
925 X("msgid", 0, MSGDSW) \
926 X("nomsgid", 0, NMSGDSW) \
927 X("push", 0, SPSHSW) \
928 X("nopush", 0, NSPSHSW) \
929 X("split seconds", 0, SPLITSW) \
930 X("unique", -6, UNIQSW) \
931 X("nounique", -8, NUNIQSW) \
932 X("verbose", 0, VERBSW) \
933 X("noverbose", 0, NVERBSW) \
934 X("watch", 0, WATCSW) \
935 X("nowatch", 0, NWATCSW) \
936 X("width columns", 0, WIDTHSW) \
937 X("version", 0, SVERSIONSW) \
938 X("help", 0, SHELPSW) \
939 X("dashstuffing", -12, BITSTUFFSW) \
940 X("nodashstuffing", -14, NBITSTUFFSW) \
941 X("client host", -6, CLIESW) \
942 X("server host", 6, SERVSW) \
943 X("snoop", -5, SNOOPSW) \
944 X("draftfolder +folder", 0, SDRFSW) \
945 X("draftmessage msg", 0, SDRMSW) \
946 X("nodraftfolder", 0, SNDRFSW) \
947 X("sasl", SASLminc(4), SASLSW) \
948 X("nosasl", SASLminc(6), NOSASLSW) \
949 X("saslmech", SASLminc(5), SASLMECHSW) \
950 X("authservice", SASLminc(0), AUTHSERVICESW) \
951 X("user username", SASLminc(4), USERSW) \
952 X("port server-port-name/number", 4, PORTSW) \
953 X("tls", TLSminc(-3), TLSSW) \
954 X("initialtls", TLSminc(-10), INITTLSSW) \
955 X("notls", TLSminc(-5), NTLSSW) \
956 X("certverify", TLSminc(-10), CERTVERSW) \
957 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
958 X("sendmail program", 0, MTSSM) \
959 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
960 X("messageid localname|random", 2, MESSAGEIDSW) \
962 #define X(sw, minchars, id) id,
963 DEFINE_SWITCH_ENUM(SEND
);
966 #define X(sw, minchars, id) { sw, minchars, id },
967 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
971 extern int debugsw
; /* from sendsbr.c */
979 extern char *altmsg
; /* .. */
980 extern char *annotext
;
981 extern char *distfile
;
985 sendit (char *sp
, char **arg
, char *file
, int pushed
)
988 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
989 char **arguments
, *savearg
[MAXARGS
], **vec
;
990 const char *user
= NULL
, *saslmech
= NULL
;
991 char *auth_svc
= NULL
;
995 * Make sure these are defined. In particular, we need
996 * savearg[1] to be NULL, in case "arg" is NULL below. It
997 * doesn't matter what is the value of savearg[0], but we
998 * set it to NULL, to help catch "off-by-one" errors.
1004 * Temporarily copy arg to savearg, since the brkstring() call in
1005 * getarguments() will wipe it out before it is merged in.
1006 * Also, we skip the first element of savearg, since getarguments()
1007 * skips it. Then we count the number of arguments
1008 * copied. The value of "n" will be one greater than
1009 * this in order to simulate the standard argc/argv.
1014 copyip (arg
, savearg
+1, MAXARGS
-1);
1021 * Merge any arguments from command line (now in savearg)
1022 * and arguments from profile.
1024 arguments
= getarguments (sp
, n
, savearg
, 1);
1037 * Get our initial arguments for postproc now
1040 vec
= argsplit(postproc
, &program
, &vecp
);
1042 vec
[vecp
++] = "-library";
1043 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
1045 if ((cp
= context_find ("fileproc"))) {
1046 vec
[vecp
++] = "-fileproc";
1050 if ((cp
= context_find ("mhlproc"))) {
1051 vec
[vecp
++] = "-mhlproc";
1055 if ((cp
= context_find ("credentials"))) {
1056 /* post doesn't read context so need to pass credentials. */
1057 vec
[vecp
++] = "-credentials";
1061 while ((cp
= *argp
++)) {
1063 switch (smatch (++cp
, sendswitches
)) {
1065 ambigsw (cp
, sendswitches
);
1068 inform("-%s unknown\n", cp
);
1072 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1073 print_help (buf
, sendswitches
, 1);
1076 print_version (invo_name
);
1087 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1088 inform("missing argument to %s", argp
[-2]);
1144 #ifdef OAUTH_SUPPORT
1145 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
1146 die("missing argument to %s", argp
[-2]);
1149 NMH_UNUSED (auth_svc
);
1150 die("not built with OAuth support");
1168 if (!(cp
= *argp
++) || *cp
== '-') {
1169 inform("missing argument to %s", argp
[-2]);
1178 if (!(cp
= *argp
++) || *cp
== '-') {
1179 inform("missing argument to %s", argp
[-2]);
1187 inform("usage: %s [switches]", sp
);
1191 /* allow Aliasfile: profile entry */
1192 if ((cp
= context_find ("Aliasfile"))) {
1195 dp
= mh_xstrdup(cp
);
1196 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1197 vec
[vecp
++] = "-alias";
1202 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1203 if ((cp
= context_find ("signature")) && *cp
)
1204 setenv("SIGNATURE", cp
, 1);
1206 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1208 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1210 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1211 inplace
= atoi (cp
);
1213 if ((cp
= getenv ("mhdist"))
1217 vec
[vecp
++] = "-dist";
1218 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
1219 die("unable to create temporary file");
1221 distfile
= mh_xstrdup(cp
);
1222 (void) m_unlink(distfile
);
1223 if (link (altmsg
, distfile
) == NOTOK
)
1224 adios (distfile
, "unable to link %s to", altmsg
);
1229 #ifdef OAUTH_SUPPORT
1230 if (auth_svc
== NULL
) {
1231 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
1232 die("must specify -authservice with -saslmech xoauth2");
1236 die("must specify -user with -saslmech xoauth2");
1240 NMH_UNUSED(saslmech
);
1241 #endif /* OAUTH_SUPPORT */
1243 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1248 if ((pushsw
= pushed
))
1253 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, auth_svc
) == OK
)
1262 whomfile (char **arg
, char *file
)
1266 char **vec
, *program
;
1268 context_save (); /* save the context file */
1271 switch (pid
= fork()) {
1273 advise ("fork", "unable to");
1277 vec
= argsplit(whomproc
, &program
, &vecp
);
1280 vec
[vecp
++] = *arg
++;
1284 execvp (program
, vec
);
1285 fprintf (stderr
, "unable to exec ");
1287 _exit(1); /* NOTREACHED */
1290 return pidwait(pid
, NOTOK
) & 0377 ? 1 : 0;
1296 * Remove the draft file
1300 removefile (char *drft
)
1302 if (m_unlink (drft
) == NOTOK
)
1303 adios (drft
, "unable to unlink");
1310 * Return 1 if we already have a MIME-Version header, 0 otherwise.
1314 checkmimeheader (char *drft
)
1317 m_getfld_state_t gstate
;
1318 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1320 bool retval
= false;
1322 if ((f
= fopen(drft
, "r")) == NULL
) {
1323 admonish(drft
, "unable to read draft");
1327 gstate
= m_getfld_state_init(f
);
1329 int bufsz
= sizeof(buf
);
1330 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1333 if (strcasecmp(name
, VRSN_FIELD
) == 0) {
1334 inform("Cannot use attach commands with already-"
1335 "formatted MIME message \"%s\"", drft
);
1346 m_getfld_state_destroy(&gstate
);