closefds() doesn't seem relevant or large enough to be a function.
[mmh] / uip / send.c
1 /*
2 ** send.c -- send a composed message
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 #include <signal.h>
13 #include <h/signals.h>
14 #include <setjmp.h>
15 #include <h/mime.h>
16 #include <h/tws.h>
17 #include <h/utils.h>
18
19 #ifdef TIME_WITH_SYS_TIME
20 # include <sys/time.h>
21 # include <time.h>
22 #else
23 # ifdef TM_IN_SYS_TIME
24 #  include <sys/time.h>
25 # else
26 #  include <time.h>
27 # endif
28 #endif
29
30 int debugsw = 0;  /* global */
31 int forwsw  = 1;
32 int pushsw  = 0;
33 int verbsw  = 0;
34 char *altmsg   = NULL;
35 char *annotext = NULL;
36 char *distfile = NULL;
37
38 static jmp_buf env;
39
40 /* name of temp file for body content */
41 static char body_file_name[MAXPATHLEN + 1];
42 /* name of mhbuild composition temporary file */
43 static char composition_file_name[MAXPATHLEN + 1]; 
44 static int field_size;  /* size of header field buffer */
45 static char *field;  /* header field buffer */
46 static FILE *draft_file;  /* draft file pointer */
47 static FILE *body_file;  /* body file pointer */
48 static FILE *composition_file;  /* composition file pointer */
49
50 /*
51 ** static prototypes
52 */
53 static int sendsbr(char **, int, char *, struct stat *, int);
54 static void armed_done(int) NORETURN;
55 static void alert(char *, int);
56 static int tmp_fd(void);
57 static void anno(struct stat *);
58 static int sendaux(char **, int, char *, struct stat *);
59 static int attach(char *);
60 static void clean_up_temporary_files(void);
61 static int get_line(void);
62 static void make_mime_composition_file_entry(char *);
63
64
65 static struct swit switches[] = {
66 #define ALIASW  0
67         { "alias aliasfile", 0 },
68 #define DEBUGSW  1
69         { "debug", -5 },
70 #define FORWSW  2
71         { "forward", 0 },
72 #define NFORWSW  3
73         { "noforward", 0 },
74 #define PUSHSW  4
75         { "push", 0 },
76 #define NPUSHSW  5
77         { "nopush", 0 },
78 #define VERBSW  6
79         { "verbose", 0 },
80 #define NVERBSW  7
81         { "noverbose", 0 },
82 #define WATCSW  8
83         { "watch", 0 },
84 #define NWATCSW  9
85         { "nowatch", 0 },
86 #define VERSIONSW  10
87         { "version", 0 },
88 #define HELPSW  11
89         { "help", 0 },
90         { NULL, 0 }
91 };
92
93
94 int
95 main(int argc, char **argv)
96 {
97         int msgp = 0, nfiles = 0, distsw = 0, vecp = 1;
98         int msgnum, status;
99         int in, out;
100         int n;
101         char *cp, *maildir = NULL;
102         char buf[BUFSIZ], **ap, **argp, **arguments;
103         char *msgs[MAXARGS], *vec[MAXARGS];
104         char *files[MAXARGS];
105         struct msgs *mp;
106         struct stat st;
107         struct stat st2;
108
109
110 #ifdef LOCALE
111         setlocale(LC_ALL, "");
112 #endif
113         invo_name = mhbasename(argv[0]);
114
115         /* read user profile/context */
116         context_read();
117
118         arguments = getarguments(invo_name, argc, argv, 1);
119         argp = arguments;
120
121         vec[vecp++] = "-library";
122         vec[vecp++] = getcpy(toabsdir("+"));
123
124         while ((cp = *argp++)) {
125                 if (*cp == '-') {
126                         switch (smatch(++cp, switches)) {
127                         case AMBIGSW:
128                                 ambigsw(cp, switches);
129                                 done(1);
130                         case UNKWNSW:
131                                 adios(NULL, "-%s unknown\n", cp);
132
133                         case HELPSW:
134                                 snprintf(buf, sizeof(buf),
135                                                 "%s [file] [switches]",
136                                                 invo_name);
137                                 print_help(buf, switches, 1);
138                                 done(1);
139                         case VERSIONSW:
140                                 print_version(invo_name);
141                                 done(1);
142
143                         case PUSHSW:
144                                 pushsw++;
145                                 continue;
146                         case NPUSHSW:
147                                 pushsw = 0;
148                                 continue;
149
150                         case FORWSW:
151                                 forwsw++;
152                                 continue;
153                         case NFORWSW:
154                                 forwsw = 0;
155                                 continue;
156
157                         case VERBSW:
158                                 verbsw++;
159                                 vec[vecp++] = --cp;
160                                 continue;
161                         case NVERBSW:
162                                 verbsw = 0;
163                                 vec[vecp++] = --cp;
164                                 continue;
165
166                         case DEBUGSW:
167                                 debugsw++;  /* fall */
168                         case WATCSW:
169                         case NWATCSW:
170                                 vec[vecp++] = --cp;
171                                 continue;
172
173                         case ALIASW:
174                                 vec[vecp++] = --cp;
175                                 if (!(cp = *argp++) || *cp == '-') {
176                                         adios(NULL, "missing argument to %s",
177                                                         argp[-2]);
178                                 }
179                                 vec[vecp++] = cp;
180                                 continue;
181
182                         }
183                 } else {
184                         if (*cp == '/') {
185                                 files[nfiles++] = cp;
186                         } else {
187                                 msgs[msgp++] = cp;
188                         }
189                 }
190         }
191
192         /* check for "Aliasfile:" profile entry */
193         if ((cp = context_find("Aliasfile"))) {
194                 char *dp = NULL;
195
196                 for (ap=brkstring(dp=getcpy(cp), " ", "\n"); ap && *ap; ap++) {
197                         vec[vecp++] = "-alias";
198                         vec[vecp++] = getcpy(etcpath(*ap));
199                 }
200         }
201
202         if (!msgp && !nfiles) {
203                 msgs[msgp++] = seq_cur;
204         }
205         maildir = toabsdir(draftfolder);
206
207         if (chdir(maildir) == NOTOK) {
208                 adios(maildir, "unable to change directory to");
209         }
210
211         if (!(mp = folder_read(draftfolder))) {
212                 adios(NULL, "unable to read draft folder %s", draftfolder);
213         }
214         if (mp->nummsg == 0) {
215                 adios(NULL, "no messages in draft folder %s", draftfolder);
216         }
217         /* parse all the message ranges/sequences and set SELECTED */
218         for (msgnum = 0; msgnum < msgp; msgnum++) {
219                 if (!m_convert(mp, msgs[msgnum])) {
220                         done(1);
221                 }
222         }
223         seq_setprev(mp);
224
225         for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
226                 if (is_selected(mp, msgnum)) {
227                         files[nfiles++] = getcpy(m_name(msgnum));
228                         unset_exists(mp, msgnum);
229                 }
230         }
231
232         mp->msgflags |= SEQMOD;
233         seq_save(mp);
234
235         if (!(cp = getenv("SIGNATURE")) || !*cp) {
236                 if ((cp = context_find("signature")) && *cp) {
237                         m_putenv("SIGNATURE", cp);
238                 }
239         }
240
241         for (msgnum = 0; msgnum < nfiles; msgnum++) {
242                 if (stat(files[msgnum], &st) == NOTOK) {
243                         adios(files[msgnum], "unable to stat draft file");
244                 }
245         }
246
247         if (!(annotext = getenv("mhannotate")) || !*annotext) {
248                 annotext = NULL;
249         }
250         if (!(altmsg = getenv("mhaltmsg")) || !*altmsg) {
251                 altmsg = NULL;  /* used by dist interface - see below */
252         }
253
254         if ((cp = getenv("mhdist")) && *cp && (distsw = atoi(cp)) && altmsg) {
255                 vec[vecp++] = "-dist";
256                 if ((in = open(altmsg, O_RDONLY)) == NOTOK) {
257                         adios(altmsg, "unable to open for reading");
258                 }
259                 fstat(in, &st2);
260                 distfile = getcpy(m_mktemp2(NULL, invo_name, NULL, NULL));
261                 if ((out = creat(distfile, (int)st2.st_mode & 0777))==NOTOK) {
262                         adios(distfile, "unable to open for writing");
263                 }
264                 cpydata(in, out, altmsg, distfile);
265                 close(in);
266                 close(out);
267         } else {
268                 distfile = NULL;
269         }
270
271         if (!altmsg || stat(altmsg, &st) == NOTOK) {
272                 st.st_mtime = 0;
273                 st.st_dev = 0;
274                 st.st_ino = 0;
275         }
276         if (pushsw) {
277                 /* push a fork into the background */
278                 pid_t pid;
279
280                 switch (pid = fork()) {
281                 case -1:
282                         /* fork error */
283                         advise(NULL, "unable to fork, so can't push...");
284                         break;
285
286                 default:
287                         /* parent, just exit */
288                         done(0);
289
290                 case 0:
291                         /* child, block a few signals and continue */
292                         SIGNAL(SIGHUP, SIG_IGN);
293                         SIGNAL(SIGINT, SIG_IGN);
294                         SIGNAL(SIGQUIT, SIG_IGN);
295                         SIGNAL(SIGTERM, SIG_IGN);
296 #ifdef SIGTSTP
297                         SIGNAL(SIGTSTP, SIG_IGN);
298                         SIGNAL(SIGTTIN, SIG_IGN);
299                         SIGNAL(SIGTTOU, SIG_IGN);
300 #endif
301                         freopen("/dev/null", "r", stdin);
302                         freopen("/dev/null", "w", stdout);
303                         break;
304                 }
305         }
306         status = 0;
307         vec[0] = "spost";
308         for (n=3; n<OPEN_MAX; n++) {
309                 close(n);
310         }
311
312         for (msgnum = 0; msgnum < nfiles; msgnum++) {
313                 switch (sendsbr(vec, vecp, files[msgnum], &st, 1)) {
314                 case DONE:
315                         done(++status);
316                 case NOTOK:
317                         status++;  /* fall */
318                 case OK:
319                         break;
320                 }
321         }
322
323         context_save();
324         done(status);
325         return 1;
326 }
327
328
329 /*
330 ** message sending back-end
331 */
332 static int
333 sendsbr(char **vec, int vecp, char *drft, struct stat *st, int rename_drft)
334 {
335         int status;
336         char buffer[BUFSIZ];
337         char *original_draft;
338         char *p;  /* string pointer for building file name */
339
340         /*
341         ** Save the original name of the draft file.  The name of the
342         ** draft file is changed to a temporary file containing the built
343         ** MIME message if there are attachments.  We need the original
344         ** name so that it can be renamed after the message is sent.
345         */
346         original_draft = drft;
347
348         /*
349         ** Convert the draft to a MIME message.
350         ** Use the mhbuild composition file for the draft if there was
351         ** a successful conversion because that now contains the MIME
352         ** message.  A nice side effect of this is that it leaves the
353         ** original draft file untouched so that it can be retrieved
354         ** and modified if desired.
355         */
356         switch (attach(drft)) {
357         case OK:
358                 drft = composition_file_name;
359                 break;
360
361         case NOTOK:
362                 return (NOTOK);
363
364         case DONE:
365                 break;
366         }
367
368         done=armed_done;
369         switch (setjmp(env)) {
370         case OK:
371                 status = sendaux(vec, vecp, drft, st) ? NOTOK : OK;
372                 /* rename the original draft */
373                 if (rename_drft && status == OK &&
374                                 rename(original_draft, strncpy(buffer,
375                                 m_backup(original_draft), sizeof(buffer)))
376                                 == NOTOK) {
377                         advise(buffer, "unable to rename %s to", drft);
378                 }
379                 break;
380
381         default:
382                 status = DONE;
383                 break;
384         }
385
386         done=exit;
387         if (distfile) {
388                 unlink(distfile);
389         }
390
391         /*
392         ** Get rid of any temporary files that we created for attachments.
393         ** Also get rid of the renamed composition file that mhbuild
394         ** leaves as a turd.  It looks confusing, but we use the body
395         ** file name to help build the renamed composition file name.
396         */
397         if (drft == composition_file_name) {
398                 clean_up_temporary_files();
399
400                 if (strlen(composition_file_name) >=
401                                 sizeof (composition_file_name) - 6) {
402                         advise(NULL, "unable to remove original composition file.");
403                 } else {
404                         if (!(p = strrchr(composition_file_name, '/'))) {
405                                 p = composition_file_name;
406                         } else {
407                                 p++;
408                         }
409                         strcpy(body_file_name, p);
410                         *p++ = ',';
411                         strcpy(p, body_file_name);
412                         strcat(p, ".orig");
413
414                         unlink(composition_file_name);
415                 }
416         }
417
418         return status;
419 }
420
421 static int
422 attach(char *draft_file_name)
423 {
424         char buf[MAXPATHLEN + 6];
425         int c;
426         int has_attachment;
427         int has_body;
428         int non_ascii; /* msg body contains non-ASCII chars */
429         int length;  /* length of attachment header field name */
430         char *p;
431
432         if (!(draft_file = fopen(draft_file_name, "r"))) {
433                 adios(NULL, "can't open draft file `%s'.", draft_file_name);
434         }
435
436         /* We'll grow the buffer as needed. */
437         field = (char *)mh_xmalloc(field_size = 256);
438
439         /*
440         ** Scan the draft file for an attachment header field name.
441         */
442         length = strlen(attach_hdr);
443         has_attachment = 0;
444         while (get_line() != EOF && *field != '\0' && *field != '-') {
445                 if (strncasecmp(field, attach_hdr, length)==0 &&
446                                 field[length] == ':') {
447                         has_attachment = 1;
448                 }
449         }
450
451         /*
452         ** Look for at least one non-blank line in the body of the
453         ** message which indicates content in the body.
454         ** Check if body contains at least one non-blank (= not empty)
455         ** and if it contains any non-ASCII chars (= need MIME).
456         */
457         has_body = 0;
458         non_ascii = 0;
459         while (get_line() != EOF) {
460                 for (p = field; *p != '\0'; p++) {
461                         if (*p != ' ' && *p != '\t') {
462                                 has_body = 1;
463                         }
464                         if (*p > 127 || *p < 0) {
465                                 non_ascii = 1;
466                         }
467                 }
468                 if (has_body && non_ascii) {
469                         break;  /* that's been already enough information */
470                 }
471         }
472
473         if (!has_attachment && non_ascii==0) {
474                 /* We don't need to convert it to MIME. */
475                 return DONE;
476         }
477
478         /*
479         ** Else: mimify
480         */
481
482         /* Make names for the temporary files.  */
483         strncpy(body_file_name,
484                         m_mktemp(toabsdir(invo_name), NULL, NULL),
485                         sizeof (body_file_name));
486         strncpy(composition_file_name,
487                         m_mktemp(toabsdir(invo_name), NULL, NULL),
488                         sizeof (composition_file_name));
489
490         if (has_body) {
491                 body_file = fopen(body_file_name, "w");
492         }
493         composition_file = fopen(composition_file_name, "w");
494
495         if ((has_body && !body_file) || !composition_file) {
496                 clean_up_temporary_files();
497                 adios(NULL, "unable to open all of the temporary files.");
498         }
499
500         /* Copy non-attachment header fields to the temp composition file. */
501         rewind(draft_file);
502         while (get_line() != EOF && *field && *field != '-') {
503                 if (strncasecmp(field, attach_hdr, length) != 0 ||
504                                 field[length] != ':') {
505                         fprintf(composition_file, "%s\n", field);
506                 }
507         }
508         fputs("--------\n", composition_file);
509
510         if (has_body) {
511                 /* Copy the message body to the temporary file. */
512                 while ((c = getc(draft_file)) != EOF) {
513                         putc(c, body_file);
514                 }
515                 fclose(body_file);
516
517                 /* Add a mhbuild MIME composition file line for the body */
518                 /* charset will be discovered/guessed by mhbuild */
519                 fprintf(composition_file, "#text/plain %s\n", body_file_name);
520         }
521
522         /*
523         ** Now, go back to the beginning of the draft file and look for
524         ** header fields that specify attachments.  Add a mhbuild MIME
525         ** composition file for each.
526         */
527         rewind(draft_file);
528         while (get_line() != EOF && *field && *field != '-') {
529                 if (strncasecmp(field, attach_hdr, length) == 0 &&
530                                 field[length] == ':') {
531                         for (p = field+length+1; *p==' ' || *p=='\t'; p++) {
532                                 continue;
533                         }
534                         if (*p == '+') {
535                                 /* forwarded message */
536                                 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
537                         } else {
538                                 /* regular attachment */
539                                 make_mime_composition_file_entry(p);
540                         }
541                 }
542         }
543         fclose(composition_file);
544
545         /* We're ready to roll! */
546         sprintf(buf, "mhbuild %s", composition_file_name);
547         if (system(buf) != 0) {
548                 /* some problem */
549                 clean_up_temporary_files();
550                 return (NOTOK);
551         }
552
553         return (OK);
554 }
555
556 static void
557 clean_up_temporary_files(void)
558 {
559         unlink(body_file_name);
560         unlink(composition_file_name);
561
562         return;
563 }
564
565 static int
566 get_line(void)
567 {
568         int c;  /* current character */
569         int n;  /* number of bytes in buffer */
570         char *p;
571
572         /*
573         ** Get a line from the input file, growing the field buffer as
574         ** needed.  We do this so that we can fit an entire line in the
575         ** buffer making it easy to do a string comparison on both the
576         ** field name and the field body which might be a long path name.
577         */
578         for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
579                 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
580                         ungetc(c, draft_file);
581                         c = '\n';
582                         break;
583                 }
584                 if (++n >= field_size - 1) {
585                         field = (char *)mh_xrealloc(field, field_size += 256);
586                         p = field + n - 1;
587                 }
588         }
589         *p = '\0';
590
591         return (c);
592 }
593
594 static void
595 make_mime_composition_file_entry(char *file_name)
596 {
597         FILE *fp;
598         struct node *np;
599         char *cp;
600         char content_type[BUFSIZ];
601         char cmdbuf[BUFSIZ];
602         char *cmd = mimetypequeryproc;
603
604         for (np = m_defs; np; np = np->n_next) {
605                 if (strcasecmp(np->n_name, mimetypequery)==0) {
606                         cmd = np->n_field;
607                         break;
608                 }
609         }
610         snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
611
612         if (!(fp = popen(cmdbuf, "r"))) {
613                 clean_up_temporary_files();
614                 adios(NULL, "unable to determine content type with `%s'",
615                                 cmdbuf);
616         }
617         if (fgets(content_type, sizeof content_type, fp) &&
618                         (cp = strrchr(content_type, '\n'))) {
619                 *cp = '\0';
620         } else {
621                 strcpy(content_type, "application/octet-stream");
622                 admonish(NULL, "problems with `%s', using fall back type `%s'",
623                                 cmdbuf, content_type);
624         }
625         pclose(fp);
626
627         /* TODO: don't use access(2) because it checks for ruid, not euid */
628         if (access(file_name, R_OK) != 0) {
629                 clean_up_temporary_files();
630                 adios(NULL, "unable to access file `%s'", file_name);
631         }
632
633         fprintf(composition_file, "#%s; name=\"%s\" <>{attachment}",
634                 content_type,
635                 (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1);
636
637         fprintf(composition_file, " %s\n", file_name);
638
639         return;
640 }
641
642 /*
643 ** The back-end of the message sending back-end.
644 ** Annotate original message, and call `spost' to send message.
645 */
646 static int
647 sendaux(char **vec, int vecp, char *drft, struct stat *st)
648 {
649         pid_t child_id;
650         int status, fd;
651         char backup[BUFSIZ];
652
653         /*
654         ** fd collects the output of spost, and is used for the
655         ** failure notice if we need to send one in alert().
656         */
657         fd = pushsw ? tmp_fd() : NOTOK;
658
659         vec[vecp++] = drft;
660         if (distfile && distout(drft, distfile, backup) == NOTOK) {
661                 done(1);
662         }
663         vec[vecp] = NULL;
664
665         switch (child_id = fork()) {
666         case -1:
667                 /* oops -- fork error */
668                 adios("fork", "unable to");
669                 break;  /* NOT REACHED */
670
671         case 0:
672                 /*
673                 ** child process -- send it
674                 **
675                 ** If fd is ok, then we are pushing and fd points to temp
676                 ** file, so capture anything on stdout and stderr there.
677                 */
678                 if (fd != NOTOK) {
679                         dup2(fd, fileno(stdout));
680                         dup2(fd, fileno(stderr));
681                         close(fd);
682                 }
683                 execvp(*vec, vec);
684                 fprintf(stderr, "unable to exec ");
685                 perror(*vec);
686                 _exit(-1);
687                 break;  /* NOT REACHED */
688
689         default:
690                 /* parent process -- wait for it */
691                 if ((status = pidwait(child_id, NOTOK)) == OK) {
692                         if (annotext) {
693                                 anno(st);
694                         }
695                 } else {
696                         /*
697                         ** If spost failed, and we have good fd (which
698                         ** means we pushed), then mail error message
699                         ** (and possibly the draft) back to the user.
700                         */
701                         if (fd != NOTOK) {
702                                 alert(drft, fd);
703                                 close(fd);
704                         } else {
705                                 advise(NULL, "message not delivered to anyone");
706                         }
707                         if (distfile) {
708                                 unlink(drft);
709                                 if (rename(backup, drft) == NOTOK) {
710                                         advise(drft, "unable to rename %s to",
711                                                         backup);
712                                 }
713                         }
714                 }
715                 break;
716         }
717
718         return status;
719 }
720
721
722 /*
723 ** Mail error notification (and possibly a copy of the
724 ** message) back to the user, using mhmail(1).
725 */
726 static void
727 alert(char *file, int out)
728 {
729         pid_t child_id;
730         int in;
731         char buf[BUFSIZ];
732
733         switch (child_id = fork()) {
734         case NOTOK:
735                 /* oops -- fork error */
736                 advise("fork", "unable to");
737
738         case OK:
739                 /* child process -- send it */
740                 SIGNAL(SIGHUP, SIG_IGN);
741                 SIGNAL(SIGINT, SIG_IGN);
742                 SIGNAL(SIGQUIT, SIG_IGN);
743                 SIGNAL(SIGTERM, SIG_IGN);
744                 if (forwsw) {
745                         if ((in = open(file, O_RDONLY)) == NOTOK) {
746                                 admonish(file, "unable to re-open");
747                         } else {
748                                 lseek(out, (off_t) 0, SEEK_END);
749                                 strncpy(buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
750                                 write(out, buf, strlen(buf));
751                                 strncpy(buf, "\n------- Unsent Draft\n\n", sizeof(buf));
752                                 write(out, buf, strlen(buf));
753                                 cpydgst(in, out, file, "temporary file");
754                                 close(in);
755                                 strncpy(buf, "\n------- End of Unsent Draft\n", sizeof(buf));
756                                 write(out, buf, strlen(buf));
757                                 if (rename(file, strncpy(buf, m_backup(file), sizeof(buf))) == NOTOK) {
758                                         admonish(buf, "unable to rename %s to", file);
759                                 }
760                         }
761                 }
762                 lseek(out, (off_t) 0, SEEK_SET);
763                 dup2(out, fileno(stdin));
764                 close(out);
765                 /* create subject for error notification */
766                 snprintf(buf, sizeof(buf), "send failed on %s",
767                                 forwsw ? "enclosed draft" : file);
768                 execlp("mhmail", "mhmail", getusername(),
769                                 "-subject", buf, NULL);
770                 fprintf(stderr, "unable to exec ");
771                 perror("mhmail");
772                 _exit(-1);
773
774         default:  /* no waiting... */
775                 break;
776         }
777 }
778
779
780 static int
781 tmp_fd(void)
782 {
783         int fd;
784         char *tfile = NULL;
785
786         tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
787         if (tfile == NULL) return NOTOK;
788         fchmod(fd, 0600);
789
790         if (debugsw) {
791                 advise(NULL, "temporary file %s selected", tfile);
792         } else {
793                 if (unlink(tfile) == NOTOK) {
794                         advise(tfile, "unable to remove");
795                 }
796         }
797
798         return fd;
799 }
800
801
802 static void
803 anno(struct stat *st)
804 {
805         struct stat st2;
806         char *msgs, *folder;
807         char buf[BUFSIZ];
808
809         if (altmsg && (stat(altmsg, &st2) == NOTOK ||
810                         st->st_mtime != st2.st_mtime ||
811                         st->st_dev != st2.st_dev ||
812                         st->st_ino != st2.st_ino)) {
813                 if (debugsw) {
814                         admonish(NULL, "$mhaltmsg mismatch");
815                 }
816                 return;
817         }
818
819         if (!(folder = getenv("mhfolder")) || !*folder) {
820                 if (debugsw) {
821                         admonish(NULL, "$mhfolder not set");
822                 }
823                 return;
824         }
825         if (!(msgs = getenv("mhmessages")) || !*msgs) {
826                 if (debugsw) {
827                         admonish(NULL, "$mhmessages not set");
828                 }
829                 return;
830         }
831         if (debugsw) {
832                 advise(NULL, "annotate as `%s': %s %s", annotext,
833                                 folder, msgs);
834         }
835         snprintf(buf, sizeof buf, "anno -comp '%s' '+%s' %s",
836                         annotext, folder, msgs);
837         if (system(buf) != 0) {
838                 advise(NULL, "unable to annotate");
839         }
840 }
841
842
843 static void
844 armed_done(int status)
845 {
846         longjmp(env, status ? status : NOTOK);
847
848         exit(status);
849 }