ratelimit fix/split up; bot_user fixes
This commit is contained in:
15
plugins.py
15
plugins.py
@@ -5,9 +5,11 @@ if '__main__' == __name__:
|
|||||||
print '''this is a plugin file, which is not meant to be executed'''
|
print '''this is a plugin file, which is not meant to be executed'''
|
||||||
exit(-1)
|
exit(-1)
|
||||||
|
|
||||||
RATE_GLOBAL = 1
|
RATE_GLOBAL = 0x01
|
||||||
RATE_NO_SILENCE = 2
|
RATE_NO_SILENCE = 0x02
|
||||||
RATE_INTERACTIVE = 4
|
RATE_INTERACTIVE = 0x04
|
||||||
|
RATE_CHAT = 0x08
|
||||||
|
RATE_URL = 0x10
|
||||||
|
|
||||||
plugins = {}
|
plugins = {}
|
||||||
plugins['parse'] = []
|
plugins['parse'] = []
|
||||||
@@ -82,6 +84,7 @@ def data_parse_other(data):
|
|||||||
|
|
||||||
if None != ret:
|
if None != ret:
|
||||||
if 'msg' in ret.keys():
|
if 'msg' in ret.keys():
|
||||||
|
ratelimit_touch(RATE_CHAT)
|
||||||
chat_write(ret['msg'])
|
chat_write(ret['msg'])
|
||||||
|
|
||||||
def command_help(args):
|
def command_help(args):
|
||||||
@@ -230,7 +233,7 @@ def data_parse_commands(data):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
# don't reply if beginning of the text matches bot_user
|
# 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
|
return None
|
||||||
|
|
||||||
if 'hangup' in data:
|
if 'hangup' in data:
|
||||||
@@ -264,6 +267,7 @@ def data_parse_commands(data):
|
|||||||
if None != ret:
|
if None != ret:
|
||||||
flag = True
|
flag = True
|
||||||
if 'msg' in ret.keys():
|
if 'msg' in ret.keys():
|
||||||
|
ratelimit_touch(RATE_CHAT)
|
||||||
chat_write(ret['msg'])
|
chat_write(ret['msg'])
|
||||||
|
|
||||||
if False != flag:
|
if False != flag:
|
||||||
@@ -292,6 +296,7 @@ if debug:
|
|||||||
def _conf(a): return 'bot'
|
def _conf(a): return 'bot'
|
||||||
def _logger(a, b): print 'logger: %s::%s' %(a, b)
|
def _logger(a, b): print 'logger: %s::%s' %(a, b)
|
||||||
def _ratelimit_exceeded(ignored=None): return False
|
def _ratelimit_exceeded(ignored=None): return False
|
||||||
|
def _ratelimit_touch(ignored=None): return True
|
||||||
|
|
||||||
try: chat_write
|
try: chat_write
|
||||||
except NameError: chat_write = _chat_write
|
except NameError: chat_write = _chat_write
|
||||||
@@ -301,6 +306,8 @@ if debug:
|
|||||||
except NameError: logger = _logger
|
except NameError: logger = _logger
|
||||||
try: ratelimit_exceeded
|
try: ratelimit_exceeded
|
||||||
except NameError: ratelimit_exceeded = _ratelimit_exceeded
|
except NameError: ratelimit_exceeded = _ratelimit_exceeded
|
||||||
|
try: ratelimit_touch
|
||||||
|
except NameError: ratelimit_touch = _ratelimit_touch
|
||||||
try: random
|
try: random
|
||||||
except NameError: import random
|
except NameError: import random
|
||||||
|
|
||||||
|
|||||||
15
urlbot.py
15
urlbot.py
@@ -131,12 +131,17 @@ def chat_write(message, prefix='/say '):
|
|||||||
except IOError:
|
except IOError:
|
||||||
logger('err', "couldn't print to fifo " + fifo_path)
|
logger('err', "couldn't print to fifo " + fifo_path)
|
||||||
|
|
||||||
def ratelimit_exceeded(ignored=None): # FIXME: separate counters
|
def ratelimit_touch(ignored=None): # FIXME: separate counters
|
||||||
global hist_flag
|
|
||||||
|
|
||||||
now = time.time()
|
now = time.time()
|
||||||
hist_ts.append(now)
|
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):
|
if hist_max_count < len(hist_ts):
|
||||||
first = hist_ts.pop(0)
|
first = hist_ts.pop(0)
|
||||||
if (now - first) < hist_max_time:
|
if (now - first) < hist_max_time:
|
||||||
@@ -155,6 +160,7 @@ def extract_url(data):
|
|||||||
result = re.findall("(https?://[^\s>]+)", data)
|
result = re.findall("(https?://[^\s>]+)", data)
|
||||||
if result:
|
if result:
|
||||||
for r in result:
|
for r in result:
|
||||||
|
ratelimit_touch()
|
||||||
if ratelimit_exceeded():
|
if ratelimit_exceeded():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -197,7 +203,7 @@ def parse_delete(filepath):
|
|||||||
fd.close()
|
fd.close()
|
||||||
os.remove(filepath) # probably better crash here
|
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
|
return
|
||||||
|
|
||||||
if 'has set the subject to:' in content:
|
if 'has set the subject to:' in content:
|
||||||
@@ -234,6 +240,7 @@ plugins.chat_write = chat_write
|
|||||||
plugins.conf = conf
|
plugins.conf = conf
|
||||||
plugins.logger = logger
|
plugins.logger = logger
|
||||||
plugins.ratelimit_exceeded = ratelimit_exceeded
|
plugins.ratelimit_exceeded = ratelimit_exceeded
|
||||||
|
plugins.ratelimit_touch = ratelimit_touch
|
||||||
|
|
||||||
plugins.register_all()
|
plugins.register_all()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user