Renamed r1bindex() to mhbasename(), to make its function becomes clear.
[mmh] / uip / sendsbr.c
1 /*
2 ** sendsbr.c -- routines to help WhatNow/Send along
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 <h/signals.h>
11 #include <setjmp.h>
12 #include <signal.h>
13 #include <fcntl.h>
14 #include <h/mime.h>
15 #include <h/tws.h>
16 #include <h/utils.h>
17
18 #ifdef TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # ifdef TM_IN_SYS_TIME
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 int debugsw = 0;  /* global */
30 int forwsw  = 1;
31 int inplace = 1;
32 int pushsw  = 0;
33 int unique  = 0;
34 int verbsw  = 0;
35
36 char *altmsg   = NULL;  /*  .. */
37 char *annotext = NULL;
38 char *distfile = NULL;
39
40 static jmp_buf env;
41
42 /* name of temp file for body content */
43 static char body_file_name[MAXPATHLEN + 1];
44 /* name of mhbuild composition temporary file */
45 static char composition_file_name[MAXPATHLEN + 1]; 
46 static int field_size;  /* size of header field buffer */
47 static char *field;  /* header field buffer */
48 static FILE *draft_file;  /* draft file pointer */
49 static FILE *body_file;  /* body file pointer */
50 static FILE *composition_file;  /* composition file pointer */
51
52 /*
53 ** external prototypes
54 */
55 int sendsbr(char **, int, char *, struct stat *, int, char *, int);
56 char *getusername(void);
57
58 /*
59 ** static prototypes
60 */
61 static void armed_done(int) NORETURN;
62 static void alert(char *, int);
63 static int tmp_fd(void);
64 static void anno(int, struct stat *);
65 static void annoaux(int);
66 static int sendaux(char **, int, char *, struct stat *);
67
68 static int attach(char *, char *, int);
69 static void clean_up_temporary_files(void);
70 static int get_line(void);
71 static void make_mime_composition_file_entry(char *, int);
72
73
74 /*
75 ** Entry point into (back-end) routines to send message.
76 */
77
78 int
79 sendsbr(char **vec, int vecp, char *drft, struct stat *st,
80         int rename_drft, char *attachment_header_field_name, int attachformat)
81 {
82         int status;
83         char buffer[BUFSIZ], file[BUFSIZ];
84         char *original_draft;  /* name of original draft file */
85         char *p;  /* string pointer for building file name */
86
87         /*
88         ** Save the original name of the draft file.  The name of the
89         ** draft file is changed to a temporary file containing the built
90         ** MIME message if there are attachments.  We need the original
91         ** name so that it can be renamed after the message is sent.
92         */
93
94         original_draft = drft;
95
96         /*
97         ** There might be attachments if a header field name for
98         ** attachments is supplied.  Convert the draft to a MIME message.
99         ** Use the mhbuild composition file for the draft if there was
100         ** a successful conversion because that now contains the MIME
101         ** message.  A nice side effect of this is that it leaves the
102         ** original draft file untouched so that it can be retrieved
103         ** and modified if desired.
104         */
105
106         if (attachment_header_field_name != (char *)0) {
107                 switch (attach(attachment_header_field_name, drft,
108                                 attachformat)) {
109                 case OK:
110                         drft = composition_file_name;
111                         break;
112
113                 case NOTOK:
114                         return (NOTOK);
115
116                 case DONE:
117                         break;
118                 }
119         }
120
121         done=armed_done;
122         switch (setjmp(env)) {
123         case OK:
124                 /*
125                 ** If given -push and -unique (which is undocumented), then
126                 ** rename the draft file.  I'm not quite sure why.
127                 */
128                 if (pushsw && unique) {
129                         char *cp = m_mktemp2(drft, invo_name, NULL, NULL);
130                         if (cp == NULL) {
131                                 adios("sendsbr", "unable to create temporary file");
132                         }
133                         if (rename(drft, strncpy(file, cp, sizeof(file)))
134                                         == NOTOK)
135                                 adios(file, "unable to rename %s to", drft);
136                         drft = file;
137                 }
138
139                 status = sendaux(vec, vecp, drft, st) ? NOTOK : OK;
140
141                 /* rename the original draft */
142                 if (rename_drft && status == OK &&
143                                 rename(original_draft, strncpy(buffer,
144                                 m_backup(original_draft), sizeof(buffer)))
145                                 == NOTOK)
146                         advise(buffer, "unable to rename %s to", drft);
147                 break;
148
149         default:
150                 status = DONE;
151                 break;
152         }
153
154         done=exit;
155         if (distfile)
156                 unlink(distfile);
157
158         /*
159         ** Get rid of any temporary files that we created for attachments.
160         ** Also get rid of the renamed composition file that mhbuild
161         ** leaves as a turd.  It looks confusing, but we use the body
162         ** file name to help build the renamed composition file name.
163         */
164
165         if (drft == composition_file_name) {
166                 clean_up_temporary_files();
167
168                 if (strlen(composition_file_name) >=
169                                 sizeof (composition_file_name) - 6)
170                         advise((char *)0, "unable to remove original composition file.");
171
172                 else {
173                         if ((p = strrchr(composition_file_name, '/')) == NULL)
174                                 p = composition_file_name;
175                         else
176                                 p++;
177
178                         (void)strcpy(body_file_name, p);
179                         *p++ = ',';
180                         (void)strcpy(p, body_file_name);
181                         (void)strcat(p, ".orig");
182
183                         (void)unlink(composition_file_name);
184                 }
185         }
186
187         return status;
188 }
189
190 static int
191 attach(char *attachment_header_field_name, char *draft_file_name,
192         int attachformat)
193 {
194         char buf[MAXPATHLEN + 6];  /* miscellaneous buffer */
195         int c;  /* current character for body copy */
196         int has_attachment;  /* draft has at least one attachment */
197         int has_body;  /* draft has a message body */
198         int length;  /* length of attachment header field name */
199         char *p;  /* miscellaneous string pointer */
200
201         /*
202         ** Open up the draft file.
203         */
204
205         if ((draft_file = fopen(draft_file_name, "r")) == (FILE *)0)
206                 adios((char *)0, "can't open draft file `%s'.",
207                                 draft_file_name);
208
209         /*
210         **  Allocate a buffer to hold the header components as they're read in.
211         **  This buffer might need to be quite large, so we grow it as needed.
212         */
213
214         field = (char *)mh_xmalloc(field_size = 256);
215
216         /*
217         ** Scan the draft file for a header field name that matches the
218         ** -attach argument.  The existence of one indicates that the
219         ** draft has attachments.  Bail out if there are no attachments
220         ** because we're done.  Read to the end of the headers even if
221         ** we have no attachments.
222         */
223
224         length = strlen(attachment_header_field_name);
225
226         has_attachment = 0;
227
228         while (get_line() != EOF && *field != '\0' && *field != '-')
229                 if (strncasecmp(field, attachment_header_field_name, length)
230                                 == 0 && field[length] == ':')
231                         has_attachment = 1;
232
233         if (has_attachment == 0)
234                 return (DONE);
235
236         /*
237         ** We have at least one attachment.  Look for at least one
238         ** non-blank line in the body of the message which indicates
239         ** content in the body.
240         */
241
242         has_body = 0;
243
244         while (get_line() != EOF) {
245                 for (p = field; *p != '\0'; p++) {
246                         if (*p != ' ' && *p != '\t') {
247                                 has_body = 1;
248                                 break;
249                         }
250                 }
251
252                 if (has_body)
253                         break;
254         }
255
256         /*
257         ** Make names for the temporary files.
258         */
259
260         (void)strncpy(body_file_name,
261                         m_mktemp(m_maildir(invo_name), NULL, NULL),
262                         sizeof (body_file_name));
263         (void)strncpy(composition_file_name,
264                         m_mktemp(m_maildir(invo_name), NULL, NULL),
265                         sizeof (composition_file_name));
266
267         if (has_body)
268                 body_file = fopen(body_file_name, "w");
269
270         composition_file = fopen(composition_file_name, "w");
271
272         if ((has_body && body_file == (FILE *)0) ||
273                         composition_file == (FILE *)0) {
274                 clean_up_temporary_files();
275                 adios((char *)0, "unable to open all of the temporary files.");
276         }
277
278         /*
279         ** Start at the beginning of the draft file.  Copy all
280         ** non-attachment header fields to the temporary composition file.
281         ** Then add the dashed line separator.
282         */
283
284         rewind(draft_file);
285
286         while (get_line() != EOF && *field != '\0' && *field != '-')
287                 if (strncasecmp(field, attachment_header_field_name, length)
288                                 != 0 || field[length] != ':')
289                         (void)fprintf(composition_file, "%s\n", field);
290
291         (void)fputs("--------\n", composition_file);
292
293         /*
294         ** Copy the message body to a temporary file.
295         */
296
297         if (has_body) {
298                 while ((c = getc(draft_file)) != EOF)
299                                 putc(c, body_file);
300
301                 (void)fclose(body_file);
302         }
303
304         /*
305         ** Add a mhbuild MIME composition file line for the body if
306         ** there was one.
307         */
308
309         if (has_body)
310                 make_mime_composition_file_entry(body_file_name, attachformat);
311
312         /*
313         ** Now, go back to the beginning of the draft file and look for
314         ** header fields that specify attachments.  Add a mhbuild MIME
315         ** composition file for each.
316         */
317
318         rewind(draft_file);
319
320         while (get_line() != EOF && *field != '\0' && *field != '-') {
321                 if (strncasecmp(field, attachment_header_field_name, length)
322                                 == 0 && field[length] == ':') {
323                         for (p = field + length + 1; *p == ' ' || *p == '\t';
324                                         p++)
325                                 ;
326
327                         make_mime_composition_file_entry(p, attachformat);
328                 }
329         }
330
331         (void)fclose(composition_file);
332
333         /*
334         ** We're ready to roll!  Run mhbuild on the composition file.
335         ** Note that mhbuild is in the context as buildmimeproc.
336         */
337
338         (void)sprintf(buf, "%s %s", buildmimeproc, composition_file_name);
339
340         if (system(buf) != 0) {
341                 clean_up_temporary_files();
342                 return (NOTOK);
343         }
344
345         return (OK);
346 }
347
348 static void
349 clean_up_temporary_files(void)
350 {
351         (void)unlink(body_file_name);
352         (void)unlink(composition_file_name);
353
354         return;
355 }
356
357 static int
358 get_line(void)
359 {
360         int c;  /* current character */
361         int n;  /* number of bytes in buffer */
362         char *p;  /* buffer pointer */
363
364         /*
365         ** Get a line from the input file, growing the field buffer as
366         ** needed.  We do this so that we can fit an entire line in the
367         ** buffer making it easy to do a string comparison on both the
368         ** field name and the field body which might be a long path name.
369         */
370
371         for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
372                 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
373                         (void)ungetc(c, draft_file);
374                         c = '\n';
375                         break;
376                 }
377
378                 if (++n >= field_size - 1) {
379                         field = (char *)mh_xrealloc((void *)field, field_size += 256);
380
381                         p = field + n - 1;
382                 }
383         }
384
385         /* NUL-terminate the field. */
386         *p = '\0';
387
388         return (c);
389 }
390
391 static void
392 make_mime_composition_file_entry(char *file_name, int attachformat)
393 {
394         int binary;  /* binary character found flag */
395         int c;  /* current character */
396         char cmd[MAXPATHLEN + 6];  /* file command buffer */
397         char *content_type;  /* mime content type */
398         FILE *fp;  /* content and pipe file pointer */
399         struct node *np;  /* context scan node pointer */
400         char *p;  /* miscellaneous string pointer */
401         struct stat st;  /* file status buffer */
402
403         content_type = (char *)0;
404
405         /*
406         ** Check the file name for a suffix.  Scan the context for that
407         ** suffix on a mhshow-suffix- entry.  We use these entries to
408         ** be compatible with mhshow, and there's no reason to make the
409         ** user specify each suffix twice.  Context entries of the form
410         ** "mhshow-suffix-contenttype" in the name have the suffix in
411         ** the field, including the dot.
412         */
413
414         if ((p = strrchr(file_name, '.')) != (char *)0) {
415                 for (np = m_defs; np; np = np->n_next) {
416                         if (strncasecmp(np->n_name, "mhshow-suffix-", 14) == 0
417                                         && mh_strcasecmp(p, np->n_field) == 0)
418                                         {
419                                 content_type = np->n_name + 14;
420                                 break;
421                         }
422                 }
423         }
424
425         /*
426         ** No content type was found, either because there was no matching
427         ** entry in the context or because the file name has no suffix.
428         ** Open the file and check for non-ASCII characters.  Choose the
429         ** content type based on this check.
430         */
431
432         if (content_type == (char *)0) {
433                 if ((fp = fopen(file_name, "r")) == (FILE *)0) {
434                         clean_up_temporary_files();
435                         adios((char *)0, "unable to access file \"%s\"",
436                                         file_name);
437                 }
438
439                 binary = 0;
440
441                 while ((c = getc(fp)) != EOF) {
442                         if (c > 127 || c < 0) {
443                                 binary = 1;
444                                 break;
445                         }
446                 }
447
448                 (void)fclose(fp);
449
450                 content_type = binary ?
451                                 "application/octet-stream" : "text/plain";
452         }
453
454         /*
455         ** Make sure that the attachment file exists and is readable.
456         ** Append a mhbuild directive to the draft file.  This starts with
457         ** the content type.  Append a file name attribute and a private
458         ** x-unix-mode attribute.  Also append a description obtained
459         ** (if possible) by running the "file" command on the file.
460         */
461
462         if (stat(file_name, &st) == -1 || access(file_name, R_OK) != 0) {
463                 clean_up_temporary_files();
464                 adios((char *)0, "unable to access file \"%s\"", file_name);
465         }
466
467         switch (attachformat) {
468         case 0:
469                 /* Insert name, file mode, and Content-Id. */
470                 (void)fprintf(composition_file,
471                                 "#%s; name=\"%s\"; x-unix-mode=0%.3ho",
472                                 content_type, ((p = strrchr(file_name, '/'))
473                                 == (char *)0) ? file_name : p + 1,
474                                 (unsigned short)(st.st_mode & 0777));
475
476                 if (strlen(file_name) > MAXPATHLEN) {
477                         clean_up_temporary_files();
478                         adios((char *)0, "attachment file name `%s' too long.",
479                                         file_name);
480                 }
481
482                 (void)sprintf(cmd, "file '%s'", file_name);
483
484                 if ((fp = popen(cmd, "r")) != (FILE *)0 &&
485                                 fgets(cmd, sizeof (cmd), fp) != (char *)0) {
486                         *strchr(cmd, '\n') = '\0';
487
488                         /*
489                         **  The output of the "file" command is of the form
490                         **
491                         **       file: description
492                         **
493                         **  Strip off the "file:" and subsequent white space.
494                         */
495
496                         for (p = cmd; *p != '\0'; p++) {
497                                 if (*p == ':') {
498                                         for (p++; *p != '\0'; p++) {
499                                                 if (*p != '\t')
500                                                         break;
501                                         }
502                                         break;
503                                 }
504                         }
505
506                         if (*p != '\0')
507                                 /* Insert Content-Description. */
508                                 (void)fprintf(composition_file, " [ %s ]", p);
509
510                         (void)pclose(fp);
511                 }
512
513                 break;
514         case 1:
515                 if (stringdex(m_maildir(invo_name), file_name) == 0) {
516                         /*
517                         ** Content had been placed by send into a temp file.
518                         ** Don't generate Content-Disposition header, because
519                         ** it confuses Microsoft Outlook, Build 10.0.6626, at
520                         ** least.
521                         */
522                         fprintf(composition_file, "#%s <>", content_type);
523                 } else {
524                         /*
525                         ** Suppress Content-Id, insert simple
526                         ** Content-Disposition.
527                         */
528                         fprintf(composition_file,
529                                 "#%s; name=\"%s\" <>{attachment}",
530                                 content_type,
531                                 ((p = strrchr(file_name, '/')) == (char *)0) ?
532                                 file_name : p + 1);
533                 }
534
535                 break;
536         case 2:
537                 if (stringdex(m_maildir(invo_name), file_name) == 0) {
538                         /*
539                         ** Content had been placed by send into a temp file.
540                         ** Don't generate Content-Disposition header, because
541                         ** it confuses Microsoft Outlook, Build 10.0.6626, at
542                         ** least.
543                         */
544                         fprintf(composition_file, "#%s <>", content_type);
545                 } else {
546                         /*
547                         ** Suppress Content-Id, insert Content-Disposition
548                         ** with modification date.
549                         */
550                         fprintf(composition_file,
551                                 "#%s; name=\"%s\" <>{attachment; modification-date=\"%s\"}",
552                                 content_type,
553                                 ((p = strrchr(file_name, '/')) == (char *)0) ?
554                                 file_name : p + 1, dtime(&st.st_mtime, 0));
555                 }
556
557                 break;
558         default:
559                 adios((char *)0, "unsupported attachformat %d", attachformat);
560         }
561
562         /*
563         ** Finish up with the file name.
564         */
565
566         (void)fprintf(composition_file, " %s\n", file_name);
567
568         return;
569 }
570
571 /*
572 ** Annotate original message, and
573 ** call `postproc' to send message.
574 */
575
576 static int
577 sendaux(char **vec, int vecp, char *drft, struct stat *st)
578 {
579         pid_t child_id;
580         int i, status, fd, fd2;
581         char backup[BUFSIZ], buf[BUFSIZ];
582
583         fd = pushsw ? tmp_fd() : NOTOK;
584         fd2 = NOTOK;
585
586         vec[vecp++] = drft;
587         if (annotext) {
588                 if ((fd2 = tmp_fd()) != NOTOK) {
589                         vec[vecp++] = "-idanno";
590                         snprintf(buf, sizeof(buf), "%d", fd2);
591                         vec[vecp++] = buf;
592                 } else {
593                         admonish(NULL, "unable to create file for annotation list");
594                 }
595         }
596         if (distfile && distout(drft, distfile, backup) == NOTOK)
597                 done(1);
598         vec[vecp] = NULL;
599
600         for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
601                 sleep(5);
602
603         switch (child_id) {
604         case -1:
605                 /* oops -- fork error */
606                 adios("fork", "unable to");
607                 break;  /* NOT REACHED */
608
609         case 0:
610                 /*
611                 ** child process -- send it
612                 **
613                 ** If fd is ok, then we are pushing and fd points to temp
614                 ** file, so capture anything on stdout and stderr there.
615                 */
616                 if (fd != NOTOK) {
617                         dup2(fd, fileno(stdout));
618                         dup2(fd, fileno(stderr));
619                         close(fd);
620                 }
621                 execvp(postproc, vec);
622                 fprintf(stderr, "unable to exec ");
623                 perror(postproc);
624                 _exit(-1);
625                 break;  /* NOT REACHED */
626
627         default:
628                 /*
629                 ** parent process -- wait for it
630                 */
631                 if ((status = pidwait(child_id, NOTOK)) == OK) {
632                         if (annotext && fd2 != NOTOK)
633                                 anno(fd2, st);
634                 } else {
635                         /*
636                         ** If postproc failed, and we have good fd (which
637                         ** means we pushed), then mail error message
638                         ** (and possibly the draft) back to the user.
639                         */
640                         if (fd != NOTOK) {
641                                 alert(drft, fd);
642                                 close(fd);
643                         } else {
644                                 advise(NULL, "message not delivered to anyone");
645                         }
646                         if (annotext && fd2 != NOTOK)
647                                 close(fd2);
648                         if (distfile) {
649                                 unlink(drft);
650                                 if (rename(backup, drft) == NOTOK)
651                                         advise(drft, "unable to rename %s to",
652                                                         backup);
653                         }
654                 }
655                 break;
656         }
657
658         return status;
659 }
660
661
662 /*
663 ** Mail error notification (and possibly a copy of the
664 ** message) back to the user, using the mailproc
665 */
666
667 static void
668 alert(char *file, int out)
669 {
670         pid_t child_id;
671         int i, in;
672         char buf[BUFSIZ];
673
674         for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
675                 sleep(5);
676
677         switch (child_id) {
678                 case NOTOK:
679                         /* oops -- fork error */
680                         advise("fork", "unable to");
681
682                 case OK:
683                         /* child process -- send it */
684                         SIGNAL(SIGHUP, SIG_IGN);
685                         SIGNAL(SIGINT, SIG_IGN);
686                         SIGNAL(SIGQUIT, SIG_IGN);
687                         SIGNAL(SIGTERM, SIG_IGN);
688                         if (forwsw) {
689                                 if ((in = open(file, O_RDONLY)) == NOTOK) {
690                                         admonish(file, "unable to re-open");
691                                 } else {
692                                         lseek(out, (off_t) 0, SEEK_END);
693                                         strncpy(buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
694                                         write(out, buf, strlen(buf));
695                                         strncpy(buf, "\n------- Unsent Draft\n\n", sizeof(buf));
696                                         write(out, buf, strlen(buf));
697                                         cpydgst(in, out, file, "temporary file");
698                                         close(in);
699                                         strncpy(buf, "\n------- End of Unsent Draft\n", sizeof(buf));
700                                         write(out, buf, strlen(buf));
701                                         if (rename(file, strncpy(buf, m_backup(file), sizeof(buf))) == NOTOK)
702                                                 admonish(buf, "unable to rename %s to", file);
703                                 }
704                         }
705                         lseek(out, (off_t) 0, SEEK_SET);
706                         dup2(out, fileno(stdin));
707                         close(out);
708                         /* create subject for error notification */
709                         snprintf(buf, sizeof(buf), "send failed on %s",
710                                         forwsw ? "enclosed draft" : file);
711
712                         execlp(mailproc, mhbasename(mailproc),
713                                         getusername(), "-subject", buf, NULL);
714                         fprintf(stderr, "unable to exec ");
715                         perror(mailproc);
716                         _exit(-1);
717
718                 default:  /* no waiting... */
719                         break;
720         }
721 }
722
723
724 static int
725 tmp_fd(void)
726 {
727         int fd;
728         char *tfile = NULL;
729
730         tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
731         if (tfile == NULL) return NOTOK;
732         fchmod(fd, 0600);
733
734         if (debugsw)
735                 advise(NULL, "temporary file %s selected", tfile);
736         else
737                 if (unlink(tfile) == NOTOK)
738                         advise(tfile, "unable to remove");
739
740         return fd;
741 }
742
743
744 static void
745 anno(int fd, struct stat *st)
746 {
747         pid_t child_id;
748         sigset_t set, oset;
749         static char *cwd = NULL;
750         struct stat st2;
751
752         if (altmsg && (stat(altmsg, &st2) == NOTOK ||
753                         st->st_mtime != st2.st_mtime ||
754                         st->st_dev != st2.st_dev ||
755                         st->st_ino != st2.st_ino)) {
756                 if (debugsw)
757                         admonish(NULL, "$mhaltmsg mismatch");
758                 return;
759         }
760
761         child_id = debugsw ? NOTOK : fork();
762         switch (child_id) {
763                 case NOTOK:  /* oops */
764                         if (!debugsw)
765                                 advise(NULL, "unable to fork, so doing annotations by hand...");
766                         if (cwd == NULL)
767                                 cwd = getcpy(pwd());
768
769                 case OK:
770                         /* block a few signals */
771                         sigemptyset(&set);
772                         sigaddset(&set, SIGHUP);
773                         sigaddset(&set, SIGINT);
774                         sigaddset(&set, SIGQUIT);
775                         sigaddset(&set, SIGTERM);
776                         SIGPROCMASK(SIG_BLOCK, &set, &oset);
777
778                         annoaux(fd);
779                         if (child_id == OK)
780                                 _exit(0);
781
782                         /* reset the signal mask */
783                         SIGPROCMASK(SIG_SETMASK, &oset, &set);
784
785                         chdir(cwd);
786                         break;
787
788                 default:  /* no waiting... */
789                         close(fd);
790                         break;
791         }
792 }
793
794
795 static void
796 annoaux(int fd)
797 {
798         int fd2, fd3, msgnum;
799         char *cp, *folder, *maildir;
800         char buffer[BUFSIZ], **ap;
801         FILE *fp;
802         struct msgs *mp;
803
804         if ((folder = getenv("mhfolder")) == NULL || *folder == 0) {
805                 if (debugsw)
806                         admonish(NULL, "$mhfolder not set");
807                 return;
808         }
809         maildir = m_maildir(folder);
810         if (chdir(maildir) == NOTOK) {
811                 if (debugsw)
812                         admonish(maildir, "unable to change directory to");
813                 return;
814         }
815         if (!(mp = folder_read(folder))) {
816                 if (debugsw)
817                         admonish(NULL, "unable to read folder %s", folder);
818                 return;
819         }
820
821         /* check for empty folder */
822         if (mp->nummsg == 0) {
823                 if (debugsw)
824                         admonish(NULL, "no messages in %s", folder);
825                 goto oops;
826         }
827
828         if ((cp = getenv("mhmessages")) == NULL || *cp == 0) {
829                 if (debugsw)
830                         admonish(NULL, "$mhmessages not set");
831                 goto oops;
832         }
833         if (!debugsw  /* MOBY HACK... */
834                         && pushsw
835                         && (fd3 = open("/dev/null", O_RDWR)) != NOTOK
836                         && (fd2 = dup(fileno(stderr))) != NOTOK) {
837                 dup2(fd3, fileno(stderr));
838                 close(fd3);
839         }
840         else
841                 fd2 = NOTOK;
842         for (ap = brkstring(cp = getcpy(cp), " ", NULL); *ap; ap++)
843                 m_convert(mp, *ap);
844         free(cp);
845         if (fd2 != NOTOK)
846                 dup2(fd2, fileno(stderr));
847         if (mp->numsel == 0) {
848                 if (debugsw)
849                         admonish(NULL, "no messages to annotate");
850                 goto oops;
851         }
852
853         lseek(fd, (off_t) 0, SEEK_SET);
854         if ((fp = fdopen(fd, "r")) == NULL) {
855                 if (debugsw)
856                         admonish(NULL, "unable to fdopen annotation list");
857                 goto oops;
858         }
859         cp = NULL;
860         while (fgets(buffer, sizeof(buffer), fp) != NULL)
861                 cp = add(buffer, cp);
862         fclose(fp);
863
864         if (debugsw)
865                 advise(NULL, "annotate%s with %s: \"%s\"",
866                                 inplace ? " inplace" : "", annotext, cp);
867         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
868                 if (is_selected(mp, msgnum)) {
869                         if (debugsw)
870                                 advise(NULL, "annotate message %d", msgnum);
871                         annotate(m_name (msgnum), annotext, cp, inplace,
872                                         1, -2, 0);
873                 }
874         }
875
876         free(cp);
877
878 oops:
879         folder_free(mp);  /* free folder/message structure */
880 }
881
882
883 static void
884 armed_done(int status)
885 {
886         longjmp(env, status ? status : NOTOK);
887
888         exit(status);
889 }