[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [gettor/master] Make locale parsing less eager



commit 829fa9fbaabd9c196701085c66621efac9d07b8f
Author: Cecylia Bocovich <cohosh@xxxxxxxxxxxxxx>
Date:   Fri Feb 14 16:36:29 2020 -0500

    Make locale parsing less eager
    
    This will make the locale parsing more restrictive so that, for example,
    sending "i like fries" will not result in the locale "fr".
---
 gettor/parse/email.py       | 16 +++++++++-------
 tests/test_email_service.py |  7 ++++++-
 2 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/gettor/parse/email.py b/gettor/parse/email.py
index 9409a99..5a3e45e 100644
--- a/gettor/parse/email.py
+++ b/gettor/parse/email.py
@@ -123,16 +123,18 @@ class EmailParser(object):
             if len(line.strip()) > 0 and line.strip()[0] == ">":
                 continue
             for word in re.split(r"\s+", line.strip()):
+                word = word.lower()
                 for locale in self.locales:
-                    if word.lower() == locale.lower():
+                    if word == 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:
+                    elif not request["language"]:
+                        parts = re.split(r"[-_]", word)
+                        if parts[0] == locale.lower()[:2]:
+                            request["language"] = locale
+                if word in self.platforms:
                     request["command"] = "links"
-                    request["platform"] = word.lower()
-                if (not request["command"])  and word.lower() == "help":
+                    request["platform"] = word
+                if (not request["command"]) and word == "help":
                     request["command"] = "help"
         return request
 
diff --git a/tests/test_email_service.py b/tests/test_email_service.py
index 71c3d4f..4326b9c 100644
--- a/tests/test_email_service.py
+++ b/tests/test_email_service.py
@@ -213,7 +213,7 @@ class EmailServiceTests(unittest.TestCase):
     def test_help_reply(self):
         #Replying to GetTor Help with a valid links request should get you links
         ep = conftests.EmailParser(self.settings, "gettor@xxxxxxxxxxxxxx")
-        ep.locales = ["en-US", "es-ES", "es-AR", "pt-BR", "fa"]
+        ep.locales = ["en-US", "es-ES", "es-AR", "pt-BR", "fa", "fr"]
         request = ep.parse("From: \"silvia [hiro]\" <hiro@xxxxxxxxxxxxxx>\n"
                 "Subject: Re: [GetTor] Help Email\r\n Reply-To: hiro@xxxxxxxxxxxxxx \nTo:"
                 "gettor@xxxxxxxxxxxxxx\n osx en\n")
@@ -223,6 +223,11 @@ class EmailServiceTests(unittest.TestCase):
 
         request = ep.parse("From: \"silvia [hiro]\" <hiro@xxxxxxxxxxxxxx>\n"
                 "Subject: Re: [GetTor] Help Email\r\n Reply-To: hiro@xxxxxxxxxxxxxx \nTo:"
+                "gettor@xxxxxxxxxxxxxx\n i like french fries\n")
+        self.assertEqual(request["command"], "help")
+
+        request = ep.parse("From: \"silvia [hiro]\" <hiro@xxxxxxxxxxxxxx>\n"
+                "Subject: Re: [GetTor] Help Email\r\n Reply-To: hiro@xxxxxxxxxxxxxx \nTo:"
                 "gettor@xxxxxxxxxxxxxx\nlinux fa\n\n"
                 "On 2020-02-10 11:54 a.m., gettor@xxxxxxxxxxxxxx wrote:\n"
                 "> This is how you can request a tor browser bundle link.\n"

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits