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