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