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__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
client_ips = {}
|
|
||||||
|
|
||||||
|
|
||||||
def cron(func):
|
def cron(func):
|
||||||
def wrapper():
|
def wrapper():
|
||||||
logger.debug("execute fun " + func)
|
logger.debug("execute CRON " + func.__name__)
|
||||||
func()
|
func()
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
@ -27,16 +25,13 @@ def cron(func):
|
||||||
@cron
|
@cron
|
||||||
def fetch_mail_answers():
|
def fetch_mail_answers():
|
||||||
|
|
||||||
msg = {}
|
for msg in mailer.fetch():
|
||||||
|
m = re.search(r"\[(\d+)\:(\w+)\]", msg["subject"])
|
||||||
if msg["request"] == "new_mail":
|
if m:
|
||||||
reply_comment_email(msg["data"])
|
full_msg = mailer.get(msg["id"])
|
||||||
mailer.delete(msg["data"])
|
if full_msg:
|
||||||
|
reply_comment_email(full_msg)
|
||||||
# data = request.get_json()
|
mailer.delete(msg["id"])
|
||||||
# logger.debug(data)
|
|
||||||
|
|
||||||
# processor.enqueue({'request': 'new_mail', 'data': data})
|
|
||||||
|
|
||||||
|
|
||||||
@cron
|
@cron
|
||||||
|
@ -58,8 +53,8 @@ def submit_new_comment():
|
||||||
url=comment.url, comment=comment_text
|
url=comment.url, comment=comment_text
|
||||||
)
|
)
|
||||||
|
|
||||||
site = Site.select().where(Site.id == Comment.site).get()
|
|
||||||
# send email
|
# send email
|
||||||
|
site = Site.select().where(Site.id == Comment.site).get()
|
||||||
subject = "STACOSYS %s: [%d:%s]" % (site.name, comment.id, site.token)
|
subject = "STACOSYS %s: [%d:%s]" % (site.name, comment.id, site.token)
|
||||||
mailer.send(site.admin_email, subject, email_body)
|
mailer.send(site.admin_email, subject, email_body)
|
||||||
logger.debug("new comment processed ")
|
logger.debug("new comment processed ")
|
||||||
|
@ -106,17 +101,13 @@ def reply_comment_email(data):
|
||||||
|
|
||||||
# put a log to help fail2ban
|
# put a log to help fail2ban
|
||||||
if message[:2].upper() == "SP": # SPAM
|
if message[:2].upper() == "SP": # SPAM
|
||||||
if comment_id in client_ips:
|
if comment.ip:
|
||||||
logger.info(
|
logger.info(
|
||||||
"SPAM comment from %s: %d" % (client_ips[comment_id], comment_id)
|
"SPAM comment from %s: %d" % (client_ips[comment_id], comment_id)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
logger.info("cannot identify SPAM source: %d" % comment_id)
|
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)
|
logger.info("discard comment: %d" % comment_id)
|
||||||
comment.delete_instance()
|
comment.delete_instance()
|
||||||
email_body = get_template("drop_comment").render(original=message)
|
email_body = get_template("drop_comment").render(original=message)
|
||||||
|
@ -124,6 +115,7 @@ def reply_comment_email(data):
|
||||||
else:
|
else:
|
||||||
# update Comment row
|
# update Comment row
|
||||||
comment.published = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
comment.published = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
comment.ip = None
|
||||||
comment.save()
|
comment.save()
|
||||||
logger.info("commit comment: %d" % comment_id)
|
logger.info("commit comment: %d" % comment_id)
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,44 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import json
|
||||||
|
import requests
|
||||||
|
from conf import config
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def fetch():
|
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):
|
def get(id):
|
||||||
pass
|
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):
|
def send(to_email, subject, message):
|
||||||
# TODO delete mail
|
headers = {"Content-Type": "application/json; charset=utf-8"}
|
||||||
pass
|
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.site import Site
|
||||||
from model.comment import Comment
|
from model.comment import Comment
|
||||||
from conf import config
|
from conf import config
|
||||||
from core import processor
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
app = config.flaskapp()
|
app = config.flaskapp()
|
||||||
|
|
Loading…
Add table
Reference in a new issue