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