[tor-commits] [gettor/master] Fix issues introduced with refactoring and clean up code

hiro at torproject.org hiro at torproject.org
Fri May 17 14:27:50 UTC 2019


commit 53383d9b9d841d592458465340af2b0bdc6043db
Author: hiro <hiro at torproject.org>
Date:   Fri Mar 1 16:34:28 2019 +0100

    Fix issues introduced with refactoring and clean up code
---
 gettor/parse/email.py             | 12 +++++++-----
 gettor/services/email/sendmail.py | 28 +++++++++++++---------------
 gettor/utils/settings.py          | 12 +++++++++++-
 scripts/process_email             |  1 +
 share/locale/en.json              |  2 ++
 5 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/gettor/parse/email.py b/gettor/parse/email.py
index 8539743..06a149b 100644
--- a/gettor/parse/email.py
+++ b/gettor/parse/email.py
@@ -101,7 +101,7 @@ class EmailParser(object):
             )
             raise AddressError("Invalid email address {}".format(msg['From']))
 
-        hid = hashlib.sha256(norm_addr)
+        hid = hashlib.sha256(norm_addr.encode('utf-8'))
         log.msg(
             "Request from {}".format(hid.hexdigest()), system="email parser"
         )
@@ -135,7 +135,8 @@ class EmailParser(object):
 
         request = {
             "id": norm_addr,
-            "command": None, "platform": None,
+            "command": None,
+            "platform": None,
             "service": "email"
         }
 
@@ -151,7 +152,7 @@ class EmailParser(object):
                     break
 
         if not request["command"]:
-            for word in re.split(r"\s+", body_str.strip()):
+            for word in re.split(r"\s+", msg_str.strip()):
                 if word.lower() in platforms:
                     request["command"] = "links"
                     request["platform"] = word.lower()
@@ -183,9 +184,10 @@ class EmailParser(object):
 
         if request["command"]:
             now_str = datetime.now().strftime("%Y%m%d%H%M%S")
-            conn = SQLite3()
+            dbname = self.settings.get("dbname")
+            conn = SQLite3(dbname)
 
-            hid = hashlib.sha256(request['id'])
+            hid = hashlib.sha256(request['id'].encode('utf-8'))
             # check limits first
             num_requests = yield conn.get_num_requests(
                 id=hid.hexdigest(), service=request['service']
diff --git a/gettor/services/email/sendmail.py b/gettor/services/email/sendmail.py
index 22ec3e9..8c86bdc 100644
--- a/gettor/services/email/sendmail.py
+++ b/gettor/services/email/sendmail.py
@@ -18,11 +18,14 @@ import hashlib
 import configparser
 from email import encoders
 from email import mime
+from email.mime.text import MIMEText
+
 from twisted.internet import defer
 from twisted.mail.smtp import sendmail
 
 from ...utils.db import SQLite3 as DB
 from ...utils.commons import log
+from ...utils import strings
 
 
 class SMTPError(Exception):
@@ -31,7 +34,7 @@ class SMTPError(Exception):
     """
     pass
 
-
+from email.mime.text import MIMEText
 class Sendmail(object):
     """
     Class for sending email replies to `help` and `links` requests.
@@ -86,22 +89,21 @@ class Sendmail(object):
         message = MIMEText(body)
 
         message['Subject'] = subject
-        message['From'] = SENDMAIL_ADDR
+        message['From'] = self.settings.get("sendmail_addr")
         message['To'] = email_addr
 
         log.debug("Calling asynchronous sendmail.")
 
         return sendmail(
-            SENDMAIL_HOST, SENDMAIL_ADDR, email_addr, message,
-            port=SENDMAIL_PORT,
-            requireAuthentication=True, requireTransportSecurity=True
+            self.settings.get("sendmail_host"), self.settings.get("sendmail_addr"), email_addr, message,
+            requireTransportSecurity=True
         ).addCallback(self.sendmail_callback).addErrback(self.sendmail_errback)
 
 
 
     @defer.inlineCallbacks
     def get_new(self):
-        """
+        """strings.load_strings("en")
         Get new requests to process. This will define the `main loop` of
         the Sendmail service.
         """
@@ -131,7 +133,7 @@ class Sendmail(object):
                     id = request[0]
                     date = request[4]
 
-                    hid = hashlib.sha256(id)
+                    hid = hashlib.sha256(id.encode('utf-8'))
                     log.info(
                         "Sending help message to {}.".format(
                             hid.hexdigest()
@@ -161,11 +163,7 @@ class Sendmail(object):
                 log.info("Got new links request.")
 
                 # for now just english
-                en = gettext.translation(
-                    'email', localedir='locales', languages=['en']
-                )
-                en.install()
-                _ = en.gettext
+                strings.load_strings("en")
 
                 for request in link_requests:
                     id = request[0]
@@ -194,11 +192,11 @@ class Sendmail(object):
                         else:
                             link_msg = link_str
 
-                    body_msg = _("links_body")
+                    body_msg = strings._("links_body")
                     body_msg = body_msg.format(links=link_msg)
-                    subject_msg = _("links_subject")
+                    subject_msg = strings._("links_subject")
 
-                    hid = hashlib.sha256(id)
+                    hid = hashlib.sha256(id.encode('utf-8'))
                     log.info(
                         "Sending links to {}.".format(
                             hid.hexdigest()
diff --git a/gettor/utils/settings.py b/gettor/utils/settings.py
index ebce5c6..5047498 100644
--- a/gettor/utils/settings.py
+++ b/gettor/utils/settings.py
@@ -56,7 +56,17 @@ class Settings(object):
                     self._settings = json.load(f)
             except:
                 pass
-
+        else:
+            self._settings = {
+              "platforms": ["linux", "osx", "windows"],
+              "dbname": "/srv/gettor.torproject.org/home/gettor-hiro/gettor.db",
+              "email_parser_logfile": "/srv/gettor.torproject.org/home/gettor-hiro/log/email_parser.log",
+              "email_requests_limit": 5,
+              "sendmail_interval": 10,
+              "sendmail_addr": "gettor+test at torproject.org",
+              "sendmail_host": "localhost",
+              "sendmail_port": 587
+            }
 
     def get(self, key):
         return self._settings[key]
diff --git a/scripts/process_email b/scripts/process_email
index 7a8236f..c670dfc 100755
--- a/scripts/process_email
+++ b/scripts/process_email
@@ -38,6 +38,7 @@ def process_email(message):
     reactor.stop()
 
 def main():
+    log.msg("Reading new email.", system="process email")
     incoming_email = sys.stdin.read()
     reactor.callWhenRunning(process_email, incoming_email)
     reactor.run()
diff --git a/share/locale/en.json b/share/locale/en.json
index 8269490..0e9d47c 100644
--- a/share/locale/en.json
+++ b/share/locale/en.json
@@ -1,4 +1,6 @@
 {
+  "links_body": "GetTor Test. Please be kind.",
+  "links_subject": "GetTor Email Test",
   "help_debug": "Log application errors to stdout",
   "help_config": "Custom config file location (optional)",
   "smtp_links_subject": "[GetTor] Links for your request",





More information about the tor-commits mailing list