]> diplodocus.org Git - nmh/blob - uip/sendsbr.c
c1914aa13c675f07ba4c3e4a6f5cd5e86e29059c
[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(NULL, "unable to create temporary file in %s",
115 get_temp_dir());
116 }
117 if (rename (drft, strncpy(file, cp, sizeof(file))) == NOTOK)
118 adios (file, "unable to rename %s to", drft);
119 drft = file;
120 }
121
122 /*
123 * Check if we need to split the message into
124 * multiple messages of type "message/partial".
125 */
126 if (splitsw >= 0 && !distfile && stat (drft, &sts) != NOTOK
127 && sts.st_size >= CPERMSG) {
128 status = splitmsg (vec, vecp, program, drft, st, splitsw) ? NOTOK : OK;
129 } else {
130 status = sendaux (vec, vecp, program, drft, st) ? NOTOK : OK;
131 }
132
133 /* rename the original draft */
134 if (rename_drft && status == OK &&
135 rename (original_draft, strncpy (buffer, m_backup (original_draft), sizeof(buffer))) == NOTOK)
136 advise (buffer, "unable to rename %s to", drft);
137 break;
138
139 default:
140 status = DONE;
141 break;
142 }
143
144 done=exit;
145 if (distfile)
146 unlink (distfile);
147
148 /*
149 * Get rid of any temporary files that we created for attachments. Also get rid of
150 * the renamed composition file that mhbuild leaves as a turd. It looks confusing,
151 * but we use the body file name to help build the renamed composition file name.
152 */
153
154 if (drft == composition_file_name) {
155 clean_up_temporary_files(body_file_name, composition_file_name);
156
157 if (strlen(composition_file_name) >= sizeof (composition_file_name) - 6)
158 advise((char *)0, "unable to remove original composition file.");
159
160 else {
161 if ((p = strrchr(composition_file_name, '/')) == (char *)0)
162 p = composition_file_name;
163 else
164 p++;
165
166 (void)strcpy(body_file_name, p);
167 *p++ = ',';
168 (void)strcpy(p, body_file_name);
169 (void)strcat(p, ".orig");
170
171 (void)unlink(composition_file_name);
172 }
173 }
174
175 return status;
176 }
177
178 /*
179 * Split large message into several messages of
180 * type "message/partial" and send them.
181 */
182
183 static int
184 splitmsg (char **vec, int vecp, char *program, char *drft,
185 struct stat *st, int delay)
186 {
187 int compnum, nparts, partno, state, status;
188 long pos, start;
189 time_t clock;
190 char *cp, *dp, buffer[BUFSIZ], msgid[BUFSIZ];
191 char subject[BUFSIZ];
192 char name[NAMESZ], partnum[BUFSIZ];
193 FILE *in;
194 m_getfld_state_t gstate = 0;
195
196 if ((in = fopen (drft, "r")) == NULL)
197 adios (drft, "unable to open for reading");
198
199 cp = dp = NULL;
200 start = 0L;
201
202 /*
203 * Scan through the message and examine the various header fields,
204 * as well as locate the beginning of the message body.
205 */
206 m_getfld_track_filepos (&gstate, in);
207 for (compnum = 1;;) {
208 int bufsz = sizeof buffer;
209 switch (state = m_getfld (&gstate, name, buffer, &bufsz, in)) {
210 case FLD:
211 case FLDPLUS:
212 compnum++;
213
214 /*
215 * This header field is discarded.
216 */
217 if (!strcasecmp (name, "Message-ID")) {
218 while (state == FLDPLUS) {
219 bufsz = sizeof buffer;
220 state = m_getfld (&gstate, name, buffer, &bufsz, in);
221 }
222 } else if (uprf (name, XXX_FIELD_PRF)
223 || !strcasecmp (name, VRSN_FIELD)
224 || !strcasecmp (name, "Subject")
225 || !strcasecmp (name, "Encrypted")) {
226 /*
227 * These header fields are copied to the enclosed
228 * header of the first message in the collection
229 * of message/partials. For the "Subject" header
230 * field, we also record it, so that a modified
231 * version of it, can be copied to the header
232 * of each messsage/partial in the collection.
233 */
234 if (!strcasecmp (name, "Subject")) {
235 size_t sublen;
236
237 strncpy (subject, buffer, BUFSIZ);
238 sublen = strlen (subject);
239 if (sublen > 0 && subject[sublen - 1] == '\n')
240 subject[sublen - 1] = '\0';
241 }
242
243 dp = add (concat (name, ":", buffer, NULL), dp);
244 while (state == FLDPLUS) {
245 bufsz = sizeof buffer;
246 state = m_getfld (&gstate, name, buffer, &bufsz, in);
247 dp = add (buffer, dp);
248 }
249 } else {
250 /*
251 * These header fields are copied to the header of
252 * each message/partial in the collection.
253 */
254 cp = add (concat (name, ":", buffer, NULL), cp);
255 while (state == FLDPLUS) {
256 bufsz = sizeof buffer;
257 state = m_getfld (&gstate, name, buffer, &bufsz, in);
258 cp = add (buffer, cp);
259 }
260 }
261
262 start = ftell (in) + 1;
263 continue;
264
265 case BODY:
266 case FILEEOF:
267 break;
268
269 case LENERR:
270 case FMTERR:
271 adios (NULL, "message format error in component #%d", compnum);
272
273 default:
274 adios (NULL, "getfld () returned %d", state);
275 }
276
277 break;
278 }
279 m_getfld_state_destroy (&gstate);
280 if (cp == NULL)
281 adios (NULL, "headers missing from draft");
282
283 nparts = 1;
284 pos = start;
285 while (fgets (buffer, sizeof(buffer) - 1, in)) {
286 long len;
287
288 if ((pos += (len = strlen (buffer))) > CPERMSG) {
289 nparts++;
290 pos = len;
291 }
292 }
293
294 /* Only one part, nothing to split */
295 if (nparts == 1) {
296 free (cp);
297 if (dp)
298 free (dp);
299
300 fclose (in);
301 return sendaux (vec, vecp, program, drft, st);
302 }
303
304 if (!pushsw) {
305 printf ("Sending as %d Partial Messages\n", nparts);
306 fflush (stdout);
307 }
308 status = OK;
309
310 vec[vecp++] = "-partno";
311 vec[vecp++] = partnum;
312 if (delay == 0)
313 vec[vecp++] = "-queued";
314
315 time (&clock);
316 snprintf (msgid, sizeof(msgid), "%s", message_id (clock, 0));
317
318 fseek (in, start, SEEK_SET);
319 for (partno = 1; partno <= nparts; partno++) {
320 char tmpdrf[BUFSIZ];
321 FILE *out;
322
323 char *cp = m_mktemp2(drft, invo_name, NULL, &out);
324 if (cp == NULL) {
325 adios(NULL, "unable to create temporary file in %s",
326 get_temp_dir());
327 }
328 strncpy(tmpdrf, cp, sizeof(tmpdrf));
329
330 /*
331 * Output the header fields
332 */
333 fputs (cp, out);
334 fprintf (out, "Subject: %s (part %d of %d)\n", subject, partno, nparts);
335 fprintf (out, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
336 fprintf (out, "%s: message/partial; id=\"%s\";\n", TYPE_FIELD, msgid);
337 fprintf (out, "\tnumber=%d; total=%d\n", partno, nparts);
338 fprintf (out, "%s: part %d of %d\n\n", DESCR_FIELD, partno, nparts);
339
340 /*
341 * If this is the first in the collection, output the
342 * header fields we are encapsulating at the beginning
343 * of the body of the first message.
344 */
345 if (partno == 1) {
346 if (dp)
347 fputs (dp, out);
348 fprintf (out, "Message-ID: %s\n", msgid);
349 fprintf (out, "\n");
350 }
351
352 pos = 0;
353 for (;;) {
354 long len;
355
356 if (!fgets (buffer, sizeof(buffer) - 1, in)) {
357 if (partno == nparts)
358 break;
359 adios (NULL, "premature eof");
360 }
361
362 if ((pos += (len = strlen (buffer))) > CPERMSG) {
363 fseek (in, -len, SEEK_CUR);
364 break;
365 }
366
367 fputs (buffer, out);
368 }
369
370 if (fflush (out))
371 adios (tmpdrf, "error writing to");
372
373 fclose (out);
374
375 if (!pushsw && verbsw) {
376 printf ("\n");
377 fflush (stdout);
378 }
379
380 /* Pause here, if a delay is specified */
381 if (delay > 0 && 1 < partno && partno <= nparts) {
382 if (!pushsw) {
383 printf ("pausing %d seconds before sending part %d...\n",
384 delay, partno);
385 fflush (stdout);
386 }
387 sleep ((unsigned int) delay);
388 }
389
390 snprintf (partnum, sizeof(partnum), "%d", partno);
391 status = sendaux (vec, vecp, program, tmpdrf, st);
392 unlink (tmpdrf);
393 if (status != OK)
394 break;
395
396 /*
397 * This is so sendaux will only annotate
398 * the altmsg the first time it is called.
399 */
400 annotext = NULL;
401 }
402
403 free (cp);
404 if (dp)
405 free (dp);
406
407 fclose (in); /* close the draft */
408 return status;
409 }
410
411
412 /*
413 * Annotate original message, and
414 * call `postproc' (which is passed down in "program") to send message.
415 */
416
417 static int
418 sendaux (char **vec, int vecp, char *program, char *drft, struct stat *st)
419 {
420 pid_t child_id;
421 int i, status, fd, fd2;
422 char backup[BUFSIZ], buf[BUFSIZ];
423
424 fd = pushsw ? tmp_fd () : NOTOK;
425 fd2 = NOTOK;
426
427 vec[vecp++] = drft;
428 if (annotext) {
429 if ((fd2 = tmp_fd ()) != NOTOK) {
430 vec[vecp++] = "-idanno";
431 snprintf (buf, sizeof(buf), "%d", fd2);
432 vec[vecp++] = buf;
433 } else {
434 admonish (NULL, "unable to create temporary file in %s "
435 "for annotation list", get_temp_dir());
436 }
437 }
438 if (distfile && distout (drft, distfile, backup) == NOTOK)
439 done (1);
440 vec[vecp] = NULL;
441
442 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
443 sleep (5);
444
445 switch (child_id) {
446 case -1:
447 /* oops -- fork error */
448 adios ("fork", "unable to");
449 break; /* NOT REACHED */
450
451 case 0:
452 /*
453 * child process -- send it
454 *
455 * If fd is ok, then we are pushing and fd points to temp
456 * file, so capture anything on stdout and stderr there.
457 */
458 if (fd != NOTOK) {
459 dup2 (fd, fileno (stdout));
460 dup2 (fd, fileno (stderr));
461 close (fd);
462 }
463 execvp (program, vec);
464 fprintf (stderr, "unable to exec ");
465 perror (postproc);
466 _exit (-1);
467
468 default:
469 /*
470 * parent process -- wait for it
471 */
472 if ((status = pidwait(child_id, NOTOK)) == OK) {
473 if (annotext && fd2 != NOTOK)
474 anno (fd2, st);
475 } else {
476 /*
477 * If postproc failed, and we have good fd (which means
478 * we pushed), then mail error message (and possibly the
479 * draft) back to the user.
480 */
481 if (fd != NOTOK) {
482 alert (drft, fd);
483 close (fd);
484 } else {
485 advise (NULL, "message not delivered to anyone");
486 }
487 if (annotext && fd2 != NOTOK)
488 close (fd2);
489 if (distfile) {
490 unlink (drft);
491 if (rename (backup, drft) == NOTOK)
492 advise (drft, "unable to rename %s to", backup);
493 }
494 }
495 break;
496 }
497
498 return status;
499 }
500
501
502 /*
503 * Mail error notification (and possibly a copy of the
504 * message) back to the user, using the mailproc
505 */
506
507 static void
508 alert (char *file, int out)
509 {
510 pid_t child_id;
511 int i, in, argp;
512 char buf[BUFSIZ];
513 char *program;
514 char **arglist;
515
516 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
517 sleep (5);
518
519 switch (child_id) {
520 case NOTOK:
521 /* oops -- fork error */
522 advise ("fork", "unable to");
523
524 case OK:
525 /* child process -- send it */
526 SIGNAL (SIGHUP, SIG_IGN);
527 SIGNAL (SIGINT, SIG_IGN);
528 SIGNAL (SIGQUIT, SIG_IGN);
529 SIGNAL (SIGTERM, SIG_IGN);
530 if (forwsw) {
531 if ((in = open (file, O_RDONLY)) == NOTOK) {
532 admonish (file, "unable to re-open");
533 } else {
534 lseek (out, (off_t) 0, SEEK_END);
535 strncpy (buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
536 write (out, buf, strlen (buf));
537 strncpy (buf, "\n------- Unsent Draft\n\n", sizeof(buf));
538 write (out, buf, strlen (buf));
539 cpydgst (in, out, file, "temporary file");
540 close (in);
541 strncpy (buf, "\n------- End of Unsent Draft\n", sizeof(buf));
542 write (out, buf, strlen (buf));
543 if (rename (file, strncpy (buf, m_backup (file), sizeof(buf))) == NOTOK)
544 admonish (buf, "unable to rename %s to", file);
545 }
546 }
547 lseek (out, (off_t) 0, SEEK_SET);
548 dup2 (out, fileno (stdin));
549 close (out);
550 /* create subject for error notification */
551 snprintf (buf, sizeof(buf), "send failed on %s",
552 forwsw ? "enclosed draft" : file);
553
554 arglist = argsplit(mailproc, &program, &argp);
555
556 arglist[argp++] = getusername();
557 arglist[argp++] = "-subject";
558 arglist[argp++] = buf;
559 arglist[argp] = NULL;
560
561 execvp (program, arglist);
562 fprintf (stderr, "unable to exec ");
563 perror (mailproc);
564 _exit (-1);
565
566 default: /* no waiting... */
567 break;
568 }
569 }
570
571
572 static int
573 tmp_fd (void)
574 {
575 int fd;
576 char *tfile;
577
578 if ((tfile = m_mktemp2(NULL, invo_name, &fd, NULL)) == NULL) return NOTOK;
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 }