mirror of
http://aero2k.de/t/repos/urlbot-native.git
synced 2017-09-06 15:25:38 +02:00
fix hangup and logging
This commit is contained in:
@@ -99,10 +99,11 @@ def start(botclass, active=False):
|
|||||||
bot.connect()
|
bot.connect()
|
||||||
bot.register_plugin('xep_0045')
|
bot.register_plugin('xep_0045')
|
||||||
bot.process()
|
bot.process()
|
||||||
|
global got_hangup
|
||||||
|
|
||||||
while 1:
|
while 1:
|
||||||
try:
|
try:
|
||||||
if not plugins.event_trigger():
|
if got_hangup or not plugins.event_trigger():
|
||||||
bot.disconnect()
|
bot.disconnect()
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|||||||
81
plugins.py
81
plugins.py
@@ -26,7 +26,6 @@ joblist = []
|
|||||||
|
|
||||||
plugins = {p: [] for p in ptypes}
|
plugins = {p: [] for p in ptypes}
|
||||||
|
|
||||||
got_hangup = False
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@@ -106,7 +105,7 @@ def parse_mental_ill(**args):
|
|||||||
break
|
break
|
||||||
|
|
||||||
if flag:
|
if flag:
|
||||||
log.plugin('sent mental illness reply')
|
log.info('sent mental illness reply')
|
||||||
return {
|
return {
|
||||||
'msg': (
|
'msg': (
|
||||||
'''Multiple exclamation/question marks are a sure sign of mental disease, with %s as a living example.''' %
|
'''Multiple exclamation/question marks are a sure sign of mental disease, with %s as a living example.''' %
|
||||||
@@ -123,7 +122,7 @@ def parse_debbug(**args):
|
|||||||
|
|
||||||
out = []
|
out = []
|
||||||
for b in bugs:
|
for b in bugs:
|
||||||
log.plugin('detected Debian bug #%s' % b)
|
log.info('detected Debian bug #%s' % b)
|
||||||
|
|
||||||
url = 'https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s' % b
|
url = 'https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s' % b
|
||||||
status, title = extract_title(url)
|
status, title = extract_title(url)
|
||||||
@@ -133,7 +132,7 @@ def parse_debbug(**args):
|
|||||||
elif 3 == status:
|
elif 3 == status:
|
||||||
out.append('error for #%s: %s' % (b, title))
|
out.append('error for #%s: %s' % (b, title))
|
||||||
else:
|
else:
|
||||||
log.plugin('unknown status %d' % status)
|
log.info('unknown status %d' % status)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'msg': out
|
'msg': out
|
||||||
@@ -146,7 +145,7 @@ def parse_cve(**args):
|
|||||||
if not cves:
|
if not cves:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
log.plugin('detected CVE handle')
|
log.info('detected CVE handle')
|
||||||
return {
|
return {
|
||||||
'msg': ['https://security-tracker.debian.org/tracker/%s' % c for c in cves]
|
'msg': ['https://security-tracker.debian.org/tracker/%s' % c for c in cves]
|
||||||
}
|
}
|
||||||
@@ -158,7 +157,7 @@ def parse_dsa(**args):
|
|||||||
if not dsas:
|
if not dsas:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
log.plugin('detected DSA handle')
|
log.info('detected DSA handle')
|
||||||
return {
|
return {
|
||||||
'msg': ['https://security-tracker.debian.org/tracker/%s' % d for d in dsas]
|
'msg': ['https://security-tracker.debian.org/tracker/%s' % d for d in dsas]
|
||||||
}
|
}
|
||||||
@@ -167,7 +166,7 @@ def parse_dsa(**args):
|
|||||||
@pluginfunction('skynet', 'parse skynet', ptypes_PARSE)
|
@pluginfunction('skynet', 'parse skynet', ptypes_PARSE)
|
||||||
def parse_skynet(**args):
|
def parse_skynet(**args):
|
||||||
if 'skynet' in args['data'].lower():
|
if 'skynet' in args['data'].lower():
|
||||||
log.plugin('sent skynet reply')
|
log.info('sent skynet reply')
|
||||||
return {
|
return {
|
||||||
'msg': '''I'm an independent bot and have nothing to do with other artificial intelligence systems!'''
|
'msg': '''I'm an independent bot and have nothing to do with other artificial intelligence systems!'''
|
||||||
}
|
}
|
||||||
@@ -186,11 +185,11 @@ def parse_moin(**args):
|
|||||||
for w in words:
|
for w in words:
|
||||||
if d.lower() == w.lower():
|
if d.lower() == w.lower():
|
||||||
if args['reply_user'] in conf('moin-disabled-user'):
|
if args['reply_user'] in conf('moin-disabled-user'):
|
||||||
log.plugin('moin blacklist match')
|
log.info('moin blacklist match')
|
||||||
return
|
return
|
||||||
|
|
||||||
if args['reply_user'] in conf('moin-modified-user'):
|
if args['reply_user'] in conf('moin-modified-user'):
|
||||||
log.plugin('being "quiet" for %s' % w)
|
log.info('being "quiet" for %s' % w)
|
||||||
return {
|
return {
|
||||||
'msg': '/me %s' % random.choice([
|
'msg': '/me %s' % random.choice([
|
||||||
"doesn't say anything at all",
|
"doesn't say anything at all",
|
||||||
@@ -199,7 +198,7 @@ def parse_moin(**args):
|
|||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
log.plugin('sent %s reply for %s' % (
|
log.info('sent %s reply for %s' % (
|
||||||
'hi' if direction is moin_strings_hi else 'bye', w
|
'hi' if direction is moin_strings_hi else 'bye', w
|
||||||
))
|
))
|
||||||
return {
|
return {
|
||||||
@@ -221,7 +220,7 @@ def parse_latex(**args):
|
|||||||
@pluginfunction('me-action', 'reacts to /me.*%{bot_user}', ptypes_PARSE)
|
@pluginfunction('me-action', 'reacts to /me.*%{bot_user}', ptypes_PARSE)
|
||||||
def parse_slash_me(**args):
|
def parse_slash_me(**args):
|
||||||
if args['data'].lower().startswith('/me') and (conf('bot_user') in args['data'].lower()):
|
if args['data'].lower().startswith('/me') and (conf('bot_user') in args['data'].lower()):
|
||||||
log.plugin('sent /me reply')
|
log.info('sent /me reply')
|
||||||
|
|
||||||
me_replys = [
|
me_replys = [
|
||||||
'are you that rude to everybody?',
|
'are you that rude to everybody?',
|
||||||
@@ -245,7 +244,7 @@ def command_help(argv, **args):
|
|||||||
return
|
return
|
||||||
|
|
||||||
if not what:
|
if not what:
|
||||||
log.plugin('empty help request, sent all commands')
|
log.info('empty help request, sent all commands')
|
||||||
commands = args['cmd_list']
|
commands = args['cmd_list']
|
||||||
commands.sort()
|
commands.sort()
|
||||||
parsers = args['parser_list']
|
parsers = args['parser_list']
|
||||||
@@ -261,7 +260,7 @@ def command_help(argv, **args):
|
|||||||
|
|
||||||
for p in plugins[ptypes_COMMAND] + plugins[ptypes_PARSE]:
|
for p in plugins[ptypes_COMMAND] + plugins[ptypes_PARSE]:
|
||||||
if what == p.plugin_name:
|
if what == p.plugin_name:
|
||||||
log.plugin('sent help for %s' % what)
|
log.info('sent help for %s' % what)
|
||||||
return {
|
return {
|
||||||
'msg': args['reply_user'] + ': help for %s %s %s: %s' % (
|
'msg': args['reply_user'] + ': help for %s %s %s: %s' % (
|
||||||
'enabled' if plugin_enabled_get(p) else 'disabled',
|
'enabled' if plugin_enabled_get(p) else 'disabled',
|
||||||
@@ -269,7 +268,7 @@ def command_help(argv, **args):
|
|||||||
what, p.plugin_desc
|
what, p.plugin_desc
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
log.plugin('no help found for %s' % what)
|
log.info('no help found for %s' % what)
|
||||||
return {
|
return {
|
||||||
'msg': args['reply_user'] + ': no such command: %s' % what
|
'msg': args['reply_user'] + ': no such command: %s' % what
|
||||||
}
|
}
|
||||||
@@ -280,7 +279,7 @@ def command_version(argv, **args):
|
|||||||
if 'version' != argv[0]:
|
if 'version' != argv[0]:
|
||||||
return
|
return
|
||||||
|
|
||||||
log.plugin('sent version string')
|
log.info('sent version string')
|
||||||
return {
|
return {
|
||||||
'msg': args['reply_user'] + (''': I'm running ''' + VERSION)
|
'msg': args['reply_user'] + (''': I'm running ''' + VERSION)
|
||||||
}
|
}
|
||||||
@@ -291,7 +290,7 @@ def command_klammer(argv, **args):
|
|||||||
if 'klammer' != argv[0]:
|
if 'klammer' != argv[0]:
|
||||||
return
|
return
|
||||||
|
|
||||||
log.plugin('sent karl klammer')
|
log.info('sent karl klammer')
|
||||||
return {
|
return {
|
||||||
'msg': (
|
'msg': (
|
||||||
args['reply_user'] + ',',
|
args['reply_user'] + ',',
|
||||||
@@ -310,7 +309,7 @@ def command_unicode(argv, **args):
|
|||||||
if 'unikot' != argv[0]:
|
if 'unikot' != argv[0]:
|
||||||
return
|
return
|
||||||
|
|
||||||
log.plugin('sent some unicode')
|
log.info('sent some unicode')
|
||||||
return {
|
return {
|
||||||
'msg': (
|
'msg': (
|
||||||
args['reply_user'] + ''', here's some''',
|
args['reply_user'] + ''', here's some''',
|
||||||
@@ -326,7 +325,7 @@ def command_source(argv, **_):
|
|||||||
if argv[0] not in ('source', 'src'):
|
if argv[0] not in ('source', 'src'):
|
||||||
return
|
return
|
||||||
|
|
||||||
log.plugin('sent source URL')
|
log.info('sent source URL')
|
||||||
return {
|
return {
|
||||||
'msg': 'My source code can be found at %s' % conf('src-url')
|
'msg': 'My source code can be found at %s' % conf('src-url')
|
||||||
}
|
}
|
||||||
@@ -359,10 +358,10 @@ def command_dice(argv, **args):
|
|||||||
for i in range(count):
|
for i in range(count):
|
||||||
if args['reply_user'] in conf('enhanced-random-user'):
|
if args['reply_user'] in conf('enhanced-random-user'):
|
||||||
rnd = 0 # this might confuse users. good.
|
rnd = 0 # this might confuse users. good.
|
||||||
log.plugin('sent random (enhanced)')
|
log.info('sent random (enhanced)')
|
||||||
else:
|
else:
|
||||||
rnd = random.randint(1, 6)
|
rnd = random.randint(1, 6)
|
||||||
log.plugin('sent random')
|
log.info('sent random')
|
||||||
|
|
||||||
# the \u200b chars ('ZERO WIDTH SPACE') avoid interpreting stuff as smileys
|
# the \u200b chars ('ZERO WIDTH SPACE') avoid interpreting stuff as smileys
|
||||||
# by some strange clients
|
# by some strange clients
|
||||||
@@ -387,7 +386,7 @@ def command_choose(argv, **args):
|
|||||||
|
|
||||||
choice = random.choice(alternatives)
|
choice = random.choice(alternatives)
|
||||||
|
|
||||||
log.plugin('sent random choice')
|
log.info('sent random choice')
|
||||||
return {
|
return {
|
||||||
'msg': '%s: I prefer %s!' % (args['reply_user'], choice)
|
'msg': '%s: I prefer %s!' % (args['reply_user'], choice)
|
||||||
}
|
}
|
||||||
@@ -407,7 +406,7 @@ def command_uptime(argv, **args):
|
|||||||
if 1 == conf('request_counter'):
|
if 1 == conf('request_counter'):
|
||||||
plural_request = ''
|
plural_request = ''
|
||||||
|
|
||||||
log.plugin('sent statistics')
|
log.info('sent statistics')
|
||||||
return {
|
return {
|
||||||
'msg': args['reply_user'] + (''': happily serving for %d second%s, %d request%s so far.''' % (
|
'msg': args['reply_user'] + (''': happily serving for %d second%s, %d request%s so far.''' % (
|
||||||
u, plural_uptime, int(conf('request_counter')), plural_request))
|
u, plural_uptime, int(conf('request_counter')), plural_request))
|
||||||
@@ -422,13 +421,13 @@ def command_ping(argv, **args):
|
|||||||
rnd = random.randint(0, 3) # 1:4
|
rnd = random.randint(0, 3) # 1:4
|
||||||
if 0 == rnd:
|
if 0 == rnd:
|
||||||
msg = args['reply_user'] + ''': peng (You're dead now.)'''
|
msg = args['reply_user'] + ''': peng (You're dead now.)'''
|
||||||
log.plugin('sent pong (variant)')
|
log.info('sent pong (variant)')
|
||||||
elif 1 == rnd:
|
elif 1 == rnd:
|
||||||
msg = args['reply_user'] + ''': I don't like you, leave me alone.'''
|
msg = args['reply_user'] + ''': I don't like you, leave me alone.'''
|
||||||
log.plugin('sent pong (dontlike)')
|
log.info('sent pong (dontlike)')
|
||||||
else:
|
else:
|
||||||
msg = args['reply_user'] + ''': pong'''
|
msg = args['reply_user'] + ''': pong'''
|
||||||
log.plugin('sent pong')
|
log.info('sent pong')
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'msg': msg
|
'msg': msg
|
||||||
@@ -440,7 +439,7 @@ def command_info(argv, **args):
|
|||||||
if 'info' != argv[0]:
|
if 'info' != argv[0]:
|
||||||
return
|
return
|
||||||
|
|
||||||
log.plugin('sent long info')
|
log.info('sent long info')
|
||||||
return {
|
return {
|
||||||
'msg': args['reply_user'] + (
|
'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
|
''': I'm a bot, my job is to extract <title> tags from posted URLs. In case I'm annoying or for further
|
||||||
@@ -471,7 +470,7 @@ def command_teatimer(argv, **args):
|
|||||||
ready = time.time() + steep
|
ready = time.time() + steep
|
||||||
|
|
||||||
try:
|
try:
|
||||||
log.plugin('tea timer set to %s' % time.strftime('%F.%T', time.localtime(ready)))
|
log.info('tea timer set to %s' % time.strftime('%F.%T', time.localtime(ready)))
|
||||||
except (ValueError, OverflowError) as e:
|
except (ValueError, OverflowError) as e:
|
||||||
return {
|
return {
|
||||||
'msg': args['reply_user'] + ': time format error: ' + str(e)
|
'msg': args['reply_user'] + ': time format error: ' + str(e)
|
||||||
@@ -498,7 +497,7 @@ def command_decode(argv, **args):
|
|||||||
'msg': args['reply_user'] + ': usage: decode {single character}'
|
'msg': args['reply_user'] + ': usage: decode {single character}'
|
||||||
}
|
}
|
||||||
|
|
||||||
log.plugin('decode called for %s' % argv[1])
|
log.info('decode called for %s' % argv[1])
|
||||||
|
|
||||||
out = []
|
out = []
|
||||||
for i, char in enumerate(argv[1]):
|
for i, char in enumerate(argv[1]):
|
||||||
@@ -516,7 +515,7 @@ def command_decode(argv, **args):
|
|||||||
try:
|
try:
|
||||||
uni_name = unicodedata.name(char)
|
uni_name = unicodedata.name(char)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.plugin('decode(%s) failed: %s' % (char, e))
|
log.info('decode(%s) failed: %s' % (char, e))
|
||||||
out.append("can't decode %s%s: %s" % (char, char_esc, e))
|
out.append("can't decode %s%s: %s" % (char, char_esc, e))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -537,7 +536,7 @@ def command_show_blacklist(argv, **args):
|
|||||||
if 'show-blacklist' != argv[0]:
|
if 'show-blacklist' != argv[0]:
|
||||||
return
|
return
|
||||||
|
|
||||||
log.plugin('sent URL blacklist')
|
log.info('sent URL blacklist')
|
||||||
|
|
||||||
argv1 = None if len(argv) < 2 else argv[1]
|
argv1 = None if len(argv) < 2 else argv[1]
|
||||||
|
|
||||||
@@ -637,7 +636,7 @@ def command_plugin_activation(argv, **args):
|
|||||||
if command not in ('enable', 'disable'):
|
if command not in ('enable', 'disable'):
|
||||||
return
|
return
|
||||||
|
|
||||||
log.plugin('plugin activation plugin called')
|
log.info('plugin activation plugin called')
|
||||||
|
|
||||||
if not plugin:
|
if not plugin:
|
||||||
return {
|
return {
|
||||||
@@ -679,7 +678,7 @@ def command_wp(argv, lang='de', **args):
|
|||||||
if 'wp' != argv[0]:
|
if 'wp' != argv[0]:
|
||||||
return
|
return
|
||||||
|
|
||||||
log.plugin('plugin called')
|
log.info('plugin called')
|
||||||
|
|
||||||
query = ' '.join(argv[1:])
|
query = ' '.join(argv[1:])
|
||||||
|
|
||||||
@@ -702,7 +701,7 @@ def command_wp(argv, lang='de', **args):
|
|||||||
lang, urllib.parse.urlencode(api)
|
lang, urllib.parse.urlencode(api)
|
||||||
)
|
)
|
||||||
|
|
||||||
log.plugin('fetching %s' % apiurl)
|
log.info('fetching %s' % apiurl)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = urllib.request.urlopen(apiurl)
|
response = urllib.request.urlopen(apiurl)
|
||||||
@@ -716,7 +715,7 @@ def command_wp(argv, lang='de', **args):
|
|||||||
lang, urllib.parse.quote(linktitle)
|
lang, urllib.parse.quote(linktitle)
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.plugin('wp(%s) failed: %s, %s' % (query, e, traceback.format_exc()))
|
log.info('wp(%s) failed: %s, %s' % (query, e, traceback.format_exc()))
|
||||||
return {
|
return {
|
||||||
'msg': args['reply_user'] + ': something failed: %s' % e
|
'msg': args['reply_user'] + ': something failed: %s' % e
|
||||||
}
|
}
|
||||||
@@ -742,7 +741,7 @@ def command_dummy(argv, **args):
|
|||||||
if 'excuse' != argv[0]:
|
if 'excuse' != argv[0]:
|
||||||
return
|
return
|
||||||
|
|
||||||
log.plugin('BOFH plugin called')
|
log.info('BOFH plugin called')
|
||||||
|
|
||||||
excuse = random.sample(excuses, 1)[0]
|
excuse = random.sample(excuses, 1)[0]
|
||||||
|
|
||||||
@@ -756,7 +755,7 @@ def command_show_moinlist(argv, **args):
|
|||||||
if 'show-moinlist' != argv[0]:
|
if 'show-moinlist' != argv[0]:
|
||||||
return
|
return
|
||||||
|
|
||||||
log.plugin('sent moin reply list')
|
log.info('sent moin reply list')
|
||||||
|
|
||||||
argv1 = None if len(argv) < 2 else argv[1]
|
argv1 = None if len(argv) < 2 else argv[1]
|
||||||
|
|
||||||
@@ -778,7 +777,7 @@ def command_list(argv, **args):
|
|||||||
if 'list' != argv[0]:
|
if 'list' != argv[0]:
|
||||||
return
|
return
|
||||||
|
|
||||||
log.plugin('list plugin called')
|
log.info('list plugin called')
|
||||||
|
|
||||||
if 'enabled' in argv and 'disabled' in argv:
|
if 'enabled' in argv and 'disabled' in argv:
|
||||||
return {
|
return {
|
||||||
@@ -856,7 +855,7 @@ def command_show_recordlist(argv, **args):
|
|||||||
if 'show-records' != argv[0]:
|
if 'show-records' != argv[0]:
|
||||||
return
|
return
|
||||||
|
|
||||||
log.plugin('sent offline records list')
|
log.info('sent offline records list')
|
||||||
|
|
||||||
argv1 = None if len(argv) < 2 else argv[1]
|
argv1 = None if len(argv) < 2 else argv[1]
|
||||||
|
|
||||||
@@ -959,7 +958,7 @@ def command_dsa_watcher(argv, **_):
|
|||||||
|
|
||||||
|
|
||||||
def else_command(args):
|
def else_command(args):
|
||||||
log.plugin('sent short info')
|
log.info('sent short info')
|
||||||
return {
|
return {
|
||||||
'msg': args['reply_user'] + ''': I'm a bot (highlight me with 'info' for more information).'''
|
'msg': args['reply_user'] + ''': I'm a bot (highlight me with 'info' for more information).'''
|
||||||
}
|
}
|
||||||
@@ -1002,9 +1001,6 @@ def register_all():
|
|||||||
|
|
||||||
|
|
||||||
def event_trigger():
|
def event_trigger():
|
||||||
if got_hangup:
|
|
||||||
return False
|
|
||||||
|
|
||||||
if 0 == len(joblist):
|
if 0 == len(joblist):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -1014,3 +1010,4 @@ def event_trigger():
|
|||||||
if t < now:
|
if t < now:
|
||||||
callback(*args)
|
callback(*args)
|
||||||
del (joblist[i])
|
del (joblist[i])
|
||||||
|
return True
|
||||||
|
|||||||
Reference in New Issue
Block a user