diff --git a/rocketchat.py b/rocketchat.py index 8fc6036..728e078 100644 --- a/rocketchat.py +++ b/rocketchat.py @@ -17,26 +17,51 @@ rocketchat_servers = [] rocketchat_config_file = None rocketchat_config_section = {} +def o( + type="string", desc="", min=0, max=999999, string_values=None, default=None, + nullable=0, check_cb=None, change_cb=None, delete_cb=None): + return { + "type": type, + "desc": desc, + "min": min, + "max": max, + "string_values": "|".join(string_values) if string_values is not None else "", + "default": default if default is not None else "", + "nullable": nullable, + "check_cb": check_cb if check_cb is not None else "", + "change_cb": change_cb if change_cb is not None else "", + "delete_cb": delete_cb if delete_cb is not None else "", + } + +options = { + "hostname": o(desc="hostname to connect to"), + "autoconnect": o(type="integer", desc="autoconnect on launch"), +} + class RocketchatServer: - def __init__(self, name, hostname): + def __init__(self, name, **kwargs): self.name = name self.buffer = "" self.client = None self._counter = 0 - options = dict( - hostname=hostname, - autoconnect="on", - autoreconnect="on", - ) self.options = dict() - for key, value in options.items(): + for key, desc in options.items(): self.options[key] = weechat.config_new_option( rocketchat_config_file, rocketchat_config_section["server"], f"{self.name}.{key}", - "string", "", "", 0, 99999999, - "", value, 0, "", self.name, "", "", "", "", + desc["type"], + desc["desc"], + desc["string_values"], + desc["min"], + desc["max"], + desc["default"], + kwargs.get(key, desc["default"]), + desc["nullable"], + desc["check_cb"], self.name, + desc["change_cb"], "", + desc["delete_cb"], "", ) async def connect(self): @@ -55,9 +80,12 @@ class RocketchatServer: weechat.prnt(self.buffer, f"rocketchat: connected to {self.name}") # run the listener in a loop - asyncio.create_task(self.main_loop()) + asyncio.create_task(self.reading_loop()) - async def main_loop(self): + # send the initial connection + await self.client.send('{"msg":"connect","version":"1","support":["1"]}') + + async def reading_loop(self): weechat.prnt(self.buffer, "starting to receive!") while True: g = await self.client.recv() @@ -78,9 +106,9 @@ def init(): "Rocketchat", "list || server || connect", "bruh", - "list %(weechat_servers)" + "list %(rocketchat_servers)" " || server add|del" - " || connect %(weechat_servers)", + " || connect %(rocketchat_servers)", "rocketchat_main_command", "" ) weechat.hook_completion("rocketchat_servers", "list of rocketchat servers", @@ -97,6 +125,14 @@ def init(): "", "", "", "", "", "", ) + weechat.config_read(rocketchat_config_file) + + for server in rocketchat_servers: + autoconnect = weechat.config_boolean(server.options["autoconnect"]) + weechat.prnt("", f"server {server.name} is {autoconnect}") + if autoconnect: + server.connect() + def rocketchat_main_command(data, buffer, args): global rocketchat_servers if args == "" or args == "list": @@ -107,7 +143,7 @@ def rocketchat_main_command(data, buffer, args): if argv[1] == "add": name = argv[2] hostname = argv[3] - server = RocketchatServer(name, hostname) + server = RocketchatServer(name, hostname=hostname) rocketchat_servers.append(server) weechat.prnt("", f"server '{name}' added!") elif argv[0] == "connect": @@ -157,8 +193,7 @@ def rocketchat_search_server_by_name(name): def rocketchat_complete_servers(data, completion_item, buffer, completion): global rocketchat_servers for server in rocketchat_servers: - weechat.hook_completion_list_add(completion, server.name, - 0, weechat.WEECHAT_LIST_POS_SORT) + weechat.hook_completion_list_add(completion, server.name, 0, weechat.WEECHAT_LIST_POS_SORT) return weechat.WEECHAT_RC_OK def rocketchat_unload():