From d59645bacf07878b83f8732c757421d839763463 Mon Sep 17 00:00:00 2001 From: braph Date: Mon, 4 Apr 2016 00:47:14 +0200 Subject: [PATCH] new plugin: comment_joins --- persistent_config.ini.spec | 2 ++ plugins/muc_online.py | 38 ++++++++++++++++++++++++++++++++++++++ urlbot.py | 2 +- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/persistent_config.ini.spec b/persistent_config.ini.spec index d04aff1..2f9afb2 100644 --- a/persistent_config.ini.spec +++ b/persistent_config.ini.spec @@ -14,6 +14,8 @@ start_time = integer(default=0) [user_records] +[user_joins] + [url_blacklist] heise = string(default='^.*heise\.de/.*-[0-9]+\.html$') wikipedia = string(default='^.*wikipedia\.org/wiki/.*$') diff --git a/plugins/muc_online.py b/plugins/muc_online.py index 9b97aa1..a65a1b8 100644 --- a/plugins/muc_online.py +++ b/plugins/muc_online.py @@ -1,4 +1,6 @@ import logging +import time +import random import config from common import ( @@ -36,3 +38,39 @@ def send_record(**args): config.runtimeconf_persist() return response + + +@pluginfunction('comment_joins', 'comments frequent joins', ptypes_MUC_ONLINE) +@config_locked +def comment_joins(**args): + # max elapsed time between the latest and the N latest join + timespan = 120 + max_joins = 6 + + comment_joins_strings = [ + "%s: please consider to fix your internet connection" + ] + + current_timestamp = int(time.time()) + + arg_user = args['reply_user'] + arg_user_key = arg_user.lower() + + if arg_user_key not in config.runtime_config_store['user_joins']: + config.runtime_config_store['user_joins'][arg_user_key] = [ current_timestamp ] + config.runtimeconf_persist() + return None + + user_joins = config.runtime_config_store['user_joins'][arg_user_key] + + user_joins = [ + ts if current_timestamp - int(ts) <= timespan else [] for ts in user_joins + ] + user_joins.append(current_timestamp) + + if len(user_joins) >= max_joins: + config.runtime_config_store['user_joins'].pop(arg_user_key) + config.runtimeconf_persist() + return { 'msg': random.choice(comment_joins_strings) % arg_user } + else: + config.runtimeconf_persist() diff --git a/urlbot.py b/urlbot.py index 9df2efb..d1650ee 100755 --- a/urlbot.py +++ b/urlbot.py @@ -229,7 +229,7 @@ class UrlBot(IdleBot): ret = plugin(reply_user=reply_user) if ret: - self._run_action(ret, plugin, msg_obj) + self._run_action(ret, plugin, None) def data_parse_commands(self, msg_obj): """