annotate spamc.py @ 0:7f4443127958 default tip

Import
author Daniele Nicolodi <daniele@grinta.net>
date Wed, 02 Nov 2011 12:03:09 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
1 # implements http://svn.apache.org/repos/asf/spamassassin/trunk/spamd/PROTOCOL
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
2
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
3 import re
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
4 import socket
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
5 import email.parser
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
6
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
7
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
8 # default spamd port
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
9 PORT = 783
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
10
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
11 # maximal line length when calling readline()
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
12 MAXLINE = 65536
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
13
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
14 # maximal amount of data to read at one time in
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
15 MAXAMOUNT = 1048576
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
16
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
17 # marker
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
18 UNKNOWN = 'UNKNOWN'
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
19
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
20
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
21 class ProtocolError(Exception):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
22 pass
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
23
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
24
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
25 class Response(object):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
26 def __init__(self, sock, method):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
27 self.fd = sock.makefile('rb', 0)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
28 self.method = method
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
29
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
30 self.version = UNKNOWN
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
31 self.status = UNKNOWN
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
32 self.reason = UNKNOWN
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
33 self.result = UNKNOWN
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
34 self.score = UNKNOWN
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
35 self.required = UNKNOWN
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
36
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
37 def _read_status_line(self):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
38 line = self.fd.readline()
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
39 if not line:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
40 raise ProtocolError('empty status line')
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
41
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
42 try:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
43 version, status, reason = line.split()
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
44 except ValueError:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
45 raise ProtocolError('bad status line: %s' % line)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
46
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
47 if not version.startswith('SPAMD/'):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
48 raise ProtocolError('bad status line: %s' % line)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
49
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
50 try:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
51 version = float(version[6:])
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
52 status = int(status)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
53 reason = reason.strip()
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
54 except ValueError:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
55 raise ProtocolError('bad status line: %s' % line)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
56
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
57 if not self.version > 1.0:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
58 raise ProtocolError('unknown protocol: %s' % self.version)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
59
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
60 return version, status, reason
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
61
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
62 def _read_headers(self):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
63 headers = []
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
64 while True:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
65 line = self.fd.readline(MAXLINE + 1)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
66 if len(line) > MAXLINE:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
67 raise ProtocolError('header line too long')
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
68 headers.append(line)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
69 if line in ('\r\n', '\n', ''):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
70 break
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
71 hstring = ''.join(headers)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
72 return email.parser.Parser().parsestr(hstring)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
73
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
74 def begin(self):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
75 # parse response status line
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
76 self.version, self.status, self.reason = self._read_status_line()
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
77
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
78 # parse headers
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
79 self.headers = self._read_headers()
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
80
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
81 # response length
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
82 try:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
83 self.length = int(self.headers.get('content-length', ''))
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
84 except ValueError:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
85 self.length = None
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
86
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
87 # if the used methods returns a Spam header
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
88 if self.method in ('CHECK', 'SYMBOLS', 'REPORT',
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
89 'REPORT_IFSPAM', 'PROCESS', 'HEADERS'):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
90 # parse Spam header
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
91 value = self.headers.get("spam")
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
92 try:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
93 m = re.match(r'^(\w+)\s;\s(-?\d+.?\d?)\s/\s(\d+.?\d?)$', value)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
94 result, score, required = m.groups()
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
95 self.result = result == 'True'
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
96 self.score = float(score)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
97 self.required = float(required)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
98 except (AttributeError, ValueError):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
99 raise ProtocolError('bad results header: %s' % value)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
100
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
101 def _read_data(self, size):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
102 s = []
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
103 while size > 0:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
104 chunk = self.fd.read(min(size, MAXAMOUNT))
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
105 if not chunk:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
106 raise ProtocolError('incomplete read')
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
107 s.append(chunk)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
108 size = size - len(chunk)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
109 return ''.join(s)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
110
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
111 def read(self, size=None):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
112 if self.fd is None:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
113 return ''
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
114
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
115 if self.length is not None:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
116 if size is None or size > self.length:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
117 # clip read to response size
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
118 size = self.length
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
119 s = self._read_data(size)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
120 self.length = self.length - size
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
121 if not self.length:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
122 # we read everything
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
123 self.close()
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
124 return s
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
125
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
126 s = self.fd.read(size)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
127 return s
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
128
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
129 def close(self):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
130 if self.fd:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
131 self.fd.close()
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
132 self.fd = None
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
133
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
134 def __str__(self):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
135 return '%s %s' % (self.status, self.reason)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
136
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
137
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
138 class Client(object):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
139 def __init__(self, host, port=PORT, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source=None):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
140 self.host = host
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
141 self.port = port
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
142 self.timeout = timeout
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
143 self.source = source
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
144 self.sock = None
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
145 self.method = None
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
146 self.response = None
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
147
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
148 def connect(self):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
149 self.sock = socket.create_connection((self.host,self.port),
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
150 self.timeout, self.source)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
151
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
152 def close(self):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
153 if self.sock:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
154 self.sock.close()
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
155 self.sock = None
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
156 if self.response:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
157 self.response.close()
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
158 self.response = None
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
159
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
160 def request(self, method, body=None, user=None, headers={}):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
161 # save the used method
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
162 self.method = method
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
163
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
164 # buffer output
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
165 output = []
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
166
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
167 # construct request
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
168 request = '%s SPAMC/1.5' % method
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
169 output.append(request)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
170 if body is not None:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
171 output.append('Content-length: %d' % len(body))
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
172 if user is not None:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
173 output.append('User: %s' % user)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
174 # remove user from provided headers
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
175 headers.pop('User', None)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
176 for header, value in headers.iteritems():
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
177 output.append('%s: %s' % (header, value))
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
178 output.append('')
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
179 if body is not None:
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
180 output.append(body)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
181 output.append('')
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
182
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
183 # send
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
184 data = '\r\n'.join(output)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
185 del output[:]
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
186 self.connect()
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
187 self.sock.sendall(data)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
188
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
189 # parse response
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
190 self.response = Response(self.sock, self.method)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
191 self.response.begin()
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
192 return self.response
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
193
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
194
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
195
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
196 MESSAGE = """\
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
197 Message-ID: <4E9D52C3.7060305@example.org>
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
198 Date: Tue, 18 Oct 2011 12:19:47 +0200
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
199 From: Example <example@example.org>
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
200 MIME-Version: 1.0
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
201 To: example@example.org
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
202 Subject: Test
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
203 Content-Type: text/plain; charset=ISO-8859-1
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
204 Content-Transfer-Encoding: 7bit
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
205
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
206 Hello
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
207
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
208 """
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
209
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
210
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
211 def main():
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
212
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
213 conn = Client(HOST, PORT)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
214 resp = conn.request('PING')
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
215 print resp
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
216 print resp.read()
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
217
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
218 for method in ('CHECK', 'SYMBOLS', 'REPORT', 'PROCESS', 'HEADERS', ):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
219 print method
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
220 conn = Client(HOST, PORT)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
221 resp = conn.request(method, MESSAGE, user='daniele')
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
222 print resp
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
223 print resp.read(1024)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
224
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
225 method = 'HEADERS'
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
226 conn = Client(HOST, PORT)
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
227 resp = conn.request(method, MESSAGE, user='daniele')
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
228
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
229 headers = email.parser.Parser().parsestr(resp.read())
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
230 for name in ('X-Spam-Checker-Version', 'X-Spam-Level', 'X-Spam-Status', ):
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
231 print name, headers.get(name, '')
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
232
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
233
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
234 if __name__ == '__main__':
Daniele Nicolodi <daniele@grinta.net>
parents:
diff changeset
235 main()