Commit bcd61c6c authored by Hiro's avatar Hiro 🏄
Browse files

Update how Gettor use settings and configs

parent 7853c126
......@@ -4,7 +4,7 @@
"email_parser_logfile": "/srv/gettor.torproject.org/home/gettor-hiro/log/email_parser.log",
"email_requests_limit": 5,
"sendmail_interval": 10,
"sendmail_addr": "email@addr",
"sendmail_host": "host",
"sendmail_addr": "gettor+test@torproject.org",
"sendmail_host": "localhost",
"sendmail_port": 587
}
......@@ -27,8 +27,7 @@ from twisted.python import log
from twisted.internet import defer
from twisted.enterprise import adbapi
from .. import PLATFORMS, EMAIL_REQUESTS_LIMIT
from ..db import SQLite3
from ..utils.db import SQLite3
class AddressError(Exception):
......@@ -48,12 +47,13 @@ class DKIMError(Exception):
class EmailParser(object):
"""Class for parsing email requests."""
def __init__(self, to_addr=None, dkim=False):
def __init__(self, settings, to_addr=None, dkim=False):
"""
Constructor.
param (Boolean) dkim: Set dkim verification to True or False.
"""
self.settings = settings
self.dkim = dkim
self.to_addr = to_addr
......@@ -72,6 +72,7 @@ class EmailParser(object):
:return dict with email address and command (`links` or `help`).
"""
platforms = self.settings.get("platforms")
log.msg("Building email message from string.", system="email parser")
msg = message_from_string(msg_str)
......@@ -141,7 +142,7 @@ class EmailParser(object):
if subject:
subject = subject.group(1)
for word in re.split(r"\s+", subject.strip()):
if word.lower() in PLATFORMS:
if word.lower() in platforms:
request["command"] = "links"
request["platform"] = word.lower()
break
......@@ -151,7 +152,7 @@ class EmailParser(object):
if not request["command"]:
for word in re.split(r"\s+", body_str.strip()):
if word.lower() in PLATFORMS:
if word.lower() in platforms:
request["command"] = "links"
request["platform"] = word.lower()
break
......@@ -174,7 +175,7 @@ class EmailParser(object):
:return: deferred whose callback/errback will log database query
execution details.
"""
email_requests_limit = self.settings.get("email_requests_limit")
log.msg(
"Found request for {}.".format(request['command']),
system="email parser"
......@@ -190,7 +191,7 @@ class EmailParser(object):
id=hid.hexdigest(), service=request['service']
)
if num_requests[0][0] > EMAIL_REQUESTS_LIMIT:
if num_requests[0][0] > email_requests_limit:
log.msg(
"Discarded. Too many requests from {}.".format(
hid.hexdigest
......
......@@ -15,14 +15,14 @@ from twisted.internet import defer, reactor
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from gettor.parse.email import EmailParser, AddressError, DKIMError
EMAIL_PARSER_LOGFILE = "/srv/gettor.torproject.org/home/gettor-hiro/log/email_parser.log"
from gettor.utils import options
@defer.inlineCallbacks
def process_email(message):
settings = options.parse_settings()
try:
ep = EmailParser("gettor+test@torproject.org")
ep = EmailParser(settings, "gettor+test@torproject.org")
yield defer.maybeDeferred(
ep.parse, message
).addCallback(ep.parse_callback).addErrback(ep.parse_errback)
......@@ -44,7 +44,9 @@ def main():
if __name__ == '__main__':
log.startLogging(open(EMAIL_PARSER_LOGFILE, 'a'))
settings = options.parse_settings()
email_parser_logfile = settings.get("email_parser_logfile")
log.startLogging(open(email_parser_logfile, 'a'))
log.msg("New email request received.", system="process email")
main()
log.msg("Email request processed.", system="process email")
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