diff options
| author | Cecylia Bocovich <cohosh@torproject.org> | 2020-02-10 16:28:21 -0500 |
|---|---|---|
| committer | Cecylia Bocovich <cohosh@torproject.org> | 2020-03-12 13:13:00 -0400 |
| commit | 0cd6443cdd8964fbfad00010d2cd0220f31e18f0 (patch) | |
| tree | 0e1cd858fc98030f756cdaccedac0b04c4600148 | |
| parent | 0297dbcddbda4085e30c6923d4d744792f59914e (diff) | |
Ensure replies to the help message will work
This commit makes some changes and adds some tests to ensure that
valid replies to the gettor help message will contain links.
Non-valid replies should still return the GetTor help message.
| -rw-r--r-- | gettor/parse/email.py | 29 | ||||
| -rw-r--r-- | tests/test_email_service.py | 41 |
2 files changed, 58 insertions, 12 deletions
diff --git a/gettor/parse/email.py b/gettor/parse/email.py index 38bc120..9409a99 100644 --- a/gettor/parse/email.py +++ b/gettor/parse/email.py @@ -13,6 +13,7 @@ from __future__ import absolute_import import re +import io import dkim import hashlib @@ -117,18 +118,22 @@ class EmailParser(object): def parse_keywords(self, text, request): - for word in re.split(r"\s+", text.strip()): - for locale in self.locales: - if word.lower() == locale.lower(): - request["language"] = locale - elif (not request["language"]) and (word.lower()[:2] == - locale.lower()[:2]): - request["language"] = locale - if word.lower() in self.platforms: - request["command"] = "links" - request["platform"] = word.lower() - if (not request["command"]) and word.lower() == "help": - request["command"] = "help" + buf = io.StringIO(text) + for line in buf: + if len(line.strip()) > 0 and line.strip()[0] == ">": + continue + for word in re.split(r"\s+", line.strip()): + for locale in self.locales: + if word.lower() == locale.lower(): + request["language"] = locale + elif (not request["language"]) and (word.lower()[:2] == + locale.lower()[:2]): + request["language"] = locale + if word.lower() in self.platforms: + request["command"] = "links" + request["platform"] = word.lower() + if (not request["command"]) and word.lower() == "help": + request["command"] = "help" return request def build_request(self, msg_str, norm_addr): diff --git a/tests/test_email_service.py b/tests/test_email_service.py index 45278bc..995ba55 100644 --- a/tests/test_email_service.py +++ b/tests/test_email_service.py @@ -210,6 +210,47 @@ class EmailServiceTests(unittest.TestCase): assert "en-US" in ep.locales del ep + def test_help_reply(self): + #Replying to GetTor Help with a valid links request should get you links + ep = conftests.EmailParser(self.settings, "gettor@torproject.org") + ep.locales = ["en-US", "es-ES", "es-AR", "pt-BR", "fa"] + request = ep.parse("From: \"silvia [hiro]\" <hiro@torproject.org>\n" + "Subject: Re: [GetTor] Help Email\r\n Reply-To: hiro@torproject.org \nTo:" + "gettor@torproject.org\n osx en\n") + self.assertEqual(request["command"], "links") + self.assertEqual(request["language"], "en-US") + self.assertEqual(request["platform"], "osx") + + request = ep.parse("From: \"silvia [hiro]\" <hiro@torproject.org>\n" + "Subject: Re: [GetTor] Help Email\r\n Reply-To: hiro@torproject.org \nTo:" + "gettor@torproject.org\nlinux fa\n\n" + "On 2020-02-10 11:54 a.m., gettor@torproject.org wrote:\n" + "> This is how you can request a tor browser bundle link.\n" + ">\n" + "> Send an email to: gettor@torproject.org\n" + ">\n" + "> In the body of the email only write: <operating system> <language>.\n" + ">\n" + "> We only support windows, osx and linux as operating systems.\n" + ">\n") + self.assertEqual(request["command"], "links") + self.assertEqual(request["language"], "fa") + self.assertEqual(request["platform"], "linux") + + request = ep.parse("From: \"silvia [hiro]\" <hiro@torproject.org>\n" + "Subject: Re: [GetTor] Help Email\r\n Reply-To: hiro@torproject.org \nTo:" + "gettor@torproject.org\n" + "On 2020-02-10 11:54 a.m., gettor@torproject.org wrote:\n" + "> This is how you can request a tor browser bundle link.\n" + ">\n" + "> Send an email to: gettor@torproject.org\n" + ">\n" + "> In the body of the email only write: <operating system> <language>.\n" + ">\n" + "> We only support windows, osx and linux as operating systems.\n" + ">\n") + self.assertEqual(request["command"], "help") + if __name__ == "__main__": unittest.main() |
