summaryrefslogtreecommitdiff
path: root/src/qmail-clean.c
blob: efeda8cbb0e93c73873cf6937d9c41ebda2e4a15 (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
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "buffer.h"
#include "byte.h"
#include "direntry.h"
#include "error.h"
#include "exit.h"
#include "fmt.h"
#include "getln.h"
#include "scan.h"
#include "sig.h"
#include "str.h"
#include "stralloc.h"

#include "auto_qmail.h"
#include "fmtqfn.h"
#include "now.h"

#define OSSIFIED 129600 /* see qmail-send.c */

stralloc line = {0};

void cleanuppid()
{
  DIR *dir;
  direntry *d;
  struct stat st;
  datetime_sec time;

  time = now();
  dir = opendir("pid");
  if (!dir) return;

  while ((d = readdir(dir))) {
    if (str_equal(d->d_name, ".")) continue;
    if (str_equal(d->d_name, "..")) continue;
    if (!stralloc_copys(&line, "pid/")) continue;
    if (!stralloc_cats(&line, d->d_name)) continue;
    if (!stralloc_0(&line)) continue;
    if (stat(line.s, &st) == -1) continue;
    if (time < st.st_atime + OSSIFIED) continue;
    unlink(line.s);
  }
  closedir(dir);
}

char fnbuf[FMTQFN];

void respond(char *s)
{
  if (buffer_putflush(buffer_1small, s, 1) == -1) _exit(100);
}

int main()
{
  int i;
  int match;
  int cleanuploop;
  unsigned long id;

  if (chdir(auto_qmail) == -1) _exit(110);
  if (chdir("queue") == -1) _exit(110);

  sig_pipeignore();

  if (!stralloc_ready(&line, 200)) _exit(111);

  cleanuploop = 0;

  for (;;) {
    if (cleanuploop) {
      --cleanuploop;
    } else {
      cleanuppid();
      cleanuploop = 30;
    }
    if (getln(buffer_0small, &line, &match, '\0') == -1) break;
    if (!match) break;
    if (line.len < 7) {
      respond("x");
      continue;
    }
    if (line.len > 100) {
      respond("x");
      continue;
    }
    if (line.s[line.len - 1]) {
      respond("x");
      continue;
    } /* impossible */

    for (i = line.len - 2; i > 4; --i) {
      if (line.s[i] == '/') break;
      if ((unsigned char)(line.s[i] - '0') > 9) {
        respond("x");
        continue;
      }
    }
    if (line.s[i] == '/' && !scan_ulong(line.s + i + 1, &id)) {
      respond("x");
      continue;
    }
#define U(prefix, flag)              \
  do {                               \
    fmtqfn(fnbuf, prefix, id, flag); \
    if (unlink(fnbuf) == -1)         \
      if (errno != ENOENT) {         \
        respond("!");                \
        continue;                    \
      }                              \
  } while (0)

    if (byte_equal(line.s, 5, "foop/")) {
      U("intd/", 1);
      U("mess/", 1);
      respond("+");
    } else if (byte_equal(line.s, 4, "todo/")) {
      U("intd/", 1);
      U("todo/", 1);
      respond("+");
    } else {
      respond("x");
    }
  }
  _exit(0);
}