Count also naked IPv4 addresses. Beautified NiXSapmSum:RE_PF.
--- a/nixspamsum Fri Feb 19 11:40:39 2010 +0000
+++ b/nixspamsum Sun Apr 11 19:00:44 2010 +0000
@@ -18,8 +18,14 @@
"""
__slots__ = ('_doms', '_mxs', '_repo')
+ RE_FQDN = '(?:[a-z0-9-]{1,63}\.){1,}[a-z]{2,6}'
+ RE_IPv4 = '(?:[\d]{1,3}\.){3}[\d]{1,3}'
"""Regular expression pattern for mail logs from Postfix"""
- RE_PF = '''^[\w\s:-]{17,80}\spostfix\/smtpd\[[\d]{3,5}\]: NOQUEUE: reject:.*blocked using ix.dnsbl.manitu.net; Spam sent to the mailhost ((?:[a-z0-9-]{1,63}\.){1,}[a-z]{2,6}) was detected by NiX Spam.*$'''
+ RE_PF = r'''^[\w\s:-]{17,80}\spostfix\/smtpd\[[\d]{1,5}\]:\sNOQUEUE:
+ \sreject:.*blocked\susing\six.dnsbl.manitu.net;
+ \sSpam\ssent\sto\sthe\smailhost\s(%s|%s)
+ \swas\sdetected\sby\sNiX\sSpam.*$''' % (RE_FQDN, RE_IPv4)
+
def __init__(self):
self._doms = {}
@@ -28,7 +34,7 @@
def setLogFormat(self, format='postfix'):
if format == 'postfix':
- self._repo = re.compile(NiXSapmSum.RE_PF)
+ self._repo = re.compile(NiXSapmSum.RE_PF, re.VERBOSE)
else:
raise Exception('MTA/Logformat not supported yet.')
@@ -43,8 +49,13 @@
self._mxs[mx] = 1
def countByDom(self):
+ ipv4po = re.compile(NiXSapmSum.RE_IPv4)
for mx in self._mxs.keys():
- dom = '.'.join(mx.split('.')[-2:])
+ mo = ipv4po.match(mx)
+ if mo:
+ dom = mo.group(0)
+ else:
+ dom = '.'.join(mx.split('.')[-2:])
try:
self._doms[dom] += self._mxs[mx]
except KeyError: