15 assert sys.version_info >= (3, 5)
20 for i in range(0, 10):
23 fifo = posix.open(fifo_path, posix.O_WRONLY | posix.O_NONBLOCK)
24 posix.write(fifo, line)
27 if e.errno == errno.ENXIO:
32 raise Exception("Error sending line!")
34 parser = argparse.ArgumentParser(description='Post message using Matrix Bot.')
35 parser.add_argument('channel', type=str, help='channel to be used for posting')
36 parser.add_argument('--html', action='store_true', dest='is_html',
37 default=False, help='post html message')
38 parser.add_argument('--markdown', action='store_true', dest='is_markdown',
39 default=False, help='post markdown message')
41 args = parser.parse_args()
43 fifo_dir = os.getenv('MTXBOT_FIFO_DIR', '/run/mtxbot')
44 fifo_path = "%s/%s" % (fifo_dir, args.channel)
46 if not os.path.isdir(fifo_dir):
47 print("Fifo directory %s does not exist!" % fifo_dir, file=sys.stderr)
50 if not ( os.path.exists(fifo_path) and
51 stat.S_ISFIFO(os.stat(fifo_path).st_mode) ):
52 print("Channel %s does not exist!" % args.channel, file=sys.stderr)
55 in_raw = sys.stdin.read()
58 in_raw = markdown2.markdown(in_raw)
60 in_bytes = in_raw.encode("UTF-8")
61 in_b64 = base64.b64encode(in_bytes).decode("UTF-8")
63 msg_id = ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
64 msg_fmt = 'HTML' if (args.is_html or args.is_markdown) else 'PLAIN'
65 msg_len = len(in_b64) // FRAG_LEN + (0 if len(in_b64) % FRAG_LEN == 0 else 1)
67 send_line( ("%s %s %d\n" % (msg_id, msg_fmt, msg_len)).encode("UTF-8") )
68 for i in range(0, len(in_b64), FRAG_LEN):
69 send_line( ("%s %d %s\n" % (msg_id, i//FRAG_LEN, in_b64[i:i+FRAG_LEN])).encode("UTF-8") )