summaryrefslogtreecommitdiff
path: root/sqmail-4.3.07/src/printforward.c
blob: 0ab7ad92076776aa8d9f6829d48458cd40360096 (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
#include <unistd.h>
#include "buffer.h"
#include "logmsg.h"
#include "stralloc.h"
#include "cdbread.h"

#define WHO "printmaillist"

void badformat()
{
  logmsg(WHO,100,FATAL,"bad database format");
}
void nomem()
{
  logmsg(WHO,111,FATAL,"out of memory");
}

void getch(char *ch)
{
  int r;
  r = buffer_get(buffer_0small,ch,1);
  if (r == -1)
    logmsg(WHO,111,FATAL,"unable to read input");
  if (r == 0)
    badformat();
}

void out(char *ch)
{
  if (buffer_put(buffer_1small,ch,1) == -1)
    logmsg(WHO,111,FATAL,"unable to write output");
}

void printbuf(char *buf)
{
  while (*buf)
    out(buf++);
}

void printsafe(char *buf,int len)
{
  char ch;

  while (len) {
    ch = *buf;
    if ((ch <= 32) || (ch == ',') || (ch == ':') || (ch == ';') || (ch == '\\') || (ch == '#'))
      out("\\");
    out(&ch);
    ++buf;
    --len;
  }
}

stralloc key = {0};
stralloc data = {0};

int main()
{
  uint32 eod;
  uint32 pos;
  uint32 klen;
  uint32 dlen;
  char buf[8];
  char ch;
  int i;
  int j;

  for (i = 0; i < 4; ++i) 
    getch(buf + i);
  eod = cdb_unpack(buf);

  for (i = 4; i < 2048; ++i) 
    getch(&ch);

  pos = 2048;
  while (pos < eod) {
    if (eod - pos < 8) badformat();
    pos += 8;
    for (i = 0; i < 8; ++i) getch(buf + i);
    klen = cdb_unpack(buf);
    dlen = cdb_unpack(buf + 4);

    if (!stralloc_copys(&key,"")) nomem();
    if (eod - pos < klen) badformat();
    pos += klen;
    while (klen) {
      --klen;
      getch(&ch);
      if (!stralloc_append(&key,&ch)) nomem();
    }

    if (eod - pos < dlen) badformat();
    pos += dlen;
    if (!stralloc_copys(&data,"")) nomem();
    while (dlen) {
      --dlen;
      getch(&ch);
      if (!stralloc_append(&data,&ch)) nomem();
    }

    if (!key.len) badformat();
    if (key.s[0] == '?') {
      printsafe(key.s + 1,key.len - 1);
      printbuf(": ?");
      printsafe(data.s,data.len);
      printbuf(";\n");
    }
    else if (key.s[0] == ':') {
      printsafe(key.s + 1,key.len - 1);
      printbuf(":\n");

      i = 0;
      for (j = 0; j < data.len; ++j)
        if (!data.s[j]) {
          if ((data.s[i] == '.') || (data.s[i] == '/')) {
            printbuf(", ");
            printsafe(data.s + i,j - i);
            printbuf("\n");
          }
          else if ((data.s[i] == '|') || (data.s[i] == '!')) {
            printbuf(", ");
            printsafe(data.s + i,j - i);
            printbuf("\n");
          }
          else if ((data.s[i] == '&') && (j - i < 900)) {
            printbuf(", ");
            printsafe(data.s + i,j - i);
            printbuf("\n");
          }
          else badformat();
          i = j + 1;
        }
      if (i != j) badformat();
      printbuf(";\n");
    }
    else badformat();
  }

  if (buffer_flush(buffer_1small) == -1)
    logmsg(WHO,111,FATAL,"unable to write output");
  _exit(0);
}