Create a gist now

Instantly share code, notes, and snippets.

mqttirc.py
#!/usr/bin/python3
import mosquitto
import select
import socket
import time
import os
IRC_HOST = "192.168.66.1"
IRC_PORT = 6667
NICK = "HomeBot"
CHANNEL = "#home"
MQTT_HOST = "192.168.10.21"
MQTT_PORT = 1883
SUBSCRIBE = ["input/button/doorbell/#"]
class UTFSocket(socket.socket):
def sendstr(self, st):
self.send(st.encode())
def recvstr(self):
return self.recv(1024).decode("UTF-8")
s = UTFSocket()
s.connect((IRC_HOST, IRC_PORT))
s.setblocking(0)
s.sendstr("NICK %s\r\n" % NICK)
s.sendstr("USER %s %s none :%s\r\n" % (NICK, IRC_HOST, NICK))
s.sendstr("JOIN %s\r\n" % CHANNEL)
mqtt = mosquitto.Mosquitto("mqtt-bot-%d" % os.getpid())
mqtt.connect(MQTT_HOST)
# Yup, we have command injection here
mqtt.on_message = lambda c, o, m: \
s.sendstr("PRIVMSG %s :MQTT %s %s\r\n" % (CHANNEL, m.topic, m.payload.decode("ascii")))
for x in SUBSCRIBE:
mqtt.subscribe(x)
buff = ""
while True:
try:
buff = buff + s.recvstr()
except:
pass
else:
split = buff.split("\r\n")
buff = split.pop()
for l in split:
if l.startswith("PING"):
s.sendstr(("PONG" + l[4:]))
mqtt.loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment