]> diplodocus.org Git - nmh/blob - uip/sendsbr.c
6d76e9cd4b0ce20a56965bb7d9d0125b78fcd2a5
[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 <fcntl.h>
14 #include <h/mime.h>
15 #include <h/tws.h>
16 #include <h/utils.h>
17
18 #ifdef HAVE_SYS_TIME_H
19 # include <sys/time.h>
20 #endif
21 #include <time.h>
22
23 int debugsw = 0; /* global */
24 int forwsw = 1;
25 int inplace = 1;
26 int pushsw = 0;
27 int splitsw = -1;
28 int unique = 0;
29 int verbsw = 0;
30
31 char *altmsg = NULL; /* .. */
32 char *annotext = NULL;
33 char *distfile = NULL;
34
35 static jmp_buf env;
36
37 static char body_file_name[PATH_MAX + 1]; /* name of temporary file for body content */
38 static char composition_file_name[PATH_MAX + 1]; /* name of mhbuild composition temporary file */
39 static int field_size; /* size of header field buffer */
40 static char *field; /* header field buffer */
41 static FILE *draft_file; /* draft file pointer */
42 static FILE *body_file; /* body file pointer */
43 static FILE *composition_file; /* composition file pointer */
44
45 /*
46 * external prototypes
47 */
48 int sendsbr (char **, int, char *, char *, struct stat *, int, char *, int);
49 char *getusername (void);
50
51 /*
52 * static prototypes
53 */
54 static void armed_done (int) NORETURN;
55 static void alert (char *, int);
56 static int tmp_fd (void);
57 static void anno (int, struct stat *);
58 static void annoaux (int);
59 static int splitmsg (char **, int, char *, char *, struct stat *, int);
60 static int sendaux (char **, int, char *, char *, struct stat *);
61
62 static int attach(char *, char *, int);
63 static void clean_up_temporary_files(void);
64 static int get_line(void);
65 static void make_mime_composition_file_entry(char *, int, char *);
66 static char *mime_type (const 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 + 8]; /* 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 if ((content_type = mime_type (file_name)) == NULL) {
430 /*
431 * Check the file name for a suffix. Scan the context for
432 * that suffix on a mhshow-suffix- entry. We use these
433 * entries to be compatible with mhnshow, and there's no
434 * reason to make the user specify each suffix twice. Context
435 * entries of the form "mhshow-suffix-contenttype" in the name
436 * have the suffix in the field, including the dot.
437 */
438 if ((p = strrchr(file_name, '.')) != (char *)0) {
439 for (np = m_defs; np; np = np->n_next) {
440 if (strncasecmp(np->n_name, "mhshow-suffix-", 14) == 0 &&
441 strcasecmp(p, np->n_field ? np->n_field : "") == 0) {
442 content_type = strdup (np->n_name + 14);
443 break;
444 }
445 }
446 }
447
448 if (content_type == NULL && default_content_type != NULL) {
449 content_type = strdup (default_content_type);
450 }
451 }
452
453 /*
454 * No content type was found, either because there was no matching entry in the
455 * context or because the file name has no suffix. Open the file and check for
456 * non-ASCII characters. Choose the content type based on this check.
457 */
458
459 if (content_type == (char *)0) {
460 if ((fp = fopen(file_name, "r")) == (FILE *)0) {
461 clean_up_temporary_files();
462 adios((char *)0, "unable to access file \"%s\"", file_name);
463 }
464
465 binary = 0;
466
467 while ((c = getc(fp)) != EOF) {
468 if (c > 127 || c < 0) {
469 binary = 1;
470 break;
471 }
472 }
473
474 (void)fclose(fp);
475
476 content_type =
477 strdup (binary ? "application/octet-stream" : "text/plain");
478 }
479
480 /*
481 * Make sure that the attachment file exists and is readable. Append a mhbuild
482 * directive to the draft file. This starts with the content type. Append a
483 * file name attribute and a private x-unix-mode attribute. Also append a
484 * description obtained (if possible) by running the "file" command on the file.
485 */
486
487 if (stat(file_name, &st) == -1 || access(file_name, R_OK) != 0) {
488 clean_up_temporary_files();
489 adios((char *)0, "unable to access file \"%s\"", file_name);
490 }
491
492 switch (attachformat) {
493 case 0:
494 /* Insert name, file mode, and Content-Id. */
495 (void)fprintf(composition_file, "#%s; name=\"%s\"; x-unix-mode=0%.3ho",
496 content_type, ((p = strrchr(file_name, '/')) == (char *)0) ? file_name : p + 1, (unsigned short)(st.st_mode & 0777));
497
498 if (strlen(file_name) > PATH_MAX) {
499 clean_up_temporary_files();
500 adios((char *)0, "attachment file name `%s' too long.", file_name);
501 }
502
503 (void)sprintf(cmd, "file '%s'", file_name);
504
505 if ((fp = popen(cmd, "r")) != (FILE *)0 && fgets(cmd, sizeof (cmd), fp) != (char *)0) {
506 *strchr(cmd, '\n') = '\0';
507
508 /*
509 * The output of the "file" command is of the form
510 *
511 * file: description
512 *
513 * Strip off the "file:" and subsequent white space.
514 */
515
516 for (p = cmd; *p != '\0'; p++) {
517 if (*p == ':') {
518 for (p++; *p != '\0'; p++) {
519 if (*p != '\t')
520 break;
521 }
522 break;
523 }
524 }
525
526 if (*p != '\0')
527 /* Insert Content-Description. */
528 (void)fprintf(composition_file, " [ %s ]", p);
529
530 (void)pclose(fp);
531 }
532
533 break;
534 case 1:
535 if (stringdex (m_maildir(invo_name), file_name) == 0) {
536 /* Content had been placed by send into a temp file.
537 Don't generate Content-Disposition header, because
538 it confuses Microsoft Outlook, Build 10.0.6626, at
539 least. */
540 (void) fprintf (composition_file, "#%s <>", content_type);
541 } else {
542 /* Suppress Content-Id, insert simple Content-Disposition
543 and Content-Description with filename.
544 The Content-Disposition type needs to be "inline" for
545 MS Outlook and BlackBerry calendar programs to properly
546 handle a text/calendar attachment. */
547 p = strrchr(file_name, '/');
548 (void) fprintf (composition_file,
549 "#%s; name=\"%s\" <> [%s]{%s}",
550 content_type,
551 (p == (char *)0) ? file_name : p + 1,
552 (p == (char *)0) ? file_name : p + 1,
553 strcmp ("text/calendar", content_type)
554 ? "attachment" : "inline");
555 }
556
557 break;
558 case 2:
559 if (stringdex (m_maildir(invo_name), file_name) == 0) {
560 /* Content had been placed by send into a temp file.
561 Don't generate Content-Disposition header, because
562 it confuses Microsoft Outlook, Build 10.0.6626, at
563 least. */
564 (void) fprintf (composition_file, "#%s <>", content_type);
565 } else {
566 /* Suppress Content-Id, insert Content-Disposition with
567 modification date and Content-Description wtih filename.
568 The Content-Disposition type needs to be "inline" for
569 MS Outlook and BlackBerry calendar programs to properly
570 handle a text/calendar attachment. */
571 p = strrchr(file_name, '/');
572 (void) fprintf (composition_file,
573 "#%s; name=\"%s\" <>[%s]{%s; "
574 "modification-date=\"%s\"}",
575 content_type,
576 (p == (char *)0) ? file_name : p + 1,
577 (p == (char *)0) ? file_name : p + 1,
578 strcmp ("text/calendar", content_type)
579 ? "attachment" : "inline",
580 dtime (&st.st_mtime, 0));
581 }
582
583 break;
584 default:
585 adios ((char *)0, "unsupported attachformat %d", attachformat);
586 }
587
588 free (content_type);
589
590 /*
591 * Finish up with the file name.
592 */
593
594 (void)fprintf(composition_file, " %s\n", file_name);
595
596 return;
597 }
598
599 /*
600 * Try to use external command to determine mime type, and possibly
601 * encoding. Caller is responsible for free'ing returned memory.
602 */
603 static char *
604 mime_type (const char *file_name) {
605 char *content_type = NULL; /* mime content type */
606
607 #ifdef MIMETYPEPROC
608 char cmd[2 * PATH_MAX + 2]; /* file command buffer */
609 char buf[BUFSIZ >= 2048 ? BUFSIZ : 2048];
610 FILE *fp; /* content and pipe file pointer */
611 char mimetypeproc[] = MIMETYPEPROC " '%s'";
612
613 if ((int) snprintf (cmd, sizeof cmd, mimetypeproc, file_name) <
614 (int) sizeof cmd) {
615 if ((fp = popen (cmd, "r")) != NULL) {
616 if (fgets (buf, sizeof buf, fp)) {
617 char *cp;
618
619 /* Skip leading <filename>:<whitespace>, if present. */
620 if ((content_type = strchr (buf, ':')) != NULL) {
621 ++content_type;
622 while (*content_type && isblank (*content_type)) {
623 ++content_type;
624 }
625 } else {
626 content_type = buf;
627 }
628
629 /* Truncate at newline (LF or CR), if present. */
630 if ((cp = strpbrk (content_type, "\n\n")) != NULL) {
631 *cp = '\0';
632 }
633 } else {
634 advise (NULL, "unable to read mime type");
635 }
636 } else {
637 advise (NULL, "unable to run %s", buf);
638 }
639 } else {
640 advise (NULL, "filename to large to deduce mime type");
641 }
642 #else
643 NMH_UNUSED (file_name);
644 #endif
645
646 return content_type ? strdup (content_type) : NULL;
647 }
648
649 /*
650 * Split large message into several messages of
651 * type "message/partial" and send them.
652 */
653
654 static int
655 splitmsg (char **vec, int vecp, char *program, char *drft,
656 struct stat *st, int delay)
657 {
658 int compnum, nparts, partno, state, status;
659 long pos, start;
660 time_t clock;
661 char *cp, *dp, buffer[BUFSIZ], msgid[BUFSIZ];
662 char subject[BUFSIZ];
663 char name[NAMESZ], partnum[BUFSIZ];
664 FILE *in;
665 m_getfld_state_t gstate = 0;
666
667 if ((in = fopen (drft, "r")) == NULL)
668 adios (drft, "unable to open for reading");
669
670 cp = dp = NULL;
671 start = 0L;
672
673 /*
674 * Scan through the message and examine the various header fields,
675 * as well as locate the beginning of the message body.
676 */
677 m_getfld_track_filepos (&gstate, in);
678 for (compnum = 1;;) {
679 int bufsz = sizeof buffer;
680 switch (state = m_getfld (&gstate, name, buffer, &bufsz, in)) {
681 case FLD:
682 case FLDPLUS:
683 compnum++;
684
685 /*
686 * This header field is discarded.
687 */
688 if (!strcasecmp (name, "Message-ID")) {
689 while (state == FLDPLUS) {
690 bufsz = sizeof buffer;
691 state = m_getfld (&gstate, name, buffer, &bufsz, in);
692 }
693 } else if (uprf (name, XXX_FIELD_PRF)
694 || !strcasecmp (name, VRSN_FIELD)
695 || !strcasecmp (name, "Subject")
696 || !strcasecmp (name, "Encrypted")) {
697 /*
698 * These header fields are copied to the enclosed
699 * header of the first message in the collection
700 * of message/partials. For the "Subject" header
701 * field, we also record it, so that a modified
702 * version of it, can be copied to the header
703 * of each messsage/partial in the collection.
704 */
705 if (!strcasecmp (name, "Subject")) {
706 size_t sublen;
707
708 strncpy (subject, buffer, BUFSIZ);
709 sublen = strlen (subject);
710 if (sublen > 0 && subject[sublen - 1] == '\n')
711 subject[sublen - 1] = '\0';
712 }
713
714 dp = add (concat (name, ":", buffer, NULL), dp);
715 while (state == FLDPLUS) {
716 bufsz = sizeof buffer;
717 state = m_getfld (&gstate, name, buffer, &bufsz, in);
718 dp = add (buffer, dp);
719 }
720 } else {
721 /*
722 * These header fields are copied to the header of
723 * each message/partial in the collection.
724 */
725 cp = add (concat (name, ":", buffer, NULL), cp);
726 while (state == FLDPLUS) {
727 bufsz = sizeof buffer;
728 state = m_getfld (&gstate, name, buffer, &bufsz, in);
729 cp = add (buffer, cp);
730 }
731 }
732
733 start = ftell (in) + 1;
734 continue;
735
736 case BODY:
737 case FILEEOF:
738 break;
739
740 case LENERR:
741 case FMTERR:
742 adios (NULL, "message format error in component #%d", compnum);
743
744 default:
745 adios (NULL, "getfld () returned %d", state);
746 }
747
748 break;
749 }
750 m_getfld_state_destroy (&gstate);
751 if (cp == NULL)
752 adios (NULL, "headers missing from draft");
753
754 nparts = 1;
755 pos = start;
756 while (fgets (buffer, sizeof(buffer) - 1, in)) {
757 long len;
758
759 if ((pos += (len = strlen (buffer))) > CPERMSG) {
760 nparts++;
761 pos = len;
762 }
763 }
764
765 /* Only one part, nothing to split */
766 if (nparts == 1) {
767 free (cp);
768 if (dp)
769 free (dp);
770
771 fclose (in);
772 return sendaux (vec, vecp, program, drft, st);
773 }
774
775 if (!pushsw) {
776 printf ("Sending as %d Partial Messages\n", nparts);
777 fflush (stdout);
778 }
779 status = OK;
780
781 vec[vecp++] = "-partno";
782 vec[vecp++] = partnum;
783 if (delay == 0)
784 vec[vecp++] = "-queued";
785
786 time (&clock);
787 snprintf (msgid, sizeof(msgid), "%s", message_id (clock, 0));
788
789 fseek (in, start, SEEK_SET);
790 for (partno = 1; partno <= nparts; partno++) {
791 char tmpdrf[BUFSIZ];
792 FILE *out;
793
794 char *cp = m_mktemp2(drft, invo_name, NULL, &out);
795 if (cp == NULL) {
796 adios (drft, "unable to create temporary file for");
797 }
798 strncpy(tmpdrf, cp, sizeof(tmpdrf));
799 chmod (tmpdrf, 0600);
800
801 /*
802 * Output the header fields
803 */
804 fputs (cp, out);
805 fprintf (out, "Subject: %s (part %d of %d)\n", subject, partno, nparts);
806 fprintf (out, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
807 fprintf (out, "%s: message/partial; id=\"%s\";\n", TYPE_FIELD, msgid);
808 fprintf (out, "\tnumber=%d; total=%d\n", partno, nparts);
809 fprintf (out, "%s: part %d of %d\n\n", DESCR_FIELD, partno, nparts);
810
811 /*
812 * If this is the first in the collection, output the
813 * header fields we are encapsulating at the beginning
814 * of the body of the first message.
815 */
816 if (partno == 1) {
817 if (dp)
818 fputs (dp, out);
819 fprintf (out, "Message-ID: %s\n", msgid);
820 fprintf (out, "\n");
821 }
822
823 pos = 0;
824 for (;;) {
825 long len;
826
827 if (!fgets (buffer, sizeof(buffer) - 1, in)) {
828 if (partno == nparts)
829 break;
830 adios (NULL, "premature eof");
831 }
832
833 if ((pos += (len = strlen (buffer))) > CPERMSG) {
834 fseek (in, -len, SEEK_CUR);
835 break;
836 }
837
838 fputs (buffer, out);
839 }
840
841 if (fflush (out))
842 adios (tmpdrf, "error writing to");
843
844 fclose (out);
845
846 if (!pushsw && verbsw) {
847 printf ("\n");
848 fflush (stdout);
849 }
850
851 /* Pause here, if a delay is specified */
852 if (delay > 0 && 1 < partno && partno <= nparts) {
853 if (!pushsw) {
854 printf ("pausing %d seconds before sending part %d...\n",
855 delay, partno);
856 fflush (stdout);
857 }
858 sleep ((unsigned int) delay);
859 }
860
861 snprintf (partnum, sizeof(partnum), "%d", partno);
862 status = sendaux (vec, vecp, program, tmpdrf, st);
863 unlink (tmpdrf);
864 if (status != OK)
865 break;
866
867 /*
868 * This is so sendaux will only annotate
869 * the altmsg the first time it is called.
870 */
871 annotext = NULL;
872 }
873
874 free (cp);
875 if (dp)
876 free (dp);
877
878 fclose (in); /* close the draft */
879 return status;
880 }
881
882
883 /*
884 * Annotate original message, and
885 * call `postproc' (which is passed down in "program") to send message.
886 */
887
888 static int
889 sendaux (char **vec, int vecp, char *program, char *drft, struct stat *st)
890 {
891 pid_t child_id;
892 int i, status, fd, fd2;
893 char backup[BUFSIZ], buf[BUFSIZ];
894
895 fd = pushsw ? tmp_fd () : NOTOK;
896 fd2 = NOTOK;
897
898 vec[vecp++] = drft;
899 if (annotext) {
900 if ((fd2 = tmp_fd ()) != NOTOK) {
901 vec[vecp++] = "-idanno";
902 snprintf (buf, sizeof(buf), "%d", fd2);
903 vec[vecp++] = buf;
904 } else {
905 admonish (NULL, "unable to create file for annotation list");
906 }
907 }
908 if (distfile && distout (drft, distfile, backup) == NOTOK)
909 done (1);
910 vec[vecp] = NULL;
911
912 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
913 sleep (5);
914
915 switch (child_id) {
916 case -1:
917 /* oops -- fork error */
918 adios ("fork", "unable to");
919 break; /* NOT REACHED */
920
921 case 0:
922 /*
923 * child process -- send it
924 *
925 * If fd is ok, then we are pushing and fd points to temp
926 * file, so capture anything on stdout and stderr there.
927 */
928 if (fd != NOTOK) {
929 dup2 (fd, fileno (stdout));
930 dup2 (fd, fileno (stderr));
931 close (fd);
932 }
933 execvp (program, vec);
934 fprintf (stderr, "unable to exec ");
935 perror (postproc);
936 _exit (-1);
937
938 default:
939 /*
940 * parent process -- wait for it
941 */
942 if ((status = pidwait(child_id, NOTOK)) == OK) {
943 if (annotext && fd2 != NOTOK)
944 anno (fd2, st);
945 } else {
946 /*
947 * If postproc failed, and we have good fd (which means
948 * we pushed), then mail error message (and possibly the
949 * draft) back to the user.
950 */
951 if (fd != NOTOK) {
952 alert (drft, fd);
953 close (fd);
954 } else {
955 advise (NULL, "message not delivered to anyone");
956 }
957 if (annotext && fd2 != NOTOK)
958 close (fd2);
959 if (distfile) {
960 unlink (drft);
961 if (rename (backup, drft) == NOTOK)
962 advise (drft, "unable to rename %s to", backup);
963 }
964 }
965 break;
966 }
967
968 return status;
969 }
970
971
972 /*
973 * Mail error notification (and possibly a copy of the
974 * message) back to the user, using the mailproc
975 */
976
977 static void
978 alert (char *file, int out)
979 {
980 pid_t child_id;
981 int i, in, argp;
982 char buf[BUFSIZ];
983 char *program;
984 char **arglist;
985
986 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
987 sleep (5);
988
989 switch (child_id) {
990 case NOTOK:
991 /* oops -- fork error */
992 advise ("fork", "unable to");
993
994 case OK:
995 /* child process -- send it */
996 SIGNAL (SIGHUP, SIG_IGN);
997 SIGNAL (SIGINT, SIG_IGN);
998 SIGNAL (SIGQUIT, SIG_IGN);
999 SIGNAL (SIGTERM, SIG_IGN);
1000 if (forwsw) {
1001 if ((in = open (file, O_RDONLY)) == NOTOK) {
1002 admonish (file, "unable to re-open");
1003 } else {
1004 lseek (out, (off_t) 0, SEEK_END);
1005 strncpy (buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
1006 write (out, buf, strlen (buf));
1007 strncpy (buf, "\n------- Unsent Draft\n\n", sizeof(buf));
1008 write (out, buf, strlen (buf));
1009 cpydgst (in, out, file, "temporary file");
1010 close (in);
1011 strncpy (buf, "\n------- End of Unsent Draft\n", sizeof(buf));
1012 write (out, buf, strlen (buf));
1013 if (rename (file, strncpy (buf, m_backup (file), sizeof(buf))) == NOTOK)
1014 admonish (buf, "unable to rename %s to", file);
1015 }
1016 }
1017 lseek (out, (off_t) 0, SEEK_SET);
1018 dup2 (out, fileno (stdin));
1019 close (out);
1020 /* create subject for error notification */
1021 snprintf (buf, sizeof(buf), "send failed on %s",
1022 forwsw ? "enclosed draft" : file);
1023
1024 arglist = argsplit(mailproc, &program, &argp);
1025
1026 arglist[argp++] = getusername();
1027 arglist[argp++] = "-subject";
1028 arglist[argp++] = buf;
1029 arglist[argp] = NULL;
1030
1031 execvp (program, arglist);
1032 fprintf (stderr, "unable to exec ");
1033 perror (mailproc);
1034 _exit (-1);
1035
1036 default: /* no waiting... */
1037 break;
1038 }
1039 }
1040
1041
1042 static int
1043 tmp_fd (void)
1044 {
1045 int fd;
1046 char *tfile = NULL;
1047
1048 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
1049 if (tfile == NULL) return NOTOK;
1050 fchmod(fd, 0600);
1051
1052 if (debugsw)
1053 advise (NULL, "temporary file %s selected", tfile);
1054 else
1055 if (unlink (tfile) == NOTOK)
1056 advise (tfile, "unable to remove");
1057
1058 return fd;
1059 }
1060
1061
1062 static void
1063 anno (int fd, struct stat *st)
1064 {
1065 pid_t child_id;
1066 sigset_t set, oset;
1067 static char *cwd = NULL;
1068 struct stat st2;
1069
1070 if (altmsg &&
1071 (stat (altmsg, &st2) == NOTOK
1072 || st->st_mtime != st2.st_mtime
1073 || st->st_dev != st2.st_dev
1074 || st->st_ino != st2.st_ino)) {
1075 if (debugsw)
1076 admonish (NULL, "$mhaltmsg mismatch");
1077 return;
1078 }
1079
1080 child_id = debugsw ? NOTOK : fork ();
1081 switch (child_id) {
1082 case NOTOK: /* oops */
1083 if (!debugsw)
1084 advise (NULL,
1085 "unable to fork, so doing annotations by hand...");
1086 if (cwd == NULL)
1087 cwd = getcpy (pwd ());
1088
1089 case OK:
1090 /* block a few signals */
1091 sigemptyset (&set);
1092 sigaddset (&set, SIGHUP);
1093 sigaddset (&set, SIGINT);
1094 sigaddset (&set, SIGQUIT);
1095 sigaddset (&set, SIGTERM);
1096 sigprocmask (SIG_BLOCK, &set, &oset);
1097
1098 annoaux (fd);
1099 if (child_id == OK)
1100 _exit (0);
1101
1102 /* reset the signal mask */
1103 sigprocmask (SIG_SETMASK, &oset, &set);
1104
1105 chdir (cwd);
1106 break;
1107
1108 default: /* no waiting... */
1109 close (fd);
1110 break;
1111 }
1112 }
1113
1114
1115 static void
1116 annoaux (int fd)
1117 {
1118 int fd2, fd3, msgnum;
1119 char *cp, *folder, *maildir;
1120 char buffer[BUFSIZ], **ap;
1121 FILE *fp;
1122 struct msgs *mp;
1123
1124 if ((folder = getenv ("mhfolder")) == NULL || *folder == 0) {
1125 if (debugsw)
1126 admonish (NULL, "$mhfolder not set");
1127 return;
1128 }
1129 maildir = m_maildir (folder);
1130 if (chdir (maildir) == NOTOK) {
1131 if (debugsw)
1132 admonish (maildir, "unable to change directory to");
1133 return;
1134 }
1135 if (!(mp = folder_read (folder, 0))) {
1136 if (debugsw)
1137 admonish (NULL, "unable to read folder %s", folder);
1138 return;
1139 }
1140
1141 /* check for empty folder */
1142 if (mp->nummsg == 0) {
1143 if (debugsw)
1144 admonish (NULL, "no messages in %s", folder);
1145 goto oops;
1146 }
1147
1148 if ((cp = getenv ("mhmessages")) == NULL || *cp == 0) {
1149 if (debugsw)
1150 admonish (NULL, "$mhmessages not set");
1151 goto oops;
1152 }
1153 if (!debugsw /* MOBY HACK... */
1154 && pushsw
1155 && (fd3 = open ("/dev/null", O_RDWR)) != NOTOK
1156 && (fd2 = dup (fileno (stderr))) != NOTOK) {
1157 dup2 (fd3, fileno (stderr));
1158 close (fd3);
1159 }
1160 else
1161 fd2 = NOTOK;
1162 for (ap = brkstring (cp = getcpy (cp), " ", NULL); *ap; ap++)
1163 m_convert (mp, *ap);
1164 free (cp);
1165 if (fd2 != NOTOK)
1166 dup2 (fd2, fileno (stderr));
1167 if (mp->numsel == 0) {
1168 if (debugsw)
1169 admonish (NULL, "no messages to annotate");
1170 goto oops;
1171 }
1172
1173 lseek (fd, (off_t) 0, SEEK_SET);
1174 if ((fp = fdopen (fd, "r")) == NULL) {
1175 if (debugsw)
1176 admonish (NULL, "unable to fdopen annotation list");
1177 goto oops;
1178 }
1179 cp = NULL;
1180 while (fgets (buffer, sizeof(buffer), fp) != NULL)
1181 cp = add (buffer, cp);
1182 fclose (fp);
1183
1184 if (debugsw)
1185 advise (NULL, "annotate%s with %s: \"%s\"",
1186 inplace ? " inplace" : "", annotext, cp);
1187 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
1188 if (is_selected(mp, msgnum)) {
1189 if (debugsw)
1190 advise (NULL, "annotate message %d", msgnum);
1191 annotate (m_name (msgnum), annotext, cp, inplace, 1, -2, 0);
1192 }
1193 }
1194
1195 free (cp);
1196
1197 oops:
1198 folder_free (mp); /* free folder/message structure */
1199 }
1200
1201
1202 static void
1203 armed_done (int status)
1204 {
1205 longjmp (env, status ? status : NOTOK);
1206 }