shrlok: prefix message length

This commit is contained in:
dece 2022-07-05 23:35:58 +02:00
parent 4d7be3d9d8
commit 9c78abd7f3
2 changed files with 14 additions and 7 deletions

View file

@ -62,7 +62,7 @@ class Bot(irc.client.SimpleIRCClient, Logger):
with open(self.config["storage_file"], "wt") as storage_file: with open(self.config["storage_file"], "wt") as storage_file:
json.dump(self.storage, storage_file, indent=2, sort_keys=True) json.dump(self.storage, storage_file, indent=2, sort_keys=True)
self.log_d("Saved storage file.") self.log_d("Saved storage file.")
except (OSError, json.decoder.JSONEncodeError) as exc: except OSError as exc:
self.log_e(f"Could not save storage file: {exc}") self.log_e(f"Could not save storage file: {exc}")
def on_welcome(self, connection, event): def on_welcome(self, connection, event):

View file

@ -4,7 +4,11 @@ from edmond.plugin import Plugin
class ShrlokPlugin(Plugin): class ShrlokPlugin(Plugin):
"""Use an available shrlok server to allow sharing longer stuff.""" """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.
"""
def __init__(self, bot): def __init__(self, bot):
super().__init__(bot) super().__init__(bot)
@ -22,15 +26,18 @@ class ShrlokPlugin(Plugin):
self.url_root = self.config["url_root"] self.url_root = self.config["url_root"]
self.bot.log_d(f"shrlok socket path '{self.socket}'") self.bot.log_d(f"shrlok socket path '{self.socket}'")
else: else:
self.bot.log_i("No socket path specified, shrlok plugin disabled.") self.bot.log_d("No socket path specified, shrlok plugin disabled.")
self.is_ready = False self.is_ready = False
def post_text(self, content): def post_text(self, content):
try: try:
with socket.socket(socket.AF_UNIX, socket.SOCKET_STREAM) as sock: with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
sock.connect(self.socket) sock.connect(self.socket)
sock.sendall(content.encode()) data = b'{"type":"txt"}\0' + content.encode()
sock.sendall(str(len(data)).encode() + b"\0" + data)
response = sock.recv(4096) response = sock.recv(4096)
except OSError as exc: except OSError as exc:
self.bot.log_e(f"Can't write text file: {exc}") self.bot.log_e(f"Can't post text file: {exc}")
self.bot.log_i("socket replied with: " + response.decode()) return None
url = response.decode().replace(self.file_root, self.url_root, count=1)
return url