]>
diplodocus.org Git - nmh/blob - uip/whatnowsbr.c
85ba4b9029fbcbab67e0e013ceb4176d24a763da
3 * whatnowsbr.c -- the WhatNow shell
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
9 * Several options have been added to ease the inclusion of attachments
10 * using the header field name mechanism added to anno and send. The
11 * -attach option is used to specify the header field name for attachments.
13 * Several commands have been added at the whatnow prompt:
15 * cd [ directory ] This option works just like the shell's
16 * cd command and lets the user change the
17 * directory from which attachments are
18 * taken so that long path names are not
19 * needed with every file.
21 * ls [ ls-options ] This option works just like the normal
22 * ls command and exists to allow the user
23 * to verify file names in the directory.
25 * pwd This option works just like the normal
26 * pwd command and exists to allow the user
27 * to verify the directory.
29 * attach files This option attaches the named files to
32 * alist [-ln] This option lists the attachments on the
33 * draft. -l gets long listings, -n gets
36 * detach files This option removes attachments from the
37 * detach -n numbers draft. This can be done by file name or
38 * by attachment number.
46 #define WHATNOW_SWITCHES \
47 X("draftfolder +folder", 0, DFOLDSW) \
48 X("draftmessage msg", 0, DMSGSW) \
49 X("nodraftfolder", 0, NDFLDSW) \
50 X("editor editor", 0, EDITRSW) \
51 X("noedit", 0, NEDITSW) \
52 X("prompt string", 4, PRMPTSW) \
53 X("version", 0, VERSIONSW) \
54 X("help", 0, HELPSW) \
55 X("attach header-field-name", 0, ATTACHSW) \
56 X("noattach", 0, NOATTACHSW) \
58 #define X(sw, minchars, id) id,
59 DEFINE_SWITCH_ENUM(WHATNOW
);
62 #define X(sw, minchars, id) { sw, minchars, id },
63 DEFINE_SWITCH_ARRAY(WHATNOW
, whatnowswitches
);
67 * Options at the "whatnow" prompt
69 #define PROMPT_SWITCHES \
70 X("edit [<editor> <switches>]", 0, EDITSW) \
71 X("refile [<switches>] +folder", 0, REFILEOPT) \
72 X("mime [<switches>]", 0, BUILDMIMESW) \
73 X("display [<switches>]", 0, DISPSW) \
74 X("list [<switches>]", 0, LISTSW) \
75 X("send [<switches>]", 0, SENDSW) \
76 X("push [<switches>]", 0, PUSHSW) \
77 X("whom [<switches>]", 0, WHOMSW) \
78 X("quit [-delete]", 0, QUITSW) \
79 X("delete", 0, DELETESW) \
80 X("cd [directory]", 0, CDCMDSW) \
81 X("pwd", 0, PWDCMDSW) \
83 X("attach", 0, ATTACHCMDSW) \
84 X("detach [-n]", 0, DETACHCMDSW) \
85 X("alist [-ln] ", 2, ALISTCMDSW) \
87 #define X(sw, minchars, id) id,
88 DEFINE_SWITCH_ENUM(PROMPT
);
91 #define X(sw, minchars, id) { sw, minchars, id },
92 DEFINE_SWITCH_ARRAY(PROMPT
, aleqs
);
95 static char *myprompt
= "\nWhat now? ";
100 static int editfile (char **, char **, char *, int, struct msgs
*,
101 char *, char *, int, int);
102 static int sendfile (char **, char *, int);
103 static void sendit (char *, char **, char *, int);
104 static int buildfile (char **, char *);
105 static int check_draft (char *);
106 static int whomfile (char **, char *);
107 static int removefile (char *);
108 static void writelscmd(char *, int, char *, char **);
109 static void writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
);
110 static FILE* popen_in_dir(const char *dir
, const char *cmd
, const char *type
);
111 static int system_in_dir(const char *dir
, const char *cmd
);
112 static int copyf (char *, char *);
116 WhatNow (int argc
, char **argv
)
118 int isdf
= 0, nedit
= 0, use
= 0, atfile
= 1;
119 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
120 char *ed
= NULL
, *drft
= NULL
, *msgnam
= NULL
;
121 char buf
[BUFSIZ
], prompt
[BUFSIZ
];
122 char **argp
, **arguments
;
124 char *attach
= NMH_ATTACH_HEADER
;/* attachment header field name */
125 char cwd
[PATH_MAX
+ 1]; /* current working directory */
126 char file
[PATH_MAX
+ 1]; /* file name buffer */
127 char shell
[PATH_MAX
+ 1]; /* shell response buffer */
128 FILE *f
; /* read pointer for bgnd proc */
129 char *l
; /* set on -l to alist command */
130 int n
; /* set on -n to alist command */
132 invo_name
= r1bindex (argv
[0], '/');
134 /* read user profile/context */
137 arguments
= getarguments (invo_name
, argc
, argv
, 1);
141 * Get the initial current working directory.
144 if (getcwd(cwd
, sizeof (cwd
)) == (char *)0) {
145 adios("getcwd", "could not get working directory");
148 while ((cp
= *argp
++)) {
150 switch (smatch (++cp
, whatnowswitches
)) {
152 ambigsw (cp
, whatnowswitches
);
155 adios (NULL
, "-%s unknown", cp
);
158 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
159 print_help (buf
, whatnowswitches
, 1);
162 print_version(invo_name
);
167 adios (NULL
, "only one draft folder at a time!");
168 if (!(cp
= *argp
++) || *cp
== '-')
169 adios (NULL
, "missing argument to %s", argp
[-2]);
170 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
171 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
175 adios (NULL
, "only one draft message at a time!");
176 if (!(dmsg
= *argp
++) || *dmsg
== '-')
177 adios (NULL
, "missing argument to %s", argp
[-2]);
185 if (!(ed
= *argp
++) || *ed
== '-')
186 adios (NULL
, "missing argument to %s", argp
[-2]);
194 if (!(myprompt
= *argp
++) || *myprompt
== '-')
195 adios (NULL
, "missing argument to %s", argp
[-2]);
199 if (!(attach
= *argp
++) || *attach
== '-')
200 adios (NULL
, "missing argument to %s", argp
[-2]);
209 adios (NULL
, "only one draft at a time!");
214 if ((drft
== NULL
&& (drft
= getenv ("mhdraft")) == NULL
) || *drft
== 0)
215 drft
= getcpy (m_draft (dfolder
, dmsg
, 1, &isdf
));
217 msgnam
= (cp
= getenv ("mhaltmsg")) && *cp
? getcpy (cp
) : NULL
;
219 if ((cp
= getenv ("mhatfile")) && *cp
)
222 if ((cp
= getenv ("mhuse")) && *cp
)
225 if (ed
== NULL
&& ((ed
= getenv ("mheditor")) == NULL
|| *ed
== 0)) {
230 /* start editing the draft, unless -noedit was given */
231 if (!nedit
&& editfile (&ed
, NULL
, drft
, use
, NULL
, msgnam
,
232 NULL
, 1, atfile
) < 0)
235 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
237 #ifdef READLINE_SUPPORT
238 if (!(argp
= getans_via_readline (prompt
, aleqs
))) {
239 #else /* ! READLINE_SUPPORT */
240 if (!(argp
= getans (prompt
, aleqs
))) {
241 #endif /* READLINE_SUPPORT */
245 switch (smatch (*argp
, aleqs
)) {
247 /* display the message being replied to, or distributed */
249 showfile (++argp
, msgnam
);
251 advise (NULL
, "no alternate message to display");
255 /* Translate MIME composition file */
256 buildfile (++argp
, drft
);
260 /* Call an editor on the draft file */
263 if (editfile (&ed
, argp
, drft
, NOUSE
, NULL
, msgnam
,
264 NULL
, 1, atfile
) == NOTOK
)
269 /* display the draft file */
270 showfile (++argp
, drft
);
274 /* Check to whom the draft would be sent */
275 whomfile (++argp
, drft
);
279 /* Quit, and possibly delete the draft */
280 if (*++argp
&& (*argp
[0] == 'd' ||
281 ((*argp
)[0] == '-' && (*argp
)[1] == 'd'))) {
284 if (stat (drft
, &st
) != NOTOK
)
285 advise (NULL
, "draft left on %s", drft
);
290 /* Delete draft and exit */
295 /* Send draft in background */
296 if (sendfile (++argp
, drft
, 1))
302 sendfile (++argp
, drft
, 0);
306 /* Refile the draft */
307 if (refile (++argp
, drft
) == 0)
312 /* Change the working directory for attachments
314 * Run the directory through the user's shell so that
315 * we can take advantage of any syntax that the user
316 * is accustomed to. Read back the absolute path.
319 if (*(argp
+1) == (char *)0) {
320 (void)sprintf(buf
, "$SHELL -c \"cd&&pwd\"");
323 writesomecmd(buf
, BUFSIZ
, "cd", "pwd", argp
);
325 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
326 fgets(cwd
, sizeof (cwd
), f
);
328 if (strchr(cwd
, '\n') != (char *)0)
329 *strchr(cwd
, '\n') = '\0';
334 advise("popen", "could not get directory");
340 /* Print the working directory for attachments */
345 /* List files in the current attachment working directory
347 * Use the user's shell so that we can take advantage of any
348 * syntax that the user is accustomed to.
350 writelscmd(buf
, sizeof(buf
), "", argp
);
351 (void)system_in_dir(cwd
, buf
);
356 * List attachments on current draft. Options are:
358 * -l long listing (full path names)
362 if (attach
== (char *)0) {
363 advise((char *)0, "can't list because no header field name was given.");
370 while (*++argp
!= (char *)0) {
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 advise((char *)0, "usage is alist [-ln].");
392 annolist(drft
, attach
, l
, n
);
398 * Attach files to current draft.
401 if (attach
== (char *)0) {
402 advise((char *)0, "can't attach because no header field name was given.");
406 if (*(argp
+1) == (char *)0) {
407 advise((char *)0, "attach command requires file argument(s).");
412 * Build a command line that causes the user's shell to list the file name
413 * arguments. This handles and wildcard expansion, tilde expansion, etc.
415 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
418 * Read back the response from the shell, which contains a number of lines
419 * with one file name per line. Remove off the newline. Determine whether
420 * we have an absolute or relative path name. Prepend the current working
421 * directory to relative path names. Add the attachment annotation to the
425 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
426 while (fgets(shell
, sizeof (shell
), f
) != (char *)0) {
427 *(strchr(shell
, '\n')) = '\0';
430 (void)annotate(drft
, attach
, shell
, 1, 0, -2, 1);
432 (void)sprintf(file
, "%s/%s", cwd
, shell
);
433 (void)annotate(drft
, attach
, file
, 1, 0, -2, 1);
440 advise("popen", "could not get file from shell");
447 * Detach files from current draft.
450 if (attach
== (char *)0) {
451 advise((char *)0, "can't detach because no header field name was given.");
456 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
457 * so this catches a -n anywhere in the argument list.
460 for (n
= 0, arguments
= argp
+ 1; *arguments
!= (char *)0; arguments
++) {
461 if (strcmp(*arguments
, "-n") == 0) {
468 * A -n was found so interpret the arguments as attachment numbers.
469 * Decrement any remaining argument number that is greater than the one
470 * just processed after processing each one so that the numbering stays
475 for (arguments
= argp
+ 1; *arguments
!= (char *)0; arguments
++) {
476 if (strcmp(*arguments
, "-n") == 0)
479 if (**arguments
!= '\0') {
480 n
= atoi(*arguments
);
481 (void)annotate(drft
, attach
, (char *)0, 1, 0, n
, 1);
483 for (argp
= arguments
+ 1; *argp
!= (char *)0; argp
++) {
484 if (atoi(*argp
) > n
) {
485 if (atoi(*argp
) == 1)
488 (void)sprintf(*argp
, "%d", atoi(*argp
) - 1);
496 * The arguments are interpreted as file names. Run them through the
497 * user's shell for wildcard expansion and other goodies. Do this from
498 * the current working directory if the argument is not an absolute path
499 * name (does not begin with a /).
501 * We feed all the file names to the shell at once, otherwise you can't
502 * provide a file name with a space in it.
504 writelscmd(buf
, sizeof(buf
), "-d --", argp
);
505 if ((f
= popen_in_dir(cwd
, buf
, "r")) != (FILE *)0) {
506 while (fgets(shell
, sizeof (shell
), f
) != (char *)0) {
507 *(strchr(shell
, '\n')) = '\0';
508 (void)annotate(drft
, attach
, shell
, 1, 0, 0, 1);
512 advise("popen", "could not get file from shell");
518 /* Unknown command */
519 advise (NULL
, "say what?");
528 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
530 writesomecmd(char *buf
, int bufsz
, char *cmd
, char *trailcmd
, char **argp
)
533 /* Note that we do not quote -- the argp from the user
534 * is assumed to be quoted as they desire. (We can't treat
535 * it as pure literal as that would prevent them using ~,
536 * wildcards, etc.) The buffer produced by this function
537 * should be given to popen_in_dir() or system_in_dir() so
538 * that the current working directory is set correctly.
540 int ln
= snprintf(buf
, bufsz
, "$SHELL -c \"%s", cmd
);
541 /* NB that some snprintf() return -1 on overflow rather than the
542 * new C99 mandated 'number of chars that would have been written'
544 /* length checks here and inside the loop allow for the
545 * trailing "&&", trailcmd, '"' and NUL
547 int trailln
= strlen(trailcmd
) + 4;
548 if (ln
< 0 || ln
+ trailln
> bufsz
)
549 adios((char *)0, "arguments too long");
553 while (*argp
&& *++argp
) {
555 /* +1 for leading space */
556 if (ln
+ trailln
+ 1 > bufsz
- (cp
-buf
))
557 adios((char *)0, "arguments too long");
559 memcpy(cp
, *argp
, ln
+1);
563 *cp
++ = '&'; *cp
++ = '&';
564 strcpy(cp
, trailcmd
);
572 * Build a command line that causes the user's shell to list the file name
573 * arguments. This handles and wildcard expansion, tilde expansion, etc.
576 writelscmd(char *buf
, int bufsz
, char *lsoptions
, char **argp
)
578 char *lscmd
= concat ("ls ", lsoptions
, NULL
);
579 writesomecmd(buf
, bufsz
, lscmd
, "", argp
);
583 /* Like system(), but run the command in directory dir.
584 * This assumes the program is single-threaded!
587 system_in_dir(const char *dir
, const char *cmd
)
592 /* ensure that $SHELL exists, as the cmd was written relying on
593 a non-blank $SHELL... */
594 setenv("SHELL","/bin/sh",0); /* don't overwrite */
596 if (getcwd(olddir
, sizeof(olddir
)) == 0)
597 adios("getcwd", "could not get working directory");
599 adios("chdir", "could not change working directory");
601 if (chdir(olddir
) != 0)
602 adios("chdir", "could not change working directory");
606 /* ditto for popen() */
608 popen_in_dir(const char *dir
, const char *cmd
, const char *type
)
613 /* ensure that $SHELL exists, as the cmd was written relying on
614 a non-blank $SHELL... */
615 setenv("SHELL","/bin/sh",0); /* don't overwrite */
617 if (getcwd(olddir
, sizeof(olddir
)) == 0)
618 adios("getcwd", "could not get working directory");
620 adios("chdir", "could not change working directory");
621 f
= popen(cmd
, type
);
622 if (chdir(olddir
) != 0)
623 adios("chdir", "could not change working directory");
632 static int reedit
= 0; /* have we been here before? */
633 static char *edsave
= NULL
; /* the editor we used previously */
637 editfile (char **ed
, char **arg
, char *file
, int use
, struct msgs
*mp
,
638 char *altmsg
, char *cwd
, int save_editor
, int atfile
)
640 int pid
, status
, vecp
;
641 char altpath
[BUFSIZ
], linkpath
[BUFSIZ
];
642 char *cp
, *prog
, **vec
;
647 /* Was there a previous edit session? */
649 if (!*ed
) { /* no explicit editor */
650 *ed
= edsave
; /* so use the previous one */
651 if ((cp
= r1bindex (*ed
, '/')) == NULL
)
654 /* unless we've specified it via "editor-next" */
655 cp
= concat (cp
, "-next", NULL
);
656 if ((cp
= context_find (cp
)) != NULL
)
660 /* set initial editor */
662 *ed
= get_default_editor();
666 if (mp
== NULL
|| *altmsg
== '/' || cwd
== NULL
)
667 strncpy (altpath
, altmsg
, sizeof(altpath
));
669 snprintf (altpath
, sizeof(altpath
), "%s/%s", mp
->foldpath
, altmsg
);
671 strncpy (linkpath
, LINK
, sizeof(linkpath
));
673 snprintf (linkpath
, sizeof(linkpath
), "%s/%s", cwd
, LINK
);
677 if (link (altpath
, linkpath
) == NOTOK
) {
678 symlink (altpath
, linkpath
);
686 context_save (); /* save the context file */
689 switch (pid
= fork()) {
691 advise ("fork", "unable to");
700 m_putenv ("mhfolder", mp
->foldpath
);
701 m_putenv ("editalt", altpath
);
704 vec
= argsplit(*ed
, &prog
, &vecp
);
708 vec
[vecp
++] = *arg
++;
713 fprintf (stderr
, "unable to exec ");
718 if ((status
= pidwait (pid
, NOTOK
))) {
719 if (((status
& 0xff00) != 0xff00)
720 && (!reedit
|| (status
& 0x00ff))) {
721 if (!use
&& (status
& 0xff00) &&
722 (rename (file
, cp
= m_backup (file
)) != NOTOK
)) {
723 advise (NULL
, "problems with edit--draft left in %s", cp
);
725 advise (NULL
, "problems with edit--%s preserved", file
);
728 status
= -2; /* maybe "reedit ? -2 : -1"? */
737 ? lstat (linkpath
, &st
) != NOTOK
738 && S_ISREG(st
.st_mode
)
739 && copyf (linkpath
, altpath
) == NOTOK
740 : stat (linkpath
, &st
) != NOTOK
742 && (unlink (altpath
) == NOTOK
743 || link (linkpath
, altpath
) == NOTOK
)))
744 advise (linkpath
, "unable to update %s from", altmsg
);
747 /* normally, we remember which editor we used */
749 edsave
= getcpy (*ed
);
752 if (altmsg
&& atfile
)
760 copyf (char *ifile
, char *ofile
)
765 if ((in
= open (ifile
, O_RDONLY
)) == NOTOK
)
767 if ((out
= open (ofile
, O_WRONLY
| O_TRUNC
)) == NOTOK
) {
768 admonish (ofile
, "unable to open and truncate");
773 while ((i
= read (in
, buffer
, sizeof(buffer
))) > OK
)
774 if (write (out
, buffer
, i
) != i
) {
775 advise (ofile
, "may have damaged");
791 sendfile (char **arg
, char *file
, int pushsw
)
795 char *cp
, *sp
, **vec
, *program
;
797 /* Translate MIME composition file, if necessary */
798 if ((cp
= context_find ("automimeproc"))
799 && (!strcmp (cp
, "1"))
800 && check_draft (file
)
801 && (buildfile (NULL
, file
) == NOTOK
))
804 /* For backwards compatibility */
805 if ((cp
= context_find ("automhnproc"))
806 && check_draft (file
)
807 && (i
= editfile (&cp
, NULL
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0)))
811 * If the sendproc is the nmh command `send', then we call
812 * those routines directly rather than exec'ing the command.
814 if (strcmp (sp
= r1bindex (sendproc
, '/'), "send") == 0) {
816 sendit (invo_name
= sp
, arg
, file
, pushsw
);
821 context_save (); /* save the context file */
824 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
828 advise (NULL
, "unable to fork, so sending directly...");
830 vec
= argsplit(sendproc
, &program
, &vecp
);
832 vec
[vecp
++] = "-push";
835 vec
[vecp
++] = *arg
++;
839 execvp (program
, vec
);
840 fprintf (stderr
, "unable to exec ");
845 if (pidwait(child_id
, OK
) == 0)
853 * Translate MIME composition file (call buildmimeproc)
857 buildfile (char **argp
, char *file
)
864 /* allocate space for arguments */
870 args
= (char **) mh_xmalloc((i
+ 2) * sizeof(char *));
873 * For backward compatibility, we need to add -build
874 * if we are using mhn as buildmimeproc
877 if (strcmp (r1bindex (ed
, '/'), "mhn") == 0)
878 args
[i
++] = "-build";
880 /* copy any other arguments */
881 while (argp
&& *argp
)
885 i
= editfile (&ed
, args
, file
, NOUSE
, NULL
, NULL
, NULL
, 0, 0);
888 return (i
? NOTOK
: OK
);
893 * Check if draft is a mhbuild composition file
897 check_draft (char *msgnam
)
900 char buf
[BUFSIZ
], name
[NAMESZ
];
902 m_getfld_state_t gstate
= 0;
904 if ((fp
= fopen (msgnam
, "r")) == NULL
)
907 int bufsz
= sizeof buf
;
908 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
912 * If draft already contains any of the
913 * Content-XXX fields, then assume it already
916 if (uprf (name
, XXX_FIELD_PRF
)) {
918 m_getfld_state_destroy (&gstate
);
921 while (state
== FLDPLUS
) {
923 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
931 for (bp
= buf
; *bp
; bp
++)
932 if (*bp
!= ' ' && *bp
!= '\t' && *bp
!= '\n') {
934 m_getfld_state_destroy (&gstate
);
939 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
940 } while (state
== BODY
);
945 m_getfld_state_destroy (&gstate
);
953 # define SASLminc(a) (a)
954 #else /* CYRUS_SASL */
955 # define SASLminc(a) 0
956 #endif /* CYRUS_SASL */
959 # define TLSminc(a) (a)
960 #else /* TLS_SUPPORT */
961 # define TLSminc(a) 0
962 #endif /* TLS_SUPPORT */
964 #define SEND_SWITCHES \
965 X("alias aliasfile", 0, ALIASW) \
966 X("debug", -5, DEBUGSW) \
967 X("filter filterfile", 0, FILTSW) \
968 X("nofilter", 0, NFILTSW) \
969 X("format", 0, FRMTSW) \
970 X("noformat", 0, NFRMTSW) \
971 X("forward", 0, FORWSW) \
972 X("noforward", 0, NFORWSW) \
973 X("mime", 0, MIMESW) \
974 X("nomime", 0, NMIMESW) \
975 X("msgid", 0, MSGDSW) \
976 X("nomsgid", 0, NMSGDSW) \
977 X("push", 0, SPSHSW) \
978 X("nopush", 0, NSPSHSW) \
979 X("split seconds", 0, SPLITSW) \
980 X("unique", -6, UNIQSW) \
981 X("nounique", -8, NUNIQSW) \
982 X("verbose", 0, VERBSW) \
983 X("noverbose", 0, NVERBSW) \
984 X("watch", 0, WATCSW) \
985 X("nowatch", 0, NWATCSW) \
986 X("width columns", 0, WIDTHSW) \
987 X("version", 0, SVERSIONSW) \
988 X("help", 0, SHELPSW) \
989 X("dashstuffing", -12, BITSTUFFSW) \
990 X("nodashstuffing", -14, NBITSTUFFSW) \
991 X("client host", -6, CLIESW) \
992 X("server host", 6, SERVSW) \
993 X("snoop", -5, SNOOPSW) \
994 X("draftfolder +folder", -6, SDRFSW) \
995 X("draftmessage msg", -6, SDRMSW) \
996 X("nodraftfolder", -3, SNDRFSW) \
997 X("sasl", SASLminc(-4), SASLSW) \
998 X("nosasl", SASLminc(-6), NOSASLSW) \
999 X("saslmaxssf", SASLminc(-10), SASLMXSSFSW) \
1000 X("saslmech", SASLminc(-5), SASLMECHSW) \
1001 X("user", SASLminc(-4), USERSW) \
1002 X("attach fieldname", 6, SNDATTACHSW) \
1003 X("noattach", 0, SNDNOATTACHSW) \
1004 X("attachformat", 7, SNDATTACHFORMAT) \
1005 X("port server-port-name/number", 4, PORTSW) \
1006 X("tls", TLSminc(-3), TLSSW) \
1007 X("initialtls", TLSminc(-10), INITTLSSW) \
1008 X("notls", TLSminc(-5), NTLSSW) \
1009 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
1010 X("messageid localname|random", 2, MESSAGEIDSW) \
1012 #define X(sw, minchars, id) id,
1013 DEFINE_SWITCH_ENUM(SEND
);
1016 #define X(sw, minchars, id) { sw, minchars, id },
1017 DEFINE_SWITCH_ARRAY(SEND
, sendswitches
);
1021 extern int debugsw
; /* from sendsbr.c */
1029 extern char *altmsg
; /* .. */
1030 extern char *annotext
;
1031 extern char *distfile
;
1035 sendit (char *sp
, char **arg
, char *file
, int pushed
)
1038 char *cp
, buf
[BUFSIZ
], **argp
, *program
;
1039 char **arguments
, *savearg
[MAXARGS
], **vec
;
1041 char *attach
= NMH_ATTACH_HEADER
;/* attachment header field name */
1042 int attachformat
= 1; /* mhbuild format specifier for
1050 * Make sure these are defined. In particular, we need
1051 * savearg[1] to be NULL, in case "arg" is NULL below. It
1052 * doesn't matter what is the value of savearg[0], but we
1053 * set it to NULL, to help catch "off-by-one" errors.
1059 * Temporarily copy arg to savearg, since the brkstring() call in
1060 * getarguments() will wipe it out before it is merged in.
1061 * Also, we skip the first element of savearg, since getarguments()
1062 * skips it. Then we count the number of arguments
1063 * copied. The value of "n" will be one greater than
1064 * this in order to simulate the standard argc/argv.
1069 copyip (arg
, savearg
+1, MAXARGS
-1);
1076 * Merge any arguments from command line (now in savearg)
1077 * and arguments from profile.
1079 arguments
= getarguments (sp
, n
, savearg
, 1);
1092 * Get our initial arguments for postproc now
1095 vec
= argsplit(postproc
, &program
, &vecp
);
1097 vec
[vecp
++] = "-library";
1098 vec
[vecp
++] = getcpy (m_maildir (""));
1100 if ((cp
= context_find ("fileproc"))) {
1101 vec
[vecp
++] = "-fileproc";
1105 if ((cp
= context_find ("mhlproc"))) {
1106 vec
[vecp
++] = "-mhlproc";
1110 if ((cp
= context_find ("credentials"))) {
1111 /* post doesn't read context so need to pass credentials. */
1112 vec
[vecp
++] = "-credentials";
1116 while ((cp
= *argp
++)) {
1118 switch (smatch (++cp
, sendswitches
)) {
1120 ambigsw (cp
, sendswitches
);
1123 advise (NULL
, "-%s unknown\n", cp
);
1127 snprintf (buf
, sizeof(buf
), "%s [switches]", sp
);
1128 print_help (buf
, sendswitches
, 1);
1131 print_version (invo_name
);
1142 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1) {
1143 advise (NULL
, "missing argument to %s", argp
[-2]);
1171 debugsw
++; /* fall */
1204 if (!(cp
= *argp
++) || *cp
== '-') {
1205 advise (NULL
, "missing argument to %s", argp
[-2]);
1213 if (!(cp
= *argp
++) || *cp
== '-') {
1214 advise (NULL
, "missing argument to %s", argp
[-2]);
1221 if (!(attach
= *argp
++) || *attach
== '-') {
1222 advise (NULL
, "missing argument to %s", argp
[-2]);
1230 case SNDATTACHFORMAT
:
1231 if (! *argp
|| **argp
== '-')
1232 adios (NULL
, "missing argument to %s", argp
[-1]);
1234 attachformat
= atoi (*argp
);
1235 if (attachformat
< 0 ||
1236 attachformat
> ATTACHFORMATS
- 1) {
1237 advise (NULL
, "unsupported attachformat %d",
1246 advise (NULL
, "usage: %s [switches]", sp
);
1250 /* allow Aliasfile: profile entry */
1251 if ((cp
= context_find ("Aliasfile"))) {
1255 for (ap
= brkstring (dp
, " ", "\n"); ap
&& *ap
; ap
++) {
1256 vec
[vecp
++] = "-alias";
1261 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
1262 if ((cp
= context_find ("signature")) && *cp
)
1263 m_putenv ("SIGNATURE", cp
);
1265 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
1267 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
1269 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
1270 inplace
= atoi (cp
);
1272 if ((cp
= getenv ("mhdist"))
1275 && (distsw
= atoi (cp
))
1276 #endif /* not lint */
1278 vec
[vecp
++] = "-dist";
1279 distfile
= getcpy (m_mktemp2(altmsg
, invo_name
, NULL
, NULL
));
1281 if (link (altmsg
, distfile
) == NOTOK
)
1282 adios (distfile
, "unable to link %s to", altmsg
);
1287 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
1292 if ((pushsw
= pushed
))
1297 if (sendsbr (vec
, vecp
, program
, file
, &st
, 1, attach
, attachformat
) == OK
)
1306 whomfile (char **arg
, char *file
)
1310 char **vec
, *program
;
1312 context_save (); /* save the context file */
1315 switch (pid
= fork()) {
1317 advise ("fork", "unable to");
1321 vec
= argsplit(whomproc
, &program
, &vecp
);
1325 vec
[vecp
++] = *arg
++;
1328 execvp (program
, vec
);
1329 fprintf (stderr
, "unable to exec ");
1331 _exit (-1); /* NOTREACHED */
1334 return (pidwait (pid
, NOTOK
) & 0377 ? 1 : 0);
1340 * Remove the draft file
1344 removefile (char *drft
)
1346 if (unlink (drft
) == NOTOK
)
1347 adios (drft
, "unable to unlink");