+#!/usr/bin/env python3
+
+import errno
+import os
+import posix
+import stat
+import sys
+import time
+
+assert sys.version_info >= (3, 5)
+
+if len(sys.argv) != 2:
+ print("Usage: %s <channel>" % sys.argv[0])
+
+fifo_dir = os.getenv('MTXBOT_FIFO_DIR', '/run/mtxbot')
+fifo_path = "%s/%s" % (fifo_dir, sys.argv[1])
+
+if not os.path.isdir(fifo_dir):
+ print("Fifo directory %s does not exist!" % fifo_dir, file=sys.stderr)
+ sys.exit(1)
+
+if not stat.S_ISFIFO( os.stat(fifo_path).st_mode ):
+ print("Channel %s does not exist!" % sys.argv[1], file=sys.stderr)
+ sys.exit(2)
+
+for i in range(0, 10):
+ fifo = -1
+ try:
+ fifo = posix.open(fifo_path, posix.O_WRONLY | posix.O_NONBLOCK)
+ posix.write(fifo, sys.stdin.read().encode())
+ sys.exit(0)
+ except OSError as e:
+ if e.errno == errno.ENXIO:
+ time.sleep(1.0)
+ finally:
+ if fifo != -1:
+ posix.close(fifo)
+
+print("Error posting to channel %s!" % sys.argv[1], file=sys.stderr)
+sys.exit(3)