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