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