Merge branch 'master' of git.thj.no:thor/cringefilter

master
Thor 3 years ago
commit d2ec16321f
  1. 17
      bogofilter.py
  2. 1
      config.json.example
  3. 65
      cringefilter.py

@ -4,7 +4,7 @@ from datetime import datetime
import quopri
BOGOFILTER_DB_DIR = "."
BOGOFILTER_COMMAND = ["bogofilter", "-T", "-d", BOGOFILTER_DB_DIR]
BOGOFILTER_COMMAND = ["bogofilter", "-T", "-c", "/dev/null", "-d", BOGOFILTER_DB_DIR, "-o", "0.53,0.05"]
# Categories
SPAM = "S"
@ -13,14 +13,15 @@ UNSURE = "U"
categories = [SPAM, HAM, UNSURE]
# Actions
CLASSIFY = []
REGISTER = ["-u"]
LEARN_SPAM = ["-s"]
UNLEARN_SPAM = ["-S"]
LEARN_HAM = ["-n"]
UNLEARN_HAM = ["-N"]
CLASSIFY = 0
REGISTER = 1
LEARN_SPAM = 2
UNLEARN_SPAM = 3
LEARN_HAM = 4
UNLEARN_HAM = 5
ACTIONS = {
CLASSIFY: [],
REGISTER: ["-u"],
LEARN_SPAM: ["-s"],
UNLEARN_SPAM: ["-S"],
@ -73,7 +74,7 @@ def run(text, actions = [CLASSIFY], category = UNSURE):
arr = cp.stdout.strip().split(" ")
if len(arr) == 2:
(category, score) = arr
return BogofilterResult(category, score)
return BogofilterResult(category, float(score))
else:
if cp.stderr:
print("Bogofilter:")

@ -1,4 +1,5 @@
{
"tag": "#cringefilter",
"max_age": 90,
"instances": [
"mastodon.social"

@ -103,6 +103,10 @@ class Instance:
self.tracker_report()
time.sleep(60)
def respond(self, status, message):
log_print(self.name, "Responded with: {}".format(message))
self.api.status_reply(status, "{}\n{}".format(message, config["tag"]), visibility = "direct", untag = True)
def tracker(self):
my_id = self.api.me()["id"]
@ -129,7 +133,7 @@ class Instance:
md_text = h2t.handle(status["content"])
if "#cringefilter" in md_text:
if config["tag"] in md_text:
continue
mail_text = toot_dict_to_mail(status).format()
@ -156,71 +160,66 @@ class Instance:
self.save_state()
else:
<<<<<<< HEAD
replied_id = status.get("in_reply_to_id", None)
log_pprint(self.name, status)
if replied_id:
try:
replied_status = self.api.status(replied_id)
replied_md = h2t.handle(replied_status["content"])
if "#cringefilter" in replied_md:
target_status_id = status.get("in_reply_to_id", None)
if config["tag"] in replied_md:
target_status_id = replied_status.get("in_reply_to_id", None)
if target_status_id:
try:
target_status = self.api.status(target_status_id)
target_timeslot_key = encode_time(status["created_at"])
target_mail_text = toot_dict_to_mail(target_status).format()
command = h2t.handle(status["content"])
if "learn spam" in command:
command = h2t.handle(status["content"]).strip()
log_print("Received command: {}".format(command))
if command == "learn spam":
bogofilter.run(target_mail_text, [bogofilter.LEARN_SPAM])
self.track_status(target_status)
self.api.status_reply(status, "@{} Learned as spam\n#cringefilter".format(self.api.me()["username"]), visibility = "direct", untag = True)
elif "unlearn spam" in command:
self.respond(status, "Learned as spam")
elif command == "unlearn spam":
bogofilter.run(target_mail_text, [bogofilter.UNLEARN_SPAM])
self.expire_status(target_timeslot_key, target_status_id)
self.api.status_reply(status, "@{} Unlearned as spam\n#cringefilter".format(self.api.me()["username"]), visibility = "direct", untag = True)
elif "relearn spam" in command:
bogofilter.run(target_mail_text, [bogofilter.UNLEARN_HAM, bogofilter.LEARM_SPAM])
self.respond(status, "Unlearned as spam")
elif command == "relearn spam":
bogofilter.run(target_mail_text, [bogofilter.UNLEARN_HAM, bogofilter.LEARN_SPAM])
self.track_status(target_status)
self.api.status_reply(status, "@{} Relearned as spam\n#cringefilter".format(self.api.me()["username"]), visibility = "direct", untag = True)
if "learn ham" in command:
self.respond(status, "Relearned as spam")
elif command == "learn ham":
bogofilter.run(target_mail_text, [bogofilter.LEARN_HAM])
self.expire_status(target_timeslot_key, target_status_id)
self.api.status_reply(status, "@{} Learned as ham\n#cringefilter".format(self.api.me()["username"]), visibility = "direct", untag = True)
elif "unlearn ham" in command:
self.respond(status, "Learned as ham")
elif command == "unlearn ham":
bogofilter.run(target_mail_text, [bogofilter.UNLEARN_HAM])
self.api.status_reply(status, "@{} Unlearned as ham\n#cringefilter".format(self.api.me()["username"]), visibility = "direct", untag = True)
elif "relearn ham" in command:
bogofilter.run(target_mail_text, [bogofilter.UNLEARN_SPAM, bogofilter.LEARM_HAM])
self.respond(status, "Unlearned as ham")
elif command == "relearn ham":
bogofilter.run(target_mail_text, [bogofilter.UNLEARN_SPAM, bogofilter.LEARN_HAM])
self.expire_status(target_timeslot_key, target_status_id)
self.api.status_reply(status, "@{} Relearned as ham\n#cringefilter".format(self.api.me()["username"]), visibility = "direct", untag = True)
self.respond(status, "Relearned as ham")
else:
self.api.status_reply(status, "@{} Unknown command\n#cringefilter".format(self.api.me()["username"]), visibility = "direct", untag = True)
continue
self.respond(status, "Unknown command")
except MastodonNotFoundError:
self.api.status_reply(status, "@{} Original status is missing\n#cringefilter".format(self.api.me()["username"]), visibility = "direct", untag = True)
continue
self.respond(status, "Original status is missing")
else:
self.api.status_reply(status, "@{} Original status not referenced\n#cringefilter".format(self.api.me()["username"]), visibility = "direct", untag = True)
continue
self.respond(status, "Original status not referenced")
continue
except MastodonNotFoundError:
log_print(self.name,
"Cannot find replied-to status {} on server".format(status_id))
pass
result = bogofilter.run(mail_text, [bogofilter.CLASSIFY, bogofilter.REGISTER])
bogo_report = "Bogofilter: Category={}, Score={}".format(result.category, result.score)
bogo_report = "Bogofilter: Category={}, Score={}".format(result.category, "{:.4f}".format(result.score))
if result.category == bogofilter.SPAM:
log_print(self.name, "SPAM: Tracking status with ID {} as spam".format(status["id"]))
self.api.status_reply(status, "@{me} Tracked as spam\n{bogo}\n#cringefilter".format(me = self.api.me()["username"], bogo = bogo_report), visibility = "direct", untag = True)
self.respond(status, "Tracked as spam\n{}".format(bogo_report))
time.sleep(1)
else:
log_print(self.name, "HAM: Not tracking status with ID {} as spam".format(status["id"]))
self.api.status_reply(status, "@{me} Tracked as ham\n{bogo}\n#cringefilter".format(me = self.api.me()["username"], bogo = bogo_report), visibility = "direct", untag = True)
self.respond(status, "Tracked as ham\n{}".format(bogo_report))
time.sleep(1)
log_print(self.name, bogo_report)
print()
print(mail_text)

Loading…
Cancel
Save