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

173 lines
5.0 KiB
Python
Raw Permalink Normal View History

2016-04-05 14:18:22 +02:00
# -*- coding: utf-8 -*-
2015-12-26 13:50:21 +01:00
import logging
2016-04-05 14:18:22 +02:00
2015-12-26 13:50:21 +01:00
import random
import re
import time
import config
2016-04-05 14:18:22 +02:00
from common import extract_title
from rate_limit import RATE_NO_SILENCE, RATE_GLOBAL, RATE_FUN, RATE_URL
2015-12-26 13:50:21 +01:00
from config import runtimeconf_get
2016-04-05 14:18:22 +02:00
from plugin_system import pluginfunction, ptypes
2016-04-05 18:40:31 +02:00
log = logging.getLogger(__name__)
2015-12-26 13:50:21 +01:00
2016-04-05 14:18:22 +02:00
@pluginfunction('mental_ill', 'parse mental illness', ptypes.PARSE, ratelimit_class=RATE_NO_SILENCE | RATE_GLOBAL)
2015-12-26 13:50:21 +01:00
def parse_mental_ill(**args):
min_ill = 3
c = 0
flag = False
# return True for min_ill '!' in a row
for d in args['data']:
if '!' == d or '?' == d:
c += 1
else:
c = 0
if min_ill <= c:
flag = True
break
if flag:
log.info('sent mental illness reply')
return {
'msg': (
'Multiple exclamation/question marks are a sure sign of mental disease, with %s as a living example.' %
args['reply_user']
)
}
2016-08-13 02:31:45 +02:00
@pluginfunction('woof', '*puts sunglasses on*', ptypes.PARSE, ratelimit_class=RATE_NO_SILENCE | RATE_GLOBAL)
def command_woof(**args):
if 'who let the bots out' in args['data']:
return {
'msg': 'beeep! beep! beep! beep! beep!'
}
2016-04-05 14:18:22 +02:00
@pluginfunction('debbug', 'parse Debian bug numbers', ptypes.PARSE, ratelimit_class=RATE_NO_SILENCE | RATE_GLOBAL)
2015-12-26 13:50:21 +01:00
def parse_debbug(**args):
bugs = re.findall(r'#(\d{4,})', args['data'])
if not bugs:
return None
out = []
for b in bugs:
log.info('detected Debian bug #%s' % b)
url = 'https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s' % b
2016-12-11 12:22:00 +01:00
title = extract_title(url)
if title:
2015-12-26 13:50:21 +01:00
out.append('Debian Bug: %s: %s' % (title, url))
return {
'msg': out
}
2016-04-05 14:18:22 +02:00
@pluginfunction('cve', 'parse a CVE handle', ptypes.PARSE, ratelimit_class=RATE_NO_SILENCE | RATE_GLOBAL)
2015-12-26 13:50:21 +01:00
def parse_cve(**args):
cves = re.findall(r'(CVE-\d\d\d\d-\d+)', args['data'].upper())
if not cves:
return None
log.info('detected CVE handle')
return {
'msg': ['https://security-tracker.debian.org/tracker/%s' % c for c in cves]
}
2016-04-05 14:18:22 +02:00
@pluginfunction('dsa', 'parse a DSA handle', ptypes.PARSE, ratelimit_class=RATE_NO_SILENCE | RATE_GLOBAL)
2015-12-26 13:50:21 +01:00
def parse_dsa(**args):
dsas = re.findall(r'(DSA-\d\d\d\d-\d+)', args['data'].upper())
if not dsas:
return None
log.info('detected DSA handle')
return {
'msg': ['https://security-tracker.debian.org/tracker/%s' % d for d in dsas]
}
2016-04-05 14:18:22 +02:00
@pluginfunction('skynet', 'parse skynet', ptypes.PARSE, ratelimit_class=RATE_FUN | RATE_GLOBAL)
2015-12-26 13:50:21 +01:00
def parse_skynet(**args):
if 'skynet' in args['data'].lower():
return {
'msg': 'I\'ll be back.'
}
2016-04-05 14:18:22 +02:00
@pluginfunction('latex', r'reacts on \LaTeX', ptypes.PARSE, ratelimit_class=RATE_FUN | RATE_GLOBAL)
2015-12-26 13:50:21 +01:00
def parse_latex(**args):
if r'\LaTeX' in args['data']:
return {
'msg': '''LaTeX is way too complex for me, I'm happy with fmt(1)'''
}
2016-04-05 14:18:22 +02:00
@pluginfunction('me-action', 'reacts to /me.*%{bot_nickname}', ptypes.PARSE, ratelimit_class=RATE_FUN | RATE_GLOBAL)
2015-12-26 13:50:21 +01:00
def parse_slash_me(**args):
if args['data'].lower().startswith('/me') and (config.conf_get('bot_nickname') in args['data'].lower()):
log.info('sent /me reply')
me_replys = [
'are you that rude to everybody?',
'oh, thank you...',
'do you really think that was nice?',
'that sounds very interesting...',
"excuse me, but I'm already late for an appointment"
]
return {
'msg': args['reply_user'] + ': %s' % random.choice(me_replys)
}
2016-04-05 14:18:22 +02:00
@pluginfunction('resolve-url-title', 'extract titles from urls', ptypes.PARSE, ratelimit_class=RATE_URL)
2015-12-26 13:50:21 +01:00
def resolve_url_title(**args):
user = args['reply_user']
user_pref_nospoiler = runtimeconf_get('user_pref', {}).get(user, {}).get('spoiler', False)
if user_pref_nospoiler:
log.info('nospoiler in userconf')
return
result = re.findall(r'(https?://[^\s>]+)', args['data'])
if not result:
return
url_blacklist = config.runtime_config_store['url_blacklist'].values()
out = []
2016-12-11 12:22:00 +01:00
for url in result[:10]:
2015-12-26 13:50:21 +01:00
if any([re.match(b, url) for b in url_blacklist]):
log.info('url blacklist match for ' + url)
break
try:
title = extract_title(url)
except UnicodeError as e:
message = 'Bug triggered (%s), invalid URL/domain part: %s' % (str(e), url)
log.warn(message)
return {'msg': message}
if title:
title = title.strip()
message = 'Title: %s' % title
message = message.replace('\n', '\\n')
out.append(message)
return {
'msg': out
}
2016-01-31 19:28:22 +01:00
2016-05-27 21:56:10 +02:00
@pluginfunction('doctor', 'parse doctor', ptypes.PARSE, ratelimit_class=RATE_FUN | RATE_GLOBAL)
def parse_doctor(**args):
if 'doctor' in args['data'].lower() or 'doktor' in args['data'].lower():
return {
'msg': 'ELIMINIEREN! ELIMINIEREN!'
}