| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# Copyright (c) Twisted Matrix Laboratories. |
| 4 |
# See LICENSE for details. |
| 5 |
|
| 6 |
""" |
| 7 |
Example of an interface to Courier's mail filter. |
| 8 |
""" |
| 9 |
|
| 10 |
LOGFILE = "/tmp/filter.log" |
| 11 |
|
| 12 |
# Setup log file |
| 13 |
from twisted.python import log |
| 14 |
|
| 15 |
log.startLogging(open(LOGFILE, "a")) |
| 16 |
import sys |
| 17 |
|
| 18 |
sys.stderr = log.logfile |
| 19 |
|
| 20 |
# Twisted imports |
| 21 |
from twisted.internet import reactor, stdio |
| 22 |
from twisted.internet.protocol import Factory, Protocol |
| 23 |
from twisted.protocols import basic |
| 24 |
|
| 25 |
FILTERS = "/var/lib/courier/filters" |
| 26 |
ALLFILTERS = "/var/lib/courier/allfilters" |
| 27 |
FILTERNAME = "twistedfilter" |
| 28 |
|
| 29 |
import email.message |
| 30 |
import email.parser |
| 31 |
import os |
| 32 |
import os.path |
| 33 |
from syslog import LOG_MAIL, openlog, syslog |
| 34 |
|
| 35 |
|
| 36 |
def trace_dump(): |
| 37 |
t, v, tb = sys.exc_info() |
| 38 |
openlog(FILTERNAME, 0, LOG_MAIL) |
| 39 |
syslog(f"Unhandled exception: {v} - {t}") |
| 40 |
while tb: |
| 41 |
syslog( |
| 42 |
"Trace: {}:{} {}".format( |
| 43 |
tb.tb_frame.f_code.co_filename, tb.tb_frame.f_code.co_name, tb.tb_lineno |
| 44 |
) |
| 45 |
) |
| 46 |
tb = tb.tb_next |
| 47 |
# just to be safe |
| 48 |
del tb |
| 49 |
|
| 50 |
|
| 51 |
def safe_del(file): |
| 52 |
try: |
| 53 |
if os.path.isdir(file): |
| 54 |
os.removedirs(file) |
| 55 |
else: |
| 56 |
os.remove(file) |
| 57 |
except OSError: |
| 58 |
pass |
| 59 |
|
| 60 |
|
| 61 |
class DieWhenLost(Protocol): |
| 62 |
def connectionLost(self, reason=None): |
| 63 |
reactor.stop() |
| 64 |
|
| 65 |
|
| 66 |
class MailProcessor(basic.LineReceiver): |
| 67 |
""" |
| 68 |
I process a mail message. |
| 69 |
|
| 70 |
Override filterMessage to do any filtering you want. |
| 71 |
""" |
| 72 |
|
| 73 |
messageFilename = None |
| 74 |
delimiter = "\n" |
| 75 |
|
| 76 |
def connectionMade(self): |
| 77 |
log.msg(f"Connection from {self.transport}") |
| 78 |
self.state = "connected" |
| 79 |
self.metaInfo = [] |
| 80 |
|
| 81 |
def lineReceived(self, line): |
| 82 |
if self.state == "connected": |
| 83 |
self.messageFilename = line |
| 84 |
self.state = "gotMessageFilename" |
| 85 |
if self.state == "gotMessageFilename": |
| 86 |
if line: |
| 87 |
self.metaInfo.append(line) |
| 88 |
else: |
| 89 |
if not self.metaInfo: |
| 90 |
self.transport.loseConnection() |
| 91 |
return |
| 92 |
self.filterMessage() |
| 93 |
|
| 94 |
def filterMessage(self): |
| 95 |
"""Override this. |
| 96 |
|
| 97 |
A trivial example is included. |
| 98 |
""" |
| 99 |
try: |
| 100 |
emailParser = email.parser.Parser() |
| 101 |
with open(self.messageFilename) as f: |
| 102 |
emailParser.parse(f) |
| 103 |
self.sendLine(b"200 Ok") |
| 104 |
except BaseException: |
| 105 |
trace_dump() |
| 106 |
self.sendLine(b"435 " + FILTERNAME.encode("ascii") + b" processing error") |
| 107 |
|
| 108 |
|
| 109 |
def main(): |
| 110 |
# Listen on the UNIX socket |
| 111 |
f = Factory() |
| 112 |
f.protocol = MailProcessor |
| 113 |
safe_del(f"{ALLFILTERS}/{FILTERNAME}") |
| 114 |
reactor.listenUNIX(f"{ALLFILTERS}/{FILTERNAME}", f, 10) |
| 115 |
|
| 116 |
# Once started, close fd 3 to let Courier know we're ready |
| 117 |
reactor.callLater(0, os.close, 3) |
| 118 |
|
| 119 |
# When stdin is closed, it's time to exit. |
| 120 |
stdio.StandardIO(DieWhenLost()) |
| 121 |
|
| 122 |
# Go! |
| 123 |
reactor.run() |
| 124 |
|
| 125 |
|
| 126 |
if __name__ == "__main__": |
| 127 |
main() |