]> diplodocus.org Git - nmh/blob - uip/sendsbr.c
Add support for outputing a few (limited) terminal attributes in format
[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 /* Make sure that buf has space for one additional
617 character, the semicolon that might be added below. */
618 if (fgets (buf, sizeof buf - 1, fp)) {
619 char *cp, *space;
620
621 /* Skip leading <filename>:<whitespace>, if present. */
622 if ((content_type = strchr (buf, ':')) != NULL) {
623 ++content_type;
624 while (*content_type && isblank (*content_type)) {
625 ++content_type;
626 }
627 } else {
628 content_type = buf;
629 }
630
631 /* Truncate at newline (LF or CR), if present. */
632 if ((cp = strpbrk (content_type, "\n\n")) != NULL) {
633 *cp = '\0';
634 }
635
636 /* If necessary, insert semicolon between content type
637 and charset. Assume that the first space is between
638 them. */
639 if ((space = strchr (content_type, ' ')) != NULL) {
640 ssize_t len = strlen (content_type);
641
642 if (space - content_type > 0 &&
643 len > space - content_type + 1) {
644 if (*(space - 1) != ';') {
645 /* The +1 is for the terminating NULL. */
646 memmove (space + 1, space,
647 len - (space - content_type) + 1);
648 *space = ';';
649 }
650 }
651 }
652 } else {
653 advise (NULL, "unable to read mime type");
654 }
655 } else {
656 advise (NULL, "unable to run %s", buf);
657 }
658 } else {
659 advise (NULL, "filename to large to deduce mime type");
660 }
661 #else
662 NMH_UNUSED (file_name);
663 #endif
664
665 return content_type ? strdup (content_type) : NULL;
666 }
667
668 /*
669 * Split large message into several messages of
670 * type "message/partial" and send them.
671 */
672
673 static int
674 splitmsg (char **vec, int vecp, char *program, char *drft,
675 struct stat *st, int delay)
676 {
677 int compnum, nparts, partno, state, status;
678 long pos, start;
679 time_t clock;
680 char *cp, *dp, buffer[BUFSIZ], msgid[BUFSIZ];
681 char subject[BUFSIZ];
682 char name[NAMESZ], partnum[BUFSIZ];
683 FILE *in;
684 m_getfld_state_t gstate = 0;
685
686 if ((in = fopen (drft, "r")) == NULL)
687 adios (drft, "unable to open for reading");
688
689 cp = dp = NULL;
690 start = 0L;
691
692 /*
693 * Scan through the message and examine the various header fields,
694 * as well as locate the beginning of the message body.
695 */
696 m_getfld_track_filepos (&gstate, in);
697 for (compnum = 1;;) {
698 int bufsz = sizeof buffer;
699 switch (state = m_getfld (&gstate, name, buffer, &bufsz, in)) {
700 case FLD:
701 case FLDPLUS:
702 compnum++;
703
704 /*
705 * This header field is discarded.
706 */
707 if (!strcasecmp (name, "Message-ID")) {
708 while (state == FLDPLUS) {
709 bufsz = sizeof buffer;
710 state = m_getfld (&gstate, name, buffer, &bufsz, in);
711 }
712 } else if (uprf (name, XXX_FIELD_PRF)
713 || !strcasecmp (name, VRSN_FIELD)
714 || !strcasecmp (name, "Subject")
715 || !strcasecmp (name, "Encrypted")) {
716 /*
717 * These header fields are copied to the enclosed
718 * header of the first message in the collection
719 * of message/partials. For the "Subject" header
720 * field, we also record it, so that a modified
721 * version of it, can be copied to the header
722 * of each messsage/partial in the collection.
723 */
724 if (!strcasecmp (name, "Subject")) {
725 size_t sublen;
726
727 strncpy (subject, buffer, BUFSIZ);
728 sublen = strlen (subject);
729 if (sublen > 0 && subject[sublen - 1] == '\n')
730 subject[sublen - 1] = '\0';
731 }
732
733 dp = add (concat (name, ":", buffer, NULL), dp);
734 while (state == FLDPLUS) {
735 bufsz = sizeof buffer;
736 state = m_getfld (&gstate, name, buffer, &bufsz, in);
737 dp = add (buffer, dp);
738 }
739 } else {
740 /*
741 * These header fields are copied to the header of
742 * each message/partial in the collection.
743 */
744 cp = add (concat (name, ":", buffer, NULL), cp);
745 while (state == FLDPLUS) {
746 bufsz = sizeof buffer;
747 state = m_getfld (&gstate, name, buffer, &bufsz, in);
748 cp = add (buffer, cp);
749 }
750 }
751
752 start = ftell (in) + 1;
753 continue;
754
755 case BODY:
756 case FILEEOF:
757 break;
758
759 case LENERR:
760 case FMTERR:
761 adios (NULL, "message format error in component #%d", compnum);
762
763 default:
764 adios (NULL, "getfld () returned %d", state);
765 }
766
767 break;
768 }
769 m_getfld_state_destroy (&gstate);
770 if (cp == NULL)
771 adios (NULL, "headers missing from draft");
772
773 nparts = 1;
774 pos = start;
775 while (fgets (buffer, sizeof(buffer) - 1, in)) {
776 long len;
777
778 if ((pos += (len = strlen (buffer))) > CPERMSG) {
779 nparts++;
780 pos = len;
781 }
782 }
783
784 /* Only one part, nothing to split */
785 if (nparts == 1) {
786 free (cp);
787 if (dp)
788 free (dp);
789
790 fclose (in);
791 return sendaux (vec, vecp, program, drft, st);
792 }
793
794 if (!pushsw) {
795 printf ("Sending as %d Partial Messages\n", nparts);
796 fflush (stdout);
797 }
798 status = OK;
799
800 vec[vecp++] = "-partno";
801 vec[vecp++] = partnum;
802 if (delay == 0)
803 vec[vecp++] = "-queued";
804
805 time (&clock);
806 snprintf (msgid, sizeof(msgid), "%s", message_id (clock, 0));
807
808 fseek (in, start, SEEK_SET);
809 for (partno = 1; partno <= nparts; partno++) {
810 char tmpdrf[BUFSIZ];
811 FILE *out;
812
813 char *cp = m_mktemp2(drft, invo_name, NULL, &out);
814 if (cp == NULL) {
815 adios (drft, "unable to create temporary file for");
816 }
817 strncpy(tmpdrf, cp, sizeof(tmpdrf));
818 chmod (tmpdrf, 0600);
819
820 /*
821 * Output the header fields
822 */
823 fputs (cp, out);
824 fprintf (out, "Subject: %s (part %d of %d)\n", subject, partno, nparts);
825 fprintf (out, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
826 fprintf (out, "%s: message/partial; id=\"%s\";\n", TYPE_FIELD, msgid);
827 fprintf (out, "\tnumber=%d; total=%d\n", partno, nparts);
828 fprintf (out, "%s: part %d of %d\n\n", DESCR_FIELD, partno, nparts);
829
830 /*
831 * If this is the first in the collection, output the
832 * header fields we are encapsulating at the beginning
833 * of the body of the first message.
834 */
835 if (partno == 1) {
836 if (dp)
837 fputs (dp, out);
838 fprintf (out, "Message-ID: %s\n", msgid);
839 fprintf (out, "\n");
840 }
841
842 pos = 0;
843 for (;;) {
844 long len;
845
846 if (!fgets (buffer, sizeof(buffer) - 1, in)) {
847 if (partno == nparts)
848 break;
849 adios (NULL, "premature eof");
850 }
851
852 if ((pos += (len = strlen (buffer))) > CPERMSG) {
853 fseek (in, -len, SEEK_CUR);
854 break;
855 }
856
857 fputs (buffer, out);
858 }
859
860 if (fflush (out))
861 adios (tmpdrf, "error writing to");
862
863 fclose (out);
864
865 if (!pushsw && verbsw) {
866 printf ("\n");
867 fflush (stdout);
868 }
869
870 /* Pause here, if a delay is specified */
871 if (delay > 0 && 1 < partno && partno <= nparts) {
872 if (!pushsw) {
873 printf ("pausing %d seconds before sending part %d...\n",
874 delay, partno);
875 fflush (stdout);
876 }
877 sleep ((unsigned int) delay);
878 }
879
880 snprintf (partnum, sizeof(partnum), "%d", partno);
881 status = sendaux (vec, vecp, program, tmpdrf, st);
882 unlink (tmpdrf);
883 if (status != OK)
884 break;
885
886 /*
887 * This is so sendaux will only annotate
888 * the altmsg the first time it is called.
889 */
890 annotext = NULL;
891 }
892
893 free (cp);
894 if (dp)
895 free (dp);
896
897 fclose (in); /* close the draft */
898 return status;
899 }
900
901
902 /*
903 * Annotate original message, and
904 * call `postproc' (which is passed down in "program") to send message.
905 */
906
907 static int
908 sendaux (char **vec, int vecp, char *program, char *drft, struct stat *st)
909 {
910 pid_t child_id;
911 int i, status, fd, fd2;
912 char backup[BUFSIZ], buf[BUFSIZ];
913
914 fd = pushsw ? tmp_fd () : NOTOK;
915 fd2 = NOTOK;
916
917 vec[vecp++] = drft;
918 if (annotext) {
919 if ((fd2 = tmp_fd ()) != NOTOK) {
920 vec[vecp++] = "-idanno";
921 snprintf (buf, sizeof(buf), "%d", fd2);
922 vec[vecp++] = buf;
923 } else {
924 admonish (NULL, "unable to create file for annotation list");
925 }
926 }
927 if (distfile && distout (drft, distfile, backup) == NOTOK)
928 done (1);
929 vec[vecp] = NULL;
930
931 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
932 sleep (5);
933
934 switch (child_id) {
935 case -1:
936 /* oops -- fork error */
937 adios ("fork", "unable to");
938 break; /* NOT REACHED */
939
940 case 0:
941 /*
942 * child process -- send it
943 *
944 * If fd is ok, then we are pushing and fd points to temp
945 * file, so capture anything on stdout and stderr there.
946 */
947 if (fd != NOTOK) {
948 dup2 (fd, fileno (stdout));
949 dup2 (fd, fileno (stderr));
950 close (fd);
951 }
952 execvp (program, vec);
953 fprintf (stderr, "unable to exec ");
954 perror (postproc);
955 _exit (-1);
956
957 default:
958 /*
959 * parent process -- wait for it
960 */
961 if ((status = pidwait(child_id, NOTOK)) == OK) {
962 if (annotext && fd2 != NOTOK)
963 anno (fd2, st);
964 } else {
965 /*
966 * If postproc failed, and we have good fd (which means
967 * we pushed), then mail error message (and possibly the
968 * draft) back to the user.
969 */
970 if (fd != NOTOK) {
971 alert (drft, fd);
972 close (fd);
973 } else {
974 advise (NULL, "message not delivered to anyone");
975 }
976 if (annotext && fd2 != NOTOK)
977 close (fd2);
978 if (distfile) {
979 unlink (drft);
980 if (rename (backup, drft) == NOTOK)
981 advise (drft, "unable to rename %s to", backup);
982 }
983 }
984 break;
985 }
986
987 return status;
988 }
989
990
991 /*
992 * Mail error notification (and possibly a copy of the
993 * message) back to the user, using the mailproc
994 */
995
996 static void
997 alert (char *file, int out)
998 {
999 pid_t child_id;
1000 int i, in, argp;
1001 char buf[BUFSIZ];
1002 char *program;
1003 char **arglist;
1004
1005 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1006 sleep (5);
1007
1008 switch (child_id) {
1009 case NOTOK:
1010 /* oops -- fork error */
1011 advise ("fork", "unable to");
1012
1013 case OK:
1014 /* child process -- send it */
1015 SIGNAL (SIGHUP, SIG_IGN);
1016 SIGNAL (SIGINT, SIG_IGN);
1017 SIGNAL (SIGQUIT, SIG_IGN);
1018 SIGNAL (SIGTERM, SIG_IGN);
1019 if (forwsw) {
1020 if ((in = open (file, O_RDONLY)) == NOTOK) {
1021 admonish (file, "unable to re-open");
1022 } else {
1023 lseek (out, (off_t) 0, SEEK_END);
1024 strncpy (buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
1025 write (out, buf, strlen (buf));
1026 strncpy (buf, "\n------- Unsent Draft\n\n", sizeof(buf));
1027 write (out, buf, strlen (buf));
1028 cpydgst (in, out, file, "temporary file");
1029 close (in);
1030 strncpy (buf, "\n------- End of Unsent Draft\n", sizeof(buf));
1031 write (out, buf, strlen (buf));
1032 if (rename (file, strncpy (buf, m_backup (file), sizeof(buf))) == NOTOK)
1033 admonish (buf, "unable to rename %s to", file);
1034 }
1035 }
1036 lseek (out, (off_t) 0, SEEK_SET);
1037 dup2 (out, fileno (stdin));
1038 close (out);
1039 /* create subject for error notification */
1040 snprintf (buf, sizeof(buf), "send failed on %s",
1041 forwsw ? "enclosed draft" : file);
1042
1043 arglist = argsplit(mailproc, &program, &argp);
1044
1045 arglist[argp++] = getusername();
1046 arglist[argp++] = "-subject";
1047 arglist[argp++] = buf;
1048 arglist[argp] = NULL;
1049
1050 execvp (program, arglist);
1051 fprintf (stderr, "unable to exec ");
1052 perror (mailproc);
1053 _exit (-1);
1054
1055 default: /* no waiting... */
1056 break;
1057 }
1058 }
1059
1060
1061 static int
1062 tmp_fd (void)
1063 {
1064 int fd;
1065 char *tfile = NULL;
1066
1067 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
1068 if (tfile == NULL) return NOTOK;
1069 fchmod(fd, 0600);
1070
1071 if (debugsw)
1072 advise (NULL, "temporary file %s selected", tfile);
1073 else
1074 if (unlink (tfile) == NOTOK)
1075 advise (tfile, "unable to remove");
1076
1077 return fd;
1078 }
1079
1080
1081 static void
1082 anno (int fd, struct stat *st)
1083 {
1084 pid_t child_id;
1085 sigset_t set, oset;
1086 static char *cwd = NULL;
1087 struct stat st2;
1088
1089 if (altmsg &&
1090 (stat (altmsg, &st2) == NOTOK
1091 || st->st_mtime != st2.st_mtime
1092 || st->st_dev != st2.st_dev
1093 || st->st_ino != st2.st_ino)) {
1094 if (debugsw)
1095 admonish (NULL, "$mhaltmsg mismatch");
1096 return;
1097 }
1098
1099 child_id = debugsw ? NOTOK : fork ();
1100 switch (child_id) {
1101 case NOTOK: /* oops */
1102 if (!debugsw)
1103 advise (NULL,
1104 "unable to fork, so doing annotations by hand...");
1105 if (cwd == NULL)
1106 cwd = getcpy (pwd ());
1107
1108 case OK:
1109 /* block a few signals */
1110 sigemptyset (&set);
1111 sigaddset (&set, SIGHUP);
1112 sigaddset (&set, SIGINT);
1113 sigaddset (&set, SIGQUIT);
1114 sigaddset (&set, SIGTERM);
1115 sigprocmask (SIG_BLOCK, &set, &oset);
1116
1117 annoaux (fd);
1118 if (child_id == OK)
1119 _exit (0);
1120
1121 /* reset the signal mask */
1122 sigprocmask (SIG_SETMASK, &oset, &set);
1123
1124 chdir (cwd);
1125 break;
1126
1127 default: /* no waiting... */
1128 close (fd);
1129 break;
1130 }
1131 }
1132
1133
1134 static void
1135 annoaux (int fd)
1136 {
1137 int fd2, fd3, msgnum;
1138 char *cp, *folder, *maildir;
1139 char buffer[BUFSIZ], **ap;
1140 FILE *fp;
1141 struct msgs *mp;
1142
1143 if ((folder = getenv ("mhfolder")) == NULL || *folder == 0) {
1144 if (debugsw)
1145 admonish (NULL, "$mhfolder not set");
1146 return;
1147 }
1148 maildir = m_maildir (folder);
1149 if (chdir (maildir) == NOTOK) {
1150 if (debugsw)
1151 admonish (maildir, "unable to change directory to");
1152 return;
1153 }
1154 if (!(mp = folder_read (folder, 0))) {
1155 if (debugsw)
1156 admonish (NULL, "unable to read folder %s", folder);
1157 return;
1158 }
1159
1160 /* check for empty folder */
1161 if (mp->nummsg == 0) {
1162 if (debugsw)
1163 admonish (NULL, "no messages in %s", folder);
1164 goto oops;
1165 }
1166
1167 if ((cp = getenv ("mhmessages")) == NULL || *cp == 0) {
1168 if (debugsw)
1169 admonish (NULL, "$mhmessages not set");
1170 goto oops;
1171 }
1172 if (!debugsw /* MOBY HACK... */
1173 && pushsw
1174 && (fd3 = open ("/dev/null", O_RDWR)) != NOTOK
1175 && (fd2 = dup (fileno (stderr))) != NOTOK) {
1176 dup2 (fd3, fileno (stderr));
1177 close (fd3);
1178 }
1179 else
1180 fd2 = NOTOK;
1181 for (ap = brkstring (cp = getcpy (cp), " ", NULL); *ap; ap++)
1182 m_convert (mp, *ap);
1183 free (cp);
1184 if (fd2 != NOTOK)
1185 dup2 (fd2, fileno (stderr));
1186 if (mp->numsel == 0) {
1187 if (debugsw)
1188 admonish (NULL, "no messages to annotate");
1189 goto oops;
1190 }
1191
1192 lseek (fd, (off_t) 0, SEEK_SET);
1193 if ((fp = fdopen (fd, "r")) == NULL) {
1194 if (debugsw)
1195 admonish (NULL, "unable to fdopen annotation list");
1196 goto oops;
1197 }
1198 cp = NULL;
1199 while (fgets (buffer, sizeof(buffer), fp) != NULL)
1200 cp = add (buffer, cp);
1201 fclose (fp);
1202
1203 if (debugsw)
1204 advise (NULL, "annotate%s with %s: \"%s\"",
1205 inplace ? " inplace" : "", annotext, cp);
1206 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
1207 if (is_selected(mp, msgnum)) {
1208 if (debugsw)
1209 advise (NULL, "annotate message %d", msgnum);
1210 annotate (m_name (msgnum), annotext, cp, inplace, 1, -2, 0);
1211 }
1212 }
1213
1214 free (cp);
1215
1216 oops:
1217 folder_free (mp); /* free folder/message structure */
1218 }
1219
1220
1221 static void
1222 armed_done (int status)
1223 {
1224 longjmp (env, status ? status : NOTOK);
1225 }