Upgrade the test tool.
[irker.git] / irk
1 #!/usr/bin/env python
2 # Illustrates how to test irkerd.  Requires that the irkerd source
3 # be in the current directory.
4 #
5 # First argument must be a channel URL. If it does not begin with "irc", 
6 # the base URL for freenode is prepended.
7 #
8 # Second argument must be a payload string.  Standard C-style escapes 
9 # such as \n and \t are decoded.
10 #
11 import json
12 import socket
13 import sys
14 import fileinput
15 import subprocess
16 import time
17 import os
18
19 def send(target, message):
20     data = {"to": target, "privmsg" : message}
21     #print(json.dumps(data))
22     try:
23         s = socket.create_connection(("localhost", 6659))
24         s.sendall(json.dumps(data))
25     except socket.error, e:
26         sys.stderr.write("irkerd: write to server failed: %r\n" % e)
27
28 spawn = None
29 try:
30     # irkerd might already be running
31     s = socket.create_connection(("localhost", 6659))
32 except:
33     # If it isn't, spawn an instance
34     print "Spawning an irker instance..."
35     subprocess.Popen("irkerd", shell=True)
36     time.sleep(0.5)
37
38 target = sys.argv[1]
39 if not "irc:" in target:
40     target = "irc://chat.freenode.net/{0}".format(target)
41 message = " ".join(sys.argv[2:])
42 message = message.decode('string_escape')
43 if message == '-':
44     for line in fileinput.input('-'):
45         send(target, line.rstrip('\n'))
46 else:
47     send(target, message)