Support piping text to irk
authorLaurent Bachelier <laurent@bachelier.name>
Wed, 10 Oct 2012 19:02:05 +0000 (21:02 +0200)
committerEric S. Raymond <esr@thyrsus.com>
Wed, 10 Oct 2012 19:27:55 +0000 (15:27 -0400)
Usage: outcmd|irk target -
Sending "-" is still possible by echo -|irk target -

Signed-off-by: Eric S. Raymond <esr@thyrsus.com>
irk

diff --git a/irk b/irk
index 320787ebc366b5e4db6026ead9210ac63249ac0f..4815db101fd12f0d8a623f8ec18b1f35c7672fc4 100755 (executable)
--- a/irk
+++ b/irk
 import json
 import socket
 import sys
+import fileinput
+
+def send(target, message):
+    data = {"to": target, "privmsg" : message}
+    print(json.dumps(data))
+    try:
+        s = socket.create_connection(("localhost", 6659))
+        s.sendall(json.dumps(data))
+    except socket.error, e:
+        sys.stderr.write("irkerd: server launch failed: %r\n" % e)
 
 target = sys.argv[1]
 if not "irc:" in target:
     target = "irc://chat.freenode.net/{0}".format(target)
 message = " ".join(sys.argv[2:])
 message = message.decode('string_escape')
-data = {"to": target, "privmsg" : message}
-print(json.dumps(data))
-try:
-    s = socket.create_connection(("localhost", 6659))
-    s.sendall(json.dumps(data))
-except socket.error, e:
-    sys.stderr.write("irkerd: server launch failed: %r\n" % e)
+if message == '-':
+    for line in fileinput.input('-'):
+        send(target, line.rstrip('\n'))
+else:
+    send(target, message)