#include <h/utils.h>
#define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
-#define FCCS 10 /* max number of fccs allowed */
struct swit switches[] = {
#define VERBSW 0
unsigned int set;
};
-
static struct headers NHeaders[] = {
{ "Return-Path", HBAD, 0 },
{ "Received", HBAD, 0 },
normal, resent
} msgstate = normal;
-static char tmpfil[] = "/tmp/spost-XXXXXX";
-static char bccfil[] = "/tmp/spost-bcc-XXXXXX";
+static char *tmpfil;
static char *subject = NULL; /* the subject field for BCC'ing */
static char fccs[BUFSIZ] = "";
-struct mailname *bccs = NULL;
+struct mailname *bccs = NULL; /* list of the bcc recipients */
static struct headers *hdrtab; /* table for the message we're doing */
static FILE *out; /* output (temp) file */
-extern char *sendmail;
-
/*
** static prototypes
*/
static int putone(char *, int, int);
static void process_fcc(char *);
static void fcc(char *, char *);
-static void make_bcc_draft(char *);
+static void process_bccs(char *);
int
main(int argc, char **argv)
{
- int state, i, pid, compnum;
+ int state, compnum;
char *cp, *msg = NULL, **argp, **arguments;
char *sargv[16], buf[BUFSIZ], name[NAMESZ];
FILE *in;
verbose++;
out = stdout;
} else {
-#ifdef HAVE_MKSTEMP
- if ((out = fdopen(mkstemp(tmpfil), "w")) == NULL)
- adios(tmpfil, "unable to create");
-#else
- mktemp(tmpfil);
- if ((out = fopen(tmpfil, "w")) == NULL)
- adios(tmpfil, "unable to create");
- chmod(tmpfil, 0600);
-#endif
+ tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
}
hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
fcc(tmpfil, fccs);
}
- /* process Bcc */
- if (bccs) {
- make_bcc_draft(tmpfil);
- }
-
- /*
- ** re-open the temp file, unlink it and exec sendmail, giving it
- ** the msg temp file as std in.
- */
- if (!freopen(tmpfil, "r", stdin)) {
- adios(tmpfil, "can't reopen for sendmail");
- }
- unlink(tmpfil);
-
argp = sargv;
*argp++ = "send-mail";
*argp++ = "-m"; /* send to me too */
}
*argp = NULL;
- if (pushflg) {
- /* fork to a child to run sendmail */
- for (i=0; (pid = fork()) == NOTOK && i < 5; i++)
- sleep(5);
- switch (pid) {
- case NOTOK:
- fprintf(verbose ? stdout : stderr,
- "%s: can't fork to %s\n",
- invo_name, sendmail);
- exit(-1);
- case OK:
- /* we're the child .. */
- break;
- default:
+ if (bccs) {
+ process_bccs(tmpfil);
+ if (!(msgflags & MVIS)) {
+ /* only Bcc rcpts: we're finished here */
+ unlink(tmpfil);
exit(0);
}
}
- if (0 /*bccs*/) {
+ /*
+ ** re-open the temp file, unlink it and exec sendmail, giving it
+ ** the msg temp file as std in.
+ */
+ if (!freopen(tmpfil, "r", stdin)) {
+ adios(tmpfil, "can't reopen for sendmail");
+ }
+ unlink(tmpfil);
+
+ if (pushflg) {
/* fork to a child to run sendmail */
- for (i=0; (pid = fork()) == NOTOK && i < 5; i++)
- sleep(5);
- switch (pid) {
+ switch (fork()) {
case NOTOK:
fprintf(verbose ? stdout : stderr,
"%s: can't fork to %s\n",
exit(-1);
case OK:
/* we're the child .. */
- exit(0);
+ break;
default:
exit(0);
}
}
-
execv(sendmail, sargv);
adios(sendmail, "can't exec");
- return 0; /* dead code to satisfy the compiler */
+ return -1;
}
+
/* DRAFT GENERATION */
static void
fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
}
if (badmsg) {
+ unlink(tmpfil);
adios(NULL, "re-format message and try again");
}
}
/* BCC GENERATION */
static void
-make_bcc_draft(char *draft)
+process_bccs(char *origmsg)
{
- int fd;
- FILE *out;
-
-#ifdef HAVE_MKSTEMP
- fd = mkstemp(bccfil);
- if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
- adios(bccfil, "unable to create");
-#else
- mktemp(bccfil);
- if ((out = fopen(bccfil, "w")) == NULL)
- adios(bccfil, "unable to create");
-#endif
- chmod(bccfil, 0600);
+ char *bccdraft = NULL;
+ char buf[BUFSIZ];
+ struct mailname *mp = NULL;
+ FILE *out = NULL;
- if (subject) {
- fprintf(out, "Subject: BCC: %s", subject);
- } else {
- fprintf(out, "Subject: BCC: message without subject");
+ for (mp=bccs; mp; mp=mp->m_next) {
+ /*
+ ** Note: This draft file will be left existing by send(1),
+ ** although renamed with backup prefix.
+ ** TODO: We should have it removed eventually.
+ */
+ bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
+ fprintf(out, "To: %s\n", mp->m_text);
+ fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
+ fprintf(out, "%s: %s\n", attach_hdr, origmsg);
+ fprintf(out, "------------\n");
+ fclose(out);
+
+ snprintf(buf, sizeof buf, "%s%s %s", sendproc,
+ pushflg ? " -push" : "", bccdraft);
+ if (system(buf) != 0) {
+ admonish(invo_name, "Problems to send Bcc to %s",
+ mp->m_text);
+ unlink(bccdraft);
+ }
+ /* TODO: unlink renamed bcc draft after send(1) */
}
- fprintf(out, "%s: %s\n", attach_hdr, draft);
- fprintf(out, "------------\n");
- fclose(out);
}