Added a plugin to search through a Mediawiki instance

This commit is contained in:
Dennis Potter 2019-01-20 23:38:39 +01:00
parent 9e139a0f3e
commit 09dd17c9f2
Signed by: Dennis
GPG Key ID: 186A8AD440942BAF
6 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,9 @@
{
"htpasswd": {
"enabled": true,
"username": "",
"password": ""
},
"base_url": "https://"
}

View File

@ -0,0 +1,88 @@
__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()

View File

@ -0,0 +1,6 @@
<h5><i>wiki</i></h5>
De Alcuinus wiki page verleend algemene informatie over de vereniging en over verschillende aflopen binnen de vereniging.
<ul>
<li><i>search &lt;string&gt;</i>:&emsp;dit commando laat je een zoekopdracht uitvoeren in de wiki. De inloggegevens zullen al voor je ingevuld zijn. Wil je bijvoorbeeld zoeken naar het reglement? Gebruik dan dan <i>wiki search reglement</i>.</li>
<li><i>login</i>:&emsp;dit commando resulteert in de link naar de interne wiki en de inloggegevens.</li>
</ul>

View File

@ -0,0 +1,26 @@
{
"login_message_htpasswd":
'''
De interne wiki bevindt zich op <a href="{}">{}</a> en is van de
buitenwereld afgeschermd met een wachtwoord. Je kunt inloggen op
de wiki met de gebruikersnaam <strong>{}</strong> en het
wachtwoord <strong>{}</strong>.
'''
"login_message":
'''
De interne wiki bevindt zich op <a href="{}">{}</a>.
'''
"search_query":
'''
Je kunt de resultaten voor je zoekopdracht "{}" <a href="{}">onder
deze link bekijken</a>!
'''
"invalid_query":
'''
Je hebt geen geldige zoekopdracht gedefinieerd. Probeer het nog een
keer 🤷‍♂️.
'''
}