From 5f8c93ea92fcaecbdc5c0d8973dfc753a406c584 Mon Sep 17 00:00:00 2001 From: jesopo Date: Sun, 2 Jun 2019 15:19:05 +0100 Subject: [PATCH] use last-seen msgid to prevent duplicate messages in `chathistory` BATCH --- modules/chathistory.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 modules/chathistory.py diff --git a/modules/chathistory.py b/modules/chathistory.py new file mode 100644 index 00000000..1f1711b1 --- /dev/null +++ b/modules/chathistory.py @@ -0,0 +1,29 @@ +#--depends-on msgid + +from src import ModuleManager, utils + +TAG = utils.irc.MessageTag("msgid", "draft/msgid") + +class Module(ModuleManager.BaseModule): + @utils.hook("received.batch.end") + def batch_end(self, event): + if event["batch"].type == "chathistory": + target_name = event["batch"].args[0] + if target_name in event["server"].channels: + target = event["server"].channels.get(target_name) + else: + target = event["server"].get_user(target_name) + + last_msgid = target.get_setting("last-msgid", None) + if not last_msgid == None: + lines = event["batch"].get_lines() + stop_index = -1 + + for i, line in enumerate(lines): + msgid = TAG.get_value(line.tags) + if msgid == last_msgid: + stop_index = i + break + + if not stop_index == -1: + return lines[stop_index+1:]