From c4f107a1a27dc60858fc694839c981e001cf447f Mon Sep 17 00:00:00 2001 From: Alexander Schmidt Date: Sat, 3 Oct 2020 21:21:56 +0200 Subject: [PATCH] added post command and use fifo dir from env --- doc/config.json | 3 --- src/mtxbot-post.py | 40 ++++++++++++++++++++++++++++++++++++++++ src/mtxbot.py | 6 ++++-- 3 files changed, 44 insertions(+), 5 deletions(-) create mode 100755 src/mtxbot-post.py diff --git a/doc/config.json b/doc/config.json index ad3d114..0f08155 100644 --- a/doc/config.json +++ b/doc/config.json @@ -3,8 +3,5 @@ "url": "https://matrix.example.com", "user": "@user:matrix.example.com", "password": "WHATEVER" - }, - "paths": { - "fifodir": "/path/to/fifo/dir" } } diff --git a/src/mtxbot-post.py b/src/mtxbot-post.py new file mode 100755 index 0000000..1ba56c4 --- /dev/null +++ b/src/mtxbot-post.py @@ -0,0 +1,40 @@ +#!/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 " % 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) diff --git a/src/mtxbot.py b/src/mtxbot.py index d92dcdc..722c8db 100755 --- a/src/mtxbot.py +++ b/src/mtxbot.py @@ -61,10 +61,12 @@ if len(sys.argv) != 2: with open(sys.argv[1]) as configFile: config = json.load(configFile) +fifo_dir = os.getenv('MTXBOT_FIFO_DIR', '/run/mtxbot') + while not stop: presences = [] - for entry in os.listdir(config['paths']['fifodir']): - fullpath = "%s/%s" % (config['paths']['fifodir'], entry) + for entry in os.listdir(fifo_dir): + fullpath = "%s/%s" % (fifo_dir, entry) if stat.S_ISFIFO(os.stat(fullpath).st_mode): logging.info("Creating presence for %s..." % entry) presences.append( Presence(entry, fullpath) ) -- 2.39.5