Browse Source

Added option to set the recipient-delimiter to provide cleaned up recipient addresses

master
Johann Schmitz 3 years ago
parent
commit
51ce06aaf6
Signed by: ercpe <johann@j-schmitz.net> GPG Key ID: A084064277C501ED
3 changed files with 42 additions and 2 deletions
  1. +3
    -0
      README.md
  2. +27
    -2
      fuglugelf/logger.py
  3. +12
    -0
      tests/test_logger.py

+ 3
- 0
README.md View File

@ -20,6 +20,9 @@ To configure the log level and/or the target host, add
loglevel = DEBUG
gelf-host = 10.11.12.13
gelf-port = 12345
# optional, if you're using postfix recipient_delimiter
# recipient-delimiter =
to your `fuglu.conf` and restart fuglu.

+ 27
- 2
fuglugelf/logger.py View File

@ -57,6 +57,12 @@ class GELFLogger(AppenderPlugin):
def log_source(self):
return self.config.getboolean(self.section, 'log-source')
@property
def recipient_delimiter(self):
if self.config.has_option(self.section, 'recipient-delimiter'):
return self.config.get(self.section, 'recipient-delimiter')
return None
def process(self, suspect, decision):
extra_data = self.build_data(suspect, actioncode_to_string(decision))
self._logger().log(self.log_level, "Suspect %s, data=%s", suspect.id, extra_data)
@ -94,8 +100,12 @@ class GELFLogger(AppenderPlugin):
_add_to_dict(result, value, path + [key])
_add_to_dict(d, {'decision': decision}, [prefix])
_add_to_dict(d, {'subject': self.get_subject(suspect)}, [prefix])
_add_to_dict(d, {
'decision': decision,
'subject': self.get_subject(suspect),
'sender': self.cleaned_address(suspect.from_address),
'recipient': self.cleaned_address(suspect.to_address, self.recipient_delimiter),
}, [prefix])
_add_to_dict(d, suspect, [prefix])
for i, rcvd in enumerate(reversed(self.info_from_rcvd(suspect))):
@ -129,3 +139,18 @@ class GELFLogger(AppenderPlugin):
result.append(x)
return result
def cleaned_address(self, addr, delimiter=None):
s = (addr or "").lower().strip()
if not s:
return s
if delimiter:
localpart = s[:s.index('@')]
domain = s[s.index('@')+1:]
if delimiter in localpart:
localpart = localpart[:localpart.index(delimiter)]
return "%s@%s" % (localpart, domain)
return s

+ 12
- 0
tests/test_logger.py View File

@ -21,3 +21,15 @@ class TestLogger(object):
suspect = Suspect("sender@example.com", "suspect@example.com", f)
assert plugin.get_subject(suspect) == "***SPAM***Webseite enthüllt: So verdient man 512 € am Tag automatisch"
def test_clean_address(self):
plugin = GELFLogger(ConfigParser())
assert plugin.cleaned_address(None) == ""
assert plugin.cleaned_address("") == ""
assert plugin.cleaned_address(" sender@example.com ") == "sender@example.com"
assert plugin.cleaned_address("SENDER@EXAMPLE.COM") == "sender@example.com"
assert plugin.cleaned_address("recipient+test@example.com") == "recipient+test@example.com"
assert plugin.cleaned_address("recipient@example.com", '+') == "recipient@example.com"
assert plugin.cleaned_address("recipient+test@example.com", "+") == "recipient@example.com"