# First argument must be a channel URL. If it does not begin with "irc",
# the base URL for freenode is prepended.
#
+# -k option kills irkerd if irk hard to spawn one.
+#
# Second argument must be a payload string. Standard C-style escapes
# such as \n and \t are decoded.
#
import subprocess
import time
import os
+import getopt
def send(target, message):
data = {"to": target, "privmsg" : message}
except socket.error, e:
sys.stderr.write("irkerd: write to server failed: %r\n" % e)
-# Spawn an irkerd instance if none is running.
-try:
- # irkerd might already be running
- s = socket.create_connection(("localhost", 6659))
-except:
- # If it isn't, spawn an instance
- print "Spawning an irker instance..."
- subprocess.Popen("irkerd", shell=True)
- time.sleep(0.5)
+if __name__ == "__main__":
+ (options, arguments) = getopt.getopt(sys.argv[1:], "k", )
+ killafter = False
+ for (opt, val) in options:
+ if opt == '-k':
+ killafter = True
+
+ # Spawn an irkerd instance if none is running.
+ try:
+ # irkerd might already be running
+ s = socket.create_connection(("localhost", 6659))
+ irkerd = None
+ except:
+ # If it isn't, spawn an instance
+ print "Spawning an irker instance..."
+ irkerd = subprocess.Popen("irkerd", shell=True)
+ time.sleep(0.5)
-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')
-if message == '-':
- for line in fileinput.input('-'):
- send(target, line.rstrip('\n'))
-else:
- send(target, message)
+ target = arguments[0]
+ if not "irc:" in target:
+ target = "irc://chat.freenode.net/{0}".format(target)
+ message = " ".join(arguments[1:])
+ message = message.decode('string_escape')
+ if message == '-':
+ for line in fileinput.input('-'):
+ send(target, line.rstrip('\n'))
+ else:
+ send(target, message)
+ if killafter:
+ if irkerd:
+ irkerd.terminate()
+ irkerd.wait()
+ print "...terminated."