2021-06-17 10:39:47 +08:00
|
|
|
#!/usr/local/bin/python3
|
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
|
|
# YYeTsBot - utils.py
|
|
|
|
|
# 6/16/21 21:42
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
__author__ = "Benny <benny.think@gmail.com>"
|
|
|
|
|
|
2021-12-09 20:07:20 +08:00
|
|
|
import contextlib
|
2021-08-15 12:28:19 +08:00
|
|
|
import os
|
|
|
|
|
import smtplib
|
2021-06-17 10:39:47 +08:00
|
|
|
import time
|
2021-08-15 12:28:19 +08:00
|
|
|
from email.header import Header
|
|
|
|
|
from email.mime.text import MIMEText
|
|
|
|
|
from email.utils import formataddr, parseaddr
|
2021-06-17 10:39:47 +08:00
|
|
|
|
2021-12-09 20:07:20 +08:00
|
|
|
from akismet import Akismet
|
|
|
|
|
|
2021-06-17 10:39:47 +08:00
|
|
|
|
|
|
|
|
def ts_date(ts=None):
|
|
|
|
|
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts))
|
2021-08-15 12:28:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _format_addr(s):
|
|
|
|
|
name, addr = parseaddr(s)
|
|
|
|
|
return formataddr((Header(name, 'utf-8').encode(), addr))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def send_mail(to: str, subject: str, body: str):
|
|
|
|
|
user = os.getenv("email_user")
|
|
|
|
|
password = os.getenv("email_password")
|
|
|
|
|
host = os.getenv("email_host") or "localhost"
|
|
|
|
|
port = os.getenv("email_port") or "1025" # mailhog
|
|
|
|
|
from_addr = os.getenv("from_addr") or "yyets@dmesg.app"
|
|
|
|
|
|
|
|
|
|
msg = MIMEText(body, 'html', 'utf-8')
|
|
|
|
|
msg['From'] = _format_addr('YYeTs <%s>' % from_addr)
|
|
|
|
|
msg['To'] = _format_addr(to)
|
|
|
|
|
msg['Subject'] = Header(subject, 'utf-8').encode()
|
|
|
|
|
|
|
|
|
|
if port == "1025":
|
2021-12-09 20:07:20 +08:00
|
|
|
server = smtplib.SMTP(host, int(port))
|
2021-08-15 12:28:19 +08:00
|
|
|
else:
|
2021-12-09 20:07:20 +08:00
|
|
|
server = smtplib.SMTP_SSL(host, int(port))
|
2021-08-15 12:28:19 +08:00
|
|
|
server.login(user, password)
|
|
|
|
|
server.sendmail(from_addr, [to], msg.as_string())
|
|
|
|
|
server.quit()
|
|
|
|
|
|
|
|
|
|
|
2021-12-09 20:07:20 +08:00
|
|
|
def check_spam(ip, ua, author, content) -> int:
|
|
|
|
|
# 0 means okay
|
|
|
|
|
token = os.getenv("askismet")
|
2022-01-14 20:07:29 +08:00
|
|
|
whitelist: "list" = os.getenv("whitelist", "").split(",")
|
|
|
|
|
if author in whitelist:
|
|
|
|
|
return 0
|
2021-12-09 20:07:20 +08:00
|
|
|
if token:
|
|
|
|
|
with contextlib.suppress(Exception):
|
|
|
|
|
akismet = Akismet(token, blog="https://yyets.dmesg.app/")
|
|
|
|
|
|
|
|
|
|
return akismet.check(ip, ua, comment_author=author, blog_lang="zh_cn",
|
|
|
|
|
comment_type="comment",
|
|
|
|
|
comment_content=content)
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
2021-08-15 12:28:19 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
send_mail("benny.think@gmail.com", "subj", 'aaaa<br>bbb')
|