2 # Illustrates how to test irkerd.
4 # First argument must be a channel URL. If it does not begin with "irc",
5 # the base URL for freenode is prepended.
7 # -k option kills irkerd if irk hard to spawn one.
9 # Second argument must be a payload string. Standard C-style escapes
10 # such as \n and \t are decoded.
22 def send(target, message):
23 data = {"to": target, "privmsg" : message}
24 #print(json.dumps(data))
26 s = socket.create_connection(("localhost", 6659))
27 s.sendall(json.dumps(data))
28 except socket.error, e:
29 sys.stderr.write("irkerd: write to server failed: %r\n" % e)
31 if __name__ == "__main__":
32 (options, arguments) = getopt.getopt(sys.argv[1:], "k", )
34 for (opt, val) in options:
38 # Spawn an irkerd instance if none is running.
40 # irkerd might already be running
41 s = socket.create_connection(("localhost", 6659))
44 # If it isn't, spawn an instance
45 print "Spawning an irker instance..."
46 irkerd = subprocess.Popen("irkerd", shell=True, preexec_fn=os.setsid)
50 if not "irc:" in target:
51 target = "irc://chat.freenode.net/{0}".format(target)
52 message = " ".join(arguments[1:])
53 message = message.decode('string_escape')
55 for line in fileinput.input('-'):
56 send(target, line.rstrip('\n'))
62 # Magic required because shell=True, terminate() won't work.
63 os.killpg(irkerd.pid, signal.SIGTERM)
64 print "...terminated."