WIP
This commit is contained in:
parent
6867e71d7c
commit
8311053d08
4 changed files with 46 additions and 66 deletions
|
@ -13,12 +13,10 @@ from model.comment import Site
|
|||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
client_ips = {}
|
||||
|
||||
|
||||
def cron(func):
|
||||
def wrapper():
|
||||
logger.debug("execute fun " + func)
|
||||
logger.debug("execute CRON " + func.__name__)
|
||||
func()
|
||||
|
||||
return wrapper
|
||||
|
@ -27,16 +25,13 @@ def cron(func):
|
|||
@cron
|
||||
def fetch_mail_answers():
|
||||
|
||||
msg = {}
|
||||
|
||||
if msg["request"] == "new_mail":
|
||||
reply_comment_email(msg["data"])
|
||||
mailer.delete(msg["data"])
|
||||
|
||||
# data = request.get_json()
|
||||
# logger.debug(data)
|
||||
|
||||
# processor.enqueue({'request': 'new_mail', 'data': data})
|
||||
for msg in mailer.fetch():
|
||||
m = re.search(r"\[(\d+)\:(\w+)\]", msg["subject"])
|
||||
if m:
|
||||
full_msg = mailer.get(msg["id"])
|
||||
if full_msg:
|
||||
reply_comment_email(full_msg)
|
||||
mailer.delete(msg["id"])
|
||||
|
||||
|
||||
@cron
|
||||
|
@ -58,8 +53,8 @@ def submit_new_comment():
|
|||
url=comment.url, comment=comment_text
|
||||
)
|
||||
|
||||
site = Site.select().where(Site.id == Comment.site).get()
|
||||
# send email
|
||||
site = Site.select().where(Site.id == Comment.site).get()
|
||||
subject = "STACOSYS %s: [%d:%s]" % (site.name, comment.id, site.token)
|
||||
mailer.send(site.admin_email, subject, email_body)
|
||||
logger.debug("new comment processed ")
|
||||
|
@ -106,17 +101,13 @@ def reply_comment_email(data):
|
|||
|
||||
# put a log to help fail2ban
|
||||
if message[:2].upper() == "SP": # SPAM
|
||||
if comment_id in client_ips:
|
||||
if comment.ip:
|
||||
logger.info(
|
||||
"SPAM comment from %s: %d" % (client_ips[comment_id], comment_id)
|
||||
)
|
||||
else:
|
||||
logger.info("cannot identify SPAM source: %d" % comment_id)
|
||||
|
||||
# forget client IP
|
||||
if comment_id in client_ips:
|
||||
del client_ips[comment_id]
|
||||
|
||||
logger.info("discard comment: %d" % comment_id)
|
||||
comment.delete_instance()
|
||||
email_body = get_template("drop_comment").render(original=message)
|
||||
|
@ -124,6 +115,7 @@ def reply_comment_email(data):
|
|||
else:
|
||||
# update Comment row
|
||||
comment.published = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
comment.ip = None
|
||||
comment.save()
|
||||
logger.info("commit comment: %d" % comment_id)
|
||||
|
||||
|
|
|
@ -1,15 +1,44 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import logging
|
||||
import json
|
||||
import requests
|
||||
from conf import config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def fetch():
|
||||
pass
|
||||
mails = []
|
||||
r = requests.get(config.get(config.MAILER_URL) + "/mbox")
|
||||
if r.status_code == 200:
|
||||
logger.info("MAILER => " + str(r.json()))
|
||||
payload = r.json()
|
||||
if payload["count"] > 0:
|
||||
mails = payload["emails"]
|
||||
return mails
|
||||
|
||||
|
||||
def send(email, subject, body):
|
||||
pass
|
||||
def get(id):
|
||||
payload = None
|
||||
r = requests.get(config.get(config.MAILER_URL) + "/mbox/" + id)
|
||||
if r.status_code == 200:
|
||||
payload = r.json()
|
||||
return payload
|
||||
|
||||
|
||||
def delete(content):
|
||||
# TODO delete mail
|
||||
pass
|
||||
def send(to_email, subject, message):
|
||||
headers = {"Content-Type": "application/json; charset=utf-8"}
|
||||
msg = {"to": to_email, "subject": subject, "content": message}
|
||||
r = requests.post(
|
||||
config.get(config.MAILER_URL) + "/mbox", data=json.dumps(msg), headers=headers
|
||||
)
|
||||
if r.status_code in (200, 201):
|
||||
logger.debug("Email for %s posted" % to_email)
|
||||
else:
|
||||
logger.warn("Cannot post email for %s" % to_email)
|
||||
|
||||
|
||||
def delete(id):
|
||||
requests.delete(config.get(config.MAILER_URL) + "/mbox/" + id)
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import logging
|
||||
import re
|
||||
import PyRSS2Gen
|
||||
import markdown
|
||||
import json
|
||||
from datetime import datetime
|
||||
from threading import Thread
|
||||
from queue import Queue
|
||||
from model.site import Site
|
||||
from model.comment import Comment
|
||||
from helper.hashing import md5
|
||||
from conf import config
|
||||
from core import mailer
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
queue = Queue()
|
||||
proc = None
|
||||
env = None
|
||||
|
||||
# keep client IP in memory until classified
|
||||
client_ips = {}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def get_email_metadata(message):
|
||||
# retrieve metadata reader email from email body sent by admin
|
||||
email = ""
|
||||
m = re.search(r"email:\s(.+@.+\..+)", message)
|
||||
if m:
|
||||
email = m.group(1)
|
||||
return email
|
||||
|
||||
|
|
@ -6,7 +6,6 @@ from flask import request, jsonify, abort
|
|||
from model.site import Site
|
||||
from model.comment import Comment
|
||||
from conf import config
|
||||
from core import processor
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
app = config.flaskapp()
|
||||
|
|
Loading…
Add table
Reference in a new issue