Browse Source

Decode subject before passing it to the logger

tags/1.0.9
Johann Schmitz 3 years ago
parent
commit
0131a75c6a
Signed by: ercpe <johann@j-schmitz.net> GPG Key ID: A084064277C501ED
5 changed files with 43 additions and 3 deletions
  1. +10
    -2
      fuglugelf/logger.py
  2. +1
    -1
      setup.py
  3. +7
    -0
      tests/01-subject.eml
  4. +2
    -0
      tests/__init__.py
  5. +23
    -0
      tests/test_logger.py

+ 10
- 2
fuglugelf/logger.py View File

@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-

import logging
from email.header import decode_header

import graypy

from fuglu.shared import AppenderPlugin, actioncode_to_string, Suspect, yesno


@@ -95,7 +95,7 @@ class GELFLogger(AppenderPlugin):
_add_to_dict(result, value, path + [key])
_add_to_dict(d, {'decision': decision}, [prefix])
_add_to_dict(d, {'subject': suspect.get_message_rep()['Subject'] or ""}, [prefix])
_add_to_dict(d, {'subject': self.get_subject(suspect)}, [prefix])
_add_to_dict(d, suspect, [prefix])

for i, rcvd in enumerate(reversed(self.info_from_rcvd(suspect))):
@@ -108,6 +108,14 @@ class GELFLogger(AppenderPlugin):
return d

def get_subject(self, suspect):
msg = suspect.get_message_rep()
raw_subject = msg['Subject'] or ""
try:
return ''.join([s for s, _ in decode_header(raw_subject)])
except:
return raw_subject

def info_from_rcvd(self, suspect):
result = []



+ 1
- 1
setup.py View File

@@ -13,7 +13,7 @@ setup(
author_email='johann@j-schmitz.net',
url='https://code.not-your-server.de/fuglu-gelf.git',
download_url='https://code.not-your-server.de/fuglu-gelf.git/tags/',
packages=find_packages(),
packages=find_packages(exclude=('tests', )),
include_package_data=True,
zip_safe=False,
license='GPL-3',


+ 7
- 0
tests/01-subject.eml View File

@@ -0,0 +1,7 @@
Date: Fri, 6 Jan 2017 13:43:26 +0100
To: recipient@example.com
From: sender@example.com
Subject: ***SPAM*** =?UTF-8?Q?Webseite_enth=C3=BCllt:_So_verdient_man_512_=E2=82=AC_am_Tag_au?=
=?UTF-8?Q?tomatisch?=

test

+ 2
- 0
tests/__init__.py View File

@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-


+ 23
- 0
tests/test_logger.py View File

@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
import os
import sys

if sys.version_info < (3, 0, 0):
from ConfigParser import ConfigParser
else:
from configparser import ConfigParser

import os
from fuglu.shared import Suspect

from fuglugelf.logger import GELFLogger


class TestLogger(object):
def test_get_subject(self):
plugin = GELFLogger(ConfigParser())
f = os.path.join(os.path.dirname(__file__), '01-subject.eml')
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"

Loading…
Cancel
Save