-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)
+if not ( os.path.exists(fifo_path) and
+ stat.S_ISFIFO(os.stat(fifo_path).st_mode) ):
+ print("Channel %s does not exist!" % args.channel, file=sys.stderr)
+ sys.exit(3)
+
+in_raw = sys.stdin.read()
+
+if args.is_markdown:
+ in_raw = markdown2.markdown(in_raw)
+
+in_bytes = in_raw.encode("UTF-8")
+in_b64 = base64.b64encode(in_bytes).decode("UTF-8")
+
+msg_id = ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
+msg_fmt = 'HTML' if (args.is_html or args.is_markdown) else 'PLAIN'
+msg_len = len(in_b64) // FRAG_LEN + (0 if len(in_b64) % FRAG_LEN == 0 else 1)
+
+send_line( ("%s %s %d\n" % (msg_id, msg_fmt, msg_len)).encode("UTF-8") )
+for i in range(0, len(in_b64), FRAG_LEN):
+ send_line( ("%s %d %s\n" % (msg_id, i//FRAG_LEN, in_b64[i:i+FRAG_LEN])).encode("UTF-8") )