1
0
mirror of http://aero2k.de/t/repos/urlbot-native.git synced 2017-09-06 15:25:38 +02:00

ratelimit fix/split up; bot_user fixes

This commit is contained in:
urlbot
2014-09-27 05:51:18 +02:00
parent c3ddeaecdc
commit 294408c4fe
2 changed files with 22 additions and 8 deletions

View File

@@ -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

View File

@@ -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()