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