A bot that tracks and auto-deletes statuses on Mastodon/Pleroma accounts after a set time if they are cringe enough
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.0 KiB

3 years ago
import subprocess
from email.utils import format_datetime
from datetime import datetime
import quopri
BOGOFILTER_DB_DIR = "."
BOGOFILTER_COMMAND = ["bogofilter", "-T", "-d", BOGOFILTER_DB_DIR]
3 years ago
# Categories
3 years ago
SPAM = "S"
HAM = "H"
UNSURE = "U"
3 years ago
categories = [SPAM, HAM, UNSURE]
# Actions
CLASSIFY = []
REGISTER = ["-u"]
LEARN_SPAM = ["-s"]
UNLEARN_SPAM = ["-S"]
LEARN_HAM = ["-n"]
UNLEARN_HAM = ["-N"]
ACTIONS = {
REGISTER: ["-u"],
LEARN_SPAM: ["-s"],
UNLEARN_SPAM: ["-S"],
LEARN_HAM: ["-n"],
UNLEARN_HAM: ["-N"],
SPAM: ["-s"],
HAM: ["-n"],
UNSURE: []
}
3 years ago
class BogofilterResult:
def __init__(self, category, score):
self.category = category
self.score = score
class Mail:
def __init__(self, headers = {}, body = None):
self.headers = {**{
3 years ago
"Date": datetime.now(),
"Content-Type": "text/html; charset=\"UTF-8\""}, **headers}
3 years ago
self.body = body
def format(self):
text = str()
for key, value in self.headers.items():
if key == "Subject":
value = "=?utf-8?Q?{}?=".format(quopri.encodestring(bytes(value, "utf-8"), header = True).decode("utf-8"))
if key == "Date":
value = format_datetime(value)
text += "{key}: {value}\n".format(key = key, value = value)
text += "\n"
if self.body:
text += self.body
text += "\n"
return text
3 years ago
def run(text, actions = [CLASSIFY], category = UNSURE):
args = []
for action in actions:
args.extend(ACTIONS[action])
3 years ago
cp = subprocess.run(BOGOFILTER_COMMAND + args, capture_output = True, encoding = "utf-8", input = text)
arr = cp.stdout.strip().split(" ")
if len(arr) == 2:
(category, score) = arr
return BogofilterResult(category, score)
else:
if cp.stderr:
print("Bogofilter:")
print(cp.stderr.strip())
return None