Browse Source

Add support for multiple, comma-separated keywords (cc: #396)

pull/409/head
Pēteris Caune 4 years ago
parent
commit
43e56ce788
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
5 changed files with 42 additions and 13 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +11
    -2
      hc/api/management/commands/smtpd.py
  3. +24
    -0
      hc/api/tests/test_smtpd.py
  4. +0
    -5
      templates/front/details.html
  5. +6
    -6
      templates/front/filtering_rules_modal.html

+ 1
- 0
CHANGELOG.md View File

@ -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


+ 11
- 2
hc/api/management/commands/smtpd.py View File

@ -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


+ 24
- 0
hc/api/tests/test_smtpd.py View File

@ -74,3 +74,27 @@ class SmtpdTestCase(BaseTestCase):
self.assertEqual(ping.scheme, "email")
self.assertEqual(ping.ua, "Email from [email protected]")
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", "[email protected]", self.email, body.encode("utf8"))
ping = Ping.objects.latest("id")
self.assertEqual(ping.scheme, "email")
self.assertEqual(ping.ua, "Email from [email protected]")
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", "[email protected]", self.email, body.encode("utf8"))
ping = Ping.objects.latest("id")
self.assertEqual(ping.scheme, "email")
self.assertEqual(ping.ua, "Email from [email protected]")
self.assertEqual(ping.kind, "fail")

+ 0
- 5
templates/front/details.html View File

@ -73,12 +73,7 @@
</p>
<code>{{ check.url }}</code>
<p>
{% 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 %}
</p>
<code>{{ check.email }}</code>


+ 6
- 6
templates/front/filtering_rules_modal.html View File

@ -61,7 +61,7 @@
<div class="form-group">
<label for="update-name-input" class="col-sm-4 control-label">
Success Keyword
Success Keywords
</label>
<div class="col-sm-7">
<input
@ -71,14 +71,14 @@
{% if not check.subject and not check.subject_fail %}disabled{% endif %}
class="input-name form-control filter-by-subject" />
<span class="help-block">
If Subject contains this keyword,
treat it as "success".
Comma-separated list of keywords. If Subject contains
any of the keywords, treat it as "success".
</span>
</div>
</div>
<div class="form-group">
<label for="update-name-input" class="col-sm-4 control-label">
Failure Keyword
Failure Keywords
</label>
<div class="col-sm-7">
<input
@ -88,8 +88,8 @@
{% if not check.subject and not check.subject_fail %}disabled{% endif %}
class="input-name form-control filter-by-subject" />
<span class="help-block">
If Subject contains this keyword,
treat it as "failure".
Comma-separated list of keywords. If Subject contains
any of the keywords, treat it as "failure".
</span>
</div>
</div>


Loading…
Cancel
Save