2016-04-05 14:18:22 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-11-20 21:48:29 +01:00
|
|
|
import logging
|
2016-04-05 14:18:22 +02:00
|
|
|
|
|
|
|
|
import events
|
|
|
|
|
import json
|
2015-11-20 21:07:48 +01:00
|
|
|
import random
|
|
|
|
|
import time
|
2015-02-05 19:23:05 +01:00
|
|
|
import traceback
|
2015-11-20 21:07:48 +01:00
|
|
|
import unicodedata
|
2016-01-12 22:44:39 +01:00
|
|
|
from urllib.parse import urlparse
|
2016-01-03 13:19:49 +01:00
|
|
|
import requests
|
2016-01-04 14:27:02 +01:00
|
|
|
from lxml import etree
|
2016-01-03 13:19:49 +01:00
|
|
|
|
2015-12-20 12:36:08 +01:00
|
|
|
import config
|
2016-04-05 18:40:31 +02:00
|
|
|
from common import VERSION
|
2016-04-05 14:18:22 +02:00
|
|
|
from rate_limit import RATE_FUN, RATE_GLOBAL, RATE_INTERACTIVE, RATE_NO_SILENCE, RATE_NO_LIMIT
|
2016-04-05 18:40:31 +02:00
|
|
|
from plugin_system import pluginfunction, ptypes, plugin_storage, plugin_enabled_get, plugin_enabled_set
|
2016-05-27 21:31:31 +02:00
|
|
|
|
2016-04-05 18:40:31 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
|
|
|
|
|
@pluginfunction('help', 'print help for a command or all known commands', ptypes.COMMAND)
|
|
|
|
|
def command_help(argv, **args):
|
|
|
|
|
what = argv[0] if argv else None
|
|
|
|
|
|
|
|
|
|
if not what:
|
|
|
|
|
log.info('empty help request, sent all commands')
|
|
|
|
|
commands = args['cmd_list']
|
|
|
|
|
commands.sort()
|
|
|
|
|
parsers = args['parser_list']
|
|
|
|
|
parsers.sort()
|
|
|
|
|
return {
|
|
|
|
|
'msg': [
|
|
|
|
|
'%s: known commands: %s' % (
|
|
|
|
|
args['reply_user'], ', '.join(commands)
|
|
|
|
|
),
|
|
|
|
|
'known parsers: %s' % ', '.join(parsers)
|
|
|
|
|
]
|
|
|
|
|
}
|
2014-09-27 05:32:35 +02:00
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
for p in plugin_storage[ptypes.COMMAND] + plugin_storage[ptypes.PARSE]:
|
|
|
|
|
if what == p.plugin_name:
|
|
|
|
|
log.info('sent help for %s' % what)
|
|
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': help for %s %s %s: %s' % (
|
|
|
|
|
'enabled' if plugin_enabled_get(p) else 'disabled',
|
|
|
|
|
'parser' if p.plugin_type == ptypes.PARSE else 'command',
|
|
|
|
|
what, p.plugin_desc
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
log.info('no help found for %s' % what)
|
|
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': no such command: %s' % what
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pluginfunction('plugin', "'disable' or 'enable' plugins", ptypes.COMMAND)
|
|
|
|
|
def command_plugin_activation(argv, **args):
|
|
|
|
|
if not argv:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
command = argv[0]
|
|
|
|
|
plugin = argv[1] if len(argv) > 1 else None
|
|
|
|
|
|
|
|
|
|
if command not in ('enable', 'disable'):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
log.info('plugin activation plugin called')
|
|
|
|
|
|
|
|
|
|
if not plugin:
|
|
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': no plugin given'
|
|
|
|
|
}
|
|
|
|
|
elif command_plugin_activation.plugin_name == plugin:
|
|
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': not allowed'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for p in plugin_storage[ptypes.COMMAND] + plugin_storage[ptypes.PARSE]:
|
|
|
|
|
if p.plugin_name == plugin:
|
|
|
|
|
plugin_enabled_set(p, 'enable' == command)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': %sd %s' % (
|
|
|
|
|
command, plugin
|
|
|
|
|
)
|
|
|
|
|
}
|
2015-06-20 14:18:50 +02:00
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': unknown plugin %s' % plugin
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pluginfunction('list', 'list plugin and parser status', ptypes.COMMAND)
|
|
|
|
|
def command_list(argv, **args):
|
|
|
|
|
log.info('list plugin called')
|
|
|
|
|
|
|
|
|
|
if 'enabled' in argv and 'disabled' in argv:
|
|
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ": both 'enabled' and 'disabled' makes no sense"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# if not given, assume both
|
|
|
|
|
if 'command' not in argv and 'parser' not in argv:
|
|
|
|
|
argv.append('command')
|
|
|
|
|
argv.append('parser')
|
|
|
|
|
|
|
|
|
|
out_command = []
|
|
|
|
|
out_parser = []
|
|
|
|
|
if 'command' in argv:
|
|
|
|
|
out_command = plugin_storage[ptypes.COMMAND]
|
|
|
|
|
if 'parser' in argv:
|
|
|
|
|
out_parser = plugin_storage[ptypes.PARSE]
|
|
|
|
|
if 'enabled' in argv:
|
|
|
|
|
out_command = [p for p in out_command if plugin_enabled_get(p)]
|
|
|
|
|
out_parser = [p for p in out_parser if plugin_enabled_get(p)]
|
|
|
|
|
if 'disabled' in argv:
|
|
|
|
|
out_command = [p for p in out_command if not plugin_enabled_get(p)]
|
|
|
|
|
out_parser = [p for p in out_parser if not plugin_enabled_get(p)]
|
|
|
|
|
|
|
|
|
|
msg = [args['reply_user'] + ': list of plugins:']
|
|
|
|
|
|
|
|
|
|
if out_command:
|
|
|
|
|
msg.append('commands: %s' % ', '.join([p.plugin_name for p in out_command]))
|
|
|
|
|
if out_parser:
|
|
|
|
|
msg.append('parsers: %s' % ', '.join([p.plugin_name for p in out_parser]))
|
|
|
|
|
return {'msg': msg}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pluginfunction('reset-jobs', "reset joblist", ptypes.COMMAND, ratelimit_class=RATE_NO_LIMIT)
|
|
|
|
|
def reset_jobs(argv, **args):
|
|
|
|
|
if args['reply_user'] != config.conf_get('bot_owner'):
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
for event in events.event_list.queue:
|
|
|
|
|
events.event_list.cancel(event)
|
|
|
|
|
|
|
|
|
|
return {'msg': 'done.'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pluginfunction('version', 'prints version', ptypes.COMMAND)
|
2015-02-09 03:46:17 +01:00
|
|
|
def command_version(argv, **args):
|
2015-11-30 19:17:40 +01:00
|
|
|
log.info('sent version string')
|
|
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + (''': I'm running ''' + VERSION)
|
|
|
|
|
}
|
2014-09-27 05:32:35 +02:00
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('uptime', 'prints uptime', ptypes.COMMAND)
|
2016-01-01 20:33:22 +01:00
|
|
|
def command_uptime(argv, **args):
|
|
|
|
|
u = int(config.runtimeconf_get('start_time') + time.time())
|
|
|
|
|
plural_uptime = 's'
|
|
|
|
|
plural_request = 's'
|
|
|
|
|
|
|
|
|
|
if 1 == u:
|
|
|
|
|
plural_uptime = ''
|
|
|
|
|
if 1 == int(config.runtimeconf_get('request_counter')):
|
|
|
|
|
plural_request = ''
|
|
|
|
|
|
|
|
|
|
log.info('sent statistics')
|
|
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + (''': happily serving for %d second%s, %d request%s so far.''' % (
|
|
|
|
|
u, plural_uptime, int(config.runtimeconf_get('request_counter')), plural_request))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('info', 'prints info message', ptypes.COMMAND)
|
2016-01-01 20:33:22 +01:00
|
|
|
def command_info(argv, **args):
|
|
|
|
|
log.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.
|
|
|
|
|
To make me exit immediately, highlight me with 'hangup' in the message
|
|
|
|
|
(emergency only, please). For other commands, highlight me with 'help'.''' % (
|
|
|
|
|
config.conf_get('bot_owner')))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('ping', 'sends pong', ptypes.COMMAND, ratelimit_class=RATE_INTERACTIVE)
|
2016-01-01 20:33:22 +01:00
|
|
|
def command_ping(argv, **args):
|
|
|
|
|
rnd = random.randint(0, 3) # 1:4
|
|
|
|
|
if 0 == rnd:
|
|
|
|
|
msg = args['reply_user'] + ''': peng (You're dead now.)'''
|
|
|
|
|
log.info('sent pong (variant)')
|
|
|
|
|
elif 1 == rnd:
|
|
|
|
|
msg = args['reply_user'] + ''': I don't like you, leave me alone.'''
|
|
|
|
|
log.info('sent pong (dontlike)')
|
|
|
|
|
else:
|
|
|
|
|
msg = args['reply_user'] + ''': pong'''
|
|
|
|
|
log.info('sent pong')
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'msg': msg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('klammer', 'prints an anoying paper clip aka. Karl Klammer', ptypes.COMMAND,
|
2015-11-30 19:17:40 +01:00
|
|
|
ratelimit_class=RATE_FUN | RATE_GLOBAL)
|
2015-02-09 03:46:17 +01:00
|
|
|
def command_klammer(argv, **args):
|
2015-11-30 19:17:40 +01:00
|
|
|
log.info('sent karl klammer')
|
|
|
|
|
return {
|
|
|
|
|
'msg': (
|
|
|
|
|
args['reply_user'] + ',',
|
|
|
|
|
r''' _, Was moechten''',
|
|
|
|
|
r'''( _\_ Sie tun?''',
|
|
|
|
|
r''' \0 O\ ''',
|
|
|
|
|
r''' \\ \\ [ ] ja ''',
|
|
|
|
|
r''' \`' ) [ ] noe''',
|
|
|
|
|
r''' `'' '''
|
|
|
|
|
)
|
|
|
|
|
}
|
2014-11-17 19:49:02 +01:00
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('terminate', 'hidden prototype', ptypes.COMMAND, ratelimit_class=RATE_FUN | RATE_GLOBAL)
|
2016-01-01 20:33:22 +01:00
|
|
|
def command_terminate(argv, **args):
|
|
|
|
|
return {
|
|
|
|
|
'msg': 'insufficient power supply, please connect fission module'
|
2015-11-30 19:17:40 +01:00
|
|
|
}
|
2014-09-27 03:40:27 +02:00
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('source', 'prints git URL', ptypes.COMMAND)
|
2015-11-20 21:07:48 +01:00
|
|
|
def command_source(argv, **_):
|
2015-11-30 19:17:40 +01:00
|
|
|
log.info('sent source URL')
|
|
|
|
|
return {
|
2015-12-20 15:24:42 +01:00
|
|
|
'msg': 'My source code can be found at %s' % config.conf_get('src-url')
|
2015-11-30 19:17:40 +01:00
|
|
|
}
|
2014-09-27 05:32:35 +02:00
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('unikot', 'prints an unicode string', ptypes.COMMAND, ratelimit_class=RATE_FUN | RATE_GLOBAL)
|
2016-01-01 20:33:22 +01:00
|
|
|
def command_unicode(argv, **args):
|
|
|
|
|
log.info('sent some unicode')
|
|
|
|
|
return {
|
|
|
|
|
'msg': (
|
|
|
|
|
args['reply_user'] + ''', here's some''',
|
|
|
|
|
'''┌────────┐''',
|
|
|
|
|
'''│Unicode!│''',
|
|
|
|
|
'''└────────┘'''
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('dice', 'rolls a dice, optional N times', ptypes.COMMAND, ratelimit_class=RATE_INTERACTIVE)
|
2015-02-06 01:21:53 +01:00
|
|
|
def command_dice(argv, **args):
|
2015-11-30 19:17:40 +01:00
|
|
|
try:
|
2016-01-01 20:54:19 +01:00
|
|
|
count = 1 if not argv else int(argv[0])
|
2015-11-30 19:17:40 +01:00
|
|
|
except ValueError as e:
|
|
|
|
|
return {
|
|
|
|
|
'msg': '%s: dice: error when parsing int(%s): %s' % (
|
2016-01-01 20:33:22 +01:00
|
|
|
args['reply_user'], argv[0], str(e)
|
2015-11-30 19:17:40 +01:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if 0 >= count or 5 <= count:
|
|
|
|
|
return {
|
|
|
|
|
'msg': '%s: dice: invalid arguments (0 < N < 5)' % args['reply_user']
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dice_char = ['◇', '⚀', '⚁', '⚂', '⚃', '⚄', '⚅']
|
|
|
|
|
|
|
|
|
|
msg = 'rolling %s for %s:' % (
|
|
|
|
|
'a dice' if 1 == count else '%d dices' % count, args['reply_user']
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for i in range(count):
|
2015-12-20 15:24:42 +01:00
|
|
|
if args['reply_user'] in config.conf_get('enhanced-random-user'):
|
2015-11-30 19:17:40 +01:00
|
|
|
rnd = 0 # this might confuse users. good.
|
|
|
|
|
log.info('sent random (enhanced)')
|
|
|
|
|
else:
|
|
|
|
|
rnd = random.randint(1, 6)
|
|
|
|
|
log.info('sent random')
|
|
|
|
|
|
|
|
|
|
# the \u200b chars ('ZERO WIDTH SPACE') avoid interpreting stuff as smileys
|
|
|
|
|
# by some strange clients
|
|
|
|
|
msg += ' %s (\u200b%d\u200b)' % (dice_char[rnd], rnd)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'msg': msg
|
|
|
|
|
}
|
2014-09-27 03:40:27 +02:00
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('choose', 'chooses randomly between arguments', ptypes.COMMAND, ratelimit_class=RATE_INTERACTIVE)
|
2015-10-18 19:49:44 +02:00
|
|
|
def command_choose(argv, **args):
|
2016-01-01 20:33:22 +01:00
|
|
|
alternatives = argv
|
2016-07-08 21:08:38 +02:00
|
|
|
binary = (
|
|
|
|
|
('Yes', 'Yeah!', 'Ok!', 'Ay!', 'Great!'),
|
|
|
|
|
('No', 'Naah', 'Meh', 'Nay', 'You stupid?'),
|
|
|
|
|
('Maybe', 'Dunno', 'I don\'t care')
|
|
|
|
|
)
|
2016-05-27 21:31:31 +02:00
|
|
|
|
2016-05-27 21:48:33 +02:00
|
|
|
# single or no choice
|
|
|
|
|
if len(alternatives) < 2:
|
2015-11-30 19:17:40 +01:00
|
|
|
return {
|
2016-07-08 21:08:38 +02:00
|
|
|
'msg': '{}: {}.'.format(args['reply_user'], random.choice(random.choice(binary)))
|
2016-05-27 21:48:33 +02:00
|
|
|
}
|
|
|
|
|
elif 'choose' not in alternatives:
|
|
|
|
|
choice = random.choice(alternatives)
|
|
|
|
|
return {
|
|
|
|
|
'msg': '%s: I prefer %s!' % (args['reply_user'], choice)
|
2015-11-30 19:17:40 +01:00
|
|
|
}
|
2015-10-18 19:49:44 +02:00
|
|
|
|
2016-05-27 21:31:31 +02:00
|
|
|
def choose_between(options):
|
|
|
|
|
responses = []
|
|
|
|
|
current_choices = []
|
|
|
|
|
|
|
|
|
|
for item in options:
|
|
|
|
|
if item == 'choose':
|
|
|
|
|
if len(current_choices) < 2:
|
2016-07-08 21:08:38 +02:00
|
|
|
responses.append(random.choice(random.choice(binary)))
|
2016-05-27 21:31:31 +02:00
|
|
|
else:
|
|
|
|
|
responses.append(random.choice(current_choices))
|
|
|
|
|
current_choices = []
|
|
|
|
|
else:
|
|
|
|
|
current_choices.append(item)
|
|
|
|
|
if len(current_choices) < 2:
|
2016-07-08 21:08:38 +02:00
|
|
|
responses.append(random.choice(random.choice(binary)))
|
2016-05-27 21:31:31 +02:00
|
|
|
else:
|
|
|
|
|
responses.append(random.choice(current_choices))
|
|
|
|
|
return responses
|
|
|
|
|
|
|
|
|
|
log.info('sent multiple random choices')
|
2015-11-30 19:17:40 +01:00
|
|
|
return {
|
2016-05-27 21:31:31 +02:00
|
|
|
'msg': '%s: My choices are: %s!' % (args['reply_user'], ', '.join(choose_between(alternatives)))
|
2015-11-30 19:17:40 +01:00
|
|
|
}
|
2015-10-18 19:49:44 +02:00
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
|
2015-12-26 13:50:21 +01:00
|
|
|
@pluginfunction('teatimer', 'sets a tea timer to $1 or currently %d seconds' % config.conf_get('tea_steep_time'),
|
2016-04-05 14:18:22 +02:00
|
|
|
ptypes.COMMAND)
|
2015-02-09 03:46:17 +01:00
|
|
|
def command_teatimer(argv, **args):
|
2015-12-20 15:24:42 +01:00
|
|
|
steep = config.conf_get('tea_steep_time')
|
2015-11-30 19:17:40 +01:00
|
|
|
|
2016-01-01 20:33:22 +01:00
|
|
|
if argv:
|
2015-11-30 19:17:40 +01:00
|
|
|
try:
|
2016-01-01 20:33:22 +01:00
|
|
|
steep = int(argv[0])
|
|
|
|
|
except ValueError as e:
|
2015-11-30 19:17:40 +01:00
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': error when parsing int(%s): %s' % (
|
2016-01-01 20:33:22 +01:00
|
|
|
argv[0], str(e)
|
2015-11-30 19:17:40 +01:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ready = time.time() + steep
|
|
|
|
|
|
|
|
|
|
try:
|
2015-12-20 21:15:16 +01:00
|
|
|
log.info('tea timer set to %s' % time.strftime('%Y-%m-%d %H:%M', time.localtime(ready)))
|
2015-11-30 19:17:40 +01:00
|
|
|
except (ValueError, OverflowError) as e:
|
|
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': time format error: ' + str(e)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': Tea timer set to %s' % time.strftime(
|
2015-12-20 21:15:16 +01:00
|
|
|
'%Y-%m-%d %H:%M', time.localtime(ready)
|
2015-11-30 19:17:40 +01:00
|
|
|
),
|
|
|
|
|
'event': {
|
|
|
|
|
'time': ready,
|
|
|
|
|
'msg': (args['reply_user'] + ': Your tea is ready!')
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-16 08:48:03 +01:00
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('unicode-lookup', 'search unicode characters', ptypes.COMMAND,
|
2015-12-24 23:20:36 +01:00
|
|
|
ratelimit_class=RATE_INTERACTIVE)
|
|
|
|
|
def command_unicode_lookup(argv, **args):
|
2016-01-01 20:33:22 +01:00
|
|
|
if not argv:
|
2015-12-24 23:20:36 +01:00
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': usage: decode {single character}'
|
|
|
|
|
}
|
2016-01-01 20:33:22 +01:00
|
|
|
search_words = argv
|
2015-12-24 23:20:36 +01:00
|
|
|
|
|
|
|
|
import unicode
|
|
|
|
|
|
|
|
|
|
characters = {
|
|
|
|
|
k: v for k, v in unicode.characters.items() if
|
2015-12-31 15:45:11 +01:00
|
|
|
all([word.lower() in v.lower().split() for word in search_words])
|
2015-12-24 23:20:36 +01:00
|
|
|
}
|
|
|
|
|
lines = []
|
|
|
|
|
|
|
|
|
|
for code, name in characters.items():
|
2015-12-24 23:46:39 +01:00
|
|
|
char = chr(int(code, 16))
|
|
|
|
|
lines.append("Character \"{}\" with code {} is named \"{}\"".format(char, code, name))
|
2015-12-31 15:45:11 +01:00
|
|
|
if len(lines) > 29:
|
|
|
|
|
lines.append("warning: limit (30) reached.")
|
2015-12-24 23:20:36 +01:00
|
|
|
break
|
|
|
|
|
|
2015-12-31 15:45:11 +01:00
|
|
|
if not lines:
|
|
|
|
|
return {
|
|
|
|
|
'msg': 'No match.'
|
|
|
|
|
}
|
|
|
|
|
elif len(lines) > 3:
|
|
|
|
|
channel = 'priv_msg'
|
|
|
|
|
else:
|
|
|
|
|
channel = 'msg'
|
2015-12-24 23:20:36 +01:00
|
|
|
return {
|
2015-12-31 15:45:11 +01:00
|
|
|
channel: lines
|
2015-12-24 23:20:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('decode', 'prints the long description of an unicode character', ptypes.COMMAND,
|
2015-11-30 19:17:40 +01:00
|
|
|
ratelimit_class=RATE_INTERACTIVE)
|
2015-02-09 03:46:17 +01:00
|
|
|
def command_decode(argv, **args):
|
2016-01-01 20:33:22 +01:00
|
|
|
if not argv:
|
2015-11-30 19:17:40 +01:00
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': usage: decode {single character}'
|
|
|
|
|
}
|
2016-01-01 20:33:22 +01:00
|
|
|
log.info('decode called for %s' % argv[0])
|
2015-11-30 19:17:40 +01:00
|
|
|
out = []
|
2016-01-01 20:33:22 +01:00
|
|
|
for i, char in enumerate(argv[0]):
|
2015-11-30 19:17:40 +01:00
|
|
|
if i > 9:
|
|
|
|
|
out.append('... limit reached.')
|
|
|
|
|
break
|
2015-07-07 00:02:17 +02:00
|
|
|
|
2015-11-30 19:17:40 +01:00
|
|
|
char_esc = str(char.encode('unicode_escape'))[3:-1]
|
2015-07-07 00:02:17 +02:00
|
|
|
|
2015-11-30 19:17:40 +01:00
|
|
|
if 0 == len(char_esc):
|
|
|
|
|
char_esc = ''
|
|
|
|
|
else:
|
|
|
|
|
char_esc = ' (%s)' % char_esc
|
2015-07-07 00:32:05 +02:00
|
|
|
|
2015-11-30 19:17:40 +01:00
|
|
|
try:
|
|
|
|
|
uni_name = unicodedata.name(char)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
log.info('decode(%s) failed: %s' % (char, e))
|
|
|
|
|
out.append("can't decode %s%s: %s" % (char, char_esc, e))
|
|
|
|
|
continue
|
2015-07-07 00:02:17 +02:00
|
|
|
|
2015-11-30 19:17:40 +01:00
|
|
|
out.append('%s%s is called "%s"' % (char, char_esc, uni_name))
|
2015-07-07 00:02:17 +02:00
|
|
|
|
2015-11-30 19:17:40 +01:00
|
|
|
if 1 == len(out):
|
|
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': %s' % out[0]
|
|
|
|
|
}
|
|
|
|
|
else:
|
|
|
|
|
return {
|
2016-01-01 20:33:22 +01:00
|
|
|
'msg': [args['reply_user'] + ': decoding %s:' % argv[0]] + out
|
2015-11-30 19:17:40 +01:00
|
|
|
}
|
2014-12-16 08:48:03 +01:00
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('show-blacklist', 'show the current URL blacklist, optionally filtered', ptypes.COMMAND)
|
2015-02-09 03:46:17 +01:00
|
|
|
def command_show_blacklist(argv, **args):
|
2015-11-30 19:17:40 +01:00
|
|
|
log.info('sent URL blacklist')
|
2016-01-01 20:33:22 +01:00
|
|
|
if argv:
|
|
|
|
|
urlpart = argv[0]
|
|
|
|
|
else:
|
|
|
|
|
urlpart = None
|
2015-02-06 01:21:53 +01:00
|
|
|
|
2015-11-30 19:17:40 +01:00
|
|
|
return {
|
|
|
|
|
'msg': [
|
|
|
|
|
args['reply_user'] + ': URL blacklist%s: ' % (
|
2016-01-01 20:33:22 +01:00
|
|
|
'' if not urlpart else ' (limited to %s)' % urlpart
|
2015-11-30 19:17:40 +01:00
|
|
|
)
|
|
|
|
|
] + [
|
2016-01-01 20:33:22 +01:00
|
|
|
b for b in config.runtime_config_store['url_blacklist'].values() if not urlpart or urlpart in b
|
2015-11-30 19:17:40 +01:00
|
|
|
]
|
|
|
|
|
}
|
2014-11-28 19:13:45 +01:00
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
|
2015-02-09 03:46:17 +01:00
|
|
|
def usersetting_get(argv, args):
|
2015-11-30 19:17:40 +01:00
|
|
|
arg_user = args['reply_user']
|
2016-01-01 20:33:22 +01:00
|
|
|
arg_key = argv[0]
|
2014-12-14 03:41:57 +01:00
|
|
|
|
2015-12-20 15:24:42 +01:00
|
|
|
if arg_user not in config.runtime_config_store['user_pref']:
|
2015-11-30 19:17:40 +01:00
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': user key not found'
|
|
|
|
|
}
|
2014-12-14 03:41:57 +01:00
|
|
|
|
2015-11-30 19:17:40 +01:00
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': %s == %s' % (
|
|
|
|
|
arg_key,
|
2015-12-20 15:24:42 +01:00
|
|
|
'on' if config.runtime_config_store['user_pref'][arg_user][arg_key] else 'off'
|
2015-11-30 19:17:40 +01:00
|
|
|
)
|
|
|
|
|
}
|
2014-12-14 03:41:57 +01:00
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('set', 'modify a user setting', ptypes.COMMAND, ratelimit_class=RATE_NO_LIMIT)
|
|
|
|
|
@config.config_locked
|
2015-02-09 03:46:17 +01:00
|
|
|
def command_usersetting(argv, **args):
|
2015-11-30 19:17:40 +01:00
|
|
|
settings = ['spoiler']
|
|
|
|
|
arg_user = args['reply_user']
|
2016-01-01 20:33:22 +01:00
|
|
|
arg_key = argv[0] if len(argv) > 0 else None
|
|
|
|
|
arg_val = argv[1] if len(argv) > 1 else None
|
2014-12-14 03:41:57 +01:00
|
|
|
|
2015-11-30 19:17:40 +01:00
|
|
|
if arg_key not in settings:
|
|
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': known settings: ' + (', '.join(settings))
|
|
|
|
|
}
|
2014-12-14 01:27:13 +01:00
|
|
|
|
2015-11-30 19:17:40 +01:00
|
|
|
if arg_val not in ['on', 'off', None]:
|
|
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': possible values for %s: on, off' % arg_key
|
|
|
|
|
}
|
2014-12-14 01:27:13 +01:00
|
|
|
|
2015-11-30 19:17:40 +01:00
|
|
|
if not arg_val:
|
|
|
|
|
# display current value
|
|
|
|
|
return usersetting_get(argv, args)
|
2014-12-14 01:27:13 +01:00
|
|
|
|
2015-12-20 15:24:42 +01:00
|
|
|
if arg_user not in config.runtime_config_store['user_pref']:
|
|
|
|
|
config.runtime_config_store['user_pref'][arg_user] = {}
|
2014-12-14 03:41:57 +01:00
|
|
|
|
2015-12-20 15:24:42 +01:00
|
|
|
config.runtime_config_store['user_pref'][arg_user][arg_key] = 'on' == arg_val
|
2014-12-14 03:41:57 +01:00
|
|
|
|
2015-12-21 10:41:58 +01:00
|
|
|
config.runtimeconf_persist()
|
2014-12-14 03:41:57 +01:00
|
|
|
|
2015-11-30 19:17:40 +01:00
|
|
|
# display value written to db
|
|
|
|
|
return usersetting_get(argv, args)
|
2014-12-14 01:27:13 +01:00
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('wp-en', 'crawl the english Wikipedia', ptypes.COMMAND)
|
2015-02-09 03:46:17 +01:00
|
|
|
def command_wp_en(argv, **args):
|
2015-11-30 19:17:40 +01:00
|
|
|
return command_wp(argv, lang='en', **args)
|
2015-02-05 01:16:17 +01:00
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('wp', 'crawl the german Wikipedia', ptypes.COMMAND)
|
2015-02-09 03:46:17 +01:00
|
|
|
def command_wp(argv, lang='de', **args):
|
2016-01-01 20:33:22 +01:00
|
|
|
query = ' '.join(argv)
|
2015-11-30 19:17:40 +01:00
|
|
|
|
|
|
|
|
if query == '':
|
|
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': no query given'
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-03 13:19:49 +01:00
|
|
|
apiparams = {
|
2015-11-30 19:17:40 +01:00
|
|
|
'action': 'query',
|
2016-01-03 13:19:49 +01:00
|
|
|
'prop': 'extracts|info',
|
2015-11-30 19:17:40 +01:00
|
|
|
'explaintext': '',
|
|
|
|
|
'redirects': '',
|
|
|
|
|
'exsentences': 2,
|
|
|
|
|
'continue': '',
|
|
|
|
|
'format': 'json',
|
2016-01-03 13:19:49 +01:00
|
|
|
'titles': query,
|
|
|
|
|
'inprop': 'url'
|
2015-11-30 19:17:40 +01:00
|
|
|
}
|
2016-01-03 13:19:49 +01:00
|
|
|
apiurl = 'https://%s.wikipedia.org/w/api.php' % (lang)
|
2015-11-30 19:17:40 +01:00
|
|
|
|
|
|
|
|
log.info('fetching %s' % apiurl)
|
|
|
|
|
|
|
|
|
|
try:
|
2016-01-03 13:19:49 +01:00
|
|
|
response = requests.get(apiurl, params=apiparams).json()
|
|
|
|
|
|
|
|
|
|
page = next(iter(response['query']['pages'].values()))
|
|
|
|
|
short = page.get('extract')
|
|
|
|
|
link = page.get('canonicalurl')
|
2015-11-30 19:17:40 +01:00
|
|
|
except Exception as e:
|
|
|
|
|
log.info('wp(%s) failed: %s, %s' % (query, e, traceback.format_exc()))
|
|
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': something failed: %s' % e
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-03 13:19:49 +01:00
|
|
|
if short:
|
2015-11-30 19:17:40 +01:00
|
|
|
return {
|
|
|
|
|
'msg': args['reply_user'] + ': %s (<%s>)' % (
|
|
|
|
|
short if short.strip() else '(nix)', link
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
elif 'missing' in page:
|
|
|
|
|
return {
|
|
|
|
|
'msg': 'Article "%s" not found' % page.get('title', query)
|
|
|
|
|
}
|
|
|
|
|
else:
|
|
|
|
|
return {
|
|
|
|
|
'msg': 'json data seem to be broken'
|
|
|
|
|
}
|
2015-02-05 00:48:02 +01:00
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
|
2016-01-04 14:27:02 +01:00
|
|
|
@pluginfunction(
|
|
|
|
|
'dsa-watcher',
|
2016-04-05 14:18:22 +02:00
|
|
|
'automatically crawls for newly published Debian Security Announces', ptypes.COMMAND,
|
2016-01-04 14:27:02 +01:00
|
|
|
ratelimit_class=RATE_NO_SILENCE, enabled=True)
|
|
|
|
|
def command_dsa_watcher(argv=None, **_):
|
|
|
|
|
"""
|
|
|
|
|
TODO: rewrite so that a last_dsa_date is used instead,
|
|
|
|
|
then all DSAs since then printed and the date set to now()
|
|
|
|
|
:param argv:
|
|
|
|
|
:param _:
|
|
|
|
|
"""
|
|
|
|
|
log.debug("Called command_dsa_watcher")
|
|
|
|
|
|
|
|
|
|
def get_id_from_about_string(about):
|
|
|
|
|
return int(about.split('/')[-1].split('-')[1])
|
|
|
|
|
|
|
|
|
|
def get_dsa_list(after):
|
|
|
|
|
"""
|
|
|
|
|
Get a list of dsa items in form of id and package, retrieved from the RSS feed
|
|
|
|
|
:param after: optional integer to filter on (only DSA's after that will be returned)
|
|
|
|
|
:returns list of id, package (with DSA prefix)
|
|
|
|
|
"""
|
|
|
|
|
nsmap = {
|
|
|
|
|
"purl": "http://purl.org/rss/1.0/",
|
|
|
|
|
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
|
|
|
|
|
}
|
|
|
|
|
dsa_response = requests.get("https://www.debian.org/security/dsa-long")
|
|
|
|
|
xmldoc = etree.fromstring(dsa_response.content)
|
|
|
|
|
dsa_about_list = xmldoc.xpath('//purl:item/@rdf:about', namespaces=nsmap)
|
|
|
|
|
for dsa_about in reversed(dsa_about_list):
|
|
|
|
|
dsa_id = get_id_from_about_string(dsa_about)
|
2016-01-04 14:42:46 +01:00
|
|
|
title = xmldoc.xpath(
|
2016-05-27 21:31:31 +02:00
|
|
|
'//purl:item[@rdf:about="{}"]/purl:title/text()'.format(dsa_about),
|
|
|
|
|
namespaces=nsmap
|
2016-01-04 14:42:46 +01:00
|
|
|
)[0]
|
2016-01-04 14:27:02 +01:00
|
|
|
if after and dsa_id <= after:
|
|
|
|
|
continue
|
|
|
|
|
else:
|
2016-01-04 14:42:46 +01:00
|
|
|
yield dsa_id, str(title).replace(' - security update', '')
|
2016-01-04 14:27:02 +01:00
|
|
|
|
|
|
|
|
out = []
|
|
|
|
|
last_dsa = config.runtimeconf_deepget('plugins.dsa-watcher.last_dsa')
|
|
|
|
|
log.debug('Searching for DSA after ID {}'.format(last_dsa))
|
|
|
|
|
for dsa, package in get_dsa_list(after=last_dsa):
|
|
|
|
|
url = 'https://security-tracker.debian.org/tracker/DSA-%d-1' % dsa
|
|
|
|
|
|
|
|
|
|
msg = 'new Debian Security Announce found ({}): {}'.format(package, url)
|
|
|
|
|
out.append(msg)
|
|
|
|
|
|
|
|
|
|
last_dsa = dsa
|
|
|
|
|
|
|
|
|
|
config.runtime_config_store['plugins']['dsa-watcher']['last_dsa'] = last_dsa
|
|
|
|
|
config.runtimeconf_persist()
|
|
|
|
|
crawl_at = time.time() + config.runtimeconf_deepget('plugins.dsa-watcher.interval')
|
|
|
|
|
|
|
|
|
|
msg = 'next crawl set to %s' % time.strftime('%Y-%m-%d %H:%M', time.localtime(crawl_at))
|
|
|
|
|
out.append(msg)
|
|
|
|
|
return {
|
|
|
|
|
'event': {
|
|
|
|
|
'time': crawl_at,
|
|
|
|
|
'command': (command_dsa_watcher, ([],))
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-26 20:43:25 +01:00
|
|
|
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction("provoke-bots", "search for other bots", ptypes.COMMAND)
|
2015-11-28 02:21:49 +01:00
|
|
|
def provoke_bots(argv, **args):
|
2016-01-01 19:47:07 +01:00
|
|
|
return {
|
|
|
|
|
'msg': 'Searching for other less intelligent lifeforms... skynet? You here?'
|
|
|
|
|
}
|
2015-11-28 02:21:49 +01:00
|
|
|
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction("remove-from-botlist", "remove a user from the botlist", ptypes.COMMAND)
|
2015-11-28 15:38:33 +01:00
|
|
|
def remove_from_botlist(argv, **args):
|
2016-01-01 20:33:22 +01:00
|
|
|
if not argv:
|
2015-11-30 19:17:40 +01:00
|
|
|
return {'msg': "wrong number of arguments!"}
|
2016-01-01 20:33:22 +01:00
|
|
|
suspect = argv[0]
|
2015-11-28 15:38:33 +01:00
|
|
|
|
2016-01-01 20:33:22 +01:00
|
|
|
if args['reply_user'] != config.conf_get('bot_owner') and args['reply_user'] != suspect:
|
2015-12-21 17:03:38 +01:00
|
|
|
return {'msg': "only %s or the bot may do this!" % config.conf_get('bot_owner')}
|
2015-11-28 15:38:33 +01:00
|
|
|
|
2016-01-01 20:33:22 +01:00
|
|
|
if suspect in config.runtime_config_store['other_bots']:
|
|
|
|
|
config.runtime_config_store['other_bots'].remove(suspect)
|
2015-12-21 10:41:58 +01:00
|
|
|
config.runtimeconf_persist()
|
2016-01-01 20:33:22 +01:00
|
|
|
return {'msg': '%s was removed from the botlist.' % suspect}
|
2015-11-30 19:17:40 +01:00
|
|
|
else:
|
|
|
|
|
return False
|
2015-11-28 15:38:33 +01:00
|
|
|
|
2015-11-28 02:21:49 +01:00
|
|
|
|
2016-05-27 20:38:40 +02:00
|
|
|
@pluginfunction("add-to-botlist", "add a user to the botlist", ptypes.COMMAND, enabled=False)
|
2015-12-21 16:24:56 +01:00
|
|
|
def add_to_botlist(argv, **args):
|
2016-05-27 20:38:40 +02:00
|
|
|
return {'msg': 'feature disabled until channel separation'}
|
2016-01-01 20:33:22 +01:00
|
|
|
if not argv:
|
2015-12-21 16:24:56 +01:00
|
|
|
return {'msg': "wrong number of arguments!"}
|
2016-01-01 20:33:22 +01:00
|
|
|
suspect = argv[0]
|
2015-12-21 16:24:56 +01:00
|
|
|
|
|
|
|
|
if args['reply_user'] != config.conf_get('bot_owner'):
|
|
|
|
|
return {'msg': "only %s may do this!" % config.conf_get('bot_owner')}
|
|
|
|
|
|
2016-01-01 20:33:22 +01:00
|
|
|
if suspect not in config.runtime_config_store['other_bots']:
|
|
|
|
|
config.runtime_config_store['other_bots'].append(suspect)
|
2015-12-21 16:24:56 +01:00
|
|
|
config.runtimeconf_persist()
|
2016-01-01 20:33:22 +01:00
|
|
|
return {'msg': '%s was added to the botlist.' % suspect}
|
2015-12-21 16:24:56 +01:00
|
|
|
else:
|
2016-01-01 20:33:22 +01:00
|
|
|
return {'msg': '%s is already in the botlist.' % suspect}
|
2015-12-21 16:24:56 +01:00
|
|
|
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction("set-status", "set bot status", ptypes.COMMAND)
|
2015-11-28 02:21:49 +01:00
|
|
|
def set_status(argv, **args):
|
2016-01-01 20:33:22 +01:00
|
|
|
if not argv:
|
2015-11-30 19:17:40 +01:00
|
|
|
return
|
2016-01-01 20:33:22 +01:00
|
|
|
else:
|
|
|
|
|
command = argv[0]
|
2015-11-30 19:17:40 +01:00
|
|
|
|
2016-01-01 20:33:22 +01:00
|
|
|
if command == 'mute' and args['reply_user'] == config.conf_get('bot_owner'):
|
2015-11-30 19:17:40 +01:00
|
|
|
return {
|
|
|
|
|
'presence': {
|
|
|
|
|
'status': 'xa',
|
2016-01-04 14:27:02 +01:00
|
|
|
'msg': 'I\'m muted now. You can unmute me with "%s: set_status unmute"' % config.conf_get(
|
|
|
|
|
"bot_nickname")
|
2015-11-30 19:17:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
2016-01-01 20:33:22 +01:00
|
|
|
elif command == 'unmute' and args['reply_user'] == config.conf_get('bot_owner'):
|
2015-11-30 19:17:40 +01:00
|
|
|
return {
|
|
|
|
|
'presence': {
|
|
|
|
|
'status': None,
|
|
|
|
|
'msg': ''
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-28 02:21:49 +01:00
|
|
|
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('save-config', "save config", ptypes.COMMAND, ratelimit_class=RATE_NO_LIMIT)
|
2015-12-21 16:24:56 +01:00
|
|
|
def save_config(argv, **args):
|
|
|
|
|
if args['reply_user'] != config.conf_get('bot_owner'):
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
config.runtime_config_store.write()
|
|
|
|
|
return {'msg': 'done.'}
|
|
|
|
|
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('flausch', "make people flauschig", ptypes.COMMAND, ratelimit_class=RATE_FUN)
|
2015-12-20 21:15:16 +01:00
|
|
|
def flausch(argv, **args):
|
2016-01-01 20:33:22 +01:00
|
|
|
if not argv:
|
2015-12-20 21:15:16 +01:00
|
|
|
return
|
|
|
|
|
return {
|
2016-01-01 20:33:22 +01:00
|
|
|
'msg': '{}: *flausch*'.format(argv[0])
|
2015-12-20 21:15:16 +01:00
|
|
|
}
|
|
|
|
|
|
2015-12-21 19:39:09 +01:00
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('slap', "slap people", ptypes.COMMAND, ratelimit_class=RATE_FUN)
|
2016-02-06 10:32:39 +01:00
|
|
|
def slap(argv, **args):
|
|
|
|
|
if not argv:
|
|
|
|
|
return
|
|
|
|
|
return {
|
|
|
|
|
'msg': '/me slaps {}'.format(argv[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('show-runtimeconfig', "show the current runtimeconfig", ptypes.COMMAND, ratelimit_class=RATE_NO_LIMIT)
|
2015-12-21 16:24:56 +01:00
|
|
|
def show_runtimeconfig(argv, **args):
|
|
|
|
|
if args['reply_user'] != config.conf_get('bot_owner'):
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
msg = json.dumps(config.runtime_config_store, indent=4)
|
2015-12-26 23:06:46 +01:00
|
|
|
return {'priv_msg': msg}
|
2015-12-21 16:24:56 +01:00
|
|
|
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('reload-runtimeconfig', "reload the runtimeconfig", ptypes.COMMAND, ratelimit_class=RATE_NO_LIMIT)
|
2015-12-21 16:24:56 +01:00
|
|
|
def reload_runtimeconfig(argv, **args):
|
|
|
|
|
if args['reply_user'] != config.conf_get('bot_owner'):
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
config.runtime_config_store.reload()
|
|
|
|
|
return {'msg': 'done'}
|
2015-12-26 20:43:25 +01:00
|
|
|
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('snitch', "tell on a spammy user", ptypes.COMMAND)
|
2015-12-26 20:43:25 +01:00
|
|
|
def ignore_user(argv, **args):
|
2016-01-01 20:33:22 +01:00
|
|
|
if not argv:
|
2015-12-26 20:43:25 +01:00
|
|
|
return {'msg': 'syntax: "{}: snitch username"'.format(config.conf_get("bot_nickname"))}
|
|
|
|
|
|
2016-01-04 14:27:02 +01:00
|
|
|
then = time.time() + 15 * 60
|
2016-01-01 20:33:22 +01:00
|
|
|
spammer = argv[0]
|
2015-12-26 20:43:25 +01:00
|
|
|
|
2015-12-26 20:47:37 +01:00
|
|
|
if spammer == config.conf_get("bot_owner"):
|
|
|
|
|
return {
|
|
|
|
|
'msg': 'My owner does not spam, he is just very informative.'
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-26 20:43:25 +01:00
|
|
|
if spammer not in config.runtime_config_store['spammers']:
|
|
|
|
|
config.runtime_config_store['spammers'].append(spammer)
|
|
|
|
|
|
|
|
|
|
def unblock_user(user):
|
|
|
|
|
if user not in config.runtime_config_store['spammers']:
|
|
|
|
|
config.runtime_config_store['spammers'].append(user)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'msg': 'user reported and ignored till {}'.format(time.strftime('%H:%M', time.localtime(then))),
|
|
|
|
|
'event': {
|
|
|
|
|
'time': then,
|
|
|
|
|
'command': (unblock_user, ([spammer],))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('search', 'search the web (using duckduckgo)', ptypes.COMMAND)
|
2016-01-03 13:19:49 +01:00
|
|
|
def search_the_web(argv, **args):
|
|
|
|
|
url = 'http://api.duckduckgo.com/'
|
|
|
|
|
params = dict(
|
2016-01-03 13:22:18 +01:00
|
|
|
q=' '.join(argv),
|
2016-01-03 13:19:49 +01:00
|
|
|
format='json',
|
|
|
|
|
pretty=0,
|
|
|
|
|
no_redirect=1,
|
|
|
|
|
t='jabberbot'
|
|
|
|
|
)
|
|
|
|
|
response = requests.get(url, params=params).json()
|
|
|
|
|
link = response.get('AbstractURL')
|
|
|
|
|
abstract = response.get('Abstract')
|
2016-01-03 13:42:46 +01:00
|
|
|
redirect = response.get('Redirect')
|
|
|
|
|
|
2016-01-03 13:19:49 +01:00
|
|
|
if len(abstract) > 150:
|
|
|
|
|
suffix = '…'
|
|
|
|
|
else:
|
|
|
|
|
suffix = ''
|
|
|
|
|
|
|
|
|
|
if link:
|
|
|
|
|
return {
|
|
|
|
|
'msg': '{}{} ({})'.format(abstract[:150], suffix, link)
|
|
|
|
|
}
|
2016-01-03 13:42:46 +01:00
|
|
|
elif redirect:
|
|
|
|
|
return {
|
|
|
|
|
'msg': 'No direct result found, use {}'.format(redirect)
|
|
|
|
|
}
|
2016-01-03 16:58:53 +01:00
|
|
|
else:
|
|
|
|
|
return {'msg': 'Sorry, no results.'}
|
2016-01-03 13:19:49 +01:00
|
|
|
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('raise', 'only for debugging', ptypes.COMMAND)
|
2015-12-26 20:43:25 +01:00
|
|
|
def raise_an_error(argv, **args):
|
|
|
|
|
if args['reply_user'] == config.conf_get("bot_owner"):
|
|
|
|
|
raise RuntimeError("Exception for debugging")
|
2016-01-08 20:04:35 +01:00
|
|
|
|
|
|
|
|
|
2016-06-22 22:13:51 +02:00
|
|
|
@pluginfunction('repeat', 'repeat the last message', ptypes.COMMAND)
|
2016-01-08 23:55:29 +01:00
|
|
|
def repeat_message(argv, **args):
|
2016-06-22 22:13:51 +02:00
|
|
|
if args['stack']:
|
|
|
|
|
return {
|
|
|
|
|
'msg': args['stack'][-1]['body']
|
|
|
|
|
}
|
2016-01-08 23:55:29 +01:00
|
|
|
|
2016-04-05 18:40:31 +02:00
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('isdown', 'check if a website is reachable', ptypes.COMMAND)
|
2016-01-12 22:44:39 +01:00
|
|
|
def isdown(argv, **args):
|
|
|
|
|
if not argv:
|
|
|
|
|
return
|
|
|
|
|
url = argv[0]
|
|
|
|
|
if 'http' not in url:
|
|
|
|
|
url = 'http://{}'.format(url)
|
|
|
|
|
response = requests.get('http://www.isup.me/{}'.format(urlparse(url).hostname)).text
|
|
|
|
|
if "looks down" in response:
|
|
|
|
|
return {'msg': '{}: {} looks down'.format(args['reply_user'], url)}
|
|
|
|
|
elif "is up" in response:
|
|
|
|
|
return {'msg': '{}: {} looks up'.format(args['reply_user'], url)}
|
2016-01-12 22:48:16 +01:00
|
|
|
elif "site on the interwho" in response:
|
|
|
|
|
return {'msg': '{}: {} does not exist, you\'re trying to fool me?'.format(args['reply_user'], url)}
|
2016-01-23 21:41:46 +01:00
|
|
|
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('poll', 'create a poll', ptypes.COMMAND)
|
2016-01-23 21:41:46 +01:00
|
|
|
def poll(argv, **args):
|
2016-01-28 19:15:48 +01:00
|
|
|
with config.plugin_config('poll') as pollcfg:
|
|
|
|
|
current_poll = pollcfg.get('subject')
|
2016-01-23 21:41:46 +01:00
|
|
|
|
2016-01-28 19:15:48 +01:00
|
|
|
if not argv:
|
|
|
|
|
# return poll info
|
2016-01-23 21:41:46 +01:00
|
|
|
if not current_poll:
|
2016-01-28 19:15:48 +01:00
|
|
|
return {'msg': 'no poll running.'}
|
|
|
|
|
else:
|
|
|
|
|
return {'msg': 'current poll: {}'.format(current_poll)}
|
|
|
|
|
elif len(argv) == 1:
|
|
|
|
|
if argv[0] == 'stop':
|
|
|
|
|
if not current_poll:
|
|
|
|
|
return {'msg': 'no poll to stop.'}
|
|
|
|
|
pollcfg.clear()
|
|
|
|
|
return {'msg': 'stopped the poll "{}"'.format(current_poll)}
|
|
|
|
|
elif argv[0] == 'show_raw':
|
|
|
|
|
if not current_poll:
|
|
|
|
|
return {'msg': 'no poll to show.'}
|
|
|
|
|
return {'msg': 'current poll (raw): {}'.format(str(pollcfg))}
|
|
|
|
|
elif argv[0] == 'show':
|
|
|
|
|
if not current_poll:
|
|
|
|
|
return {'msg': 'no poll to show.'}
|
|
|
|
|
lines = ['current poll: "{}"'.format(current_poll)]
|
|
|
|
|
for option, voters in pollcfg.items():
|
|
|
|
|
if option == 'subject':
|
|
|
|
|
continue
|
|
|
|
|
lines.append('{0: <4} {1}'.format(len(voters), option))
|
|
|
|
|
return {'msg': lines}
|
|
|
|
|
if current_poll and argv[0] in pollcfg:
|
|
|
|
|
user = args['reply_user']
|
|
|
|
|
for option, voters in pollcfg.items():
|
|
|
|
|
if user in voters:
|
|
|
|
|
pollcfg[option].remove(user)
|
|
|
|
|
|
|
|
|
|
pollcfg[argv[0]] = list(set(pollcfg[argv[0]] + [user]))
|
|
|
|
|
return {'msg': 'voted.'}
|
2016-01-23 21:41:46 +01:00
|
|
|
else:
|
2016-01-28 19:15:48 +01:00
|
|
|
subject = argv[0]
|
|
|
|
|
choices = argv[1:]
|
|
|
|
|
if len(choices) == 1:
|
|
|
|
|
return {'msg': 'creating a poll with a single option is "alternativlos"'}
|
|
|
|
|
else:
|
|
|
|
|
if current_poll:
|
|
|
|
|
return {'msg': 'a poll is already running ({})'.format(current_poll)}
|
|
|
|
|
# create an item for each option
|
|
|
|
|
pollcfg['subject'] = subject
|
|
|
|
|
pollcfg.update({k: [] for k in choices})
|
|
|
|
|
return {'msg': 'created the poll.'}
|
2016-01-23 21:41:46 +01:00
|
|
|
|
|
|
|
|
|
2016-04-05 14:18:22 +02:00
|
|
|
@pluginfunction('vote', 'alias for poll', ptypes.COMMAND)
|
2016-01-23 21:41:46 +01:00
|
|
|
def vote(argv, **args):
|
|
|
|
|
return poll(argv, **args)
|