3 * sendsbr.c -- routines to help WhatNow/Send along
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.
13 #include <h/signals.h>
19 #ifdef TIME_WITH_SYS_TIME
20 # include <sys/time.h>
23 # ifdef TM_IN_SYS_TIME
24 # include <sys/time.h>
30 int debugsw = 0; /* global */
38 char *altmsg = NULL; /* .. */
39 char *annotext = NULL;
40 char *distfile = NULL;
48 int sendsbr (char **, int, char *, struct stat *, int);
50 char *getusername (void);
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 *);
64 * Entry point into (back-end) routines to send message.
68 sendsbr (char **vec, int vecp, char *drft, struct stat *st, int rename_drft)
71 char buffer[BUFSIZ], file[BUFSIZ];
75 switch (setjmp (env)) {
78 * If given -push and -unique (which is undocumented), then
79 * rename the draft file. I'm not quite sure why.
81 if (pushsw && unique) {
82 if (rename (drft, strncpy (file, m_scratch (drft, invo_name), sizeof(file)))
84 adios (file, "unable to rename %s to", drft);
89 * Check if we need to split the message into
90 * multiple messages of type "message/partial".
92 if (splitsw >= 0 && !distfile && stat (drft, &sts) != NOTOK
93 && sts.st_size >= CPERMSG) {
94 status = splitmsg (vec, vecp, drft, st, splitsw) ? NOTOK : OK;
96 status = sendaux (vec, vecp, drft, st) ? NOTOK : OK;
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);
119 * Split large message into several messages of
120 * type "message/partial" and send them.
124 splitmsg (char **vec, int vecp, char *drft, struct stat *st, int delay)
126 int compnum, nparts, partno, state, status;
129 char *cp, *dp, buffer[BUFSIZ], msgid[BUFSIZ];
130 char subject[BUFSIZ];
131 char name[NAMESZ], partnum[BUFSIZ];
134 if ((in = fopen (drft, "r")) == NULL)
135 adios (drft, "unable to open for reading");
141 * Scan through the message and examine the various header fields,
142 * as well as locate the beginning of the message body.
144 for (compnum = 1, state = FLD;;) {
145 switch (state = m_getfld (state, name, buffer, sizeof(buffer), in)) {
152 * This header field is discarded.
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")) {
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.
169 if (!strcasecmp (name, "Subject")) {
172 strncpy (subject, buffer, BUFSIZ);
173 sublen = strlen (subject);
174 if (sublen > 0 && subject[sublen - 1] == '\n')
175 subject[sublen - 1] = '\0';
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);
185 * These header fields are copied to the header of
186 * each message/partial in the collection.
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);
195 if (state != FLDEOF) {
196 start = ftell (in) + 1;
208 adios (NULL, "message format error in component #%d", compnum);
211 adios (NULL, "getfld () returned %d", state);
217 adios (NULL, "headers missing from draft");
221 while (fgets (buffer, sizeof(buffer) - 1, in)) {
224 if ((pos += (len = strlen (buffer))) > CPERMSG) {
230 /* Only one part, nothing to split */
237 return sendaux (vec, vecp, drft, st);
241 printf ("Sending as %d Partial Messages\n", nparts);
246 vec[vecp++] = "-partno";
247 vec[vecp++] = partnum;
249 vec[vecp++] = "-queued";
252 snprintf (msgid, sizeof(msgid), "<%d.%ld@%s>",
253 (int) getpid(), (long) clock, LocalName());
255 fseek (in, start, SEEK_SET);
256 for (partno = 1; partno <= nparts; partno++) {
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);
266 * Output the header fields
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);
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.
283 fprintf (out, "Message-ID: %s\n", msgid);
291 if (!fgets (buffer, sizeof(buffer) - 1, in)) {
292 if (partno == nparts)
294 adios (NULL, "premature eof");
297 if ((pos += (len = strlen (buffer))) > CPERMSG) {
298 fseek (in, -len, SEEK_CUR);
306 adios (tmpdrf, "error writing to");
310 if (!pushsw && verbsw) {
315 /* Pause here, if a delay is specified */
316 if (delay > 0 && 1 < partno && partno <= nparts) {
318 printf ("pausing %d seconds before sending part %d...\n",
322 sleep ((unsigned int) delay);
325 snprintf (partnum, sizeof(partnum), "%d", partno);
326 status = sendaux (vec, vecp, tmpdrf, st);
332 * This is so sendaux will only annotate
333 * the altmsg the first time it is called.
342 fclose (in); /* close the draft */
348 * Annotate original message, and
349 * call `postproc' to send message.
353 sendaux (char **vec, int vecp, char *drft, struct stat *st)
356 int i, status, fd, fd2;
357 char backup[BUFSIZ], buf[BUFSIZ];
359 fd = pushsw ? tmp_fd () : NOTOK;
364 if ((fd2 = tmp_fd ()) != NOTOK) {
365 vec[vecp++] = "-idanno";
366 snprintf (buf, sizeof(buf), "%d", fd2);
369 admonish (NULL, "unable to create file for annotation list");
372 if (distfile && distout (drft, distfile, backup) == NOTOK)
376 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
381 /* oops -- fork error */
382 adios ("fork", "unable to");
383 break; /* NOT REACHED */
387 * child process -- send it
389 * If fd is ok, then we are pushing and fd points to temp
390 * file, so capture anything on stdout and stderr there.
393 dup2 (fd, fileno (stdout));
394 dup2 (fd, fileno (stderr));
397 execvp (postproc, vec);
398 fprintf (stderr, "unable to exec ");
401 break; /* NOT REACHED */
405 * parent process -- wait for it
407 if ((status = pidwait(child_id, NOTOK)) == OK) {
408 if (annotext && fd2 != NOTOK)
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.
420 advise (NULL, "message not delivered to anyone");
422 if (annotext && fd2 != NOTOK)
426 if (rename (backup, drft) == NOTOK)
427 advise (drft, "unable to rename %s to", backup);
438 * Mail error notification (and possibly a copy of the
439 * message) back to the user, using the mailproc
443 alert (char *file, int out)
449 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
454 /* oops -- fork error */
455 advise ("fork", "unable to");
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);
464 if ((in = open (file, O_RDONLY)) == NOTOK) {
465 admonish (file, "unable to re-open");
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");
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);
480 lseek (out, (off_t) 0, SEEK_SET);
481 dup2 (out, fileno (stdin));
483 /* create subject for error notification */
484 snprintf (buf, sizeof(buf), "send failed on %s",
485 forwsw ? "enclosed draft" : file);
487 execlp (mailproc, r1bindex (mailproc, '/'), getusername (),
488 "-subject", buf, NULL);
489 fprintf (stderr, "unable to exec ");
493 default: /* no waiting... */
505 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
506 if ((fd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
509 advise (NULL, "temporary file %s selected", tmpfil);
511 if (unlink (tmpfil) == NOTOK)
512 advise (tmpfil, "unable to remove");
519 anno (int fd, struct stat *st)
523 static char *cwd = NULL;
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)) {
532 admonish (NULL, "$mhaltmsg mismatch");
536 child_id = debugsw ? NOTOK : fork ();
538 case NOTOK: /* oops */
541 "unable to fork, so doing annotations by hand...");
543 cwd = getcpy (pwd ());
546 /* block a few signals */
548 sigaddset (&set, SIGHUP);
549 sigaddset (&set, SIGINT);
550 sigaddset (&set, SIGQUIT);
551 sigaddset (&set, SIGTERM);
552 SIGPROCMASK (SIG_BLOCK, &set, &oset);
558 /* reset the signal mask */
559 SIGPROCMASK (SIG_SETMASK, &oset, &set);
564 default: /* no waiting... */
574 int fd2, fd3, msgnum;
575 char *cp, *folder, *maildir;
576 char buffer[BUFSIZ], **ap;
580 if ((folder = getenv ("mhfolder")) == NULL || *folder == 0) {
582 admonish (NULL, "$mhfolder not set");
585 maildir = m_maildir (folder);
586 if (chdir (maildir) == NOTOK) {
588 admonish (maildir, "unable to change directory to");
591 if (!(mp = folder_read (folder))) {
593 admonish (NULL, "unable to read folder %s");
597 /* check for empty folder */
598 if (mp->nummsg == 0) {
600 admonish (NULL, "no messages in %s", folder);
604 if ((cp = getenv ("mhmessages")) == NULL || *cp == 0) {
606 admonish (NULL, "$mhmessages not set");
609 if (!debugsw /* MOBY HACK... */
611 && (fd3 = open ("/dev/null", O_RDWR)) != NOTOK
612 && (fd2 = dup (fileno (stderr))) != NOTOK) {
613 dup2 (fd3, fileno (stderr));
618 for (ap = brkstring (cp = getcpy (cp), " ", NULL); *ap; ap++)
622 dup2 (fd2, fileno (stderr));
623 if (mp->numsel == 0) {
625 admonish (NULL, "no messages to annotate");
629 lseek (fd, (off_t) 0, SEEK_SET);
630 if ((fp = fdopen (fd, "r")) == NULL) {
632 admonish (NULL, "unable to fdopen annotation list");
636 while (fgets (buffer, sizeof(buffer), fp) != NULL)
637 cp = add (buffer, cp);
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)) {
646 advise (NULL, "annotate message %d", msgnum);
647 annotate (m_name (msgnum), annotext, cp, inplace, 1);
654 folder_free (mp); /* free folder/message structure */
662 longjmp (env, status ? status : NOTOK);
665 return 1; /* dead code to satisfy the compiler */