Latest changes

master
Thor 3 years ago
parent 702757f17a
commit 5e1e2af743
  1. 17
      bogofilter.py
  2. 1
      config.json.example
  3. 65
      cringefilter.py

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

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

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

Loading…
Cancel
Save