fixed stuff; dynamic plugin reg; debugging infrastructure
This commit is contained in:
@@ -212,8 +212,8 @@ def parse_delete(filepath):
|
||||
return
|
||||
|
||||
if True != extract_url(content):
|
||||
plugins.parse_commands(content)
|
||||
plugins.parse_other(content)
|
||||
plugins.data_parse_commands(content)
|
||||
plugins.data_parse_other(content)
|
||||
return
|
||||
|
||||
def get_version_git():
|
||||
@@ -235,6 +235,8 @@ plugins.conf = conf
|
||||
plugins.logger = logger
|
||||
plugins.ratelimit_exceeded = ratelimit_exceeded
|
||||
|
||||
plugins.register_all()
|
||||
|
||||
if '__main__' == __name__:
|
||||
VERSION = get_version_git()
|
||||
print sys.argv[0] + ' ' + VERSION
|
||||
|
||||
298
plugins.py
298
plugins.py
@@ -9,12 +9,23 @@ RATE_GLOBAL = 1
|
||||
RATE_NO_SILENCE = 2
|
||||
RATE_INTERACTIVE = 4
|
||||
|
||||
plugins = {}
|
||||
plugins['parse'] = []
|
||||
plugins['command'] = []
|
||||
|
||||
def get_reply_user(data):
|
||||
# FIXME: we can't determine if a user named 'foo> ' just wrote ' > bar'
|
||||
# or a user 'foo' just wrote '> > bar'
|
||||
return data.split(' ')[0].strip('<>')
|
||||
|
||||
def parse_mental_ill(args):
|
||||
if 'register' == args:
|
||||
return {
|
||||
'name': 'parse mental illness',
|
||||
'args': ('data', 'reply_user'),
|
||||
'ratelimit_class': RATE_NO_SILENCE | RATE_GLOBAL
|
||||
}
|
||||
|
||||
min_ill = 3
|
||||
c = 0
|
||||
flag = False
|
||||
@@ -31,83 +42,111 @@ def parse_mental_ill(args):
|
||||
|
||||
if True == flag:
|
||||
return {
|
||||
'msg': '''Multiple exclamation/question marks are a sure sign of mental disease, with %s as a living example.''' % args['reply_user'],
|
||||
'ratelimit_class': RATE_NO_SILENCE | RATE_GLOBAL
|
||||
'msg': '''Multiple exclamation/question marks are a sure sign of mental disease, with %s as a living example.''' % args['reply_user']
|
||||
}
|
||||
|
||||
def parse_skynet(args):
|
||||
if 'skynet' in args['data'].lower():
|
||||
if 'register' == args:
|
||||
return {
|
||||
'msg': '''I'm an independent bot and have nothing to do with other artificial intelligence systems!''',
|
||||
'name': 'parse skynet',
|
||||
'args': ('data',),
|
||||
'ratelimit_class': RATE_GLOBAL
|
||||
}
|
||||
|
||||
if 'skynet' in args['data'].lower():
|
||||
return {
|
||||
'msg': '''I'm an independent bot and have nothing to do with other artificial intelligence systems!'''
|
||||
}
|
||||
|
||||
def parse_other(data):
|
||||
def data_parse_other(data):
|
||||
reply_user = get_reply_user(data)
|
||||
|
||||
plugins = (
|
||||
{
|
||||
'name': 'parse mental illness',
|
||||
'func': parse_mental_ill,
|
||||
'param': {
|
||||
'data': data,
|
||||
'reply_user': reply_user
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'parse skynet',
|
||||
'func': parse_skynet,
|
||||
'param': {
|
||||
'data': data
|
||||
}
|
||||
}
|
||||
)
|
||||
for p in plugins['parse']:
|
||||
if ratelimit_exceeded(p['ratelimit_class']):
|
||||
continue
|
||||
|
||||
for p in plugins:
|
||||
ret = p['func'](p['param'])
|
||||
args = {}
|
||||
|
||||
if 'args' in p.keys():
|
||||
for a in p['args']:
|
||||
if None == a: continue
|
||||
|
||||
if 'data' == a:
|
||||
args['data'] = data
|
||||
elif 'reply_user' == a:
|
||||
args['reply_user'] = reply_user
|
||||
else:
|
||||
logger('warn', 'unknown required arg for %s: %s' %(f, a))
|
||||
|
||||
ret = p['func'](args)
|
||||
|
||||
if None != ret:
|
||||
if ratelimit_exceeded(ret['ratelimit_class']):
|
||||
return False
|
||||
|
||||
if 'msg' in ret.keys():
|
||||
chat_write(ret['msg'])
|
||||
|
||||
return True
|
||||
|
||||
def command_help(args):
|
||||
if 'register' == args:
|
||||
return {
|
||||
'name': 'prints help',
|
||||
'args': ('data', 'reply_user'),
|
||||
'ratelimit_class': RATE_GLOBAL
|
||||
}
|
||||
|
||||
if 'command' in args['data']:
|
||||
return {
|
||||
'msg': args['reply_user'] + (""": known commands: 'command', 'dice', 'info', 'hangup', 'nospoiler', 'ping', 'uptime', 'source', 'version'"""),
|
||||
'ratelimit_class': RATE_GLOBAL
|
||||
'msg': args['reply_user'] + (""": known commands: 'command', 'dice', 'info', 'hangup', 'nospoiler', 'ping', 'uptime', 'source', 'version'""")
|
||||
}
|
||||
|
||||
def command_version(args):
|
||||
if 'version' in args['data']:
|
||||
if 'register' == args:
|
||||
return {
|
||||
'msg': args['reply_user'] + (''': I'm running ''' + VERSION),
|
||||
'name': 'prints version',
|
||||
'args': ('data', 'reply_user'),
|
||||
'ratelimit_class': RATE_GLOBAL
|
||||
}
|
||||
|
||||
if 'version' in args['data']:
|
||||
return {
|
||||
'msg': args['reply_user'] + (''': I'm running ''' + VERSION)
|
||||
}
|
||||
|
||||
def command_unicode(args):
|
||||
if 'register' == args:
|
||||
return {
|
||||
'name': 'prints an unicode string',
|
||||
'args': ('data', 'reply_user'),
|
||||
'ratelimit_class': RATE_GLOBAL
|
||||
}
|
||||
|
||||
if 'unikot' in args['data']:
|
||||
return {
|
||||
'msg':
|
||||
args['reply_user'] + u''': ┌────────┐''' + '\n' +
|
||||
args['reply_user'] + u''': │Unicode!│''' + '\n' +
|
||||
args['reply_user'] + u''': └────────┘''',
|
||||
'ratelimit_class': RATE_GLOBAL
|
||||
args['reply_user'] + u''': └────────┘'''
|
||||
}
|
||||
|
||||
def command_source(args):
|
||||
if 'source' in args['data']:
|
||||
if 'register' == args:
|
||||
return {
|
||||
'msg': 'My source code can be found at %s' % conf('src-url'),
|
||||
'name': 'prints git URL',
|
||||
'args': ('data', 'reply_user'),
|
||||
'ratelimit_class': RATE_GLOBAL
|
||||
}
|
||||
|
||||
if 'source' in args['data']:
|
||||
return {
|
||||
'msg': 'My source code can be found at %s' % conf('src-url')
|
||||
}
|
||||
|
||||
def command_dice(args):
|
||||
if 'register' == args:
|
||||
return {
|
||||
'name': 'rolls a dice',
|
||||
'args': ('data', 'reply_user'),
|
||||
'ratelimit_class': RATE_INTERACTIVE
|
||||
}
|
||||
|
||||
if 'dice' in args['data']:
|
||||
if args['reply_user'] in conf('enhanced-random-user'):
|
||||
rnd = 0 # this might confuse users. good.
|
||||
@@ -116,11 +155,17 @@ def command_dice(args):
|
||||
|
||||
dice_char = [u'◇', u'⚀', u'⚁', u'⚂', u'⚃', u'⚄', u'⚅']
|
||||
return {
|
||||
'msg': 'rolling a dice for %s: %s (%d)' %(args['reply_user'], dice_char[rnd], rnd),
|
||||
'ratelimit_class': RATE_INTERACTIVE
|
||||
'msg': 'rolling a dice for %s: %s (%d)' %(args['reply_user'], dice_char[rnd], rnd)
|
||||
}
|
||||
|
||||
def command_uptime(args):
|
||||
if 'register' == args:
|
||||
return {
|
||||
'name': 'prints uptime',
|
||||
'args': ('data', 'reply_user'),
|
||||
'ratelimit_class': RATE_GLOBAL
|
||||
}
|
||||
|
||||
if 'uptime' in args['data']:
|
||||
u = int(uptime + time.time())
|
||||
plural_uptime = 's'
|
||||
@@ -131,11 +176,17 @@ def command_uptime(args):
|
||||
|
||||
logger('info', 'sent statistics')
|
||||
return {
|
||||
'msg': args['reply_user'] + (''': happily serving for %d second%s, %d request%s so far.''' %(u, plural_uptime, request_counter, plural_request)),
|
||||
'ratelimit_class': RATE_GLOBAL
|
||||
'msg': args['reply_user'] + (''': happily serving for %d second%s, %d request%s so far.''' %(u, plural_uptime, request_counter, plural_request))
|
||||
}
|
||||
|
||||
def command_ping(args):
|
||||
if 'register' == args:
|
||||
return {
|
||||
'name': 'pong',
|
||||
'args': ('data', 'reply_user'),
|
||||
'ratelimit_class': RATE_INTERACTIVE
|
||||
}
|
||||
|
||||
if 'ping' in args['data']:
|
||||
rnd = random.randint(0, 3) # 1:4
|
||||
if 0 == rnd:
|
||||
@@ -149,26 +200,30 @@ def command_ping(args):
|
||||
logger('info', 'sent pong')
|
||||
|
||||
return {
|
||||
'msg': msg,
|
||||
'ratelimit_class': RATE_INTERACTIVE
|
||||
'msg': msg
|
||||
}
|
||||
|
||||
def command_info(args):
|
||||
if 'register' == args:
|
||||
return {
|
||||
'name': 'prints info message',
|
||||
'args': ('data', 'reply_user'),
|
||||
'ratelimit_class': RATE_GLOBAL
|
||||
}
|
||||
|
||||
if 'info' in args['data']:
|
||||
logger('info', 'sent long info')
|
||||
return {
|
||||
'msg': args['reply_user'] + (''': I'm a bot, my job is to extract <title> tags from posted URLs. In case I'm annoying or for further questions, please talk to my master %s. I'm rate limited and shouldn't post more than %d messages per %d seconds. To make me exit immediately, highlight me with 'hangup' in the message (emergency only, please). For other commands, highlight me with 'command'.''' %(conf('bot_owner'), hist_max_count, hist_max_time)),
|
||||
'ratelimit_class': RATE_GLOBAL
|
||||
'msg': args['reply_user'] + (''': I'm a bot, my job is to extract <title> tags from posted URLs. In case I'm annoying or for further questions, please talk to my master %s. I'm rate limited and shouldn't post more than %d messages per %d seconds. To make me exit immediately, highlight me with 'hangup' in the message (emergency only, please). For other commands, highlight me with 'command'.''' %(conf('bot_owner'), hist_max_count, hist_max_time))
|
||||
}
|
||||
|
||||
def command_else(args):
|
||||
logger('info', 'sent short info')
|
||||
return {
|
||||
'msg': args['reply_user'] + ''': I'm a bot (highlight me with 'info' for more information).''',
|
||||
'ratelimit_class': RATE_GLOBAL
|
||||
'msg': args['reply_user'] + ''': I'm a bot (highlight me with 'info' for more information).'''
|
||||
}
|
||||
|
||||
def parse_commands(data):
|
||||
def data_parse_commands(data):
|
||||
words = data.split(' ')
|
||||
|
||||
if 2 > len(words): # need at least two words
|
||||
@@ -185,83 +240,29 @@ def parse_commands(data):
|
||||
|
||||
reply_user = get_reply_user(data)
|
||||
|
||||
plugins = (
|
||||
{
|
||||
'name': 'prints help',
|
||||
'func': command_help,
|
||||
'param': {
|
||||
'data': data,
|
||||
'reply_user': reply_user
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'prints version',
|
||||
'func': command_version,
|
||||
'param': {
|
||||
'data': data,
|
||||
'reply_user': reply_user
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'prints an unicode string',
|
||||
'func': command_unicode,
|
||||
'param': {
|
||||
'data': data,
|
||||
'reply_user': reply_user
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'prints git URL',
|
||||
'func': command_source,
|
||||
'param': {
|
||||
'data': data,
|
||||
'reply_user': reply_user
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'rolls a dice',
|
||||
'func': command_dice,
|
||||
'param': {
|
||||
'data': data,
|
||||
'reply_user': reply_user
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'prints uptime',
|
||||
'func': command_uptime,
|
||||
'param': {
|
||||
'data': data,
|
||||
'reply_user': reply_user
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'pong',
|
||||
'func': command_ping,
|
||||
'param': {
|
||||
'data': data,
|
||||
'reply_user': reply_user
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'prints info message',
|
||||
'func': command_info,
|
||||
'param': {
|
||||
'data': data,
|
||||
'reply_user': reply_user
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
flag = False
|
||||
|
||||
for p in plugins:
|
||||
ret = p['func'](p['param'])
|
||||
for p in plugins['command']:
|
||||
if ratelimit_exceeded(p['ratelimit_class']):
|
||||
continue
|
||||
|
||||
args = {}
|
||||
|
||||
if 'args' in p.keys():
|
||||
for a in p['args']:
|
||||
if None == a: continue
|
||||
|
||||
if 'data' == a:
|
||||
args['data'] = data
|
||||
elif 'reply_user' == a:
|
||||
args['reply_user'] = reply_user
|
||||
else:
|
||||
logger('warn', 'unknown required arg for %s: %s' %(f, a))
|
||||
|
||||
ret = p['func'](args)
|
||||
|
||||
if None != ret:
|
||||
flag = True
|
||||
|
||||
if ratelimit_exceeded(ret['ratelimit_class']):
|
||||
return False
|
||||
|
||||
if 'msg' in ret.keys():
|
||||
chat_write(ret['msg'])
|
||||
|
||||
@@ -275,3 +276,58 @@ def parse_commands(data):
|
||||
|
||||
if 'msg' in ret.keys():
|
||||
chat_write(ret['msg'])
|
||||
|
||||
funcs = {}
|
||||
funcs['parse'] = (parse_mental_ill, parse_skynet)
|
||||
funcs['command'] = (
|
||||
command_help, command_version, command_unicode, command_source,
|
||||
command_dice, command_uptime, command_ping, command_info
|
||||
)
|
||||
|
||||
_dir = dir()
|
||||
|
||||
debug = False
|
||||
if debug:
|
||||
def _chat_write(a): _logger('chat_write', a)
|
||||
def _conf(a): return 'bot'
|
||||
def _logger(a, b): print 'logger: %s::%s' %(a, b)
|
||||
def _ratelimit_exceeded(ignored=None): return False
|
||||
|
||||
try: chat_write
|
||||
except NameError: chat_write = _chat_write
|
||||
try: conf
|
||||
except NameError: conf = _conf
|
||||
try: logger
|
||||
except NameError: logger = _logger
|
||||
try: ratelimit_exceeded
|
||||
except NameError: ratelimit_exceeded = _ratelimit_exceeded
|
||||
try: random
|
||||
except NameError: import random
|
||||
|
||||
def register(func_type, auto=False):
|
||||
plugins[func_type] = []
|
||||
|
||||
if auto:
|
||||
# FIXME: this is broken. dir() returns str, but not
|
||||
# the addr of the functions which we'd need here.
|
||||
for f in _dir:
|
||||
print 'testing(%s)' % f
|
||||
if not f.startswith(func_type + '_'):
|
||||
continue
|
||||
|
||||
try:
|
||||
ret = f('register')
|
||||
ret['func'] = f
|
||||
plugins[func_type].append(ret)
|
||||
except Exception as e:
|
||||
logger('warn', 'auto-registering %s failed: %s' %(f, e))
|
||||
|
||||
else:
|
||||
for f in funcs[func_type]:
|
||||
ret = f('register')
|
||||
ret['func'] = f
|
||||
plugins[func_type].append(ret)
|
||||
|
||||
def register_all():
|
||||
register('parse')
|
||||
register('command')
|
||||
|
||||
Reference in New Issue
Block a user