From 43e56ce788b688b9b897145c6e754c7a41613c32 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C4=93teris=20Caune?=
Date: Thu, 23 Jul 2020 12:06:17 +0300
Subject: [PATCH] Add support for multiple, comma-separated keywords (cc: #396)
---
CHANGELOG.md | 1 +
hc/api/management/commands/smtpd.py | 13 ++++++++++--
hc/api/tests/test_smtpd.py | 24 ++++++++++++++++++++++
templates/front/details.html | 5 -----
templates/front/filtering_rules_modal.html | 12 +++++------
5 files changed, 42 insertions(+), 13 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 72679cdc..8b5f2da3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file.
- Spike.sh integration (#402)
- Updated Discord integration to use discord.com instead of discordapp.com
- Add "Failure Keyword" filtering for inbound emails (#396)
+- Add support for multiple, comma-separated keywords (#396)
### Bug Fixes
- Removing Pager Team integration, project appears to be discontinued
diff --git a/hc/api/management/commands/smtpd.py b/hc/api/management/commands/smtpd.py
index 5dfb758b..173c582d 100644
--- a/hc/api/management/commands/smtpd.py
+++ b/hc/api/management/commands/smtpd.py
@@ -12,6 +12,15 @@ RE_UUID = re.compile(
)
+def _match(subject, keywords):
+ for s in keywords.split(","):
+ s = s.strip()
+ if s and s in subject:
+ return True
+
+ return False
+
+
def _process_message(remote_addr, mailfrom, mailto, data):
to_parts = mailto.split("@")
code = to_parts[0]
@@ -33,9 +42,9 @@ def _process_message(remote_addr, mailfrom, mailto, data):
if check.subject or check.subject_fail:
action = "ign"
subject = email.message_from_string(data).get("subject", "")
- if check.subject and check.subject in subject:
+ if check.subject and _match(subject, check.subject):
action = "success"
- elif check.subject_fail and check.subject_fail in subject:
+ elif check.subject_fail and _match(subject, check.subject_fail):
action = "fail"
ua = "Email from %s" % mailfrom
diff --git a/hc/api/tests/test_smtpd.py b/hc/api/tests/test_smtpd.py
index ae8240d2..5d583ab3 100644
--- a/hc/api/tests/test_smtpd.py
+++ b/hc/api/tests/test_smtpd.py
@@ -74,3 +74,27 @@ class SmtpdTestCase(BaseTestCase):
self.assertEqual(ping.scheme, "email")
self.assertEqual(ping.ua, "Email from foo@example.org")
self.assertEqual(ping.kind, "ign")
+
+ def test_it_handles_multiple_subject_keywords(self):
+ self.check.subject = "SUCCESS, OK"
+ self.check.save()
+
+ body = PAYLOAD_TMPL % "[OK] Backup completed"
+ _process_message("1.2.3.4", "foo@example.org", self.email, body.encode("utf8"))
+
+ ping = Ping.objects.latest("id")
+ self.assertEqual(ping.scheme, "email")
+ self.assertEqual(ping.ua, "Email from foo@example.org")
+ self.assertEqual(ping.kind, None)
+
+ def test_it_handles_multiple_subject_fail_keywords(self):
+ self.check.subject_fail = "FAIL, WARNING"
+ self.check.save()
+
+ body = PAYLOAD_TMPL % "[WARNING] Backup did not complete"
+ _process_message("1.2.3.4", "foo@example.org", self.email, body.encode("utf8"))
+
+ ping = Ping.objects.latest("id")
+ self.assertEqual(ping.scheme, "email")
+ self.assertEqual(ping.ua, "Email from foo@example.org")
+ self.assertEqual(ping.kind, "fail")
diff --git a/templates/front/details.html b/templates/front/details.html
index bc4d9c58..093dd09a 100644
--- a/templates/front/details.html
+++ b/templates/front/details.html
@@ -73,12 +73,7 @@
{{ check.url }}
- {% if check.subject %}
- Or by sending emails with "{{ check.subject }}"
- in the subject line to this address:
- {% else %}
Or by sending emails to this address:
- {% endif %}
{{ check.email }}
diff --git a/templates/front/filtering_rules_modal.html b/templates/front/filtering_rules_modal.html
index 6b51b5c9..cb6a9e6a 100644
--- a/templates/front/filtering_rules_modal.html
+++ b/templates/front/filtering_rules_modal.html
@@ -61,7 +61,7 @@