fix a couple of bugs

This commit is contained in:
Yax 2021-01-28 19:00:54 +01:00
parent 7070e2a273
commit 3a19db0b2d
8 changed files with 30 additions and 11 deletions

View file

@ -31,6 +31,7 @@ password = MYPASSWORD
[smtp] [smtp]
host = mail.gandi.net host = mail.gandi.net
starttls = true starttls = true
ssl = false
port = 587 port = 587
login = blog@mydomain.com login = blog@mydomain.com
password = MYPASSWORD password = MYPASSWORD

View file

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "stacosys" name = "stacosys"
version = "2.0b2" version = "2.0b3"
description = "STAtic COmmenting SYStem" description = "STAtic COmmenting SYStem"
authors = ["Yax"] authors = ["Yax"]
readme = "README.md" readme = "README.md"

5
run.py
View file

@ -11,6 +11,8 @@ from stacosys.core import database
from stacosys.core.rss import Rss from stacosys.core.rss import Rss
from stacosys.core.mailer import Mailer from stacosys.core.mailer import Mailer
from stacosys.interface import app from stacosys.interface import app
from stacosys.interface import api
from stacosys.interface import form
from stacosys.interface import scheduler from stacosys.interface import scheduler
@ -43,6 +45,7 @@ def stacosys_server(config_pathname):
# initialize config # initialize config
conf = Config.load(config_pathname) conf = Config.load(config_pathname)
logger.info(conf.__repr__())
# check database file exists (prevents from creating a fresh db) # check database file exists (prevents from creating a fresh db)
db_pathname = conf.get(ConfigParameter.DB_SQLITE_FILE) db_pathname = conf.get(ConfigParameter.DB_SQLITE_FILE)
@ -76,6 +79,7 @@ def stacosys_server(config_pathname):
conf.get(ConfigParameter.SMTP_HOST), conf.get(ConfigParameter.SMTP_HOST),
conf.get_int(ConfigParameter.SMTP_PORT), conf.get_int(ConfigParameter.SMTP_PORT),
conf.get_bool(ConfigParameter.SMTP_STARTTLS), conf.get_bool(ConfigParameter.SMTP_STARTTLS),
conf.get_bool(ConfigParameter.SMTP_SSL),
conf.get(ConfigParameter.SMTP_LOGIN), conf.get(ConfigParameter.SMTP_LOGIN),
conf.get(ConfigParameter.SMTP_PASSWORD), conf.get(ConfigParameter.SMTP_PASSWORD),
) )
@ -94,6 +98,7 @@ def stacosys_server(config_pathname):
# inject config parameters into flask # inject config parameters into flask
app.config.update(SITE_TOKEN=conf.get(ConfigParameter.SITE_TOKEN)) app.config.update(SITE_TOKEN=conf.get(ConfigParameter.SITE_TOKEN))
logger.info(f"start interfaces {api} {form}")
# start Flask # start Flask
app.run( app.run(

View file

@ -25,6 +25,7 @@ class ConfigParameter(Enum):
IMAP_PASSWORD = "imap.password" IMAP_PASSWORD = "imap.password"
SMTP_STARTTLS = "smtp.starttls" SMTP_STARTTLS = "smtp.starttls"
SMTP_SSL = "smtp.ssl"
SMTP_HOST = "smtp.host" SMTP_HOST = "smtp.host"
SMTP_PORT = "smtp.port" SMTP_PORT = "smtp.port"
SMTP_LOGIN = "smtp.login" SMTP_LOGIN = "smtp.login"
@ -62,3 +63,6 @@ class Config:
def get_bool(self, key: ConfigParameter): def get_bool(self, key: ConfigParameter):
return self._params[key.value].lower() in ("yes", "true") return self._params[key.value].lower() in ("yes", "true")
def __repr__(self):
return self._params.__repr__()

View file

@ -8,6 +8,8 @@ import re
from stacosys.core.templater import Templater, Template from stacosys.core.templater import Templater, Template
from stacosys.model.comment import Comment from stacosys.model.comment import Comment
from stacosys.model.email import Email from stacosys.model.email import Email
from stacosys.core.rss import Rss
from stacosys.core.mailer import Mailer
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -16,14 +18,14 @@ template_path = os.path.abspath(os.path.join(current_path, "../templates"))
templater = Templater(template_path) 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(): for msg in mailer.fetch():
if re.search(r".*STACOSYS.*\[(\d+)\:(\w+)\]", msg.subject, re.DOTALL): if re.search(r".*STACOSYS.*\[(\d+)\:(\w+)\]", msg.subject, re.DOTALL):
if _reply_comment_email(lang, mailer, rss, msg, site_token): if _reply_comment_email(lang, mailer, rss, msg, site_token):
mailer.delete(msg.id) 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) m = re.search(r"\[(\d+)\:(\w+)\]", email.subject)
if not m: 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) logger.info("commit comment: %d" % comment_id)
# rebuild RSS # rebuild RSS
rss.generate_site(token) rss.generate()
# send approval confirmation email to admin # send approval confirmation email to admin
new_email_body = templater.get_template(lang, Template.APPROVE_COMMENT).render( 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): def submit_new_comment(lang, site_name, site_token, site_admin_email, mailer):
for comment in Comment.select().where(Comment.notified.is_null()): for comment in Comment.select().where(Comment.notified.is_null()):
comment_list = ( comment_list = (
"author: %s" % comment.author_name, "author: %s" % comment.author_name,
"site: %s" % comment.author_site, "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() comment.notify_site_admin()
else: else:
logger.warn("rescheduled. send mail failure " + subject) logger.warn("rescheduled. send mail failure " + subject)

View file

@ -21,6 +21,7 @@ class Mailer:
smtp_host, smtp_host,
smtp_port, smtp_port,
smtp_starttls, smtp_starttls,
smtp_ssl,
smtp_login, smtp_login,
smtp_password, smtp_password,
): ):
@ -32,6 +33,7 @@ class Mailer:
self._smtp_host = smtp_host self._smtp_host = smtp_host
self._smtp_port = smtp_port self._smtp_port = smtp_port
self._smtp_starttls = smtp_starttls self._smtp_starttls = smtp_starttls
self._smtp_ssl = smtp_ssl
self._smtp_login = smtp_login self._smtp_login = smtp_login
self._smtp_password = smtp_password self._smtp_password = smtp_password
@ -65,7 +67,10 @@ class Mailer:
success = True success = True
try: 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: if self._smtp_starttls:
s.starttls() s.starttls()
s.login(self._smtp_login, self._smtp_password) s.login(self._smtp_login, self._smtp_password)

View file

@ -2,4 +2,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from flask import Flask from flask import Flask
app = Flask(__name__) app = Flask(__name__)

View file

@ -1,21 +1,25 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
from dataclasses import dataclass
from datetime import datetime from datetime import datetime
from typing import List from typing import List
@dataclass
class Part: class Part:
content: str content: str
content_type: str content_type: str
@dataclass
class Attachment: class Attachment:
filename: str filename: str
content: str content: str
content_type: str content_type: str
@dataclass
class Email: class Email:
id: int id: int
encoding: str encoding: str