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