diff --git a/edmond/plugins/shrlok.py b/edmond/plugins/shrlok.py new file mode 100644 index 0000000..7d98299 --- /dev/null +++ b/edmond/plugins/shrlok.py @@ -0,0 +1,36 @@ +import socket + +from edmond.plugin import Plugin + + +class ShrlokPlugin(Plugin): + """Use an available shrlok server to allow sharing longer stuff.""" + + def __init__(self, bot): + super().__init__(bot) + self.socket = "" + self.file_root = "" + self.url_root = "" + + def on_welcome(self, _): + if all( + conf in self.config + for conf in ("socket_path", "file_root", "url_root") + ): + self.socket = self.config["socket_path"] + self.file_root = self.config["file_root"] + self.url_root = self.config["url_root"] + self.bot.log_d(f"shrlok socket path '{self.socket}'") + else: + self.bot.log_i("No socket path specified, shrlok plugin disabled.") + self.is_ready = False + + def post_text(self, content): + try: + with socket.socket(socket.AF_UNIX, socket.SOCKET_STREAM) as sock: + sock.connect(self.socket) + sock.sendall(content.encode()) + response = sock.recv(4096) + except OSError as exc: + self.bot.log_e(f"Can't write text file: {exc}") + self.bot.log_i("socket replied with: " + response.decode())