From: Thorsten <mail@aero2k.de>
Date: Thu, 4 Oct 2018 17:53:10 +0000 (+0200)
Subject: enable reverse lookup of commands to plugin names for help
X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=a2f1647ccb0298f6c2b238fe3d282fbae171d9bc;p=urlbot-v3.git

enable reverse lookup of commands to plugin names for help
---

diff --git a/distbot/plugins/plugin_help.py b/distbot/plugins/plugin_help.py
old mode 100755
new mode 100644
index ae650e1..ce52450
--- a/distbot/plugins/plugin_help.py
+++ b/distbot/plugins/plugin_help.py
@@ -31,6 +31,7 @@ class Plugins(Worker):
     usage = "bot: help [plugin name], bot: plugin activate/deactivate <plugin name>"
 
     plugin_storage = {}
+    reverse_lookup = {}
 
     def __init__(self, actionqueue, queue="work"):
         super(Plugins, self).__init__(actionqueue, queue)
@@ -46,15 +47,24 @@ class Plugins(Worker):
     def callback_plugins(self, ch, method, properties, body):
         body = json.loads(body.decode("utf-8"))
         logger.debug("received plugin in registry")
-        self.plugin_storage[body["name"]] = body
+        self.plugin_storage[body["name"].lower()] = body
         ch.basic_ack(delivery_tag=method.delivery_tag)
 
-    def send_help(self, plugin_name=None):
+    def build_reverse_lookup(self):
+        reverse_lookup = {}
+        for plugin, definition in self.plugin_storage.items():
+            for key in definition['binding_keys']:
+                if key.startswith("nick"):
+                    reverse_lookup[key.split('.')[1]] = plugin
 
+    def send_help(self, plugin_name=None):
         if not plugin_name:
             msg = "Known commands/reactions:\n" + ", ".join(self.plugin_storage.keys())
         else:
-            definition = self.plugin_storage[plugin_name]
+            definition = self.plugin_storage.get(plugin_name.lower()) \
+                         or self.plugin_storage.get(self.reverse_lookup.get(plugin_name.lower()))
+            if not definition:
+                return Action(msg="unknown command, try help")
             msg = ("{}: \n"
                    "Description:\t{}\n"
                    "Usage:\t{}\n\n").format(