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