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