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