Skip to content

Commit

Permalink
added mqtt.py, sends your private messages over mqtt
Browse files Browse the repository at this point in the history
  • Loading branch information
terminaldweller committed Dec 30, 2023
1 parent 7c8882a commit d8ede65
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions scripts/mqtt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import irssi

Check failure on line 1 in scripts/mqtt.py

View workflow job for this annotation

GitHub Actions / test

No IRSSI header in script or name not given

Check failure on line 1 in scripts/mqtt.py

View workflow job for this annotation

GitHub Actions / test

Missing __version__ in script
import asyncio

Check warning on line 2 in scripts/mqtt.py

View workflow job for this annotation

GitHub Actions / test

F401 'asyncio' imported but unused
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")

Check warning on line 51 in scripts/mqtt.py

View workflow job for this annotation

GitHub Actions / test

F841 local variable 'mqtt_server' is assigned to but never used
mqtt_port = irssi.settings_get_int(b"mqtt_port")

Check warning on line 52 in scripts/mqtt.py

View workflow job for this annotation

GitHub Actions / test

F841 local variable 'mqtt_port' is assigned to but never used
mqtt_topic = irssi.settings_get_str(b"mqtt_topic")

Check warning on line 53 in scripts/mqtt.py

View workflow job for this annotation

GitHub Actions / test

F841 local variable 'mqtt_topic' is assigned to but never used
mqtt_pass = irssi.settings_get_str(b"mqtt_pass")

Check warning on line 54 in scripts/mqtt.py

View workflow job for this annotation

GitHub Actions / test

F841 local variable 'mqtt_pass' is assigned to but never used
mqtt_retain = irssi.settings_get_bool(b"mqtt_retain")

Check warning on line 55 in scripts/mqtt.py

View workflow job for this annotation

GitHub Actions / test

F841 local variable 'mqtt_retain' is assigned to but never used

threading.Thread(target=work(client, content)).start()
# @args = ("mosquitto_pub", "-h", $MQTTServ, "-p", $MQTTPort, "-q", $MQTTQoS, "-I", $MQTTClient, "-u", $MQTTUser, "-P", $MQTTPass, "-t", $MQTTTopic,);

Check failure on line 58 in scripts/mqtt.py

View workflow job for this annotation

GitHub Actions / test

E501 line too long (154 > 127 characters)
# 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()

0 comments on commit d8ede65

Please sign in to comment.