Skip to content

Commit

Permalink
First commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
GUIpsp committed May 19, 2011
0 parents commit 06b3598
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
29 changes: 29 additions & 0 deletions net/GUIpsp/GuiBot/BasePlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.GUIpsp.GuiBot;

public abstract class BasePlugin {

public abstract void main() throws Throwable;

public abstract String version();

public abstract String pluginName();

public abstract String author();

public boolean hidden() {
return false;
};

public final void registerCmd(String cmd, Object toCall, Object that) {
Main.cmdmap.put(cmd, toCall);
Main.classmap.put(cmd, that);

System.out
.println("Hooked command \""
+ cmd
+ "\" to "
+ toCall.toString().split(" ")[toCall.toString().split(
" ").length - 1]);

}
}
52 changes: 52 additions & 0 deletions net/GUIpsp/GuiBot/GuiBot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package net.GUIpsp.GuiBot;

import java.lang.reflect.*;
import java.util.regex.*;

import org.jibble.pircbot.*;

public class GuiBot extends PircBot {
public static String pref = "$";

public GuiBot() {
this.setName("GuiBot");
}

public void onMessage(String channel, String sender, String login,
String hostname, String message) {
if (message.trim().startsWith(pref)) {
message = message.replaceFirst(Matcher.quoteReplacement(pref), "");
String[] parts = message.split(" ");
String first = parts[0];
if ((first.equalsIgnoreCase(("exit"))
|| message.equalsIgnoreCase("quit"))&& sender.equalsIgnoreCase("guipsp")) {
System.exit(0);
} else if (first.equalsIgnoreCase("reload")) {
try {
System.out.println("Reloading...");
Main.reLoad();
System.out.println("Reloaded");
Main.bot.sendMessage(channel, "Plugins reloaded.");
} catch (Throwable e) {
e.printStackTrace();
}
} else if (Main.cmdmap.containsKey(first)) {
try {
Method met = (Method) Main.cmdmap.get(first);
String result = "";
for (int i = 1; i < parts.length; i++) {
if (result.length() != 0)
result += " ";
result += parts[i];
}
met.invoke((BasePlugin) Main.classmap.get(first), channel,
sender, login, hostname, result);

} catch (Throwable e) {
e.printStackTrace();
}

}
}
}
}
51 changes: 51 additions & 0 deletions net/GUIpsp/GuiBot/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package net.GUIpsp.GuiBot;

import java.io.*;

import java.net.*;
import java.util.*;
import org.jibble.pircbot.*;

public class Main {
public static Map classmap = new HashMap();
public static Map cmdmap = new HashMap();
static String plugdir = "/home/guipsp/Desktop/GuiBot/plugins/";
static File directory = new File(plugdir);
public static GuiBot bot = new GuiBot();

public static void main(String[] args) throws Throwable {
reLoad();
bot.setVerbose(true);
try {
bot.connect("irc.esper.net");
} catch (NickAlreadyInUseException e) {
System.out.println("Nickname is in use.");
}
//bot.identify("");
bot.joinChannel("##crow");
}

public static void reLoad() throws Throwable {
classmap.clear();
cmdmap.clear();
URL classUrl;
classUrl = new URL("file://" + plugdir);
URL[] classUrls = { classUrl };
URLClassLoader ucl = new URLClassLoader(classUrls);
File files[] = directory.listFiles();
int count=0;
for (File f : files) {
if (f.getName().endsWith(".class")) {
String safename = f.getName().replaceAll(".class", "");
// bot.sendMessage("##crow", safename);
Class<?> cls = ucl.loadClass(safename);
BasePlugin inst = (BasePlugin) ((Class<?>) cls).newInstance();
inst.getClass().getMethod("main").invoke(inst);
System.out.println("Loaded " + inst.pluginName() + " "
+ inst.version() + " by " + inst.author());
count++;
}
}
System.out.println(count+" plugins");
}
}

0 comments on commit 06b3598

Please sign in to comment.