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