diff --git a/.gitignore b/.gitignore index bca9f4f..c6344d9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Local IRCCat config +irccat.xml # Ant... build dist +*~ diff --git a/command_runner.py b/command_runner.py new file mode 100755 index 0000000..9f6db1d --- /dev/null +++ b/command_runner.py @@ -0,0 +1,57 @@ +#!/usr/bin/python +import os, sys, re, subprocess, difflib + +# If this script is set as your command handler, when any ?command is run in IRC it will +# look in the path defined below for a script matching that command name and run it. +# +# e.g. ?uptime would look in "/usr/share/irccat/" (the default) for any script called +# "uptime", with any extension. It would happily run both uptime.sh and uptime.py, or +# a script in whatever language you like. Command names are limited to [0-9a-z]. + +paths = ['/opt/irccat/irccat-commands/', '/opt/irccat/irccat-commands-contrib/'] + +args = sys.argv[1] +bits = args.split(' ') +command = bits[3].lower() + +found = False +commands = [] + +os.chdir("/opt/irccat/irccat-data") + +if re.match('^[a-z0-9]+$', command): + for path in paths: + for file in os.listdir(path): + + # Build the index, in case we never find a command + m = re.match('^([a-z0-9]+)\.[a-z]+$', file) + if m: + commands.append(m.group(1)) + + if re.match('^%s\.[a-z]+$' % command, file): + found = True + procArgs = [path + file] + procArgs.extend(bits) + proc = subprocess.Popen(procArgs, stdout=subprocess.PIPE) + stdout = proc.stdout + + while True: + # We do this to avoid buffering from the subprocess stdout + sys.stdout.write(os.read(stdout.fileno(), 65535)) + sys.stdout.flush() + + if proc.poll() != None: + break + + if proc.poll() != None: + break + + if found == True: + break + +if found == False: + + # Didn't find a command to run. Maybe can we help. + matches = difflib.get_close_matches(command, commands, 1) + if matches: + print "%s, I don't understand '%s'. Did you mean '%s'?" % (bits[0], command, matches[0]) diff --git a/examples/command_runner_autocomplete.py b/examples/command_runner_autocomplete.py new file mode 100755 index 0000000..15b2e5e --- /dev/null +++ b/examples/command_runner_autocomplete.py @@ -0,0 +1,58 @@ +#!/usr/bin/python +import os, sys, re, subprocess, difflib + +# If this script is set as your command handler, when any ?command is run in IRC it will +# look in the path defined below for a script matching that command name and run it. +# +# e.g. ?uptime would look in "/usr/share/irccat/" (the default) for any script called +# "uptime", with any extension. It would happily run both uptime.sh and uptime.py, or +# a script in whatever language you like. Command names are limited to [0-9a-z]. + +path = '/usr/share/irccat/' + +args = sys.argv[1] +bits = args.split(' ') +command = bits[3].lower() + +found = False +commands = [] + +if re.match('^[a-z0-9]+$', command): + for file in os.listdir(path): + + # Build the index, in case we never find a command + m = re.match('^([a-z0-9]+)\.[a-z]+$', file) + if m: + commands.append(m.group(1)) + + if re.match('^%s\.[a-z]+$' % command, file): + found = True + + procArgs = [path + file] + procArgs.extend(bits) + proc = subprocess.Popen(procArgs, stdout=subprocess.PIPE) + stdout = proc.stdout + + while True: + + # We do this to avoid buffering from the subprocess stdout + data = os.read(stdout.fileno(), 65536) + + # Kill useless lines in the output + for line in re.split("[\r\n]+", data): + line = line.strip() + if line: + print line + + sys.stdout.flush() + + if proc.poll() != None: + break + + +if found == False: + + # Didn't find a command to run. Maybe can we help. + matches = difflib.get_close_matches(command, commands, 1) + if matches: + print "%s, I don't understand '%s'. Did you mean '%s'?" % (bits[0], command, matches[0]) diff --git a/irccat.sh b/irccat.sh index 881a386..76ed82f 100755 --- a/irccat.sh +++ b/irccat.sh @@ -10,4 +10,8 @@ do cp="$cp:$jar" done -exec java -cp "build/:$cp" fm.last.irccat.IRCCat "${1:-irccat.xml}" +while true +do + java -cp "build/:$cp" fm.last.irccat.IRCCat "${1:-irccat.xml}" + sleep 10 +done diff --git a/src/fm/last/irccat/CatHandler.java b/src/fm/last/irccat/CatHandler.java index 8020bc0..c5c43cb 100644 --- a/src/fm/last/irccat/CatHandler.java +++ b/src/fm/last/irccat/CatHandler.java @@ -76,19 +76,12 @@ public void run(){ } // now send it to the recipients: - if(all) { - if(topic) { - bot.catTopicToAll(inputLine); - }else { - bot.catStuffToAll(inputLine); - } - }else { - if(topic) { - bot.catTopic(inputLine, recipients); - }else { - bot.catStuff(inputLine, recipients); - } - } + if(topic) { + bot.catTopic(inputLine, recipients); + }else { + bot.catStuff(inputLine, recipients); + } + } in.close(); //System.out.println("Handler finished."); diff --git a/src/fm/last/irccat/IRCCat.java b/src/fm/last/irccat/IRCCat.java index b269b67..012bffa 100644 --- a/src/fm/last/irccat/IRCCat.java +++ b/src/fm/last/irccat/IRCCat.java @@ -116,8 +116,15 @@ public IRCCat(XMLConfiguration c) throws Exception { "server.password", "")); if(tries>1) Thread.sleep(10000); } + } catch (UnknownHostException uhe) { + // we get stuck here on java.net.UnknownHostException + // so just exit and let supervisor restart us + System.err.println(uhe.toString()); + System.err.println(uhe.getMessage()); + System.exit(-1); } catch (Exception e) { - System.out.println(e.toString()); + System.err.println(e.toString()); + System.err.println(e.getMessage()); } } @@ -334,31 +341,6 @@ protected String handleBuiltInCommand(String cmd, String sender) { return "Joining: " + toks[1]; } - // PART A CHANNEL - if (method.equals("part") && toks.length == 2) { - sendMessage(toks[1], "<" + sender + "> !" + cmd); - partChannel(toks[1]); - return "Leaving: " + toks[1]; - } - - // BROADCAST MSG TO ALL CHANNELS - if (method.equals("spam")) { - this.catStuffToAll("<" + sender + "> " + cmd.substring(5)); - } - - // LIST CHANNELS THE BOT IS IN - if (method.equals("channels")) { - String[] c = getChannels(); - StringBuffer sb = new StringBuffer("I am in " + c.length - + " channels: "); - for (int i = 0; i < c.length; ++i) - sb.append(c[i] + " "); - return sb.toString(); - } - - // EXIT() - if (method.equals("exit")) - System.exit(0); return ""; } @@ -369,20 +351,6 @@ public void catTopic(String stuff, String[] recips) { } } - public void catTopicToAll(String stuff) { - String[] channels = getChannels(); - for (int i = 0; i < channels.length; i++) { - changeTopic(channels[i], stuff); - } - } - - public void catStuffToAll(String stuff) { - String[] channels = getChannels(); - for (int i = 0; i < channels.length; i++) { - sendMsg(channels[i], stuff); - } - } - public void catStuff(String stuff, String[] recips) { for (int ci = 0; ci < recips.length; ci++) { sendMsg(recips[ci], stuff);