]> git.treefish.org Git - mtxbot.git/commitdiff
added post command and use fifo dir from env
authorAlexander Schmidt <alex@treefish.org>
Sat, 3 Oct 2020 19:21:56 +0000 (21:21 +0200)
committerAlexander Schmidt <alex@treefish.org>
Sat, 3 Oct 2020 19:21:56 +0000 (21:21 +0200)
doc/config.json
src/mtxbot-post.py [new file with mode: 0755]
src/mtxbot.py

index ad3d11439d5f22f4386c1dadec5f4091e1d235f9..0f08155b19ee54a7cb06de5f50b494afea21d84f 100644 (file)
@@ -3,8 +3,5 @@
         "url": "https://matrix.example.com",
         "user": "@user:matrix.example.com",
         "password": "WHATEVER"
         "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 (executable)
index 0000000..1ba56c4
--- /dev/null
@@ -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 <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)
index d92dcdc2e2dd95368ee8e9ccf10a2bf87d9f470a..722c8dbb40097d514e70f61ce4da5147b8e12d95 100755 (executable)
@@ -61,10 +61,12 @@ if len(sys.argv) != 2:
 with open(sys.argv[1]) as configFile:
     config = json.load(configFile)
 
 with open(sys.argv[1]) as configFile:
     config = json.load(configFile)
 
+fifo_dir = os.getenv('MTXBOT_FIFO_DIR', '/run/mtxbot')
+
 while not stop:
     presences = []
 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) )
         if stat.S_ISFIFO(os.stat(fullpath).st_mode):
             logging.info("Creating presence for %s..." % entry)
             presences.append( Presence(entry, fullpath) )