Skip to content

Instantly share code, notes, and snippets.

@atx
Created August 16, 2014 12:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atx/48bf5e3a6d99e5464d64 to your computer and use it in GitHub Desktop.
Save atx/48bf5e3a6d99e5464d64 to your computer and use it in GitHub Desktop.
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