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