From 5f6cc378f1ca4576281ef6ed3a462b5821aa0436 Mon Sep 17 00:00:00 2001
From: dadav <33197631+dadav@users.noreply.github.com>
Date: Tue, 15 Oct 2019 08:54:29 +0200
Subject: [PATCH] Implement webhook

---
 pwnagotchi/plugins/default/example.py | 12 ++++++++++++
 pwnagotchi/ui/web.py                  |  9 +++++++++
 2 files changed, 21 insertions(+)

diff --git a/pwnagotchi/plugins/default/example.py b/pwnagotchi/plugins/default/example.py
index 3f3e570..72087cf 100644
--- a/pwnagotchi/plugins/default/example.py
+++ b/pwnagotchi/plugins/default/example.py
@@ -14,6 +14,18 @@ import pwnagotchi.ui.fonts as fonts
 # Will be set with the options in config.yml config['main']['plugins'][__name__]
 OPTIONS = dict()
 
+# called when <host>:<port>/plugins/<pluginname> is opened
+def on_webhook(response, path):
+    res = "<html><body><a>Hook triggered</a></body></html>"
+    response.send_response(200)
+    response.send_header('Content-type', 'text/html')
+    response.end_headers()
+
+    try:
+        response.wfile.write(bytes(res, "utf-8"))
+    except Exception as ex:
+        logging.error(ex)
+
 # called when the plugin is loaded
 def on_loaded():
     logging.warning("WARNING: plugin %s should be disabled!" % __name__)
diff --git a/pwnagotchi/ui/web.py b/pwnagotchi/ui/web.py
index 9eb5b8f..e318846 100644
--- a/pwnagotchi/ui/web.py
+++ b/pwnagotchi/ui/web.py
@@ -1,3 +1,4 @@
+import re
 import _thread
 from http.server import BaseHTTPRequestHandler, HTTPServer
 from threading import Lock
@@ -5,6 +6,7 @@ import shutil
 import logging
 
 import pwnagotchi
+from pwnagotchi import plugins
 
 frame_path = '/root/pwnagotchi.png'
 frame_format = 'PNG'
@@ -159,6 +161,13 @@ class Handler(BaseHTTPRequestHandler):
 
         elif self.path.startswith('/ui'):
             self._image()
+        elif self.path.startswith('/plugins'):
+            plugin_from_path = re.match(r'\/plugins\/([^\/]+)(\/.*)?', self.path)
+            if plugin_from_path:
+                plugin_name = plugin_from_path.groups()[0]
+                right_path = plugin_from_path.groups()[1] if len(plugin_from_path.groups()) == 2 else None
+                if plugin_name in plugins.loaded and hasattr(plugins.loaded[plugin_name], 'on_webhook'):
+                    plugins.loaded[plugin_name].on_webhook(self, right_path)
         else:
             self.send_response(404)