]> diplodocus.org Git - nmh/blob - uip/whatnowsbr.c
Fix typo in man page
[nmh] / uip / whatnowsbr.c
1
2 /*
3 * whatnowsbr.c -- the WhatNow shell
4 *
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.
8 *
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.
12 *
13 * Several commands have been added at the whatnow prompt:
14 *
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.
20 *
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.
24 *
25 * pwd This option works just like the normal
26 * pwd command and exists to allow the user
27 * to verify the directory.
28 *
29 * attach [-v] files This option attaches the named files to
30 * the draft. -v displays the mhbuild
31 * directive that send(1) will use.
32 *
33 * alist [-ln] This option lists the attachments on the
34 * draft. -l gets long listings, -n gets
35 * numbered listings.
36 *
37 * detach files This option removes attachments from the
38 * detach -n numbers draft. This can be done by file name or
39 * by attachment number.
40 */
41
42 #include <h/mh.h>
43 #include <fcntl.h>
44 #include <h/mime.h>
45 #include <h/utils.h>
46
47 #define WHATNOW_SWITCHES \
48 X("draftfolder +folder", 0, DFOLDSW) \
49 X("draftmessage msg", 0, DMSGSW) \
50 X("nodraftfolder", 0, NDFLDSW) \
51 X("editor editor", 0, EDITRSW) \
52 X("noedit", 0, NEDITSW) \
53 X("prompt string", 4, PRMPTSW) \
54 X("version", 0, VERSIONSW) \
55 X("help", 0, HELPSW) \
56 X("attach header-field-name", -6, ATTACHSW) \
57 X("noattach", -8, NOATTACHSW) \
58
59
60 #define X(sw, minchars, id) id,
61 DEFINE_SWITCH_ENUM(WHATNOW);
62 #undef X
63
64 #define X(sw, minchars, id) { sw, minchars, id },
65 DEFINE_SWITCH_ARRAY(WHATNOW, whatnowswitches);
66 #undef X
67
68 /*
69 * Options at the "whatnow" prompt
70 */
71 #define PROMPT_SWITCHES \
72 X("edit [<editor> <switches>]", 0, EDITSW) \
73 X("refile [<switches>] +folder", 0, REFILEOPT) \
74 X("mime [<switches>]", 0, BUILDMIMESW) \
75 X("display [<switches>]", 0, DISPSW) \
76 X("list [<switches>]", 0, LISTSW) \
77 X("send [<switches>]", 0, SENDSW) \
78 X("push [<switches>]", 0, PUSHSW) \
79 X("whom [<switches>]", 0, WHOMSW) \
80 X("quit [-delete]", 0, QUITSW) \
81 X("delete", 0, DELETESW) \
82 X("cd [directory]", 0, CDCMDSW) \
83 X("pwd", 0, PWDCMDSW) \
84 X("ls", 2, LSCMDSW) \
85 X("attach [-v]", 0, ATTACHCMDSW) \
86 X("detach [-n]", 0, DETACHCMDSW) \
87 X("alist [-ln] ", 2, ALISTCMDSW) \
88
89 #define X(sw, minchars, id) id,
90 DEFINE_SWITCH_ENUM(PROMPT);
91 #undef X
92
93 #define X(sw, minchars, id) { sw, minchars, id },
94 DEFINE_SWITCH_ARRAY(PROMPT, aleqs);
95 #undef X
96
97 static char *myprompt = "\nWhat now? ";
98
99 /*
100 * static prototypes
101 */
102 static int editfile (char **, char **, char *, int, struct msgs *,
103 char *, char *, int, int);
104 static int sendfile (char **, char *, int);
105 static void sendit (char *, char **, char *, int);
106 static int buildfile (char **, char *);
107 static int whomfile (char **, char *);
108 static int removefile (char *);
109 static int checkmimeheader (char *);
110 static void writelscmd(char *, int, char *, char **);
111 static void writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp);
112 static FILE* popen_in_dir(const char *dir, const char *cmd, const char *type);
113 static int system_in_dir(const char *dir, const char *cmd);
114 static int copyf (char *, char *);
115
116
117 int
118 WhatNow (int argc, char **argv)
119 {
120 int isdf = 0, nedit = 0, use = 0, atfile = 1;
121 char *cp, *dfolder = NULL, *dmsg = NULL;
122 char *ed = NULL, *drft = NULL, *msgnam = NULL;
123 char buf[BUFSIZ], prompt[BUFSIZ];
124 char **argp, **arguments;
125 struct stat st;
126 char cwd[PATH_MAX + 1]; /* current working directory */
127 char file[PATH_MAX + 1]; /* file name buffer */
128 char shell[PATH_MAX + 1]; /* shell response buffer */
129 FILE *f; /* read pointer for bgnd proc */
130 char *l; /* set on -l to alist command */
131 int n; /* set on -n to alist command */
132
133 /* Need this if called from what_now(). */
134 invo_name = r1bindex (argv[0], '/');
135
136 arguments = getarguments (invo_name, argc, argv, 1);
137 argp = arguments;
138
139 /*
140 * Get the initial current working directory.
141 */
142
143 if (getcwd(cwd, sizeof (cwd)) == (char *)0) {
144 adios("getcwd", "could not get working directory");
145 }
146
147 while ((cp = *argp++)) {
148 if (*cp == '-') {
149 switch (smatch (++cp, whatnowswitches)) {
150 case AMBIGSW:
151 ambigsw (cp, whatnowswitches);
152 done (1);
153 case UNKWNSW:
154 adios (NULL, "-%s unknown", cp);
155
156 case HELPSW:
157 snprintf (buf, sizeof(buf), "%s [switches] [file]", invo_name);
158 print_help (buf, whatnowswitches, 1);
159 done (0);
160 case VERSIONSW:
161 print_version(invo_name);
162 done (0);
163
164 case DFOLDSW:
165 if (dfolder)
166 adios (NULL, "only one draft folder at a time!");
167 if (!(cp = *argp++) || *cp == '-')
168 adios (NULL, "missing argument to %s", argp[-2]);
169 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
170 *cp != '@' ? TFOLDER : TSUBCWF);
171 continue;
172 case DMSGSW:
173 if (dmsg)
174 adios (NULL, "only one draft message at a time!");
175 if (!(dmsg = *argp++) || *dmsg == '-')
176 adios (NULL, "missing argument to %s", argp[-2]);
177 continue;
178 case NDFLDSW:
179 dfolder = NULL;
180 isdf = NOTOK;
181 continue;
182
183 case EDITRSW:
184 if (!(ed = *argp++) || *ed == '-')
185 adios (NULL, "missing argument to %s", argp[-2]);
186 nedit = 0;
187 continue;
188 case NEDITSW:
189 nedit++;
190 continue;
191
192 case PRMPTSW:
193 if (!(myprompt = *argp++) || *myprompt == '-')
194 adios (NULL, "missing argument to %s", argp[-2]);
195 continue;
196
197 case ATTACHSW:
198 advise(NULL, "The -attach switch is deprecated");
199 continue;
200
201 case NOATTACHSW:
202 advise(NULL, "The -noattach switch is deprecated");
203 continue;
204 }
205 }
206 if (drft)
207 adios (NULL, "only one draft at a time!");
208 else
209 drft = cp;
210 }
211
212 if ((drft == NULL && (drft = getenv ("mhdraft")) == NULL) || *drft == 0)
213 drft = getcpy (m_draft (dfolder, dmsg, 1, &isdf));
214
215 msgnam = (cp = getenv ("mhaltmsg")) && *cp ? getcpy (cp) : NULL;
216
217 if ((cp = getenv ("mhatfile")) && *cp)
218 atfile = atoi(cp);
219
220 if ((cp = getenv ("mhuse")) && *cp)
221 use = atoi (cp);
222
223 if (ed == NULL && ((ed = getenv ("mheditor")) == NULL || *ed == 0)) {
224 ed = NULL;
225 nedit++;
226 }
227
228 /* start editing the draft, unless -noedit was given */
229 if (!nedit && editfile (&ed, NULL, drft, use, NULL, msgnam,
230 NULL, 1, atfile) < 0)
231 done (1);
232
233 snprintf (prompt, sizeof(prompt), myprompt, invo_name);
234 for (;;) {
235 #ifdef READLINE_SUPPORT
236 if (!(argp = getans_via_readline (prompt, aleqs))) {
237 #else /* ! READLINE_SUPPORT */
238 if (!(argp = getans (prompt, aleqs))) {
239 #endif /* READLINE_SUPPORT */
240 (void) m_unlink (LINK);
241 done (1);
242 }
243 switch (smatch (*argp, aleqs)) {
244 case DISPSW:
245 /* display the message being replied to, or distributed */
246 if (msgnam)
247 showfile (++argp, msgnam);
248 else
249 advise (NULL, "no alternate message to display");
250 break;
251
252 case BUILDMIMESW:
253 /* Translate MIME composition file */
254 buildfile (++argp, drft);
255 break;
256
257 case EDITSW:
258 /* Call an editor on the draft file */
259 if (*++argp)
260 ed = *argp++;
261 if (editfile (&ed, argp, drft, NOUSE, NULL, msgnam,
262 NULL, 1, atfile) == NOTOK)
263 done (1);
264 break;
265
266 case LISTSW:
267 /* display the draft file */
268 showfile (++argp, drft);
269 break;
270
271 case WHOMSW:
272 /* Check to whom the draft would be sent */
273 whomfile (++argp, drft);
274 break;
275
276 case QUITSW:
277 /* Quit, and possibly delete the draft */
278 if (*++argp && (*argp[0] == 'd' ||
279 ((*argp)[0] == '-' && (*argp)[1] == 'd'))) {
280 removefile (drft);
281 } else {
282 if (stat (drft, &st) != NOTOK)
283 advise (NULL, "draft left on %s", drft);
284 }
285 done (1);
286
287 case DELETESW:
288 /* Delete draft and exit */
289 removefile (drft);
290 done (1);
291
292 case PUSHSW:
293 /* Send draft in background */
294 if (sendfile (++argp, drft, 1))
295 done (1);
296 break;
297
298 case SENDSW:
299 /* Send draft */
300 sendfile (++argp, drft, 0);
301 break;
302
303 case REFILEOPT:
304 /* Refile the draft */
305 if (refile (++argp, drft) == 0)
306 done (0);
307 break;
308
309 case CDCMDSW:
310 /* Change the working directory for attachments
311 *
312 * Run the directory through the user's shell so that
313 * we can take advantage of any syntax that the user
314 * is accustomed to. Read back the absolute path.
315 */
316
317 if (*(argp+1) == (char *)0) {
318 (void)sprintf(buf, "$SHELL -c \"cd&&pwd\"");
319 }
320 else {
321 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
322 }
323 if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
324 if (fgets(cwd, sizeof (cwd), f) == NULL) {
325 advise (buf, "fgets");
326 }
327
328 if (strchr(cwd, '\n') != (char *)0)
329 *strchr(cwd, '\n') = '\0';
330
331 pclose(f);
332 }
333 else {
334 advise("popen", "could not get directory");
335 }
336
337 break;
338
339 case PWDCMDSW:
340 /* Print the working directory for attachments */
341 printf("%s\n", cwd);
342 break;
343
344 case LSCMDSW:
345 /* List files in the current attachment working directory
346 *
347 * Use the user's shell so that we can take advantage of any
348 * syntax that the user is accustomed to.
349 */
350 writelscmd(buf, sizeof(buf), "", argp);
351 (void)system_in_dir(cwd, buf);
352 break;
353
354 case ALISTCMDSW:
355 /*
356 * List attachments on current draft. Options are:
357 *
358 * -l long listing (full path names)
359 * -n numbers listing
360 */
361
362 if (checkmimeheader(drft))
363 break;
364
365 l = (char *)0;
366 n = 0;
367
368 while (*++argp != (char *)0) {
369 if (strcmp(*argp, "-l") == 0)
370 l = "/";
371
372 else if (strcmp(*argp, "-n") == 0)
373 n = 1;
374
375 else if (strcmp(*argp, "-ln") == 0 || strcmp(*argp, "-nl") == 0) {
376 l = "/";
377 n = 1;
378 }
379
380 else {
381 n = -1;
382 break;
383 }
384 }
385
386 if (n == -1)
387 advise((char *)0, "usage is alist [-ln].");
388
389 else
390 annolist(drft, ATTACH_FIELD, l, n);
391
392 break;
393
394 case ATTACHCMDSW: {
395 /*
396 * Attach files to current draft.
397 */
398
399 int verbose = 0;
400 char **ap;
401
402 if (checkmimeheader(drft))
403 break;
404
405 for (ap = argp+1; *ap; ++ap) {
406 if (strcmp(*ap, "-v") == 0) {
407 ++argp;
408 verbose = 1;
409 } else if (*ap[0] != '-') {
410 break;
411 }
412 }
413
414 if (*(argp+1) == (char *)0) {
415 advise(NULL, "attach command requires file argument(s).");
416 break;
417 }
418
419 /*
420 * Build a command line that causes the user's shell to list the file name
421 * arguments. This handles and wildcard expansion, tilde expansion, etc.
422 */
423 writelscmd(buf, sizeof(buf), "-d --", argp);
424
425 /*
426 * Read back the response from the shell, which contains a number of lines
427 * with one file name per line. Remove off the newline. Determine whether
428 * we have an absolute or relative path name. Prepend the current working
429 * directory to relative path names. Add the attachment annotation to the
430 * draft.
431 */
432
433 if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
434 while (fgets(shell, sizeof (shell), f) != (char *)0) {
435 char *ctype;
436
437 *(strchr(shell, '\n')) = '\0';
438
439 if (*shell == '/') {
440 strncpy(file, shell, sizeof(file));
441 file[sizeof(file) - 1] = '\0';
442 } else {
443 snprintf(file, sizeof(file), "%s/%s", cwd, shell);
444 }
445
446 annotate(drft, ATTACH_FIELD, file, 1, 0, -2, 1);
447 if (verbose) {
448 ctype = mime_type(file);
449 }
450
451 if (verbose) {
452 printf ("Attaching %s as a %s\n", file, ctype);
453 free (ctype);
454 }
455 }
456
457 pclose(f);
458 }
459 else {
460 advise("popen", "could not get file from shell");
461 }
462
463 break;
464 }
465 case DETACHCMDSW:
466 /*
467 * Detach files from current draft.
468 */
469
470 /*
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.
473 */
474
475 if (checkmimeheader(drft))
476 break;
477
478 for (n = 0, arguments = argp + 1; *arguments != (char *)0; arguments++) {
479 if (strcmp(*arguments, "-n") == 0) {
480 n = 1;
481 break;
482 }
483 }
484
485 /*
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
489 * correct.
490 */
491
492 if (n == 1) {
493 for (arguments = argp + 1; *arguments != (char *)0; arguments++) {
494 if (strcmp(*arguments, "-n") == 0)
495 continue;
496
497 if (**arguments != '\0') {
498 n = atoi(*arguments);
499 annotate(drft, ATTACH_FIELD, (char *)0, 1, 0, n, 1);
500
501 for (argp = arguments + 1; *argp != (char *)0; argp++) {
502 if (atoi(*argp) > n) {
503 if (atoi(*argp) == 1)
504 *argp = "";
505 else
506 (void)sprintf(*argp, "%d", atoi(*argp) - 1);
507 }
508 }
509 }
510 }
511 }
512
513 /*
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 /).
518 *
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.
521 */
522 writelscmd(buf, sizeof(buf), "-d --", argp);
523 if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
524 while (fgets(shell, sizeof (shell), f) != (char *)0) {
525 *(strchr(shell, '\n')) = '\0';
526 annotate(drft, ATTACH_FIELD, shell, 1, 0, 0, 1);
527 }
528 pclose(f);
529 } else {
530 advise("popen", "could not get file from shell");
531 }
532
533 break;
534
535 default:
536 /* Unknown command */
537 advise (NULL, "say what?");
538 break;
539 }
540 }
541 /*NOTREACHED*/
542 }
543
544
545
546 /* Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; trailcmd". */
547 static void
548 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
549 {
550 char *cp;
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.
557 */
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'
561 */
562 /* length checks here and inside the loop allow for the
563 * trailing "&&", trailcmd, '"' and NUL
564 */
565 int trailln = strlen(trailcmd) + 4;
566 if (ln < 0 || ln + trailln > bufsz)
567 adios((char *)0, "arguments too long");
568
569 cp = buf + ln;
570
571 while (*argp && *++argp) {
572 ln = strlen(*argp);
573 /* +1 for leading space */
574 if (ln + trailln + 1 > bufsz - (cp-buf))
575 adios((char *)0, "arguments too long");
576 *cp++ = ' ';
577 memcpy(cp, *argp, ln+1);
578 cp += ln;
579 }
580 if (*trailcmd) {
581 *cp++ = '&'; *cp++ = '&';
582 strcpy(cp, trailcmd);
583 cp += trailln - 4;
584 }
585 *cp++ = '"';
586 *cp = 0;
587 }
588
589 /*
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.
592 */
593 static void
594 writelscmd(char *buf, int bufsz, char *lsoptions, char **argp)
595 {
596 char *lscmd = concat ("ls ", lsoptions, NULL);
597 writesomecmd(buf, bufsz, lscmd, "", argp);
598 free (lscmd);
599 }
600
601 /* Like system(), but run the command in directory dir.
602 * This assumes the program is single-threaded!
603 */
604 static int
605 system_in_dir(const char *dir, const char *cmd)
606 {
607 char olddir[BUFSIZ];
608 int r;
609
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 */
613
614 if (getcwd(olddir, sizeof(olddir)) == 0)
615 adios("getcwd", "could not get working directory");
616 if (chdir(dir) != 0)
617 adios("chdir", "could not change working directory");
618 r = system(cmd);
619 if (chdir(olddir) != 0)
620 adios("chdir", "could not change working directory");
621 return r;
622 }
623
624 /* ditto for popen() */
625 static FILE*
626 popen_in_dir(const char *dir, const char *cmd, const char *type)
627 {
628 char olddir[BUFSIZ];
629 FILE *f;
630
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 */
634
635 if (getcwd(olddir, sizeof(olddir)) == 0)
636 adios("getcwd", "could not get working directory");
637 if (chdir(dir) != 0)
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");
642 return f;
643 }
644
645
646 /*
647 * EDIT
648 */
649
650 static int reedit = 0; /* have we been here before? */
651 static char *edsave = NULL; /* the editor we used previously */
652
653
654 static int
655 editfile (char **ed, char **arg, char *file, int use, struct msgs *mp,
656 char *altmsg, char *cwd, int save_editor, int atfile)
657 {
658 int pid, status, vecp;
659 char altpath[BUFSIZ], linkpath[BUFSIZ];
660 char *cp, *prog, **vec;
661 struct stat st;
662
663 int slinked = 0;
664
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)
670 cp = *ed;
671
672 /* unless we've specified it via "editor-next" */
673 cp = concat (cp, "-next", NULL);
674 if ((cp = context_find (cp)) != NULL)
675 *ed = cp;
676 }
677 } else {
678 /* set initial editor */
679 if (*ed == NULL)
680 *ed = get_default_editor();
681 }
682
683 if (altmsg) {
684 if (mp == NULL || *altmsg == '/' || cwd == NULL)
685 strncpy (altpath, altmsg, sizeof(altpath));
686 else
687 snprintf (altpath, sizeof(altpath), "%s/%s", mp->foldpath, altmsg);
688 if (cwd == NULL)
689 strncpy (linkpath, LINK, sizeof(linkpath));
690 else
691 snprintf (linkpath, sizeof(linkpath), "%s/%s", cwd, LINK);
692
693 if (atfile) {
694 (void) m_unlink (linkpath);
695 if (link (altpath, linkpath) == NOTOK) {
696 if (symlink (altpath, linkpath) < 0) {
697 adios (linkpath, "symlink");
698 }
699 slinked = 1;
700 } else {
701 slinked = 0;
702 }
703 }
704 }
705
706 context_save (); /* save the context file */
707 fflush (stdout);
708
709 switch (pid = fork()) {
710 case NOTOK:
711 advise ("fork", "unable to");
712 status = NOTOK;
713 break;
714
715 case OK:
716 if (cwd) {
717 if (chdir (cwd) < 0) {
718 advise (cwd, "chdir");
719 }
720 }
721 if (altmsg) {
722 if (mp)
723 m_putenv ("mhfolder", mp->foldpath);
724 m_putenv ("editalt", altpath);
725 }
726
727 vec = argsplit(*ed, &prog, &vecp);
728
729 if (arg)
730 while (*arg)
731 vec[vecp++] = *arg++;
732 vec[vecp++] = file;
733 vec[vecp] = NULL;
734
735 execvp (prog, vec);
736 fprintf (stderr, "unable to exec ");
737 perror (*ed);
738 _exit (-1);
739
740 default:
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 advise (NULL, "problems with edit--draft left in %s", cp);
747 } else {
748 advise (NULL, "problems with edit--%s preserved", file);
749 }
750 }
751 status = -2; /* maybe "reedit ? -2 : -1"? */
752 break;
753 }
754
755 reedit++;
756 if (altmsg
757 && mp
758 && !is_readonly(mp)
759 && (slinked
760 ? lstat (linkpath, &st) != NOTOK
761 && S_ISREG(st.st_mode)
762 && copyf (linkpath, altpath) == NOTOK
763 : stat (linkpath, &st) != NOTOK
764 && st.st_nlink == 1
765 && (m_unlink (altpath) == NOTOK
766 || link (linkpath, altpath) == NOTOK)))
767 advise (linkpath, "unable to update %s from", altmsg);
768 }
769
770 /* normally, we remember which editor we used */
771 if (save_editor)
772 edsave = getcpy (*ed);
773
774 *ed = NULL;
775 if (altmsg && atfile)
776 (void) m_unlink (linkpath);
777
778 return status;
779 }
780
781
782 static int
783 copyf (char *ifile, char *ofile)
784 {
785 int i, in, out;
786 char buffer[BUFSIZ];
787
788 if ((in = open (ifile, O_RDONLY)) == NOTOK)
789 return NOTOK;
790 if ((out = open (ofile, O_WRONLY | O_TRUNC)) == NOTOK) {
791 admonish (ofile, "unable to open and truncate");
792 close (in);
793 return NOTOK;
794 }
795
796 while ((i = read (in, buffer, sizeof(buffer))) > OK)
797 if (write (out, buffer, i) != i) {
798 advise (ofile, "may have damaged");
799 i = NOTOK;
800 break;
801 }
802
803 close (in);
804 close (out);
805 return i;
806 }
807
808
809 /*
810 * SEND
811 */
812
813 static int
814 sendfile (char **arg, char *file, int pushsw)
815 {
816 pid_t child_id;
817 int i, vecp;
818 char *cp, *sp, **vec, *program;
819
820 /*
821 * If the sendproc is the nmh command `send', then we call
822 * those routines directly rather than exec'ing the command.
823 */
824 if (strcmp (sp = r1bindex (sendproc, '/'), "send") == 0) {
825 cp = invo_name;
826 sendit (invo_name = sp, arg, file, pushsw);
827 invo_name = cp;
828 return 1;
829 }
830
831 context_save (); /* save the context file */
832 fflush (stdout);
833
834 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
835 sleep (5);
836 switch (child_id) {
837 case NOTOK:
838 advise (NULL, "unable to fork, so sending directly...");
839 case OK:
840 vec = argsplit(sendproc, &program, &vecp);
841 if (pushsw)
842 vec[vecp++] = "-push";
843 if (arg)
844 while (*arg)
845 vec[vecp++] = *arg++;
846 vec[vecp++] = file;
847 vec[vecp] = NULL;
848
849 execvp (program, vec);
850 fprintf (stderr, "unable to exec ");
851 perror (sendproc);
852 _exit (-1);
853
854 default:
855 if (pidwait(child_id, OK) == 0)
856 done (0);
857 return 1;
858 }
859 }
860
861
862 /*
863 * Translate MIME composition file (call buildmimeproc)
864 */
865
866 static int
867 buildfile (char **argp, char *file)
868 {
869 int i;
870 char **args, *ed;
871
872 ed = buildmimeproc;
873
874 /* allocate space for arguments */
875 i = 0;
876 if (argp) {
877 while (argp[i])
878 i++;
879 }
880 args = (char **) mh_xmalloc((i + 2) * sizeof(char *));
881
882 /*
883 * For backward compatibility, we need to add -build
884 * if we are using mhn as buildmimeproc
885 */
886 i = 0;
887 if (strcmp (r1bindex (ed, '/'), "mhn") == 0)
888 args[i++] = "-build";
889
890 /* copy any other arguments */
891 while (argp && *argp)
892 args[i++] = *argp++;
893 args[i] = NULL;
894
895 i = editfile (&ed, args, file, NOUSE, NULL, NULL, NULL, 0, 0);
896 free (args);
897
898 return (i ? NOTOK : OK);
899 }
900
901
902 #ifndef CYRUS_SASL
903 # define SASLminc(a) (a)
904 #else /* CYRUS_SASL */
905 # define SASLminc(a) 0
906 #endif /* CYRUS_SASL */
907
908 #ifndef TLS_SUPPORT
909 # define TLSminc(a) (a)
910 #else /* TLS_SUPPORT */
911 # define TLSminc(a) 0
912 #endif /* TLS_SUPPORT */
913
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", -6, SDRFSW) \
945 X("draftmessage msg", -6, SDRMSW) \
946 X("nodraftfolder", -3, SNDRFSW) \
947 X("sasl", SASLminc(-4), SASLSW) \
948 X("nosasl", SASLminc(-6), NOSASLSW) \
949 X("saslmaxssf", SASLminc(-10), SASLMXSSFSW) \
950 X("saslmech", SASLminc(-5), SASLMECHSW) \
951 X("user", SASLminc(-4), USERSW) \
952 X("attach fieldname", 6, SNDATTACHSW) \
953 X("noattach", 0, SNDNOATTACHSW) \
954 X("attachformat", 7, SNDATTACHFORMAT) \
955 X("port server-port-name/number", 4, PORTSW) \
956 X("tls", TLSminc(-3), TLSSW) \
957 X("initialtls", TLSminc(-10), INITTLSSW) \
958 X("notls", TLSminc(-5), NTLSSW) \
959 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
960 X("messageid localname|random", 2, MESSAGEIDSW) \
961
962 #define X(sw, minchars, id) id,
963 DEFINE_SWITCH_ENUM(SEND);
964 #undef X
965
966 #define X(sw, minchars, id) { sw, minchars, id },
967 DEFINE_SWITCH_ARRAY(SEND, sendswitches);
968 #undef X
969
970
971 extern int debugsw; /* from sendsbr.c */
972 extern int forwsw;
973 extern int inplace;
974 extern int pushsw;
975 extern int splitsw;
976 extern int unique;
977 extern int verbsw;
978
979 extern char *altmsg; /* .. */
980 extern char *annotext;
981 extern char *distfile;
982
983
984 static void
985 sendit (char *sp, char **arg, char *file, int pushed)
986 {
987 int vecp, n = 1;
988 char *cp, buf[BUFSIZ], **argp, *program;
989 char **arguments, *savearg[MAXARGS], **vec;
990 struct stat st;
991
992 #ifndef lint
993 int distsw = 0;
994 #endif
995
996 /*
997 * Make sure these are defined. In particular, we need
998 * savearg[1] to be NULL, in case "arg" is NULL below. It
999 * doesn't matter what is the value of savearg[0], but we
1000 * set it to NULL, to help catch "off-by-one" errors.
1001 */
1002 savearg[0] = NULL;
1003 savearg[1] = NULL;
1004
1005 /*
1006 * Temporarily copy arg to savearg, since the brkstring() call in
1007 * getarguments() will wipe it out before it is merged in.
1008 * Also, we skip the first element of savearg, since getarguments()
1009 * skips it. Then we count the number of arguments
1010 * copied. The value of "n" will be one greater than
1011 * this in order to simulate the standard argc/argv.
1012 */
1013 if (arg) {
1014 char **bp;
1015
1016 copyip (arg, savearg+1, MAXARGS-1);
1017 bp = savearg+1;
1018 while (*bp++)
1019 n++;
1020 }
1021
1022 /*
1023 * Merge any arguments from command line (now in savearg)
1024 * and arguments from profile.
1025 */
1026 arguments = getarguments (sp, n, savearg, 1);
1027 argp = arguments;
1028
1029 debugsw = 0;
1030 forwsw = 1;
1031 inplace = 1;
1032 unique = 0;
1033
1034 altmsg = NULL;
1035 annotext = NULL;
1036 distfile = NULL;
1037
1038 /*
1039 * Get our initial arguments for postproc now
1040 */
1041
1042 vec = argsplit(postproc, &program, &vecp);
1043
1044 vec[vecp++] = "-library";
1045 vec[vecp++] = getcpy (m_maildir (""));
1046
1047 if ((cp = context_find ("fileproc"))) {
1048 vec[vecp++] = "-fileproc";
1049 vec[vecp++] = cp;
1050 }
1051
1052 if ((cp = context_find ("mhlproc"))) {
1053 vec[vecp++] = "-mhlproc";
1054 vec[vecp++] = cp;
1055 }
1056
1057 if ((cp = context_find ("credentials"))) {
1058 /* post doesn't read context so need to pass credentials. */
1059 vec[vecp++] = "-credentials";
1060 vec[vecp++] = cp;
1061 }
1062
1063 while ((cp = *argp++)) {
1064 if (*cp == '-') {
1065 switch (smatch (++cp, sendswitches)) {
1066 case AMBIGSW:
1067 ambigsw (cp, sendswitches);
1068 return;
1069 case UNKWNSW:
1070 advise (NULL, "-%s unknown\n", cp);
1071 return;
1072
1073 case SHELPSW:
1074 snprintf (buf, sizeof(buf), "%s [switches]", sp);
1075 print_help (buf, sendswitches, 1);
1076 return;
1077 case SVERSIONSW:
1078 print_version (invo_name);
1079 return;
1080
1081 case SPSHSW:
1082 pushed++;
1083 continue;
1084 case NSPSHSW:
1085 pushed = 0;
1086 continue;
1087
1088 case SPLITSW:
1089 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1) {
1090 advise (NULL, "missing argument to %s", argp[-2]);
1091 return;
1092 }
1093 continue;
1094
1095 case UNIQSW:
1096 unique++;
1097 continue;
1098 case NUNIQSW:
1099 unique = 0;
1100 continue;
1101 case FORWSW:
1102 forwsw++;
1103 continue;
1104 case NFORWSW:
1105 forwsw = 0;
1106 continue;
1107
1108 case VERBSW:
1109 verbsw++;
1110 vec[vecp++] = --cp;
1111 continue;
1112 case NVERBSW:
1113 verbsw = 0;
1114 vec[vecp++] = --cp;
1115 continue;
1116
1117 case DEBUGSW:
1118 debugsw++; /* fall */
1119 case NFILTSW:
1120 case FRMTSW:
1121 case NFRMTSW:
1122 case BITSTUFFSW:
1123 case NBITSTUFFSW:
1124 case MIMESW:
1125 case NMIMESW:
1126 case MSGDSW:
1127 case NMSGDSW:
1128 case WATCSW:
1129 case NWATCSW:
1130 case SNOOPSW:
1131 case SASLSW:
1132 case NOSASLSW:
1133 case TLSSW:
1134 case INITTLSSW:
1135 case NTLSSW:
1136 vec[vecp++] = --cp;
1137 continue;
1138
1139 case ALIASW:
1140 case FILTSW:
1141 case WIDTHSW:
1142 case CLIESW:
1143 case SERVSW:
1144 case SASLMXSSFSW:
1145 case SASLMECHSW:
1146 case USERSW:
1147 case PORTSW:
1148 case MTSSW:
1149 case MESSAGEIDSW:
1150 vec[vecp++] = --cp;
1151 if (!(cp = *argp++) || *cp == '-') {
1152 advise (NULL, "missing argument to %s", argp[-2]);
1153 return;
1154 }
1155 vec[vecp++] = cp;
1156 continue;
1157
1158 case SDRFSW:
1159 case SDRMSW:
1160 if (!(cp = *argp++) || *cp == '-') {
1161 advise (NULL, "missing argument to %s", argp[-2]);
1162 return;
1163 }
1164 case SNDRFSW:
1165 continue;
1166
1167 case SNDATTACHSW:
1168 advise(NULL, "The -attach switch is deprecated");
1169 continue;
1170 case SNDNOATTACHSW:
1171 advise(NULL, "The -noattach switch is deprecated");
1172 continue;
1173
1174 case SNDATTACHFORMAT:
1175 advise(NULL, "The -attachformat switch is deprecated");
1176 continue;
1177 }
1178 }
1179 advise (NULL, "usage: %s [switches]", sp);
1180 return;
1181 }
1182
1183 /* allow Aliasfile: profile entry */
1184 if ((cp = context_find ("Aliasfile"))) {
1185 char **ap, *dp;
1186
1187 dp = getcpy (cp);
1188 for (ap = brkstring (dp, " ", "\n"); ap && *ap; ap++) {
1189 vec[vecp++] = "-alias";
1190 vec[vecp++] = *ap;
1191 }
1192 }
1193
1194 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
1195 if ((cp = context_find ("signature")) && *cp)
1196 m_putenv ("SIGNATURE", cp);
1197
1198 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
1199 annotext = NULL;
1200 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
1201 altmsg = NULL;
1202 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
1203 inplace = atoi (cp);
1204
1205 if ((cp = getenv ("mhdist"))
1206 && *cp
1207 #ifndef lint
1208 && (distsw = atoi (cp))
1209 #endif /* not lint */
1210 && altmsg) {
1211 vec[vecp++] = "-dist";
1212 if ((cp = m_mktemp2(altmsg, invo_name, NULL, NULL)) == NULL) {
1213 adios(NULL, "unable to create temporary file in %s",
1214 get_temp_dir());
1215 }
1216 distfile = getcpy (cp);
1217 (void) m_unlink(distfile);
1218 if (link (altmsg, distfile) == NOTOK)
1219 adios (distfile, "unable to link %s to", altmsg);
1220 } else {
1221 distfile = NULL;
1222 }
1223
1224 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
1225 st.st_mtime = 0;
1226 st.st_dev = 0;
1227 st.st_ino = 0;
1228 }
1229 if ((pushsw = pushed))
1230 push ();
1231
1232 closefds (3);
1233
1234 if (sendsbr (vec, vecp, program, file, &st, 1) == OK)
1235 done (0);
1236 }
1237
1238 /*
1239 * WHOM
1240 */
1241
1242 static int
1243 whomfile (char **arg, char *file)
1244 {
1245 pid_t pid;
1246 int vecp;
1247 char **vec, *program;
1248
1249 context_save (); /* save the context file */
1250 fflush (stdout);
1251
1252 switch (pid = fork()) {
1253 case NOTOK:
1254 advise ("fork", "unable to");
1255 return 1;
1256
1257 case OK:
1258 vec = argsplit(whomproc, &program, &vecp);
1259 vec[vecp++] = file;
1260 if (arg)
1261 while (*arg)
1262 vec[vecp++] = *arg++;
1263 vec[vecp] = NULL;
1264
1265 execvp (program, vec);
1266 fprintf (stderr, "unable to exec ");
1267 perror (whomproc);
1268 _exit (-1); /* NOTREACHED */
1269
1270 default:
1271 return (pidwait (pid, NOTOK) & 0377 ? 1 : 0);
1272 }
1273 }
1274
1275
1276 /*
1277 * Remove the draft file
1278 */
1279
1280 static int
1281 removefile (char *drft)
1282 {
1283 if (m_unlink (drft) == NOTOK)
1284 adios (drft, "unable to unlink");
1285
1286 return OK;
1287 }
1288
1289
1290 /*
1291 * Return 1 if we already have a MIME-Verson header, 0 otherwise.
1292 */
1293
1294 static int
1295 checkmimeheader (char *drft)
1296 {
1297 FILE *f;
1298 m_getfld_state_t gstate = 0;
1299 char buf[BUFSIZ], name[NAMESZ];
1300 int state, retval = 0;
1301
1302 if ((f = fopen(drft, "r")) == NULL) {
1303 admonish(drft, "unable to read draft");
1304 return (0);
1305 }
1306
1307 for (;;) {
1308 int bufsz = sizeof(buf);
1309 switch (state = m_getfld(&gstate, name, buf, &bufsz, f)) {
1310 case FLD:
1311 case FLDPLUS:
1312 if (strcasecmp(name, VRSN_FIELD) == 0) {
1313 advise(NULL, "Cannot use attach commands with already-"
1314 "formatted MIME message \"%s\"", drft);
1315 retval = 1;
1316 break;
1317 }
1318 continue;
1319 default:
1320 break;
1321 }
1322 break;
1323 }
1324
1325 m_getfld_state_destroy(&gstate);
1326 fclose(f);
1327
1328 return retval;
1329 }