summaryrefslogtreecommitdiff
path: root/src/qmail-qread.c
blob: 8fbc96f0319fe7a7c2e4955efb63066784bf74ba (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "error.h"
#include "exit.h"
#include "fmt.h"
#include "getln.h"
#include "open.h"
#include "str.h"
#include "stralloc.h"

#include "auto_qmail.h"
#include "date822fmt.h"
#include "datetime.h"
#include "fmtqfn.h"
#include "readsubdir.h"

readsubdir rs;

void die(int n)
{
  buffer_flush(buffer_1);
  _exit(n);
}

void warn(char *s1, char *s2)
{
  char *x;
  x = error_str(errno);
  buffer_puts(buffer_1, s1);
  buffer_puts(buffer_1, s2);
  buffer_puts(buffer_1, ": ");
  buffer_puts(buffer_1, x);
  buffer_puts(buffer_1, "\n");
}

void die_nomem()
{
  buffer_puts(buffer_1, "fatal: out of memory\n");
  die(111);
}

void die_chdir()
{
  warn("fatal: unable to chdir", "");
  die(110);
}

void die_opendir(fn) char *fn;
{
  warn("fatal: unable to opendir ", fn);
  die(110);
}

void err(unsigned long id)
{
  char foo[FMT_ULONG];
  foo[fmt_ulong(foo, id)] = 0;
  warn("warning: trouble with #", foo);
}

char fnmess[FMTQFN];
char fninfo[FMTQFN];
char fnlocal[FMTQFN];
char fnremote[FMTQFN];
char fnbounce[FMTQFN];

char inbuf[1024];
stralloc sender = {0};

unsigned long id;
datetime_sec qtime;
int flagbounce;
unsigned long size;

unsigned int fmtstats(char *s)
{
  struct datetime dt;
  unsigned int len;
  unsigned int i;

  len = 0;
  datetime_tai(&dt, qtime);
  i = date822fmt(s, &dt) - 7 /*XXX*/;
  len += i;
  if (s) s += i;
  i = fmt_str(s, " GMT  #");
  len += i;
  if (s) s += i;
  i = fmt_ulong(s, id);
  len += i;
  if (s) s += i;
  i = fmt_str(s, "  ");
  len += i;
  if (s) s += i;
  i = fmt_ulong(s, size);
  len += i;
  if (s) s += i;
  i = fmt_str(s, "  <");
  len += i;
  if (s) s += i;
  i = fmt_str(s, sender.s + 1);
  len += i;
  if (s) s += i;
  i = fmt_str(s, "> ");
  len += i;
  if (s) s += i;
  if (flagbounce) {
    i = fmt_str(s, " bouncing");
    len += i;
    if (s) s += i;
  }

  return len;
}

stralloc stats = {0};

void out(char *s, unsigned int n)
{
  while (n > 0) {
    buffer_put(buffer_1, ((*s >= 32) && (*s <= 126)) ? s : "_", 1);
    --n;
    ++s;
  }
}

void outs(char *s)
{
  out(s, str_len(s));
}

void outok(char *s)
{
  buffer_puts(buffer_1, s);
}

void putstats()
{
  if (!stralloc_ready(&stats, fmtstats(FMT_LEN))) die_nomem();
  stats.len = fmtstats(stats.s);
  out(stats.s, stats.len);
  outok("\n");
}

stralloc line = {0};

int main()
{
  int channel;
  int match;
  struct stat st;
  int fd;
  buffer b;
  int x;

  if (chdir(auto_qmail) == -1) die_chdir();
  if (chdir("queue") == -1) die_chdir();
  readsubdir_init(&rs, "info", die_opendir);

  while ((x = readsubdir_next(&rs, &id))) {
    if (x > 0) {
      fmtqfn(fnmess, "mess/", id, 1);
      fmtqfn(fninfo, "info/", id, 1);
      fmtqfn(fnlocal, "local/", id, 1);
      fmtqfn(fnremote, "remote/", id, 1);
      fmtqfn(fnbounce, "bounce/", id, 0);

      if (stat(fnmess, &st) == -1) {
        err(id);
        continue;
      }
      size = st.st_size;
      flagbounce = !stat(fnbounce, &st);

      fd = open_read(fninfo);
      if (fd == -1) {
        err(id);
        continue;
      }
      buffer_init(&b, read, fd, inbuf, sizeof(inbuf));
      if (getln(&b, &sender, &match, 0) == -1) die_nomem();
      if (fstat(fd, &st) == -1) {
        close(fd);
        err(id);
        continue;
      }
      close(fd);
      qtime = st.st_mtime;

      putstats();

      for (channel = 0; channel < 2; ++channel) {
        fd = open_read(channel ? fnremote : fnlocal);
        if (fd == -1) {
          if (errno != ENOENT) err(id);
        } else {
          for (;;) {
            if (getln(&b, &line, &match, 0) == -1) die_nomem();
            if (!match) break;
            switch (line.s[0]) {
              case 'D': outok("  done");
              case 'T':
                outok(channel ? "\tremote\t" : "\tlocal\t");
                outs(line.s + 1);
                outok("\n");
                break;
            }
          }
          close(fd);
        }
      }
    }
  }

  die(0);
}