X-Git-Url: http://git.treefish.org/~alex/mtxbot.git/blobdiff_plain/c4f107a1a27dc60858fc694839c981e001cf447f..6c4dbc2edd8bf5cac128c2fa48728d8abe52432e:/src/mtxbot-post.py diff --git a/src/mtxbot-post.py b/src/mtxbot-post.py index 1ba56c4..3e97ef2 100755 --- a/src/mtxbot-post.py +++ b/src/mtxbot-post.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +import base64 import errno import os import posix @@ -11,23 +12,28 @@ assert sys.version_info >= (3, 5) if len(sys.argv) != 2: print("Usage: %s " % sys.argv[0]) + sys.exit(1) 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) + sys.exit(2) -if not stat.S_ISFIFO( os.stat(fifo_path).st_mode ): +if not ( os.path.exists(fifo_path) and + stat.S_ISFIFO(os.stat(fifo_path).st_mode) ): print("Channel %s does not exist!" % sys.argv[1], file=sys.stderr) - sys.exit(2) + sys.exit(3) + +inBytes = sys.stdin.read().encode("UTF-8") +inB64 = base64.b64encode(inBytes) 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()) + posix.write(fifo, inB64) sys.exit(0) except OSError as e: if e.errno == errno.ENXIO: @@ -37,4 +43,4 @@ for i in range(0, 10): posix.close(fifo) print("Error posting to channel %s!" % sys.argv[1], file=sys.stderr) -sys.exit(3) +sys.exit(4)