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