Skip to content
This repository was archived by the owner on Nov 14, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Local IRCCat config
irccat.xml
# Ant...
build
dist
*~
57 changes: 57 additions & 0 deletions command_runner.py
Original file line number Diff line number Diff line change
@@ -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])
58 changes: 58 additions & 0 deletions examples/command_runner_autocomplete.py
Original file line number Diff line number Diff line change
@@ -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])
6 changes: 5 additions & 1 deletion irccat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 6 additions & 13 deletions src/fm/last/irccat/CatHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
48 changes: 8 additions & 40 deletions src/fm/last/irccat/IRCCat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
Expand Down Expand Up @@ -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 "";
}
Expand All @@ -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);
Expand Down