89 lines
3.0 KiB
Python
89 lines
3.0 KiB
Python
__author__ = "Dennis Potter"
|
|
__copyright__ = "Copyright 2019, Dennis Potter"
|
|
__credits__ = ["Dennis Potter"]
|
|
__license__ = "GPL-3.0"
|
|
__version__ = "0.5.0"
|
|
__maintainer__ = "Dennis Potter"
|
|
__email__ = "dennis@dennispotter.eu"
|
|
|
|
from matrix_bot_api.mregex_handler import MRegexHandler
|
|
import os
|
|
import re
|
|
import hjson
|
|
|
|
MESSAGE_DIR = os.path.join(os.path.dirname(__file__), 'messages')
|
|
CONFIG_LOCATION = os.path.join(os.path.dirname(__file__), 'config.hjson')
|
|
|
|
HELP_LOCATION = MESSAGE_DIR + '/help'
|
|
MESSAGES_LOCATION = MESSAGE_DIR + '/messages.dutch.hjson'
|
|
|
|
class Plugin:
|
|
""" This plugins searches in a Mediawiki instance. If the wiki
|
|
is protected by an .htpasswd, this password can be returned by the
|
|
bot, and will also be included in search queries.
|
|
"""
|
|
|
|
def __init__(self, bot):
|
|
# Load the configuration
|
|
with open(CONFIG_LOCATION) as hjson_data:
|
|
self.config = hjson.load(hjson_data)
|
|
|
|
# Load all messages for this plugin
|
|
with open(MESSAGES_LOCATION) as hjson_data:
|
|
self.messages = hjson.load(hjson_data)
|
|
|
|
# Define sensitivity
|
|
self.handler = []
|
|
|
|
self.handler.append(MRegexHandler("Peter wiki search", self.search))
|
|
|
|
self.handler.append(MRegexHandler("Peter wiki login", self.login))
|
|
|
|
|
|
# Save parent bot
|
|
self.bot = bot
|
|
|
|
def search(self, room, event):
|
|
try:
|
|
searchMatchObj = re.match(
|
|
r'.*wiki search (.*)',
|
|
event['content']['body'],
|
|
re.M|re.I)
|
|
|
|
if self.config['htpasswd']['enabled']:
|
|
urlMatchObj = re.match(
|
|
r'(http://|https://)(.*)',
|
|
self.config['base_url'],
|
|
re.M|re.I)
|
|
|
|
url = "{}{}:{}@{}/index.php?search={}".format(
|
|
urlMatchObj.group(1), # http:// or https://
|
|
self.config['htpasswd']['username'],
|
|
self.config['htpasswd']['password'],
|
|
urlMatchObj.group(2), # remainder of base url
|
|
searchMatchObj.group(1))
|
|
else:
|
|
url = "{}/index.php?search={}".format(
|
|
self.config['base_url'],
|
|
searchMatchObj.group(1))
|
|
|
|
room.send_html(self.messages['search_query'].format(
|
|
searchMatchObj.group(1),
|
|
url))
|
|
except AttributeError:
|
|
room.send_html(self.messages['invalid_query'])
|
|
|
|
def login(self, room, event):
|
|
if self.config['htpasswd']['enabled']:
|
|
room.send_html(self.messages['login_message_htpasswd'].format(
|
|
self.config['base_url'],
|
|
self.config['base_url'],
|
|
self.config['htpasswd']['username'],
|
|
self.config['htpasswd']['password']))
|
|
else:
|
|
room.send_html(self.messages['login_message'].format(
|
|
self.config['base_url']))
|
|
|
|
def help(self):
|
|
return open(HELP_LOCATION, mode="r").read()
|