-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtemplate.py
194 lines (164 loc) · 5.93 KB
/
template.py
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
## To roll your own milter, create a class that extends Milter.
# This is a useless example to show basic features of Milter.
# See the pymilter project at https://pymilter.org based
# on Sendmail's milter API
# This code is open-source on the same terms as Python.
## Milter calls methods of your class at milter events.
## Return REJECT,TEMPFAIL,ACCEPT to short circuit processing for a message.
## You can also add/del recipients, replacebody, add/del headers, etc.
from __future__ import print_function
import Milter
try:
from StringIO import StringIO as BytesIO
except:
from io import BytesIO
import time
import email
from email import message_from_binary_file
from email import policy
import mimetypes
import os
import sys
from socket import AF_INET, AF_INET6
from Milter.utils import parse_addr
if True:
# for logging process - usually not needed
from multiprocessing import Process as Thread, Queue
else:
from threading import Thread
from Queue import Queue
logq = None
class myMilter(Milter.Base):
def __init__(self): # A new instance with each new connection.
self.id = Milter.uniqueID() # Integer incremented with each call.
# each connection runs in its own thread and has its own myMilter
# instance. Python code must be thread safe. This is trivial if only stuff
# in myMilter instances is referenced.
@Milter.noreply
def connect(self, IPname, family, hostaddr):
# (self, 'ip068.subnet71.example.com', AF_INET, ('215.183.71.68', 4720) )
# (self, 'ip6.mxout.example.com', AF_INET6,
# ('3ffe:80e8:d8::1', 4720, 1, 0) )
self.IP = hostaddr[0]
self.port = hostaddr[1]
if family == AF_INET6:
self.flow = hostaddr[2]
self.scope = hostaddr[3]
else:
self.flow = None
self.scope = None
self.IPname = IPname # Name from a reverse IP lookup
self.H = None
self.fp = None
self.receiver = self.getsymval('j')
self.log("connect from %s at %s" % (IPname, hostaddr) )
return Milter.CONTINUE
## def hello(self,hostname):
def hello(self, heloname):
# (self, 'mailout17.dallas.texas.example.com')
self.H = heloname
self.log("HELO %s" % heloname)
if heloname.find('.') < 0: # illegal helo name
# NOTE: example only - too many real braindead clients to reject on this
self.setreply('550','5.7.1','Sheesh people! Use a proper helo name!')
return Milter.REJECT
return Milter.CONTINUE
## def envfrom(self,f,*str):
def envfrom(self, mailfrom, *str):
self.F = mailfrom
self.R = [] # list of recipients
self.fromparms = Milter.dictfromlist(str) # ESMTP parms
self.user = self.getsymval('{auth_authen}') # authenticated user
self.log("mail from:", mailfrom, *str)
# NOTE: self.fp is only an *internal* copy of message data. You
# must use addheader, chgheader, replacebody to change the message
# on the MTA.
self.fp = BytesIO()
self.canon_from = '@'.join(parse_addr(mailfrom))
self.fp.write(b'From %s %s\n' % (self.canon_from.encode(),
time.ctime().encode()))
return Milter.CONTINUE
## def envrcpt(self, to, *str):
@Milter.noreply
def envrcpt(self, to, *str):
rcptinfo = to,Milter.dictfromlist(str)
self.R.append(rcptinfo)
return Milter.CONTINUE
@Milter.noreply
def header(self, name, hval):
self.fp.write(b'%s: %s\n' % (name.encode(),hval.encode())) # add header to buffer
return Milter.CONTINUE
@Milter.noreply
def eoh(self):
self.fp.write(b'\n') # terminate headers
return Milter.CONTINUE
@Milter.noreply
def body(self, chunk):
self.fp.write(chunk)
return Milter.CONTINUE
def eom(self):
self.fp.seek(0)
msg = email.message_from_binary_file(self.fp, policy=policy.default)
#example on how to iterate through attachments
for attachment in msg.iter_attachments():
#attachment holds the attachment object so that it can be used with a new MIMEMultipart() message
self.log("Attachment filename is %s" % (attachment.get_filename(),))
self.log("Attachment content/type is %s" % (attachment.get_content_type(),))
data = attachment.get_content()
self.log("Attachment content is %s" % (data,))
# many milter functions can only be called from eom()
# example of adding a Bcc:
self.addrcpt('<%s>' % '[email protected]')
return Milter.ACCEPT
def close(self):
# always called, even when abort is called. Clean up
# any external resources here.
return Milter.CONTINUE
def abort(self):
# client disconnected prematurely
return Milter.CONTINUE
## === Support Functions ===
def log(self,*msg):
t = (msg,self.id,time.time())
if logq:
logq.put(t)
else:
# logmsg(*t)
pass
def logmsg(msg,id,ts):
print("%s [%d]" % (time.strftime('%Y%b%d %H:%M:%S',time.localtime(ts)),id),
end=None)
# 2005Oct13 02:34:11 [1] msg1 msg2 msg3 ...
for i in msg: print(i,end=None)
print()
sys.stdout.flush()
def background():
while True:
t = logq.get()
if not t: break
logmsg(*t)
## ===
def main():
bt = Thread(target=background)
bt.start()
# This is NOT a good socket location for production, it is for
# playing around. I suggest /var/run/milter/myappnamesock for production.
socketname = os.path.expanduser('~/pythonsock')
timeout = 600
# Register to have the Milter factory create instances of your class:
Milter.factory = myMilter
flags = Milter.CHGBODY + Milter.CHGHDRS + Milter.ADDHDRS
flags += Milter.ADDRCPT
flags += Milter.DELRCPT
Milter.set_flags(flags) # tell Sendmail which features we use
print("%s milter startup" % time.strftime('%Y%b%d %H:%M:%S'))
sys.stdout.flush()
Milter.runmilter("pythonfilter",socketname,timeout)
logq.put(None)
bt.join()
print("%s bms milter shutdown" % time.strftime('%Y%b%d %H:%M:%S'))
if __name__ == "__main__":
# You probably do not need a logging process, but if you do, this
# is one way to do it.
logq = Queue(maxsize=4)
main()