Commit 53383d9b authored by Hiro's avatar Hiro 🏄
Browse files

Fix issues introduced with refactoring and clean up code

parent bcd61c6c
......@@ -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']
......
......@@ -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()
......
......@@ -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@torproject.org",
"sendmail_host": "localhost",
"sendmail_port": 587
}
def get(self, key):
return self._settings[key]
......@@ -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()
......
{
"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",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment