From d8ede65a59f29b4a0e3e98feeacadf7f975e19f0 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Sat, 30 Dec 2023 10:41:51 -0500 Subject: [PATCH 1/4] added mqtt.py, sends your private messages over mqtt --- scripts/mqtt.py | 118 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 scripts/mqtt.py diff --git a/scripts/mqtt.py b/scripts/mqtt.py new file mode 100644 index 000000000..bafcb7c12 --- /dev/null +++ b/scripts/mqtt.py @@ -0,0 +1,118 @@ +import irssi +import asyncio +import paho.mqtt.client as mqtt +import threading + +__version__ = "0.1.0" + +IRSSI = { + "authors": "terminaldweller", + "contact": "https://terminaldweller.com", + "name": "mqtt", + "description": "publish messages on mqtt", + "license": "GPL3 or newer", + "url": "https://github.com/irssi/scripts.irssi.org", +} + + +def on_connect(client, userdata, flags, rc): + pass + + +def on_message(client, userdata, msg): + pass + + +def work(client, content): + mqtt_server = irssi.settings_get_str(b"mqtt_server") + mqtt_port = irssi.settings_get_int(b"mqtt_port") + mqtt_topic = irssi.settings_get_str(b"mqtt_topic") + mqtt_pass = irssi.settings_get_str(b"mqtt_pass") + mqtt_retain = irssi.settings_get_bool(b"mqtt_retain") + client.username_pw_set("irssi", mqtt_pass) + client.connect(mqtt_server, mqtt_port, 60) + client.loop_start() + client.publish( + mqtt_topic.decode("utf-8"), + content.decode("utf-8"), + qos=0, + retain=mqtt_retain, + ) + + +def publish( + content: bytes, target: bytes, nick: bytes, server: irssi.IrcServer +) -> None: + client = mqtt.Client( + clean_session=True, userdata=None, protocol=mqtt.MQTTv311, transport="tcp" + ) + client.on_connect = on_connect + client.on_message = on_message + mqtt_server = irssi.settings_get_str(b"mqtt_server") + mqtt_port = irssi.settings_get_int(b"mqtt_port") + mqtt_topic = irssi.settings_get_str(b"mqtt_topic") + mqtt_pass = irssi.settings_get_str(b"mqtt_pass") + mqtt_retain = irssi.settings_get_bool(b"mqtt_retain") + + threading.Thread(target=work(client, content)).start() + # @args = ("mosquitto_pub", "-h", $MQTTServ, "-p", $MQTTPort, "-q", $MQTTQoS, "-I", $MQTTClient, "-u", $MQTTUser, "-P", $MQTTPass, "-t", $MQTTTopic,); + # await asyncio.create_subprocess_exec( + # "mosquitto_pub", + # "-h", + # mqtt_server, + # "-p", + # repr(mqtt_port), + # "-P", + # mqtt_pass, + # "-t", + # mqtt_topic.decode("utf-8"), + # "-m", + # content.decode("utf-8"), + # stdout=asyncio.subprocess.PIPE, + # stderr=asyncio.subprocess.PIPE, + # ) + # task = asyncio.create_task(work(client, content)) + # await asyncio.sleep(0) + + +def mqtt_sig_handler(*args, **kwargs) -> None: + server = args[0] + msg = args[1] + nick = args[2] + target = args[4] + mqtt_server = irssi.settings_get_str(b"mqtt_server") + if mqtt_server != "": + publish(msg, target, nick, server) + + +def run_on_script_load() -> None: + irssi.settings_add_str( + b"misc", + b"mqtt_server", + b"172.17.0.1", + ) + irssi.settings_add_int( + b"misc", + b"mqtt_port", + 1883, + ) + irssi.settings_add_str( + b"misc", + b"mqtt_topic", + b"test", + ) + irssi.settings_add_str( + b"misc", + b"mqtt_pass", + b"", + ) + irssi.settings_add_bool( + b"misc", + b"mqtt_retain", + False, + ) + + irssi.signal_add(b"message private", mqtt_sig_handler) + + +run_on_script_load() From 78f6ce35d30aebd9145784ce278b9596b7189206 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Sat, 30 Dec 2023 10:54:19 -0500 Subject: [PATCH 2/4] removed some junk --- scripts/mqtt.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/scripts/mqtt.py b/scripts/mqtt.py index bafcb7c12..704dd130a 100644 --- a/scripts/mqtt.py +++ b/scripts/mqtt.py @@ -1,5 +1,4 @@ import irssi -import asyncio import paho.mqtt.client as mqtt import threading @@ -55,24 +54,6 @@ def publish( mqtt_retain = irssi.settings_get_bool(b"mqtt_retain") threading.Thread(target=work(client, content)).start() - # @args = ("mosquitto_pub", "-h", $MQTTServ, "-p", $MQTTPort, "-q", $MQTTQoS, "-I", $MQTTClient, "-u", $MQTTUser, "-P", $MQTTPass, "-t", $MQTTTopic,); - # await asyncio.create_subprocess_exec( - # "mosquitto_pub", - # "-h", - # mqtt_server, - # "-p", - # repr(mqtt_port), - # "-P", - # mqtt_pass, - # "-t", - # mqtt_topic.decode("utf-8"), - # "-m", - # content.decode("utf-8"), - # stdout=asyncio.subprocess.PIPE, - # stderr=asyncio.subprocess.PIPE, - # ) - # task = asyncio.create_task(work(client, content)) - # await asyncio.sleep(0) def mqtt_sig_handler(*args, **kwargs) -> None: From 7c6e11337a5b2c139ece5b83b3a66d77d43ecefb Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Sat, 30 Dec 2023 11:01:59 -0500 Subject: [PATCH 3/4] remove unused vars --- scripts/mqtt.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/scripts/mqtt.py b/scripts/mqtt.py index 704dd130a..d9caec171 100644 --- a/scripts/mqtt.py +++ b/scripts/mqtt.py @@ -1,3 +1,4 @@ +"""mqtt.py, sends yur privmsgs over mqtt""" import irssi import paho.mqtt.client as mqtt import threading @@ -47,11 +48,6 @@ def publish( ) client.on_connect = on_connect client.on_message = on_message - mqtt_server = irssi.settings_get_str(b"mqtt_server") - mqtt_port = irssi.settings_get_int(b"mqtt_port") - mqtt_topic = irssi.settings_get_str(b"mqtt_topic") - mqtt_pass = irssi.settings_get_str(b"mqtt_pass") - mqtt_retain = irssi.settings_get_bool(b"mqtt_retain") threading.Thread(target=work(client, content)).start() From cbd88e37eb8517dedfc6d01294cf2d28647be90e Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Tue, 30 Jan 2024 11:53:01 -0500 Subject: [PATCH 4/4] bumped the version to check the new python req auto-detect script --- scripts/mqtt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/mqtt.py b/scripts/mqtt.py index d9caec171..29a45e30f 100644 --- a/scripts/mqtt.py +++ b/scripts/mqtt.py @@ -3,7 +3,7 @@ import paho.mqtt.client as mqtt import threading -__version__ = "0.1.0" +__version__ = "0.1.1" IRSSI = { "authors": "terminaldweller",