X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/3c9700d8d045f3ff26ce5dd2a174454dafc14822..7253abffef36f7eb39ca606a08793560c0d4cfd8:/uip/sendsbr.c diff --git a/uip/sendsbr.c b/uip/sendsbr.c index b7c4cf24..2b946689 100644 --- a/uip/sendsbr.c +++ b/uip/sendsbr.c @@ -2,15 +2,23 @@ /* * sendsbr.c -- routines to help WhatNow/Send along * - * $Id$ + * This code is Copyright (c) 2002, by the authors of nmh. See the + * COPYRIGHT file in the root directory of the nmh distribution for + * complete copyright information. */ #include #include #include -#include #include #include +#include +#include + +#ifdef HAVE_SYS_TIME_H +# include +#endif +#include int debugsw = 0; /* global */ int forwsw = 1; @@ -24,25 +32,24 @@ char *altmsg = NULL; /* .. */ char *annotext = NULL; char *distfile = NULL; -static int armed = 0; static jmp_buf env; /* * external prototypes */ -int sendsbr (char **, int, char *, struct stat *, int); -int done (int); +int sendsbr (char **, int, char *, char *, struct stat *, int, char *, int); char *getusername (void); /* * static prototypes */ +static void armed_done (int) NORETURN; static void alert (char *, int); static int tmp_fd (void); static void anno (int, struct stat *); static void annoaux (int); -static int splitmsg (char **, int, char *, struct stat *, int); -static int sendaux (char **, int, char *, struct stat *); +static int splitmsg (char **, int, char *, char *, struct stat *, int); +static int sendaux (char **, int, char *, char *, struct stat *); /* @@ -50,13 +57,51 @@ static int sendaux (char **, int, char *, struct stat *); */ int -sendsbr (char **vec, int vecp, char *drft, struct stat *st, int rename_drft) +sendsbr (char **vec, int vecp, char *program, char *drft, struct stat *st, + int rename_drft, char *attachment_header_field_name, int attachformat) { int status; char buffer[BUFSIZ], file[BUFSIZ]; struct stat sts; + char *original_draft; /* name of original draft file */ + char *p; /* string pointer for building file name */ + char composition_file_name[PATH_MAX + 1]; /* name of mhbuild composition temporary file */ + char body_file_name[PATH_MAX + 1]; /* name of temporary file for body content */ + + /* + * Save the original name of the draft file. The name of the draft file is changed + * to a temporary file containing the built MIME message if there are attachments. + * We need the original name so that it can be renamed after the message is sent. + */ + + original_draft = drft; + + /* + * There might be attachments if a header field name for attachments is supplied. + * Convert the draft to a MIME message. Use the mhbuild composition file for the + * draft if there was a successful conversion because that now contains the MIME + * message. A nice side effect of this is that it leaves the original draft file + * untouched so that it can be retrieved and modified if desired. + */ + + if (attachment_header_field_name != (char *)0) { + switch (attach(attachment_header_field_name, drft, + body_file_name, sizeof body_file_name, + composition_file_name, sizeof composition_file_name, + attachformat)) { + case OK: + drft = composition_file_name; + break; + + case NOTOK: + return (NOTOK); + + case DONE: + break; + } + } - armed++; + done=armed_done; switch (setjmp (env)) { case OK: /* @@ -64,8 +109,11 @@ sendsbr (char **vec, int vecp, char *drft, struct stat *st, int rename_drft) * rename the draft file. I'm not quite sure why. */ if (pushsw && unique) { - if (rename (drft, strncpy (file, m_scratch (drft, invo_name), sizeof(file))) - == NOTOK) + char *cp = m_mktemp2(drft, invo_name, NULL, NULL); + if (cp == NULL) { + adios ("sendsbr", "unable to create temporary file"); + } + if (rename (drft, strncpy(file, cp, sizeof(file))) == NOTOK) adios (file, "unable to rename %s to", drft); drft = file; } @@ -76,14 +124,14 @@ sendsbr (char **vec, int vecp, char *drft, struct stat *st, int rename_drft) */ if (splitsw >= 0 && !distfile && stat (drft, &sts) != NOTOK && sts.st_size >= CPERMSG) { - status = splitmsg (vec, vecp, drft, st, splitsw) ? NOTOK : OK; + status = splitmsg (vec, vecp, program, drft, st, splitsw) ? NOTOK : OK; } else { - status = sendaux (vec, vecp, drft, st) ? NOTOK : OK; + status = sendaux (vec, vecp, program, drft, st) ? NOTOK : OK; } /* rename the original draft */ if (rename_drft && status == OK && - rename (drft, strncpy (buffer, m_backup (drft), sizeof(buffer))) == NOTOK) + rename (original_draft, strncpy (buffer, m_backup (original_draft), sizeof(buffer))) == NOTOK) advise (buffer, "unable to rename %s to", drft); break; @@ -92,21 +140,48 @@ sendsbr (char **vec, int vecp, char *drft, struct stat *st, int rename_drft) break; } - armed = 0; + done=exit; if (distfile) unlink (distfile); + /* + * Get rid of any temporary files that we created for attachments. Also get rid of + * the renamed composition file that mhbuild leaves as a turd. It looks confusing, + * but we use the body file name to help build the renamed composition file name. + */ + + if (drft == composition_file_name) { + clean_up_temporary_files(body_file_name, composition_file_name); + + if (strlen(composition_file_name) >= sizeof (composition_file_name) - 6) + advise((char *)0, "unable to remove original composition file."); + + else { + if ((p = strrchr(composition_file_name, '/')) == (char *)0) + p = composition_file_name; + else + p++; + + (void)strcpy(body_file_name, p); + *p++ = ','; + (void)strcpy(p, body_file_name); + (void)strcat(p, ".orig"); + + (void)unlink(composition_file_name); + } + } + return status; } - /* * Split large message into several messages of * type "message/partial" and send them. */ static int -splitmsg (char **vec, int vecp, char *drft, struct stat *st, int delay) +splitmsg (char **vec, int vecp, char *program, char *drft, + struct stat *st, int delay) { int compnum, nparts, partno, state, status; long pos, start; @@ -115,6 +190,7 @@ splitmsg (char **vec, int vecp, char *drft, struct stat *st, int delay) char subject[BUFSIZ]; char name[NAMESZ], partnum[BUFSIZ]; FILE *in; + m_getfld_state_t gstate = 0; if ((in = fopen (drft, "r")) == NULL) adios (drft, "unable to open for reading"); @@ -126,19 +202,22 @@ splitmsg (char **vec, int vecp, char *drft, struct stat *st, int delay) * Scan through the message and examine the various header fields, * as well as locate the beginning of the message body. */ - for (compnum = 1, state = FLD;;) { - switch (state = m_getfld (state, name, buffer, sizeof(buffer), in)) { + m_getfld_track_filepos (&gstate, in); + for (compnum = 1;;) { + int bufsz = sizeof buffer; + switch (state = m_getfld (&gstate, name, buffer, &bufsz, in)) { case FLD: case FLDPLUS: - case FLDEOF: compnum++; /* * This header field is discarded. */ if (!strcasecmp (name, "Message-ID")) { - while (state == FLDPLUS) - state = m_getfld (state, name, buffer, sizeof(buffer), in); + while (state == FLDPLUS) { + bufsz = sizeof buffer; + state = m_getfld (&gstate, name, buffer, &bufsz, in); + } } else if (uprf (name, XXX_FIELD_PRF) || !strcasecmp (name, VRSN_FIELD) || !strcasecmp (name, "Subject") @@ -162,7 +241,8 @@ splitmsg (char **vec, int vecp, char *drft, struct stat *st, int delay) dp = add (concat (name, ":", buffer, NULL), dp); while (state == FLDPLUS) { - state = m_getfld (state, name, buffer, sizeof(buffer), in); + bufsz = sizeof buffer; + state = m_getfld (&gstate, name, buffer, &bufsz, in); dp = add (buffer, dp); } } else { @@ -172,19 +252,16 @@ splitmsg (char **vec, int vecp, char *drft, struct stat *st, int delay) */ cp = add (concat (name, ":", buffer, NULL), cp); while (state == FLDPLUS) { - state = m_getfld (state, name, buffer, sizeof(buffer), in); + bufsz = sizeof buffer; + state = m_getfld (&gstate, name, buffer, &bufsz, in); cp = add (buffer, cp); } } - if (state != FLDEOF) { - start = ftell (in) + 1; - continue; - } - /* else fall... */ + start = ftell (in) + 1; + continue; case BODY: - case BODYEOF: case FILEEOF: break; @@ -198,6 +275,7 @@ splitmsg (char **vec, int vecp, char *drft, struct stat *st, int delay) break; } + m_getfld_state_destroy (&gstate); if (cp == NULL) adios (NULL, "headers missing from draft"); @@ -219,7 +297,7 @@ splitmsg (char **vec, int vecp, char *drft, struct stat *st, int delay) free (dp); fclose (in); - return sendaux (vec, vecp, drft, st); + return sendaux (vec, vecp, program, drft, st); } if (!pushsw) { @@ -234,17 +312,18 @@ splitmsg (char **vec, int vecp, char *drft, struct stat *st, int delay) vec[vecp++] = "-queued"; time (&clock); - snprintf (msgid, sizeof(msgid), "<%d.%ld@%s>", - (int) getpid(), (long) clock, LocalName()); + snprintf (msgid, sizeof(msgid), "%s", message_id (clock, 0)); fseek (in, start, SEEK_SET); for (partno = 1; partno <= nparts; partno++) { char tmpdrf[BUFSIZ]; FILE *out; - strncpy (tmpdrf, m_scratch (drft, invo_name), sizeof(tmpdrf)); - if ((out = fopen (tmpdrf, "w")) == NULL) - adios (tmpdrf, "unable to open for writing"); + char *cp = m_mktemp2(drft, invo_name, NULL, &out); + if (cp == NULL) { + adios (drft, "unable to create temporary file for"); + } + strncpy(tmpdrf, cp, sizeof(tmpdrf)); chmod (tmpdrf, 0600); /* @@ -308,7 +387,7 @@ splitmsg (char **vec, int vecp, char *drft, struct stat *st, int delay) } snprintf (partnum, sizeof(partnum), "%d", partno); - status = sendaux (vec, vecp, tmpdrf, st); + status = sendaux (vec, vecp, program, tmpdrf, st); unlink (tmpdrf); if (status != OK) break; @@ -331,11 +410,11 @@ splitmsg (char **vec, int vecp, char *drft, struct stat *st, int delay) /* * Annotate original message, and - * call `postproc' to send message. + * call `postproc' (which is passed down in "program") to send message. */ static int -sendaux (char **vec, int vecp, char *drft, struct stat *st) +sendaux (char **vec, int vecp, char *program, char *drft, struct stat *st) { pid_t child_id; int i, status, fd, fd2; @@ -358,7 +437,7 @@ sendaux (char **vec, int vecp, char *drft, struct stat *st) done (1); vec[vecp] = NULL; - for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++) + for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++) sleep (5); switch (child_id) { @@ -379,11 +458,10 @@ sendaux (char **vec, int vecp, char *drft, struct stat *st) dup2 (fd, fileno (stderr)); close (fd); } - execvp (postproc, vec); + execvp (program, vec); fprintf (stderr, "unable to exec "); perror (postproc); _exit (-1); - break; /* NOT REACHED */ default: /* @@ -428,8 +506,10 @@ static void alert (char *file, int out) { pid_t child_id; - int i, in; + int i, in, argp; char buf[BUFSIZ]; + char *program; + char **arglist; for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++) sleep (5); @@ -469,8 +549,14 @@ alert (char *file, int out) snprintf (buf, sizeof(buf), "send failed on %s", forwsw ? "enclosed draft" : file); - execlp (mailproc, r1bindex (mailproc, '/'), getusername (), - "-subject", buf, NULL); + arglist = argsplit(mailproc, &program, &argp); + + arglist[argp++] = getusername(); + arglist[argp++] = "-subject"; + arglist[argp++] = buf; + arglist[argp] = NULL; + + execvp (program, arglist); fprintf (stderr, "unable to exec "); perror (mailproc); _exit (-1); @@ -485,16 +571,17 @@ static int tmp_fd (void) { int fd; - char tmpfil[BUFSIZ]; + char *tfile = NULL; + + tfile = m_mktemp2(NULL, invo_name, &fd, NULL); + if (tfile == NULL) return NOTOK; + fchmod(fd, 0600); - strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil)); - if ((fd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK) - return NOTOK; if (debugsw) - advise (NULL, "temporary file %s selected", tmpfil); + advise (NULL, "temporary file %s selected", tfile); else - if (unlink (tmpfil) == NOTOK) - advise (tmpfil, "unable to remove"); + if (unlink (tfile) == NOTOK) + advise (tfile, "unable to remove"); return fd; } @@ -534,14 +621,14 @@ anno (int fd, struct stat *st) sigaddset (&set, SIGINT); sigaddset (&set, SIGQUIT); sigaddset (&set, SIGTERM); - SIGPROCMASK (SIG_BLOCK, &set, &oset); + sigprocmask (SIG_BLOCK, &set, &oset); annoaux (fd); if (child_id == OK) _exit (0); /* reset the signal mask */ - SIGPROCMASK (SIG_SETMASK, &oset, &set); + sigprocmask (SIG_SETMASK, &oset, &set); chdir (cwd); break; @@ -573,9 +660,9 @@ annoaux (int fd) admonish (maildir, "unable to change directory to"); return; } - if (!(mp = folder_read (folder))) { + if (!(mp = folder_read (folder, 0))) { if (debugsw) - admonish (NULL, "unable to read folder %s"); + admonish (NULL, "unable to read folder %s", folder); return; } @@ -629,7 +716,7 @@ annoaux (int fd) if (is_selected(mp, msgnum)) { if (debugsw) advise (NULL, "annotate message %d", msgnum); - annotate (m_name (msgnum), annotext, cp, inplace, 1); + annotate (m_name (msgnum), annotext, cp, inplace, 1, -2, 0); } } @@ -640,12 +727,8 @@ oops: } -int -done (int status) +static void +armed_done (int status) { - if (armed) - longjmp (env, status ? status : NOTOK); - - exit (status); - return 1; /* dead code to satisfy the compiler */ + longjmp (env, status ? status : NOTOK); }