From 96e8dd539ff5316d25c24ece9d5934583bf7d41c Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Tue, 26 Nov 2013 18:32:42 -0500 Subject: [PATCH] An attempt at making irk clean up after itself. Perplexingly, the terminate() method call doesn't. --- irk | 54 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/irk b/irk index a8a2f42..49a083f 100755 --- a/irk +++ b/irk @@ -4,6 +4,8 @@ # 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. # @@ -14,6 +16,7 @@ import fileinput import subprocess import time import os +import getopt def send(target, message): data = {"to": target, "privmsg" : message} @@ -24,23 +27,36 @@ def send(target, 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." -- 2.26.2