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