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