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