diff --git a/plugins/mediawiki_search/README.md b/plugins/mediawiki_search/README.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/plugins/mediawiki_search/README.md @@ -0,0 +1 @@ + diff --git a/plugins/mediawiki_search/config.hjson.template b/plugins/mediawiki_search/config.hjson.template new file mode 100644 index 0000000..4398d69 --- /dev/null +++ b/plugins/mediawiki_search/config.hjson.template @@ -0,0 +1,9 @@ +{ + "htpasswd": { + "enabled": true, + "username": "", + "password": "" + }, + + "base_url": "https://" +} diff --git a/plugins/mediawiki_search/mediawiki_search.py b/plugins/mediawiki_search/mediawiki_search.py new file mode 100644 index 0000000..5835043 --- /dev/null +++ b/plugins/mediawiki_search/mediawiki_search.py @@ -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() diff --git a/plugins/mediawiki_search/messages/help b/plugins/mediawiki_search/messages/help new file mode 100644 index 0000000..3eaf57c --- /dev/null +++ b/plugins/mediawiki_search/messages/help @@ -0,0 +1,6 @@ +