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