From 294408c4fe8300de3d5ff5e3c1c5539d754ba4e9 Mon Sep 17 00:00:00 2001 From: urlbot Date: Sat, 27 Sep 2014 05:51:18 +0200 Subject: [PATCH] ratelimit fix/split up; bot_user fixes --- plugins.py | 15 +++++++++++---- urlbot.py | 15 +++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/plugins.py b/plugins.py index 7103b78..16a68ec 100644 --- a/plugins.py +++ b/plugins.py @@ -5,9 +5,11 @@ if '__main__' == __name__: print '''this is a plugin file, which is not meant to be executed''' exit(-1) -RATE_GLOBAL = 1 -RATE_NO_SILENCE = 2 -RATE_INTERACTIVE = 4 +RATE_GLOBAL = 0x01 +RATE_NO_SILENCE = 0x02 +RATE_INTERACTIVE = 0x04 +RATE_CHAT = 0x08 +RATE_URL = 0x10 plugins = {} plugins['parse'] = [] @@ -82,6 +84,7 @@ def data_parse_other(data): if None != ret: if 'msg' in ret.keys(): + ratelimit_touch(RATE_CHAT) chat_write(ret['msg']) def command_help(args): @@ -230,7 +233,7 @@ def data_parse_commands(data): return None # don't reply if beginning of the text matches bot_user - if words[1][0:len(conf('bot_user'))] != conf('bot_user'): + if not words[1].startswith(conf('bot_user')): return None if 'hangup' in data: @@ -264,6 +267,7 @@ def data_parse_commands(data): if None != ret: flag = True if 'msg' in ret.keys(): + ratelimit_touch(RATE_CHAT) chat_write(ret['msg']) if False != flag: @@ -292,6 +296,7 @@ if debug: def _conf(a): return 'bot' def _logger(a, b): print 'logger: %s::%s' %(a, b) def _ratelimit_exceeded(ignored=None): return False + def _ratelimit_touch(ignored=None): return True try: chat_write except NameError: chat_write = _chat_write @@ -301,6 +306,8 @@ if debug: except NameError: logger = _logger try: ratelimit_exceeded except NameError: ratelimit_exceeded = _ratelimit_exceeded + try: ratelimit_touch + except NameError: ratelimit_touch = _ratelimit_touch try: random except NameError: import random diff --git a/urlbot.py b/urlbot.py index f355288..3d6e78a 100755 --- a/urlbot.py +++ b/urlbot.py @@ -131,12 +131,17 @@ def chat_write(message, prefix='/say '): except IOError: logger('err', "couldn't print to fifo " + fifo_path) -def ratelimit_exceeded(ignored=None): # FIXME: separate counters - global hist_flag - +def ratelimit_touch(ignored=None): # FIXME: separate counters now = time.time() hist_ts.append(now) + if hist_max_count < len(hist_ts): + hist_ts.pop(0) + + +def ratelimit_exceeded(ignored=None): # FIXME: separate counters + global hist_flag + if hist_max_count < len(hist_ts): first = hist_ts.pop(0) if (now - first) < hist_max_time: @@ -155,6 +160,7 @@ def extract_url(data): result = re.findall("(https?://[^\s>]+)", data) if result: for r in result: + ratelimit_touch() if ratelimit_exceeded(): return False @@ -197,7 +203,7 @@ def parse_delete(filepath): fd.close() os.remove(filepath) # probably better crash here - if content[1:1+len(bot_user)] == bot_user: + if content[1:1+len(conf('bot_user'))] == conf('bot_user'): return if 'has set the subject to:' in content: @@ -234,6 +240,7 @@ plugins.chat_write = chat_write plugins.conf = conf plugins.logger = logger plugins.ratelimit_exceeded = ratelimit_exceeded +plugins.ratelimit_touch = ratelimit_touch plugins.register_all()