summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Baumann <derflob@derflob.de>2016-07-15 12:13:48 +1200
committerFlorian Baumann <derflob@derflob.de>2016-07-15 17:20:46 +1200
commit11c4ddfa8a828a4ba671b427bae11574bafd6202 (patch)
tree5608c62a04d58b68ef2de092f44608d2e2d19cf7
parent3bdaaced4752ad82ca3356351d230bd234930a47 (diff)
downloadxmpp-11c4ddfa8a828a4ba671b427bae11574bafd6202.tar.gz
xmpp-11c4ddfa8a828a4ba671b427bae11574bafd6202.tar.bz2
Receive commands via XMPP
-rw-r--r--__init__.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/__init__.py b/__init__.py
index 03537c3..c173954 100644
--- a/__init__.py
+++ b/__init__.py
@@ -1,5 +1,6 @@
# coding=utf-8
from __future__ import absolute_import
+import re
import io
import requests
import sleekxmpp
@@ -23,6 +24,7 @@ class OctoXMPP(EventHandlerPlugin, SettingsPlugin):
self._join_groupchats()
self._xmpp.add_event_handler('session_start', __session_start)
+ self._xmpp.add_event_handler('message', self._process_message)
self._xmpp.register_plugin('xep_0030') # disco
self._xmpp.register_plugin('xep_0045') # muc
@@ -86,6 +88,76 @@ class OctoXMPP(EventHandlerPlugin, SettingsPlugin):
elif (event == "Connected"):
self._msg_recipients("Connected to Rupert.")
+ def _process_message(self, msg):
+ if (msg.get_type() == "groupchat"):
+ nick = self._settings.get(['xmpp', 'resource'])
+ pattern = r"({}[\s\W]*)(?P<cmd>.*)".format(nick)
+ m = re.match(pattern, msg['body'], re.I)
+ if (m):
+ cmd_str = m.group('cmd')
+ else:
+ return
+ else:
+ cmd_str = msg['body']
+
+ if (' ' in cmd_str):
+ (cmd, cmd_args) = cmd_str.split(' ', 1)
+ else:
+ cmd = cmd_str
+ cmd_args = None
+
+ if (cmd_args is not None):
+ if (' ' in cmd_args):
+ cmd_args = cmd_args.split(' ')
+ else:
+ cmd_args = [cmd_args]
+ else:
+ cmd_args = []
+
+ fn = getattr(self, "_cmd_{}".format(cmd), self.__cmd_unknown(cmd))
+
+ fn(msg, *cmd_args)
+
+ def __cmd_unknown(self, cmd):
+ def __cmd_unknown_reply(msg, *args):
+ reply = msg.reply(body="Unknown command '{}' ({})!".format(cmd, ", ".join(args)))
+ reply.send()
+
+ return __cmd_unknown_reply
+
+ def _cmd_status(self, msg, subsystem='all'):
+ subsystems = ['temp', 'job', 'machine']
+
+ if (subsystem == 'all'):
+ reply = '\n\n'.join([getattr(self, "_status_{}".format(s))() for s in subsystems])
+ elif (subsystem in subsystems):
+ reply = getattr(self, "_status_{}".format(subsystem))()
+ else:
+ reply = "Unknown subsystem ({}).\nAvailable: {}".format(subsystem, ", ".join(['all'] + subsystems))
+
+ msg.reply(body=reply).send()
+
+ def _status_temp(self):
+ if (self._printer.is_closed_or_error()):
+ return "Temperatures:\nNo temperatures available."
+ else:
+ return "Temperatures:\n{}".format("\n".join(
+ ["{tool}: {actual}°C (Target: {target}°C; Offset: {offset}°K".format(tool=tool, **temp)
+ for (tool, temp) in self._printer.get_current_temperatures()]))
+
+ def _status_job(self):
+ if (self._printer.is_printing()):
+ return "Job state:\nSomething printing."
+ else:
+ return "Job state:\nNothing printing."
+
+ def _status_machine(self):
+ if (self._printer.is_closed_or_error()):
+ return "Machine state:\nNot Connected/Error"
+ else:
+ return "Machine state:\n{}".format(self._printer.get_state_string())
+
+
__plugin_name__ = "OctoXMPP"
__plugin_version__ = "0.2.0"
__plugin_description__ = "Notify about some Events over XMPP."