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