From 59e020228ad7373f137e8b792577414e352abc0d Mon Sep 17 00:00:00 2001 From: Yax <1949284+kianby@users.noreply.github.com> Date: Thu, 28 Jan 2021 19:00:54 +0100 Subject: [PATCH] fix a couple of bugs --- config.ini | 1 + pyproject.toml | 2 +- run.py | 7 ++++++- stacosys/conf/config.py | 4 ++++ stacosys/core/cron.py | 11 +++++------ stacosys/core/mailer.py | 7 ++++++- stacosys/interface/__init__.py | 5 +++-- stacosys/model/email.py | 4 ++++ 8 files changed, 30 insertions(+), 11 deletions(-) diff --git a/config.ini b/config.ini index f4ba682..33e896a 100755 --- a/config.ini +++ b/config.ini @@ -31,6 +31,7 @@ password = MYPASSWORD [smtp] host = mail.gandi.net starttls = true +ssl = false port = 587 login = blog@mydomain.com password = MYPASSWORD diff --git a/pyproject.toml b/pyproject.toml index fea1773..93c2f93 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "stacosys" -version = "2.0b2" +version = "2.0b3" description = "STAtic COmmenting SYStem" authors = ["Yax"] readme = "README.md" diff --git a/run.py b/run.py index 754dee6..4ebef94 100644 --- a/run.py +++ b/run.py @@ -11,6 +11,8 @@ from stacosys.core import database from stacosys.core.rss import Rss from stacosys.core.mailer import Mailer from stacosys.interface import app +from stacosys.interface import api +from stacosys.interface import form from stacosys.interface import scheduler @@ -43,7 +45,8 @@ def stacosys_server(config_pathname): # initialize config conf = Config.load(config_pathname) - + logger.info(conf.__repr__()) + # check database file exists (prevents from creating a fresh db) db_pathname = conf.get(ConfigParameter.DB_SQLITE_FILE) if not os.path.isfile(db_pathname): @@ -76,6 +79,7 @@ def stacosys_server(config_pathname): conf.get(ConfigParameter.SMTP_HOST), conf.get_int(ConfigParameter.SMTP_PORT), conf.get_bool(ConfigParameter.SMTP_STARTTLS), + conf.get_bool(ConfigParameter.SMTP_SSL), conf.get(ConfigParameter.SMTP_LOGIN), conf.get(ConfigParameter.SMTP_PASSWORD), ) @@ -94,6 +98,7 @@ def stacosys_server(config_pathname): # inject config parameters into flask app.config.update(SITE_TOKEN=conf.get(ConfigParameter.SITE_TOKEN)) + logger.info(f"start interfaces {api} {form}") # start Flask app.run( diff --git a/stacosys/conf/config.py b/stacosys/conf/config.py index a2571bd..8b8c0a2 100644 --- a/stacosys/conf/config.py +++ b/stacosys/conf/config.py @@ -25,6 +25,7 @@ class ConfigParameter(Enum): IMAP_PASSWORD = "imap.password" SMTP_STARTTLS = "smtp.starttls" + SMTP_SSL = "smtp.ssl" SMTP_HOST = "smtp.host" SMTP_PORT = "smtp.port" SMTP_LOGIN = "smtp.login" @@ -62,3 +63,6 @@ class Config: def get_bool(self, key: ConfigParameter): return self._params[key.value].lower() in ("yes", "true") + + def __repr__(self): + return self._params.__repr__() \ No newline at end of file diff --git a/stacosys/core/cron.py b/stacosys/core/cron.py index 3f02c30..ff498cc 100644 --- a/stacosys/core/cron.py +++ b/stacosys/core/cron.py @@ -8,6 +8,8 @@ import re from stacosys.core.templater import Templater, Template from stacosys.model.comment import Comment from stacosys.model.email import Email +from stacosys.core.rss import Rss +from stacosys.core.mailer import Mailer logger = logging.getLogger(__name__) @@ -16,14 +18,14 @@ template_path = os.path.abspath(os.path.join(current_path, "../templates")) templater = Templater(template_path) -def fetch_mail_answers(lang, mailer, rss, site_token): +def fetch_mail_answers(lang, mailer: Mailer, rss: Rss, site_token): for msg in mailer.fetch(): if re.search(r".*STACOSYS.*\[(\d+)\:(\w+)\]", msg.subject, re.DOTALL): if _reply_comment_email(lang, mailer, rss, msg, site_token): mailer.delete(msg.id) -def _reply_comment_email(lang, mailer, rss, email: Email, site_token): +def _reply_comment_email(lang, mailer: Mailer, rss: Rss, email: Email, site_token): m = re.search(r"\[(\d+)\:(\w+)\]", email.subject) if not m: @@ -64,7 +66,7 @@ def _reply_comment_email(lang, mailer, rss, email: Email, site_token): logger.info("commit comment: %d" % comment_id) # rebuild RSS - rss.generate_site(token) + rss.generate() # send approval confirmation email to admin new_email_body = templater.get_template(lang, Template.APPROVE_COMMENT).render( @@ -77,9 +79,7 @@ def _reply_comment_email(lang, mailer, rss, email: Email, site_token): def submit_new_comment(lang, site_name, site_token, site_admin_email, mailer): - for comment in Comment.select().where(Comment.notified.is_null()): - comment_list = ( "author: %s" % comment.author_name, "site: %s" % comment.author_site, @@ -103,4 +103,3 @@ def submit_new_comment(lang, site_name, site_token, site_admin_email, mailer): comment.notify_site_admin() else: logger.warn("rescheduled. send mail failure " + subject) - diff --git a/stacosys/core/mailer.py b/stacosys/core/mailer.py index b2b712e..4a5a406 100644 --- a/stacosys/core/mailer.py +++ b/stacosys/core/mailer.py @@ -21,6 +21,7 @@ class Mailer: smtp_host, smtp_port, smtp_starttls, + smtp_ssl, smtp_login, smtp_password, ): @@ -32,6 +33,7 @@ class Mailer: self._smtp_host = smtp_host self._smtp_port = smtp_port self._smtp_starttls = smtp_starttls + self._smtp_ssl = smtp_ssl self._smtp_login = smtp_login self._smtp_password = smtp_password @@ -65,7 +67,10 @@ class Mailer: success = True try: - s = smtplib.SMTP(self._smtp_host, self._smtp_port) + if self._smtp_ssl: + s = smtplib.SMTP_SSL(self._smtp_host, self._smtp_port) + else: + s = smtplib.SMTP(self._smtp_host, self._smtp_port) if self._smtp_starttls: s.starttls() s.login(self._smtp_login, self._smtp_password) diff --git a/stacosys/interface/__init__.py b/stacosys/interface/__init__.py index b7714c3..1fab892 100644 --- a/stacosys/interface/__init__.py +++ b/stacosys/interface/__init__.py @@ -1,5 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from flask import Flask -app = Flask(__name__) \ No newline at end of file +from flask import Flask + +app = Flask(__name__) diff --git a/stacosys/model/email.py b/stacosys/model/email.py index 4d0b081..e67fecf 100644 --- a/stacosys/model/email.py +++ b/stacosys/model/email.py @@ -1,21 +1,25 @@ #!/usr/bin/python # -*- coding: UTF-8 -*- +from dataclasses import dataclass from datetime import datetime from typing import List +@dataclass class Part: content: str content_type: str +@dataclass class Attachment: filename: str content: str content_type: str +@dataclass class Email: id: int encoding: str