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
|
#! /usr/bin/env python3
import sys
sys.stdin.reconfigure(encoding='utf-8')
sys.stdout.reconfigure(encoding='utf-8')
stdin = sys.stdin
stdout = sys.stdout
mailaddr = sys.argv[1]
inheader = {}
# Change to actual file for logging
logfile = open("/dev/null", "a")
def log(l, i):
logfile.write("{} {}\n".format(i, l))
logfile.flush()
def send(l):
log(l, ">")
stdout.write("{}\n".format(l))
stdout.flush()
def token_and_sid(version, sid, token):
if version < "0.5":
return "{}|{}".format(token, sid)
else:
return "{}|{}".format(sid, token)
log("started", "l")
while True:
line = stdin.readline().strip()
log(line, "<")
if not line:
log("finished", "l")
break
splitted = line.split("|")
if line == "config|ready":
log("in config ready", "l")
send("register|filter|smtp-in|mail-from")
send("register|filter|smtp-in|data-line")
send("register|ready")
if splitted[0] != "filter":
continue
if len(splitted) < 7:
send("invalid filter command: expected >6 fields!")
sys.exit(1)
version = splitted[1]
action = splitted[4]
sid = splitted[5]
token = splitted[6]
token_sid = token_and_sid(version, sid, token)
rest = "|".join(splitted[7:])
if action == "mail-from":
inheader[sid] = True
send("filter-result|{}|rewrite|<{}>".format(token_sid, mailaddr))
continue
if action == "data-line":
if rest == "" and inheader.get(sid, False):
inheader[sid] = False
if rest == "." and not inheader.get(sid):
del(inheader[sid])
if inheader.get(sid, False) and rest.upper().startswith("FROM:"):
send("filter-dataline|{}|From: {}".format(token_sid, mailaddr))
else:
send("filter-dataline|{}|{}".format(token_sid, rest))
continue
send("filter-result|{}|proceed".format(token_sid))
|