Trivial whitespace changes.
[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                 if ((in = open(altmsg, O_RDONLY)) == NOTOK) {
256                         adios(altmsg, "unable to open for reading");
257                 }
258                 fstat(in, &st2);
259                 distfile = getcpy(m_mktemp2(NULL, invo_name, NULL, NULL));
260                 if ((out = creat(distfile, (int)st2.st_mode & 0777))==NOTOK) {
261                         adios(distfile, "unable to open for writing");
262                 }
263                 cpydata(in, out, altmsg, distfile);
264                 close(in);
265                 close(out);
266         } else {
267                 distfile = NULL;
268         }
269
270         if (!altmsg || stat(altmsg, &st) == NOTOK) {
271                 st.st_mtime = 0;
272                 st.st_dev = 0;
273                 st.st_ino = 0;
274         }
275         if (pushsw) {
276                 push();
277         }
278         status = 0;
279         vec[0] = mhbasename(postproc);
280         closefds(3);
281
282         for (msgnum = 0; msgnum < nfiles; msgnum++) {
283                 switch (sendsbr(vec, vecp, files[msgnum], &st, 1)) {
284                 case DONE:
285                         done(++status);
286                 case NOTOK:
287                         status++;  /* fall */
288                 case OK:
289                         break;
290                 }
291         }
292
293         context_save();
294         done(status);
295         return 1;
296 }
297
298
299 /*
300 ** message sending back-end
301 */
302 static int
303 sendsbr(char **vec, int vecp, char *drft, struct stat *st, int rename_drft)
304 {
305         int status;
306         char buffer[BUFSIZ];
307         char *original_draft;
308         char *p;  /* string pointer for building file name */
309
310         /*
311         ** Save the original name of the draft file.  The name of the
312         ** draft file is changed to a temporary file containing the built
313         ** MIME message if there are attachments.  We need the original
314         ** name so that it can be renamed after the message is sent.
315         */
316         original_draft = drft;
317
318         /*
319         ** Convert the draft to a MIME message.
320         ** Use the mhbuild composition file for the draft if there was
321         ** a successful conversion because that now contains the MIME
322         ** message.  A nice side effect of this is that it leaves the
323         ** original draft file untouched so that it can be retrieved
324         ** and modified if desired.
325         */
326         switch (attach(drft)) {
327         case OK:
328                 drft = composition_file_name;
329                 break;
330
331         case NOTOK:
332                 return (NOTOK);
333
334         case DONE:
335                 break;
336         }
337
338         done=armed_done;
339         switch (setjmp(env)) {
340         case OK:
341                 status = sendaux(vec, vecp, drft, st) ? NOTOK : OK;
342                 /* rename the original draft */
343                 if (rename_drft && status == OK &&
344                                 rename(original_draft, strncpy(buffer,
345                                 m_backup(original_draft), sizeof(buffer)))
346                                 == NOTOK) {
347                         advise(buffer, "unable to rename %s to", drft);
348                 }
349                 break;
350
351         default:
352                 status = DONE;
353                 break;
354         }
355
356         done=exit;
357         if (distfile) {
358                 unlink(distfile);
359         }
360
361         /*
362         ** Get rid of any temporary files that we created for attachments.
363         ** Also get rid of the renamed composition file that mhbuild
364         ** leaves as a turd.  It looks confusing, but we use the body
365         ** file name to help build the renamed composition file name.
366         */
367         if (drft == composition_file_name) {
368                 clean_up_temporary_files();
369
370                 if (strlen(composition_file_name) >=
371                                 sizeof (composition_file_name) - 6) {
372                         advise(NULL, "unable to remove original composition file.");
373                 } else {
374                         if (!(p = strrchr(composition_file_name, '/'))) {
375                                 p = composition_file_name;
376                         } else {
377                                 p++;
378                         }
379                         strcpy(body_file_name, p);
380                         *p++ = ',';
381                         strcpy(p, body_file_name);
382                         strcat(p, ".orig");
383
384                         unlink(composition_file_name);
385                 }
386         }
387
388         return status;
389 }
390
391 static int
392 attach(char *draft_file_name)
393 {
394         char buf[MAXPATHLEN + 6];
395         int c;
396         int has_attachment;
397         int has_body;
398         int non_ascii; /* msg body contains non-ASCII chars */
399         int length;  /* length of attachment header field name */
400         char *p;
401
402         if (!(draft_file = fopen(draft_file_name, "r"))) {
403                 adios(NULL, "can't open draft file `%s'.", draft_file_name);
404         }
405
406         /* We'll grow the buffer as needed. */
407         field = (char *)mh_xmalloc(field_size = 256);
408
409         /*
410         ** Scan the draft file for an attachment header field name.
411         */
412         length = strlen(attach_hdr);
413         has_attachment = 0;
414         while (get_line() != EOF && *field != '\0' && *field != '-') {
415                 if (strncasecmp(field, attach_hdr, length)==0 &&
416                                 field[length] == ':') {
417                         has_attachment = 1;
418                 }
419         }
420
421         /*
422         ** Look for at least one non-blank line in the body of the
423         ** message which indicates content in the body.
424         ** Check if body contains at least one non-blank (= not empty)
425         ** and if it contains any non-ASCII chars (= need MIME).
426         */
427         has_body = 0;
428         non_ascii = 0;
429         while (get_line() != EOF) {
430                 for (p = field; *p != '\0'; p++) {
431                         if (*p != ' ' && *p != '\t') {
432                                 has_body = 1;
433                         }
434                         if (*p > 127 || *p < 0) {
435                                 non_ascii = 1;
436                         }
437                 }
438                 if (has_body && non_ascii) {
439                         break;  /* that's been already enough information */
440                 }
441         }
442
443         if (!has_attachment && non_ascii==0) {
444                 /* We don't need to convert it to MIME. */
445                 return DONE;
446         }
447
448         /*
449         ** Else: mimify
450         */
451
452         /* Make names for the temporary files.  */
453         strncpy(body_file_name,
454                         m_mktemp(toabsdir(invo_name), NULL, NULL),
455                         sizeof (body_file_name));
456         strncpy(composition_file_name,
457                         m_mktemp(toabsdir(invo_name), NULL, NULL),
458                         sizeof (composition_file_name));
459
460         if (has_body) {
461                 body_file = fopen(body_file_name, "w");
462         }
463         composition_file = fopen(composition_file_name, "w");
464
465         if ((has_body && !body_file) || !composition_file) {
466                 clean_up_temporary_files();
467                 adios(NULL, "unable to open all of the temporary files.");
468         }
469
470         /* Copy non-attachment header fields to the temp composition file. */
471         rewind(draft_file);
472         while (get_line() != EOF && *field && *field != '-') {
473                 if (strncasecmp(field, attach_hdr, length) != 0 ||
474                                 field[length] != ':') {
475                         fprintf(composition_file, "%s\n", field);
476                 }
477         }
478         fputs("--------\n", composition_file);
479
480         if (has_body) {
481                 /* Copy the message body to the temporary file. */
482                 while ((c = getc(draft_file)) != EOF) {
483                         putc(c, body_file);
484                 }
485                 fclose(body_file);
486
487                 /* Add a mhbuild MIME composition file line for the body */
488                 /* charset will be discovered/guessed by buildmimeproc */
489                 fprintf(composition_file, "#text/plain %s\n", body_file_name);
490         }
491
492         /*
493         ** Now, go back to the beginning of the draft file and look for
494         ** header fields that specify attachments.  Add a mhbuild MIME
495         ** composition file for each.
496         */
497         rewind(draft_file);
498         while (get_line() != EOF && *field && *field != '-') {
499                 if (strncasecmp(field, attach_hdr, length) == 0 &&
500                                 field[length] == ':') {
501                         for (p = field+length+1; *p==' ' || *p=='\t'; p++) {
502                                 continue;
503                         }
504                         if (*p == '+') {
505                                 /* forwarded message */
506                                 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
507                         } else {
508                                 /* regular attachment */
509                                 make_mime_composition_file_entry(p);
510                         }
511                 }
512         }
513         fclose(composition_file);
514
515         /*
516         ** We're ready to roll!  Run mhbuild on the composition file.
517         ** Note that mhbuild is in the context as buildmimeproc.
518         */
519         sprintf(buf, "%s %s", buildmimeproc, composition_file_name);
520         if (system(buf) != 0) {
521                 /* some problem */
522                 clean_up_temporary_files();
523                 return (NOTOK);
524         }
525
526         return (OK);
527 }
528
529 static void
530 clean_up_temporary_files(void)
531 {
532         unlink(body_file_name);
533         unlink(composition_file_name);
534
535         return;
536 }
537
538 static int
539 get_line(void)
540 {
541         int c;  /* current character */
542         int n;  /* number of bytes in buffer */
543         char *p;
544
545         /*
546         ** Get a line from the input file, growing the field buffer as
547         ** needed.  We do this so that we can fit an entire line in the
548         ** buffer making it easy to do a string comparison on both the
549         ** field name and the field body which might be a long path name.
550         */
551         for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
552                 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
553                         ungetc(c, draft_file);
554                         c = '\n';
555                         break;
556                 }
557                 if (++n >= field_size - 1) {
558                         field = (char *)mh_xrealloc(field, field_size += 256);
559                         p = field + n - 1;
560                 }
561         }
562         *p = '\0';
563
564         return (c);
565 }
566
567 static void
568 make_mime_composition_file_entry(char *file_name)
569 {
570         FILE *fp;
571         struct node *np;
572         char *cp;
573         char content_type[BUFSIZ];
574         char cmdbuf[BUFSIZ];
575         char *cmd = mimetypequeryproc;
576
577         for (np = m_defs; np; np = np->n_next) {
578                 if (strcasecmp(np->n_name, mimetypequery)==0) {
579                         cmd = np->n_field;
580                         break;
581                 }
582         }
583         snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
584
585         if (!(fp = popen(cmdbuf, "r"))) {
586                 clean_up_temporary_files();
587                 adios(NULL, "unable to determine content type with `%s'",
588                                 cmdbuf);
589         }
590         if (fgets(content_type, sizeof content_type, fp) &&
591                         (cp = strrchr(content_type, '\n'))) {
592                 *cp = '\0';
593         } else {
594                 strcpy(content_type, "application/octet-stream");
595                 admonish(NULL, "problems with `%s', using fall back type `%s'",
596                                 cmdbuf, content_type);
597         }
598         pclose(fp);
599
600         /* TODO: don't use access(2) because it checks for ruid, not euid */
601         if (access(file_name, R_OK) != 0) {
602                 clean_up_temporary_files();
603                 adios(NULL, "unable to access file `%s'", file_name);
604         }
605
606         fprintf(composition_file, "#%s; name=\"%s\" <>{attachment}",
607                 content_type,
608                 (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1);
609
610         fprintf(composition_file, " %s\n", file_name);
611
612         return;
613 }
614
615 /*
616 ** The back-end of the message sending back-end.
617 ** Annotate original message, and call `postproc' to send message.
618 */
619 static int
620 sendaux(char **vec, int vecp, char *drft, struct stat *st)
621 {
622         pid_t child_id;
623         int i, status, fd;
624         char backup[BUFSIZ];
625
626         /*
627         ** fd collects the output of postproc, and is used for the
628         ** failure notice if we need to send one in alert().
629         */
630         fd = pushsw ? tmp_fd() : NOTOK;
631
632         vec[vecp++] = drft;
633         if (distfile && distout(drft, distfile, backup) == NOTOK) {
634                 done(1);
635         }
636         vec[vecp] = NULL;
637
638         for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++) {
639                 sleep(5);
640         }
641         switch (child_id) {
642         case -1:
643                 /* oops -- fork error */
644                 adios("fork", "unable to");
645                 break;  /* NOT REACHED */
646
647         case 0:
648                 /*
649                 ** child process -- send it
650                 **
651                 ** If fd is ok, then we are pushing and fd points to temp
652                 ** file, so capture anything on stdout and stderr there.
653                 */
654                 if (fd != NOTOK) {
655                         dup2(fd, fileno(stdout));
656                         dup2(fd, fileno(stderr));
657                         close(fd);
658                 }
659                 execvp(postproc, vec);
660                 fprintf(stderr, "unable to exec ");
661                 perror(postproc);
662                 _exit(-1);
663                 break;  /* NOT REACHED */
664
665         default:
666                 /* parent process -- wait for it */
667                 if ((status = pidwait(child_id, NOTOK)) == OK) {
668                         if (annotext) {
669                                 anno(st);
670                         }
671                 } else {
672                         /*
673                         ** If postproc failed, and we have good fd (which
674                         ** means we pushed), then mail error message
675                         ** (and possibly the draft) back to the user.
676                         */
677                         if (fd != NOTOK) {
678                                 alert(drft, fd);
679                                 close(fd);
680                         } else {
681                                 advise(NULL, "message not delivered to anyone");
682                         }
683                         if (distfile) {
684                                 unlink(drft);
685                                 if (rename(backup, drft) == NOTOK) {
686                                         advise(drft, "unable to rename %s to",
687                                                         backup);
688                                 }
689                         }
690                 }
691                 break;
692         }
693
694         return status;
695 }
696
697
698 /*
699 ** Mail error notification (and possibly a copy of the
700 ** message) back to the user, using the mailproc
701 */
702 static void
703 alert(char *file, int out)
704 {
705         pid_t child_id;
706         int i, in;
707         char buf[BUFSIZ];
708
709         for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++) {
710                 sleep(5);
711         }
712
713         switch (child_id) {
714         case NOTOK:
715                 /* oops -- fork error */
716                 advise("fork", "unable to");
717
718         case OK:
719                 /* child process -- send it */
720                 SIGNAL(SIGHUP, SIG_IGN);
721                 SIGNAL(SIGINT, SIG_IGN);
722                 SIGNAL(SIGQUIT, SIG_IGN);
723                 SIGNAL(SIGTERM, SIG_IGN);
724                 if (forwsw) {
725                         if ((in = open(file, O_RDONLY)) == NOTOK) {
726                                 admonish(file, "unable to re-open");
727                         } else {
728                                 lseek(out, (off_t) 0, SEEK_END);
729                                 strncpy(buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
730                                 write(out, buf, strlen(buf));
731                                 strncpy(buf, "\n------- Unsent Draft\n\n", sizeof(buf));
732                                 write(out, buf, strlen(buf));
733                                 cpydgst(in, out, file, "temporary file");
734                                 close(in);
735                                 strncpy(buf, "\n------- End of Unsent Draft\n", sizeof(buf));
736                                 write(out, buf, strlen(buf));
737                                 if (rename(file, strncpy(buf, m_backup(file), sizeof(buf))) == NOTOK) {
738                                         admonish(buf, "unable to rename %s to", file);
739                                 }
740                         }
741                 }
742                 lseek(out, (off_t) 0, SEEK_SET);
743                 dup2(out, fileno(stdin));
744                 close(out);
745                 /* create subject for error notification */
746                 snprintf(buf, sizeof(buf), "send failed on %s",
747                                 forwsw ? "enclosed draft" : file);
748                 execlp(mailproc, mhbasename(mailproc), getusername(),
749                                 "-subject", buf, NULL);
750                 fprintf(stderr, "unable to exec ");
751                 perror(mailproc);
752                 _exit(-1);
753
754         default:  /* no waiting... */
755                 break;
756         }
757 }
758
759
760 static int
761 tmp_fd(void)
762 {
763         int fd;
764         char *tfile = NULL;
765
766         tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
767         if (tfile == NULL) return NOTOK;
768         fchmod(fd, 0600);
769
770         if (debugsw) {
771                 advise(NULL, "temporary file %s selected", tfile);
772         } else {
773                 if (unlink(tfile) == NOTOK) {
774                         advise(tfile, "unable to remove");
775                 }
776         }
777
778         return fd;
779 }
780
781
782 static void
783 anno(struct stat *st)
784 {
785         struct stat st2;
786         char *msgs, *folder;
787         char buf[BUFSIZ];
788
789         if (altmsg && (stat(altmsg, &st2) == NOTOK ||
790                         st->st_mtime != st2.st_mtime ||
791                         st->st_dev != st2.st_dev ||
792                         st->st_ino != st2.st_ino)) {
793                 if (debugsw) {
794                         admonish(NULL, "$mhaltmsg mismatch");
795                 }
796                 return;
797         }
798
799         if (!(folder = getenv("mhfolder")) || !*folder) {
800                 if (debugsw) {
801                         admonish(NULL, "$mhfolder not set");
802                 }
803                 return;
804         }
805         if (!(msgs = getenv("mhmessages")) || !*msgs) {
806                 if (debugsw) {
807                         admonish(NULL, "$mhmessages not set");
808                 }
809                 return;
810         }
811         if (debugsw) {
812                 advise(NULL, "annotate as `%s': %s %s", annotext,
813                                 folder, msgs);
814         }
815         snprintf(buf, sizeof buf, "anno -comp '%s' '+%s' %s",
816                         annotext, folder, msgs);
817         if (system(buf) != 0) {
818                 advise(NULL, "unable to annotate");
819         }
820 }
821
822
823 static void
824 armed_done(int status)
825 {
826         longjmp(env, status ? status : NOTOK);
827
828         exit(status);
829 }