]> diplodocus.org Git - nmh/blob - uip/sendsbr.c
Fixed three LC_ALL settings:
[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 /*
38 * external prototypes
39 */
40 int sendsbr (char **, int, char *, char *, struct stat *, int, char *, int);
41 char *getusername (void);
42
43 /*
44 * static prototypes
45 */
46 static void armed_done (int) NORETURN;
47 static void alert (char *, int);
48 static int tmp_fd (void);
49 static void anno (int, struct stat *);
50 static void annoaux (int);
51 static int splitmsg (char **, int, char *, char *, struct stat *, int);
52 static int sendaux (char **, int, char *, char *, struct stat *);
53
54
55 /*
56 * Entry point into (back-end) routines to send message.
57 */
58
59 int
60 sendsbr (char **vec, int vecp, char *program, char *drft, struct stat *st,
61 int rename_drft, char *attachment_header_field_name, int attachformat)
62 {
63 int status;
64 char buffer[BUFSIZ], file[BUFSIZ];
65 struct stat sts;
66 char *original_draft; /* name of original draft file */
67 char *p; /* string pointer for building file name */
68 char composition_file_name[PATH_MAX + 1]; /* name of mhbuild composition temporary file */
69 char body_file_name[PATH_MAX + 1]; /* name of temporary file for body content */
70
71 /*
72 * Save the original name of the draft file. The name of the draft file is changed
73 * to a temporary file containing the built MIME message if there are attachments.
74 * We need the original name so that it can be renamed after the message is sent.
75 */
76
77 original_draft = drft;
78
79 /*
80 * There might be attachments if a header field name for attachments is supplied.
81 * Convert the draft to a MIME message. Use the mhbuild composition file for the
82 * draft if there was a successful conversion because that now contains the MIME
83 * message. A nice side effect of this is that it leaves the original draft file
84 * untouched so that it can be retrieved and modified if desired.
85 */
86
87 if (attachment_header_field_name != (char *)0) {
88 switch (attach(attachment_header_field_name, drft,
89 body_file_name, sizeof body_file_name,
90 composition_file_name, sizeof composition_file_name,
91 attachformat)) {
92 case OK:
93 drft = composition_file_name;
94 break;
95
96 case NOTOK:
97 return (NOTOK);
98
99 case DONE:
100 break;
101 }
102 }
103
104 done=armed_done;
105 switch (setjmp (env)) {
106 case OK:
107 /*
108 * If given -push and -unique (which is undocumented), then
109 * rename the draft file. I'm not quite sure why.
110 */
111 if (pushsw && unique) {
112 char *cp = m_mktemp2(drft, invo_name, NULL, NULL);
113 if (cp == NULL) {
114 adios ("sendsbr", "unable to create temporary file");
115 }
116 if (rename (drft, strncpy(file, cp, sizeof(file))) == NOTOK)
117 adios (file, "unable to rename %s to", drft);
118 drft = file;
119 }
120
121 /*
122 * Check if we need to split the message into
123 * multiple messages of type "message/partial".
124 */
125 if (splitsw >= 0 && !distfile && stat (drft, &sts) != NOTOK
126 && sts.st_size >= CPERMSG) {
127 status = splitmsg (vec, vecp, program, drft, st, splitsw) ? NOTOK : OK;
128 } else {
129 status = sendaux (vec, vecp, program, drft, st) ? NOTOK : OK;
130 }
131
132 /* rename the original draft */
133 if (rename_drft && status == OK &&
134 rename (original_draft, strncpy (buffer, m_backup (original_draft), sizeof(buffer))) == NOTOK)
135 advise (buffer, "unable to rename %s to", drft);
136 break;
137
138 default:
139 status = DONE;
140 break;
141 }
142
143 done=exit;
144 if (distfile)
145 unlink (distfile);
146
147 /*
148 * Get rid of any temporary files that we created for attachments. Also get rid of
149 * the renamed composition file that mhbuild leaves as a turd. It looks confusing,
150 * but we use the body file name to help build the renamed composition file name.
151 */
152
153 if (drft == composition_file_name) {
154 clean_up_temporary_files(body_file_name, composition_file_name);
155
156 if (strlen(composition_file_name) >= sizeof (composition_file_name) - 6)
157 advise((char *)0, "unable to remove original composition file.");
158
159 else {
160 if ((p = strrchr(composition_file_name, '/')) == (char *)0)
161 p = composition_file_name;
162 else
163 p++;
164
165 (void)strcpy(body_file_name, p);
166 *p++ = ',';
167 (void)strcpy(p, body_file_name);
168 (void)strcat(p, ".orig");
169
170 (void)unlink(composition_file_name);
171 }
172 }
173
174 return status;
175 }
176
177 /*
178 * Split large message into several messages of
179 * type "message/partial" and send them.
180 */
181
182 static int
183 splitmsg (char **vec, int vecp, char *program, char *drft,
184 struct stat *st, int delay)
185 {
186 int compnum, nparts, partno, state, status;
187 long pos, start;
188 time_t clock;
189 char *cp, *dp, buffer[BUFSIZ], msgid[BUFSIZ];
190 char subject[BUFSIZ];
191 char name[NAMESZ], partnum[BUFSIZ];
192 FILE *in;
193 m_getfld_state_t gstate = 0;
194
195 if ((in = fopen (drft, "r")) == NULL)
196 adios (drft, "unable to open for reading");
197
198 cp = dp = NULL;
199 start = 0L;
200
201 /*
202 * Scan through the message and examine the various header fields,
203 * as well as locate the beginning of the message body.
204 */
205 m_getfld_track_filepos (&gstate, in);
206 for (compnum = 1;;) {
207 int bufsz = sizeof buffer;
208 switch (state = m_getfld (&gstate, name, buffer, &bufsz, in)) {
209 case FLD:
210 case FLDPLUS:
211 compnum++;
212
213 /*
214 * This header field is discarded.
215 */
216 if (!strcasecmp (name, "Message-ID")) {
217 while (state == FLDPLUS) {
218 bufsz = sizeof buffer;
219 state = m_getfld (&gstate, name, buffer, &bufsz, in);
220 }
221 } else if (uprf (name, XXX_FIELD_PRF)
222 || !strcasecmp (name, VRSN_FIELD)
223 || !strcasecmp (name, "Subject")
224 || !strcasecmp (name, "Encrypted")) {
225 /*
226 * These header fields are copied to the enclosed
227 * header of the first message in the collection
228 * of message/partials. For the "Subject" header
229 * field, we also record it, so that a modified
230 * version of it, can be copied to the header
231 * of each messsage/partial in the collection.
232 */
233 if (!strcasecmp (name, "Subject")) {
234 size_t sublen;
235
236 strncpy (subject, buffer, BUFSIZ);
237 sublen = strlen (subject);
238 if (sublen > 0 && subject[sublen - 1] == '\n')
239 subject[sublen - 1] = '\0';
240 }
241
242 dp = add (concat (name, ":", buffer, NULL), dp);
243 while (state == FLDPLUS) {
244 bufsz = sizeof buffer;
245 state = m_getfld (&gstate, name, buffer, &bufsz, in);
246 dp = add (buffer, dp);
247 }
248 } else {
249 /*
250 * These header fields are copied to the header of
251 * each message/partial in the collection.
252 */
253 cp = add (concat (name, ":", buffer, NULL), cp);
254 while (state == FLDPLUS) {
255 bufsz = sizeof buffer;
256 state = m_getfld (&gstate, name, buffer, &bufsz, in);
257 cp = add (buffer, cp);
258 }
259 }
260
261 start = ftell (in) + 1;
262 continue;
263
264 case BODY:
265 case FILEEOF:
266 break;
267
268 case LENERR:
269 case FMTERR:
270 adios (NULL, "message format error in component #%d", compnum);
271
272 default:
273 adios (NULL, "getfld () returned %d", state);
274 }
275
276 break;
277 }
278 m_getfld_state_destroy (&gstate);
279 if (cp == NULL)
280 adios (NULL, "headers missing from draft");
281
282 nparts = 1;
283 pos = start;
284 while (fgets (buffer, sizeof(buffer) - 1, in)) {
285 long len;
286
287 if ((pos += (len = strlen (buffer))) > CPERMSG) {
288 nparts++;
289 pos = len;
290 }
291 }
292
293 /* Only one part, nothing to split */
294 if (nparts == 1) {
295 free (cp);
296 if (dp)
297 free (dp);
298
299 fclose (in);
300 return sendaux (vec, vecp, program, drft, st);
301 }
302
303 if (!pushsw) {
304 printf ("Sending as %d Partial Messages\n", nparts);
305 fflush (stdout);
306 }
307 status = OK;
308
309 vec[vecp++] = "-partno";
310 vec[vecp++] = partnum;
311 if (delay == 0)
312 vec[vecp++] = "-queued";
313
314 time (&clock);
315 snprintf (msgid, sizeof(msgid), "%s", message_id (clock, 0));
316
317 fseek (in, start, SEEK_SET);
318 for (partno = 1; partno <= nparts; partno++) {
319 char tmpdrf[BUFSIZ];
320 FILE *out;
321
322 char *cp = m_mktemp2(drft, invo_name, NULL, &out);
323 if (cp == NULL) {
324 adios (drft, "unable to create temporary file for");
325 }
326 strncpy(tmpdrf, cp, sizeof(tmpdrf));
327 chmod (tmpdrf, 0600);
328
329 /*
330 * Output the header fields
331 */
332 fputs (cp, out);
333 fprintf (out, "Subject: %s (part %d of %d)\n", subject, partno, nparts);
334 fprintf (out, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
335 fprintf (out, "%s: message/partial; id=\"%s\";\n", TYPE_FIELD, msgid);
336 fprintf (out, "\tnumber=%d; total=%d\n", partno, nparts);
337 fprintf (out, "%s: part %d of %d\n\n", DESCR_FIELD, partno, nparts);
338
339 /*
340 * If this is the first in the collection, output the
341 * header fields we are encapsulating at the beginning
342 * of the body of the first message.
343 */
344 if (partno == 1) {
345 if (dp)
346 fputs (dp, out);
347 fprintf (out, "Message-ID: %s\n", msgid);
348 fprintf (out, "\n");
349 }
350
351 pos = 0;
352 for (;;) {
353 long len;
354
355 if (!fgets (buffer, sizeof(buffer) - 1, in)) {
356 if (partno == nparts)
357 break;
358 adios (NULL, "premature eof");
359 }
360
361 if ((pos += (len = strlen (buffer))) > CPERMSG) {
362 fseek (in, -len, SEEK_CUR);
363 break;
364 }
365
366 fputs (buffer, out);
367 }
368
369 if (fflush (out))
370 adios (tmpdrf, "error writing to");
371
372 fclose (out);
373
374 if (!pushsw && verbsw) {
375 printf ("\n");
376 fflush (stdout);
377 }
378
379 /* Pause here, if a delay is specified */
380 if (delay > 0 && 1 < partno && partno <= nparts) {
381 if (!pushsw) {
382 printf ("pausing %d seconds before sending part %d...\n",
383 delay, partno);
384 fflush (stdout);
385 }
386 sleep ((unsigned int) delay);
387 }
388
389 snprintf (partnum, sizeof(partnum), "%d", partno);
390 status = sendaux (vec, vecp, program, tmpdrf, st);
391 unlink (tmpdrf);
392 if (status != OK)
393 break;
394
395 /*
396 * This is so sendaux will only annotate
397 * the altmsg the first time it is called.
398 */
399 annotext = NULL;
400 }
401
402 free (cp);
403 if (dp)
404 free (dp);
405
406 fclose (in); /* close the draft */
407 return status;
408 }
409
410
411 /*
412 * Annotate original message, and
413 * call `postproc' (which is passed down in "program") to send message.
414 */
415
416 static int
417 sendaux (char **vec, int vecp, char *program, char *drft, struct stat *st)
418 {
419 pid_t child_id;
420 int i, status, fd, fd2;
421 char backup[BUFSIZ], buf[BUFSIZ];
422
423 fd = pushsw ? tmp_fd () : NOTOK;
424 fd2 = NOTOK;
425
426 vec[vecp++] = drft;
427 if (annotext) {
428 if ((fd2 = tmp_fd ()) != NOTOK) {
429 vec[vecp++] = "-idanno";
430 snprintf (buf, sizeof(buf), "%d", fd2);
431 vec[vecp++] = buf;
432 } else {
433 admonish (NULL, "unable to create file for annotation list");
434 }
435 }
436 if (distfile && distout (drft, distfile, backup) == NOTOK)
437 done (1);
438 vec[vecp] = NULL;
439
440 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
441 sleep (5);
442
443 switch (child_id) {
444 case -1:
445 /* oops -- fork error */
446 adios ("fork", "unable to");
447 break; /* NOT REACHED */
448
449 case 0:
450 /*
451 * child process -- send it
452 *
453 * If fd is ok, then we are pushing and fd points to temp
454 * file, so capture anything on stdout and stderr there.
455 */
456 if (fd != NOTOK) {
457 dup2 (fd, fileno (stdout));
458 dup2 (fd, fileno (stderr));
459 close (fd);
460 }
461 execvp (program, vec);
462 fprintf (stderr, "unable to exec ");
463 perror (postproc);
464 _exit (-1);
465
466 default:
467 /*
468 * parent process -- wait for it
469 */
470 if ((status = pidwait(child_id, NOTOK)) == OK) {
471 if (annotext && fd2 != NOTOK)
472 anno (fd2, st);
473 } else {
474 /*
475 * If postproc failed, and we have good fd (which means
476 * we pushed), then mail error message (and possibly the
477 * draft) back to the user.
478 */
479 if (fd != NOTOK) {
480 alert (drft, fd);
481 close (fd);
482 } else {
483 advise (NULL, "message not delivered to anyone");
484 }
485 if (annotext && fd2 != NOTOK)
486 close (fd2);
487 if (distfile) {
488 unlink (drft);
489 if (rename (backup, drft) == NOTOK)
490 advise (drft, "unable to rename %s to", backup);
491 }
492 }
493 break;
494 }
495
496 return status;
497 }
498
499
500 /*
501 * Mail error notification (and possibly a copy of the
502 * message) back to the user, using the mailproc
503 */
504
505 static void
506 alert (char *file, int out)
507 {
508 pid_t child_id;
509 int i, in, argp;
510 char buf[BUFSIZ];
511 char *program;
512 char **arglist;
513
514 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
515 sleep (5);
516
517 switch (child_id) {
518 case NOTOK:
519 /* oops -- fork error */
520 advise ("fork", "unable to");
521
522 case OK:
523 /* child process -- send it */
524 SIGNAL (SIGHUP, SIG_IGN);
525 SIGNAL (SIGINT, SIG_IGN);
526 SIGNAL (SIGQUIT, SIG_IGN);
527 SIGNAL (SIGTERM, SIG_IGN);
528 if (forwsw) {
529 if ((in = open (file, O_RDONLY)) == NOTOK) {
530 admonish (file, "unable to re-open");
531 } else {
532 lseek (out, (off_t) 0, SEEK_END);
533 strncpy (buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
534 write (out, buf, strlen (buf));
535 strncpy (buf, "\n------- Unsent Draft\n\n", sizeof(buf));
536 write (out, buf, strlen (buf));
537 cpydgst (in, out, file, "temporary file");
538 close (in);
539 strncpy (buf, "\n------- End of Unsent Draft\n", sizeof(buf));
540 write (out, buf, strlen (buf));
541 if (rename (file, strncpy (buf, m_backup (file), sizeof(buf))) == NOTOK)
542 admonish (buf, "unable to rename %s to", file);
543 }
544 }
545 lseek (out, (off_t) 0, SEEK_SET);
546 dup2 (out, fileno (stdin));
547 close (out);
548 /* create subject for error notification */
549 snprintf (buf, sizeof(buf), "send failed on %s",
550 forwsw ? "enclosed draft" : file);
551
552 arglist = argsplit(mailproc, &program, &argp);
553
554 arglist[argp++] = getusername();
555 arglist[argp++] = "-subject";
556 arglist[argp++] = buf;
557 arglist[argp] = NULL;
558
559 execvp (program, arglist);
560 fprintf (stderr, "unable to exec ");
561 perror (mailproc);
562 _exit (-1);
563
564 default: /* no waiting... */
565 break;
566 }
567 }
568
569
570 static int
571 tmp_fd (void)
572 {
573 int fd;
574 char *tfile = NULL;
575
576 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
577 if (tfile == NULL) return NOTOK;
578 fchmod(fd, 0600);
579
580 if (debugsw)
581 advise (NULL, "temporary file %s selected", tfile);
582 else
583 if (unlink (tfile) == NOTOK)
584 advise (tfile, "unable to remove");
585
586 return fd;
587 }
588
589
590 static void
591 anno (int fd, struct stat *st)
592 {
593 pid_t child_id;
594 sigset_t set, oset;
595 static char *cwd = NULL;
596 struct stat st2;
597
598 if (altmsg &&
599 (stat (altmsg, &st2) == NOTOK
600 || st->st_mtime != st2.st_mtime
601 || st->st_dev != st2.st_dev
602 || st->st_ino != st2.st_ino)) {
603 if (debugsw)
604 admonish (NULL, "$mhaltmsg mismatch");
605 return;
606 }
607
608 child_id = debugsw ? NOTOK : fork ();
609 switch (child_id) {
610 case NOTOK: /* oops */
611 if (!debugsw)
612 advise (NULL,
613 "unable to fork, so doing annotations by hand...");
614 if (cwd == NULL)
615 cwd = getcpy (pwd ());
616
617 case OK:
618 /* block a few signals */
619 sigemptyset (&set);
620 sigaddset (&set, SIGHUP);
621 sigaddset (&set, SIGINT);
622 sigaddset (&set, SIGQUIT);
623 sigaddset (&set, SIGTERM);
624 sigprocmask (SIG_BLOCK, &set, &oset);
625
626 annoaux (fd);
627 if (child_id == OK)
628 _exit (0);
629
630 /* reset the signal mask */
631 sigprocmask (SIG_SETMASK, &oset, &set);
632
633 chdir (cwd);
634 break;
635
636 default: /* no waiting... */
637 close (fd);
638 break;
639 }
640 }
641
642
643 static void
644 annoaux (int fd)
645 {
646 int fd2, fd3, msgnum;
647 char *cp, *folder, *maildir;
648 char buffer[BUFSIZ], **ap;
649 FILE *fp;
650 struct msgs *mp;
651
652 if ((folder = getenv ("mhfolder")) == NULL || *folder == 0) {
653 if (debugsw)
654 admonish (NULL, "$mhfolder not set");
655 return;
656 }
657 maildir = m_maildir (folder);
658 if (chdir (maildir) == NOTOK) {
659 if (debugsw)
660 admonish (maildir, "unable to change directory to");
661 return;
662 }
663 if (!(mp = folder_read (folder, 0))) {
664 if (debugsw)
665 admonish (NULL, "unable to read folder %s", folder);
666 return;
667 }
668
669 /* check for empty folder */
670 if (mp->nummsg == 0) {
671 if (debugsw)
672 admonish (NULL, "no messages in %s", folder);
673 goto oops;
674 }
675
676 if ((cp = getenv ("mhmessages")) == NULL || *cp == 0) {
677 if (debugsw)
678 admonish (NULL, "$mhmessages not set");
679 goto oops;
680 }
681 if (!debugsw /* MOBY HACK... */
682 && pushsw
683 && (fd3 = open ("/dev/null", O_RDWR)) != NOTOK
684 && (fd2 = dup (fileno (stderr))) != NOTOK) {
685 dup2 (fd3, fileno (stderr));
686 close (fd3);
687 }
688 else
689 fd2 = NOTOK;
690 for (ap = brkstring (cp = getcpy (cp), " ", NULL); *ap; ap++)
691 m_convert (mp, *ap);
692 free (cp);
693 if (fd2 != NOTOK)
694 dup2 (fd2, fileno (stderr));
695 if (mp->numsel == 0) {
696 if (debugsw)
697 admonish (NULL, "no messages to annotate");
698 goto oops;
699 }
700
701 lseek (fd, (off_t) 0, SEEK_SET);
702 if ((fp = fdopen (fd, "r")) == NULL) {
703 if (debugsw)
704 admonish (NULL, "unable to fdopen annotation list");
705 goto oops;
706 }
707 cp = NULL;
708 while (fgets (buffer, sizeof(buffer), fp) != NULL)
709 cp = add (buffer, cp);
710 fclose (fp);
711
712 if (debugsw)
713 advise (NULL, "annotate%s with %s: \"%s\"",
714 inplace ? " inplace" : "", annotext, cp);
715 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
716 if (is_selected(mp, msgnum)) {
717 if (debugsw)
718 advise (NULL, "annotate message %d", msgnum);
719 annotate (m_name (msgnum), annotext, cp, inplace, 1, -2, 0);
720 }
721 }
722
723 free (cp);
724
725 oops:
726 folder_free (mp); /* free folder/message structure */
727 }
728
729
730 static void
731 armed_done (int status)
732 {
733 longjmp (env, status ? status : NOTOK);
734 }