2022-07-05 22:06:44 +02:00
|
|
|
import socket
|
|
|
|
|
|
|
|
from edmond.plugin import Plugin
|
|
|
|
|
|
|
|
|
|
|
|
class ShrlokPlugin(Plugin):
|
2022-07-05 23:35:58 +02:00
|
|
|
"""Use an available shrlok server to allow sharing longer stuff.
|
|
|
|
|
|
|
|
This plugin does not do anything on its own, but can be used from other
|
|
|
|
plugins to share longer texts through a Web portal.
|
|
|
|
"""
|
2022-07-05 22:06:44 +02:00
|
|
|
|
|
|
|
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:
|
2022-07-05 23:35:58 +02:00
|
|
|
self.bot.log_d("No socket path specified, shrlok plugin disabled.")
|
2022-07-05 22:06:44 +02:00
|
|
|
self.is_ready = False
|
|
|
|
|
2022-07-10 18:25:40 +02:00
|
|
|
def post(self, content_type, content_data):
|
|
|
|
header = '{{"type":"{}"}}'.format(content_type).encode() + b"\0"
|
2022-07-05 22:06:44 +02:00
|
|
|
try:
|
2022-07-05 23:35:58 +02:00
|
|
|
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
|
2022-07-05 22:06:44 +02:00
|
|
|
sock.connect(self.socket)
|
2022-07-10 18:25:40 +02:00
|
|
|
data = header + content_data
|
2022-07-05 23:35:58 +02:00
|
|
|
sock.sendall(str(len(data)).encode() + b"\0" + data)
|
2022-07-05 22:06:44 +02:00
|
|
|
response = sock.recv(4096)
|
|
|
|
except OSError as exc:
|
2022-07-10 18:25:40 +02:00
|
|
|
self.bot.log_e(f"Can't post data: {exc}")
|
2022-07-05 23:35:58 +02:00
|
|
|
return None
|
2022-07-06 17:18:01 +02:00
|
|
|
url = response.decode().replace(self.file_root, self.url_root, 1)
|
2022-07-05 23:35:58 +02:00
|
|
|
return url
|
2022-07-10 18:25:40 +02:00
|
|
|
|
|
|
|
def post_text(self, text):
|
|
|
|
self.post("txt", text.encode())
|
|
|
|
|
|
|
|
def post_html(self, html):
|
|
|
|
self.post("html", html.encode())
|