summaryrefslogtreecommitdiff
path: root/src/qreceipt.c
blob: 9cdfb46304f40b752d63bf90d835faa41655acee (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <unistd.h>

#include "alloc.h"
#include "buffer.h"
#include "env.h"
#include "error.h"
#include "exit.h"
#include "getln.h"
#include "open.h"
#include "sig.h"
#include "str.h"
#include "stralloc.h"

#include "headerbody.h"
#include "hfield.h"
#include "qmail.h"
#include "quote.h"
#include "token822.h"

#define WHO "qreceipt"

static void die_noreceipt()
{
  _exit(0);
}

static void die()
{
  _exit(100);
}

static void die_temp()
{
  _exit(111);
}

static void die_nomem()
{
  buffer_putsflush(buffer_2, "qreceipt: fatal: out of memory\n");
  die_temp();
}

static void die_fork()
{
  buffer_putsflush(buffer_2, "qreceipt: fatal: unable to fork\n");
  die_temp();
}

static void die_qqperm()
{
  buffer_putsflush(buffer_2, "qreceipt: fatal: permanent qmail-queue error\n");
  die();
}

static void die_qqtemp()
{
  buffer_putsflush(buffer_2, "qreceipt: fatal: temporary qmail-queue error\n");
  die_temp();
}

static void die_usage()
{
  buffer_putsflush(buffer_2, "qreceipt: usage: qreceipt deliveryaddress\n");
  die();
}

static void die_read()
{
  if (errno == ENOMEM) die_nomem();
  buffer_putsflush(buffer_2, "qreceipt: fatal: read error\n");
  die_temp();
}

static void doordie(stralloc *sa, int r)
{
  if (r == 1) return;
  if (r == -1) die_nomem();
  buffer_putsflush(buffer_2, "qreceipt: fatal: unable to parse this: ");
  buffer_putflush(buffer_2, sa->s, sa->len);
  die();
}

char *target;

int flagreceipt = 0;

char *returnpath;
stralloc messageid = {0};
stralloc sanotice = {0};

static int rwnotice(token822_alloc *addr)
{
  token822_reverse(addr);
  if (token822_unquote(&sanotice, addr) != 1) die_nomem();
  if (sanotice.len == str_len(target))
    if (!str_diffn(sanotice.s, target, sanotice.len)) flagreceipt = 1;
  token822_reverse(addr);
  return 1;
}

struct qmail qqt;

stralloc quoted = {0};

static void finishheader()
{
  char *qqx;

  if (!flagreceipt) die_noreceipt();
  if (str_equal(returnpath, "")) die_noreceipt();
  if (str_equal(returnpath, "#@[]")) die_noreceipt();

  if (!quote2(&quoted, returnpath)) die_nomem();

  if (qmail_open(&qqt) == -1) die_fork();

  qmail_puts(&qqt, "From: DELIVERY NOTICE SYSTEM <");
  qmail_put(&qqt, quoted.s, quoted.len);
  qmail_puts(&qqt, ">\n");
  qmail_puts(&qqt, "To: <");
  qmail_put(&qqt, quoted.s, quoted.len);
  qmail_puts(&qqt, ">\n");
  qmail_puts(
      &qqt,
      "Subject: success notice\n\n"
      "Hi! This is the qreceipt program. Your message was delivered to the\n"
      "following address: ");
  qmail_puts(&qqt, target);
  qmail_puts(&qqt, ". Thanks for asking.\n");
  if (messageid.s) {
    qmail_puts(&qqt, "Your ");
    qmail_put(&qqt, messageid.s, messageid.len);
  }

  qmail_from(&qqt, "");
  qmail_to(&qqt, returnpath);
  qqx = qmail_close(&qqt);

  if (*qqx) {
    if (*qqx == 'D')
      die_qqperm();
    else
      die_qqtemp();
  }
}

stralloc hfbuf = {0};
token822_alloc hfin = {0};
token822_alloc hfrewrite = {0};
token822_alloc hfaddr = {0};

static void doheaderfield(stralloc *h)
{
  switch (hfield_known(h->s, h->len)) {
    case H_MESSAGEID:
      if (!stralloc_copy(&messageid, h)) die_nomem();
      break;
    case H_NOTICEREQUESTEDUPONDELIVERYTO:
      doordie(h, token822_parse(&hfin, h, &hfbuf));
      doordie(h, token822_addrlist(&hfrewrite, &hfaddr, &hfin, rwnotice));
      break;
  }
}

static void dobody(stralloc *h) {}

int main(int argc, char **argv)
{
  sig_pipeignore();
  if (!(target = argv[1])) die_usage();
  if (!(returnpath = env_get("SENDER"))) die_usage();
  if (headerbody(buffer_0, doheaderfield, finishheader, dobody) == -1) die_read();
  die_noreceipt();
}