summaryrefslogtreecommitdiff
path: root/src/forward.c
blob: 83372d7aaf12c66e5062228175f71035a1a1d118 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <unistd.h>

#include "buffer.h"
#include "env.h"
#include "fmt.h"
#include "logmsg.h"
#include "sig.h"

#include "qmail.h"

#define WHO "forward"

static void die_nomem()
{
  logmsg(WHO, 111, FATAL, "out of memory");
}

struct qmail qqt;

static ssize_t mywrite(int fd, char *buf, int len)
{
  qmail_put(&qqt, buf, len);
  return len;
}

char inbuf[BUFFER_INSIZE];
buffer bi = BUFFER_INIT(read, 0, inbuf, sizeof(inbuf));
char outbuf[1];
buffer bo = BUFFER_INIT(mywrite, -1, outbuf, sizeof(outbuf));

char num[FMT_ULONG];

int main(int argc, char **argv)
{
  char *sender;
  char *dtline;
  char *qqx;

  sig_pipeignore();

  sender = env_get("NEWSENDER");
  if (!sender) logmsg(WHO, 100, FATAL, "NEWSENDER not set");
  dtline = env_get("DTLINE");
  if (!dtline) logmsg(WHO, 100, FATAL, "DTLINE not set");

  if (qmail_open(&qqt) == -1) logmsg(WHO, 111, FATAL, "unable to fork: ");
  qmail_puts(&qqt, dtline);
  if (buffer_copy(&bo, &bi) != 0) logmsg(WHO, 111, FATAL, "unable to read message: ");
  buffer_flush(&bo);

  num[fmt_ulong(num, qmail_qp(&qqt))] = 0;

  qmail_from(&qqt, sender);
  while (*++argv) qmail_to(&qqt, *argv);
  qqx = qmail_close(&qqt);
  if (*qqx) logmsg(WHO, *qqx == 'D' ? 100 : 111, FATAL, qqx + 1);
  logmsg(WHO, 0, LOG, B("qp ", num));
}