3 * whatnowsbr.c -- the WhatNow shell
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
11 * Several options have been added to ease the inclusion of attachments
12 * using the header field name mechanism added to anno and send. The
13 * -attach option is used to specify the header field name for attachments.
15 * Several commands have been added at the whatnow prompt:
17 * cd [ directory ] This option works just like the shell's
18 * cd command and lets the user change the
19 * directory from which attachments are
20 * taken so that long path names are not
21 * needed with every file.
23 * ls [ ls-options ] This option works just like the normal
24 * ls command and exists to allow the user
25 * to verify file names in the directory.
27 * pwd This option works just like the normal
28 * pwd command and exists to allow the user
29 * to verify the directory.
31 * attach files This option attaches the named files to
34 * alist [-ln] This option lists the attachments on the
35 * draft. -l gets long listings, -n gets
38 * detach files This option removes attachments from the
39 * detach -n numbers draft. This can be done by file name or
40 * by attachment number.
48 static struct swit whatnowswitches[] = {
50 { "draftfolder +folder", 0 },
52 { "draftmessage msg", 0 },
54 { "nodraftfolder", 0 },
56 { "editor editor", 0 },
60 { "prompt string", 4 },
66 { "attach header-field-name", 0 },
71 * Options at the "whatnow" prompt
73 static struct swit aleqs[] = {
75 { "edit [<editor> <switches>]", 0 },
77 { "refile [<switches>] +folder", 0 },
79 { "mime [<switches>]", 0 },
81 { "display [<switches>]", 0 },
83 { "list [<switches>]", 0 },
85 { "send [<switches>]", 0 },
87 { "push [<switches>]", 0 },
89 { "whom [<switches>]", 0 },
91 { "quit [-delete]", 0 },
95 { "cd [directory]", 0 },
100 #define ATTACHCMDSW 13
102 #define DETACHCMDSW 14
103 { "detach [-n]", 2 },
104 #define ALISTCMDSW 15
105 { "alist [-ln] ", 2 },
109 static char *myprompt = "\nWhat now? ";
114 static int editfile (char **, char **, char *, int, struct msgs *,
115 char *, char *, int);
116 static int sendfile (char **, char *, int);
117 static void sendit (char *, char **, char *, int);
118 static int buildfile (char **, char *);
119 static int check_draft (char *);
120 static int whomfile (char **, char *);
121 static int removefile (char *);
124 static int copyf (char *, char *);
129 WhatNow (int argc, char **argv)
131 int isdf = 0, nedit = 0, use = 0;
132 char *cp, *dfolder = NULL, *dmsg = NULL;
133 char *ed = NULL, *drft = NULL, *msgnam = NULL;
134 char buf[BUFSIZ], prompt[BUFSIZ];
135 char **argp, **arguments;
137 char *attach = (char *)0; /* attachment header field name */
138 char cwd[MAXPATHLEN + 1]; /* current working directory */
139 char file[MAXPATHLEN + 1]; /* file name buffer */
140 char shell[MAXPATHLEN + 1]; /* shell response buffer */
141 FILE *f; /* read pointer for bgnd proc */
142 char *l; /* set on -l to alist command */
143 int n; /* set on -n to alist command */
145 invo_name = r1bindex (argv[0], '/');
147 /* read user profile/context */
150 arguments = getarguments (invo_name, argc, argv, 1);
154 * Get the initial current working directory.
157 if (getcwd(cwd, sizeof (cwd)) == (char *)0) {
158 adios("getcwd", "could not get working directory");
161 while ((cp = *argp++)) {
163 switch (smatch (++cp, whatnowswitches)) {
165 ambigsw (cp, whatnowswitches);
168 adios (NULL, "-%s unknown", cp);
171 snprintf (buf, sizeof(buf), "%s [switches] [file]", invo_name);
172 print_help (buf, whatnowswitches, 1);
175 print_version(invo_name);
180 adios (NULL, "only one draft folder at a time!");
181 if (!(cp = *argp++) || *cp == '-')
182 adios (NULL, "missing argument to %s", argp[-2]);
183 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
184 *cp != '@' ? TFOLDER : TSUBCWF);
188 adios (NULL, "only one draft message at a time!");
189 if (!(dmsg = *argp++) || *dmsg == '-')
190 adios (NULL, "missing argument to %s", argp[-2]);
198 if (!(ed = *argp++) || *ed == '-')
199 adios (NULL, "missing argument to %s", argp[-2]);
207 if (!(myprompt = *argp++) || *myprompt == '-')
208 adios (NULL, "missing argument to %s", argp[-2]);
212 if (attach != (char *)0)
213 adios(NULL, "only one attachment header field name at a time!");
214 if (!(attach = *argp++) || *attach == '-')
215 adios (NULL, "missing argument to %s", argp[-2]);
220 adios (NULL, "only one draft at a time!");
225 if ((drft == NULL && (drft = getenv ("mhdraft")) == NULL) || *drft == 0)
226 drft = getcpy (m_draft (dfolder, dmsg, 1, &isdf));
228 msgnam = (cp = getenv ("mhaltmsg")) && *cp ? getcpy (cp) : NULL;
230 if ((cp = getenv ("mhuse")) && *cp)
233 if (ed == NULL && ((ed = getenv ("mheditor")) == NULL || *ed == 0)) {
238 /* start editing the draft, unless -noedit was given */
239 if (!nedit && editfile (&ed, NULL, drft, use, NULL, msgnam, NULL, 1) < 0)
242 snprintf (prompt, sizeof(prompt), myprompt, invo_name);
244 if (!(argp = getans (prompt, aleqs))) {
248 switch (smatch (*argp, aleqs)) {
250 /* display the message being replied to, or distributed */
252 showfile (++argp, msgnam);
254 advise (NULL, "no alternate message to display");
258 /* Translate MIME composition file */
259 buildfile (++argp, drft);
263 /* Call an editor on the draft file */
266 if (editfile (&ed, argp, drft, NOUSE, NULL, msgnam, NULL, 1) == NOTOK)
271 /* display the draft file */
272 showfile (++argp, drft);
276 /* Check to whom the draft would be sent */
277 whomfile (++argp, drft);
281 /* Quit, and possibly delete the draft */
282 if (*++argp && (*argp[0] == 'd' ||
283 ((*argp)[0] == '-' && (*argp)[1] == 'd'))) {
286 if (stat (drft, &st) != NOTOK)
287 advise (NULL, "draft left on %s", drft);
292 /* Delete draft and exit */
297 /* Send draft in background */
298 if (sendfile (++argp, drft, 1))
304 sendfile (++argp, drft, 0);
308 /* Refile the draft */
309 if (refile (++argp, drft) == 0)
314 /* Change the working directory for attachments
316 * Run the directory through the user's shell so that
317 * we can take advantage of any syntax that the user
318 * is accustomed to. Read back the absolute path.
321 if (*++argp == (char *)0) {
322 (void)sprintf(buf, "$SHELL -c \"cd;pwd\"");
324 else if (strlen(*argp) >= BUFSIZ) {
325 adios((char *)0, "arguments too long");
328 (void)sprintf(buf, "$SHELL -c \"cd %s;cd %s;pwd\"", cwd, *argp);
330 if ((f = popen(buf, "r")) != (FILE *)0) {
331 fgets(cwd, sizeof (cwd), f);
333 if (strchr(cwd, '\n') != (char *)0)
334 *strchr(cwd, '\n') = '\0';
339 advise("popen", "could not get directory");
345 /* Print the working directory for attachments */
350 /* List files in the current attachment working directory
352 * Use the user's shell so that we can take advantage of any
353 * syntax that the user is accustomed to.
356 cp = buf + sprintf(buf, "$SHELL -c \" cd %s;ls", cwd);
358 while (*++argp != (char *)0) {
359 if (cp + strlen(*argp) + 2 >= buf + BUFSIZ)
360 adios((char *)0, "arguments too long");
362 cp += sprintf(cp, " %s", *argp);
365 (void)strcat(cp, "\"");
371 * List attachments on current draft. Options are:
373 * -l long listing (full path names)
377 if (attach == (char *)0) {
378 advise((char *)0, "can't list because no header field name was given.");
385 while (*++argp != (char *)0) {
386 if (strcmp(*argp, "-l") == 0)
389 else if (strcmp(*argp, "-n") == 0)
392 else if (strcmp(*argp, "-ln") == 0 || strcmp(*argp, "-nl") == 0) {
404 advise((char *)0, "usage is alist [-ln].");
407 annolist(drft, attach, l, n);
413 * Attach files to current draft.
416 if (attach == (char *)0) {
417 advise((char *)0, "can't attach because no header field name was given.");
422 * Build a command line that causes the user's shell to list the file name
423 * arguments. This handles and wildcard expansion, tilde expansion, etc.
426 cp = buf + sprintf(buf, "$SHELL -c \" cd %s;ls", cwd);
428 while (*++argp != (char *)0) {
429 if (cp + strlen(*argp) + 3 >= buf + BUFSIZ)
430 adios((char *)0, "arguments too long");
432 cp += sprintf(cp, " %s", *argp);
435 (void)strcat(cp, "\"");
438 * Read back the response from the shell, which contains a number of lines
439 * with one file name per line. Remove off the newline. Determine whether
440 * we have an absolute or relative path name. Prepend the current working
441 * directory to relative path names. Add the attachment annotation to the
445 if ((f = popen(buf, "r")) != (FILE *)0) {
446 while (fgets(shell, sizeof (shell), f) != (char *)0) {
447 *(strchr(shell, '\n')) = '\0';
450 (void)annotate(drft, attach, shell, 1, 0, -1, 1);
452 (void)sprintf(file, "%s/%s", cwd, shell);
453 (void)annotate(drft, attach, file, 1, 0, -1, 1);
460 advise("popen", "could not get file from shell");
467 * Detach files from current draft.
470 if (attach == (char *)0) {
471 advise((char *)0, "can't detach because no header field name was given.");
476 * Scan the arguments for a -n. Mixed file names and numbers aren't allowed,
477 * so this catches a -n anywhere in the argument list.
480 for (n = 0, arguments = argp + 1; *arguments != (char *)0; arguments++) {
481 if (strcmp(*arguments, "-n") == 0) {
488 * A -n was found so interpret the arguments as attachment numbers.
489 * Decrement any remaining argument number that is greater than the one
490 * just processed after processing each one so that the numbering stays
495 for (arguments = argp + 1; *arguments != (char *)0; arguments++) {
496 if (strcmp(*arguments, "-n") == 0)
499 if (**arguments != '\0') {
500 n = atoi(*arguments);
501 (void)annotate(drft, attach, (char *)0, 1, 0, n, 1);
503 for (argp = arguments + 1; *argp != (char *)0; argp++) {
504 if (atoi(*argp) > n) {
505 if (atoi(*argp) == 1)
508 (void)sprintf(*argp, "%d", atoi(*argp) - 1);
516 * The arguments are interpreted as file names. Run them through the
517 * user's shell for wildcard expansion and other goodies. Do this from
518 * the current working directory if the argument is not an absolute path
519 * name (does not begin with a /).
523 for (arguments = argp + 1; *arguments != (char *)0; arguments++) {
524 if (**arguments == '/') {
525 if (strlen(*arguments) + sizeof ("$SHELL -c \"ls \"") >= sizeof (buf))
526 adios((char *)0, "arguments too long");
528 (void)sprintf(buf, "$SHELL -c \"ls %s\"", *arguments);
531 if (strlen(cwd) + strlen(*arguments) + sizeof ("$SHELL -c \" cd ; ls \"") >= sizeof (buf))
532 adios((char *)0, "arguments too long");
534 (void)sprintf(buf, "$SHELL -c \" cd %s;ls %s\"", cwd, *arguments);
537 if ((f = popen(buf, "r")) != (FILE *)0) {
538 while (fgets(shell, sizeof (cwd), f) != (char *)0) {
539 *(strchr(shell, '\n')) = '\0';
540 (void)annotate(drft, attach, shell, 1, 0, 0, 1);
546 advise("popen", "could not get file from shell");
554 /* Unknown command */
555 advise (NULL, "say what?");
566 static int reedit = 0; /* have we been here before? */
567 static char *edsave = NULL; /* the editor we used previously */
571 editfile (char **ed, char **arg, char *file, int use, struct msgs *mp,
572 char *altmsg, char *cwd, int save_editor)
574 int pid, status, vecp;
575 char altpath[BUFSIZ], linkpath[BUFSIZ];
576 char *cp, *vec[MAXARGS];
582 int oumask; /* PJS: for setting permissions on symlinks. */
584 #endif /* HAVE_LSTAT */
586 /* Was there a previous edit session? */
588 if (!*ed) { /* no explicit editor */
589 *ed = edsave; /* so use the previous one */
590 if ((cp = r1bindex (*ed, '/')) == NULL)
593 /* unless we've specified it via "editor-next" */
594 cp = concat (cp, "-next", NULL);
595 if ((cp = context_find (cp)) != NULL)
599 /* set initial editor */
600 if (*ed == NULL && (*ed = context_find ("editor")) == NULL)
605 if (mp == NULL || *altmsg == '/' || cwd == NULL)
606 strncpy (altpath, altmsg, sizeof(altpath));
608 snprintf (altpath, sizeof(altpath), "%s/%s", mp->foldpath, altmsg);
610 strncpy (linkpath, LINK, sizeof(linkpath));
612 snprintf (linkpath, sizeof(linkpath), "%s/%s", cwd, LINK);
618 if (link (altpath, linkpath) == NOTOK) {
620 /* I don't think permission on symlinks matters /JLR */
621 oumask = umask(0044); /* PJS: else symlinks are world 'r' */
623 symlink (altpath, linkpath);
625 umask(oumask); /* PJS: else symlinks are world 'r' */
631 #else /* not HAVE_LSTAT */
632 link (altpath, linkpath);
633 #endif /* not HAVE_LSTAT */
636 context_save (); /* save the context file */
639 switch (pid = vfork ()) {
641 advise ("fork", "unable to");
650 m_putenv ("mhfolder", mp->foldpath);
651 m_putenv ("editalt", altpath);
655 vec[vecp++] = r1bindex (*ed, '/');
658 vec[vecp++] = *arg++;
663 fprintf (stderr, "unable to exec ");
668 if ((status = pidwait (pid, NOTOK))) {
670 if ((cp = r1bindex (*ed, '/'))
671 && strcmp (cp, "vi") == 0
672 && (status & 0x00ff) == 0)
676 if (((status & 0xff00) != 0xff00)
677 && (!reedit || (status & 0x00ff))) {
678 if (!use && (status & 0xff00) &&
679 (rename (file, cp = m_backup (file)) != NOTOK)) {
680 advise (NULL, "problems with edit--draft left in %s", cp);
682 advise (NULL, "problems with edit--%s preserved", file);
685 status = -2; /* maybe "reedit ? -2 : -1"? */
698 ? lstat (linkpath, &st) != NOTOK
699 && S_ISREG(st.st_mode)
700 && copyf (linkpath, altpath) == NOTOK
701 : stat (linkpath, &st) != NOTOK
703 && (unlink (altpath) == NOTOK
704 || link (linkpath, altpath) == NOTOK)))
705 advise (linkpath, "unable to update %s from", altmsg);
706 #else /* HAVE_LSTAT */
710 && stat (linkpath, &st) != NOTOK
712 && (unlink (altpath) == NOTOK
713 || link (linkpath, altpath) == NOTOK))
714 advise (linkpath, "unable to update %s from", altmsg);
715 #endif /* HAVE_LSTAT */
718 /* normally, we remember which editor we used */
720 edsave = getcpy (*ed);
732 copyf (char *ifile, char *ofile)
737 if ((in = open (ifile, O_RDONLY)) == NOTOK)
739 if ((out = open (ofile, O_WRONLY | O_TRUNC)) == NOTOK) {
740 admonish (ofile, "unable to open and truncate");
745 while ((i = read (in, buffer, sizeof(buffer))) > OK)
746 if (write (out, buffer, i) != i) {
747 advise (ofile, "may have damaged");
756 #endif /* HAVE_LSTAT */
764 sendfile (char **arg, char *file, int pushsw)
768 char *cp, *sp, *vec[MAXARGS];
770 /* Translate MIME composition file, if necessary */
771 if ((cp = context_find ("automimeproc"))
772 && (!strcmp (cp, "1"))
773 && !getenv ("NOMHNPROC")
774 && check_draft (file)
775 && (buildfile (NULL, file) == NOTOK))
778 /* For backwards compatibility */
779 if ((cp = context_find ("automhnproc"))
780 && !getenv ("NOMHNPROC")
781 && check_draft (file)
782 && (i = editfile (&cp, NULL, file, NOUSE, NULL, NULL, NULL, 0)))
786 * If the sendproc is the nmh command `send', then we call
787 * those routines directly rather than exec'ing the command.
789 if (strcmp (sp = r1bindex (sendproc, '/'), "send") == 0) {
791 sendit (invo_name = sp, arg, file, pushsw);
796 context_save (); /* save the context file */
799 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
803 advise (NULL, "unable to fork, so sending directly...");
806 vec[vecp++] = invo_name;
808 vec[vecp++] = "-push";
811 vec[vecp++] = *arg++;
815 execvp (sendproc, vec);
816 fprintf (stderr, "unable to exec ");
821 if (pidwait(child_id, OK) == 0)
829 * Translate MIME composition file (call buildmimeproc)
833 buildfile (char **argp, char *file)
840 /* allocate space for arguments */
846 if ((args = (char **) malloc((i + 2) * sizeof(char *))) == NULL)
847 adios (NULL, "unable to malloc memory");
850 * For backward compatibility, we need to add -build
851 * if we are using mhn as buildmimeproc
854 if (strcmp (r1bindex (ed, '/'), "mhn") == 0)
855 args[i++] = "-build";
857 /* copy any other arguments */
858 while (argp && *argp)
862 i = editfile (&ed, args, file, NOUSE, NULL, NULL, NULL, 0);
865 return (i ? NOTOK : OK);
870 * Check if draft is a mhbuild composition file
874 check_draft (char *msgnam)
877 char buf[BUFSIZ], name[NAMESZ];
880 if ((fp = fopen (msgnam, "r")) == NULL)
883 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
888 * If draft already contains any of the
889 * Content-XXX fields, then assume it already
892 if (uprf (name, XXX_FIELD_PRF)) {
896 while (state == FLDPLUS)
897 state = m_getfld (state, name, buf, sizeof(buf), fp);
904 for (bp = buf; *bp; bp++)
905 if (*bp != ' ' && *bp != '\t' && *bp != '\n') {
910 state = m_getfld (state, name, buf, sizeof(buf), fp);
911 } while (state == BODY);
922 # define SASLminc(a) (a)
923 #else /* CYRUS_SASL */
924 # define SASLminc(a) 0
925 #endif /* CYRUS_SASL */
927 static struct swit sendswitches[] = {
929 { "alias aliasfile", 0 },
933 { "filter filterfile", 0 },
957 { "split seconds", 0 },
971 { "width columns", 0 },
972 #define SVERSIONSW 22
976 #define BITSTUFFSW 24
977 { "dashstuffing", -12 },
978 #define NBITSTUFFSW 25
979 { "nodashstuffing", -14 },
989 { "client host", -6 },
991 { "server host", -6 },
995 { "draftfolder +folder", -6 },
997 { "draftmessage msg", -6 },
999 { "nodraftfolder", -3 },
1001 { "sasl", SASLminc(-4) },
1002 #define SASLMECHSW 37
1003 { "saslmech", SASLminc(-5) },
1005 { "user", SASLminc(-4) },
1006 #define SNDATTACHSW 39
1007 { "attach file", 6 },
1012 extern int debugsw; /* from sendsbr.c */
1020 extern char *altmsg; /* .. */
1021 extern char *annotext;
1022 extern char *distfile;
1026 sendit (char *sp, char **arg, char *file, int pushed)
1029 char *cp, buf[BUFSIZ], **argp;
1030 char **arguments, *vec[MAXARGS];
1032 char *attach = (char *)0; /* attachment header field name */
1042 * Make sure these are defined. In particular, we need
1043 * vec[1] to be NULL, in case "arg" is NULL below. It
1044 * doesn't matter what is the value of vec[0], but we
1045 * set it to NULL, to help catch "off-by-one" errors.
1051 * Temporarily copy arg to vec, since the brkstring() call in
1052 * getarguments() will wipe it out before it is merged in.
1053 * Also, we skip the first element of vec, since getarguments()
1054 * skips it. Then we count the number of arguments
1055 * copied. The value of "n" will be one greater than
1056 * this in order to simulate the standard argc/argv.
1061 copyip (arg, vec+1, MAXARGS-1);
1068 * Merge any arguments from command line (now in vec)
1069 * and arguments from profile.
1071 arguments = getarguments (sp, n, vec, 1);
1083 vecp = 1; /* we'll get the zero'th element later */
1084 vec[vecp++] = "-library";
1085 vec[vecp++] = getcpy (m_maildir (""));
1087 while ((cp = *argp++)) {
1089 switch (smatch (++cp, sendswitches)) {
1091 ambigsw (cp, sendswitches);
1094 advise (NULL, "-%s unknown\n", cp);
1098 snprintf (buf, sizeof(buf), "%s [switches]", sp);
1099 print_help (buf, sendswitches, 1);
1102 print_version (invo_name);
1113 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1) {
1114 advise (NULL, "missing argument to %s", argp[-2]);
1142 debugsw++; /* fall */
1171 if (!(cp = *argp++) || *cp == '-') {
1172 advise (NULL, "missing argument to %s", argp[-2]);
1180 if (!(cp = *argp++) || *cp == '-') {
1181 advise (NULL, "missing argument to %s", argp[-2]);
1188 if (!(attach = *argp++) || *attach == '-') {
1189 advise (NULL, "missing argument to %s", argp[-2]);
1195 advise (NULL, "usage: %s [switches]", sp);
1199 /* allow Aliasfile: profile entry */
1200 if ((cp = context_find ("Aliasfile"))) {
1204 for (ap = brkstring (dp, " ", "\n"); ap && *ap; ap++) {
1205 vec[vecp++] = "-alias";
1210 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
1211 if ((cp = context_find ("signature")) && *cp)
1212 m_putenv ("SIGNATURE", cp);
1215 snprintf (buf, sizeof(buf), "%s/.signature", mypath);
1216 if ((fp = fopen (buf, "r")) != NULL
1217 && fgets (buf, sizeof(buf), fp) != NULL) {
1219 if (cp = strchr (buf, '\n'))
1221 m_putenv ("SIGNATURE", buf);
1226 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
1228 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
1230 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
1231 inplace = atoi (cp);
1233 if ((cp = getenv ("mhdist"))
1236 && (distsw = atoi (cp))
1237 #endif /* not lint */
1239 vec[vecp++] = "-dist";
1240 distfile = getcpy (m_scratch (altmsg, invo_name));
1241 if (link (altmsg, distfile) == NOTOK)
1242 adios (distfile, "unable to link %s to", altmsg);
1247 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
1252 if ((pushsw = pushed))
1255 vec[0] = r1bindex (postproc, '/');
1258 if (sendsbr (vec, vecp, file, &st, 1, attach) == OK)
1267 whomfile (char **arg, char *file)
1273 context_save (); /* save the context file */
1276 switch (pid = vfork ()) {
1278 advise ("fork", "unable to");
1283 vec[vecp++] = r1bindex (whomproc, '/');
1287 vec[vecp++] = *arg++;
1290 execvp (whomproc, vec);
1291 fprintf (stderr, "unable to exec ");
1293 _exit (-1); /* NOTREACHED */
1296 return (pidwait (pid, NOTOK) & 0377 ? 1 : 0);
1302 * Remove the draft file
1306 removefile (char *drft)
1308 if (unlink (drft) == NOTOK)
1309 adios (drft, "unable to unlink");