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