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