Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr/23' into merge-23
Browse files Browse the repository at this point in the history
  • Loading branch information
dhellmann committed Nov 15, 2017
2 parents 7c5137b + 5e4a4fb commit 15f5973
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
12 changes: 12 additions & 0 deletions doc/source/configuring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ The rules are organized by mailbox, and then listed in order. The
first rule that matches a message triggers the associated action, and
then processing for that message stops.

TimeLimit Rules
----------------

An Time Limit ``time-limit`` rule is added by specifying the 'age',
number of days for the email to "live" in the specified mailbox.
If age = 0, the rule is ignored.

.. code-block:: yaml
- time-limit:
age: 30
Header Rules
------------

Expand Down
24 changes: 23 additions & 1 deletion imapautofiler/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import abc
import logging
import re

from email.utils import parsedate_to_datetime
from datetime import datetime, timedelta
from imapautofiler import i18n
from imapautofiler import lookup

Expand Down Expand Up @@ -254,6 +255,27 @@ def __init__(self, rule_data, cfg):
super().__init__(rule_data, cfg)


class TimeLimit(Rule):
"""True if message is older than the specified 'age' measured
in number of days."""

_log = logging.getLogger('TimeLimit')
NAME = 'time-limit'

def __init__(self, rule_data, cfg, ):
super().__init__(rule_data, cfg)
self._age = rule_data['time-limit']['age']

def check(self, message):
date = parsedate_to_datetime(i18n.get_header_value(message, 'date'))
if self._age:
time_limit = datetime.now() - timedelta(days=self._age)
if date <= time_limit:
return True
else:
return 0


_lookup_table = lookup.make_lookup_table(Rule, 'NAME')


Expand Down
26 changes: 22 additions & 4 deletions imapautofiler/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
import logging
from email.header import Header
from email.message import Message

import fixtures
import testtools
from email.utils import format_datetime
import datetime


def construct_message(headers):
Expand All @@ -29,6 +30,10 @@ def construct_message(headers):
return msg.as_string()


date = format_datetime(datetime.datetime.now())
past_date = format_datetime(
datetime.datetime.now() - datetime.timedelta(days=90)
)
MESSAGE = {
'From': 'Sender Name <sender@example.com>',
'Content-Type':
Expand All @@ -38,7 +43,7 @@ def construct_message(headers):
'Mime-Version': '1.0 (Mac OS X Mail 9.2 \(3112\))',
'X-Smtp-Server': 'AE35BF63-D70A-4AB0-9FAA-3F18EB9802A9',
'Subject': 'Re: reply to previous message',
'Date': 'Sat, 23 Jan 2016 16:19:10 -0500',
'Date': '{}'.format(past_date),
'X-Universally-Unique-Identifier': 'CC844EE1-C406-4ABA-9DA5-685759BBC15A',
'References': '<33509d2c-e2a7-48c0-8bf3-73b4ba352b2f@example.com>',
'To': 'recipient1@example.com',
Expand All @@ -54,9 +59,16 @@ def construct_message(headers):
'Subject': 'Re: ответ на предыдущее сообщение',
})

RECENT_MESSAGE = MESSAGE.copy()
RECENT_MESSAGE.update({
'Date': '{}'.format(date),
})


class TestCase(testtools.TestCase):
_msg = None
_recent_msg = None
_i18n_msg = None

def setUp(self):
super().setUp()
Expand All @@ -74,12 +86,18 @@ def msg(self):
)
return self._msg

_i18n_msg = None

@property
def i18n_msg(self):
if self._i18n_msg is None:
self._i18n_msg = email.parser.Parser().parsestr(
construct_message(I18N_MESSAGE)
)
return self._i18n_msg

@property
def recent_msg(self):
if self._recent_msg is None:
self._recent_msg = email.parser.Parser().parsestr(
construct_message(RECENT_MESSAGE)
)
return self._recent_msg
20 changes: 20 additions & 0 deletions imapautofiler/tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def test_known(self):
'or',
'and',
'recipient',
'time-limit',
'headers',
'header-exists',
'is-mailing-list',
Expand Down Expand Up @@ -475,3 +476,22 @@ def test_create_recursive(self):
},
r._data,
)


class TestTimeLimit(base.TestCase):
"""Test TimeLimit class handling of passed and permitted messages."""
def get_def(self):
rule_def = {
'time-limit': {
'age': 30,
}
}
return rule_def

def test_time_limit_expired(self):
r = rules.TimeLimit(self.get_def(), {})
self.assertTrue(r.check(self.msg))

def test_time_limit_current(self):
r = rules.TimeLimit(self.get_def(), {})
self.assertEqual(r.check(self.recent_msg), 0)

0 comments on commit 15f5973

Please sign in to comment.