private void connect() throws UnknownHostException, IOException { IrcServer = new Socket(serverName, 6667); BufferedReader br = new BufferedReader( new InputStreamReader(IrcServer.getInputStream(), BotStats.getInstance().getCharset())); PrintWriter pw = new PrintWriter( new OutputStreamWriter( IrcServer.getOutputStream(), BotStats.getInstance().getCharset()), true); ih = new InputHandler(br); oh = new OutputHandler(pw); ih.start(); oh.start(); new Message("", "NICK", BotStats.getInstance().getBotname(), "").send(); new Message( "", "USER", "goat" + " nowhere.com " + serverName, BotStats.getInstance().getVersion()) .send(); // we sleep until we are connected, don't want to send these next messages too soon while (!connected) { try { sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } joinChannels(); }
public void run() { String messageString; long lastActivity = System.currentTimeMillis(); while (keeprunning) { try { if (in.ready()) { messageString = in.readLine(); if (messageString == null) continue; Message m = new Message(messageString); if (m.getCommand().matches("\\d+")) try { int intcommand = Integer.parseInt(m.getCommand()); if (intcommand == Constants.RPL_ENDOFMOTD) { System.err.println("MOTD SEEN, must be connected."); if (connected) joinChannels(); connected = true; } else if (intcommand == Constants.ERR_NICKNAMEINUSE) { System.err.println("NICKNAMEINUSE"); namecount++; BotStats.getInstance().setBotname(botdefaultname + namecount); new Message("", "NICK", BotStats.getInstance().getBotname(), "").send(); new Message( "", "USER", BotStats.getInstance().getBotname() + " nowhere.com " + BotStats.getInstance().getServername(), BotStats.getInstance().getClientName() + " v." + BotStats.getInstance().getVersion()) .send(); System.err.println("Setting nick to:" + botdefaultname + namecount); } } catch (NumberFormatException nfe) { // we ignore this System.err.println("Unknown command:" + m.getCommand()); } if (m.getCommand().equals("PING")) { outqueue.add(new Message("", "PONG", "", m.getTrailing())); if (debug) System.out.println("PUNG at " + new Date()); } else if (BotStats.getInstance().containsIgnoreName(m.getSender())) { if (debug) System.out.println("Ignored: " + m); } else { inqueue.add(m); // add to inqueue if (debug) System.out.println(m.toString()); // System.out.println("Inbuffer: prefix: " + m.prefix + " params: " + m.params + " // trailing:" // + m.trailing + " command:" + m.command + " sender: " + m.sender + "\n " // + "isCTCP:" + m.isCTCP + " isPrivate:" + m.isPrivate + " CTCPCommand:" + // m.CTCPCommand // + " CTCPMessage:" + m.CTCPMessage); } lastActivity = System.currentTimeMillis(); } else { if (System.currentTimeMillis() - lastActivity > 400000) { System.err.println("400 seconds since last activity! Attempting reconnect"); in.close(); oh.disconnect(); keeprunning = false; reconnect(); return; } sleep(100); } } catch (IOException ioe) { System.out.println("EOF on connection: " + ioe.getMessage()); } catch (InterruptedException ie) { System.out.println("Interrupted: " + ie.getMessage()); } catch (Exception e) { System.err.println("Unexpected exception in InputHandler :"); e.printStackTrace(); } } }
private void joinChannels() { String[] channels = BotStats.getInstance().getChannels(); for (String channel : channels) new Message("", "JOIN", channel, "").send(); }
/** * Loads and unloads Modules. Provides methods allowing you to find out what Modules are loaded, to * control the Modules, and to return them. * * @version Date: 17-Dec-2003 * @author */ public class ModuleController { private ExecutorService pool = Executors.newCachedThreadPool(); public ExecutorService getPool() { return pool; } // private ArrayList<Module> loadedModules = new ArrayList<Module>(); private ArrayList<Class<? extends Module>> allModules = new ArrayList<Class<? extends Module>>(); // private ArrayList<String> allCommands = new ArrayList<String>() ; private ArrayList<File> allScriptModules = new ArrayList<File>(); private BotStats bot = BotStats.getInstance(); public ModuleController() { buildAllModulesList(); buildScriptModulesList(); } /** * Loads a module. * * @param moduleName * @return * @throws IllegalAccessException * @throws InstantiationException * @throws ClassNotFoundException * @throws IOException * @throws ScriptException * @throws NoSuchMethodException * @throws NoClassDefFoundError * @throws ClassCastException */ public Module load(String moduleName) throws ClassNotFoundException, IllegalAccessException, InstantiationException, IOException, NoSuchMethodException, ScriptException { if (moduleName.contains(".")) { // if the name has a dot, assume a script System.out.print("Loading Module " + moduleName + " ... "); File file = new File(scriptDir(moduleName) + File.separatorChar + moduleName); BufferedReader bis = new BufferedReader(new FileReader(file)); String line; String script = ""; while ((line = bis.readLine()) != null) { script += line + "\n"; } String extension = moduleName.replaceAll(".*\\.", ""); ScriptEngine engine = new ScriptEngineManager().getEngineByExtension(extension); engine.eval(script); Invocable inv = (Invocable) engine; Module mod; Object moduleCandidate = inv.invokeFunction("getInstance"); if (moduleCandidate instanceof Module) { mod = (Module) moduleCandidate; } else { mod = new JSR223Module(extension, script); } mod.moduleName = moduleName; bootModule(mod); return mod; } else { moduleName = "goat.module." + moduleName; return load(Class.forName(moduleName)); } } public Module loadInAllChannels(String moduleName) throws ClassNotFoundException, IllegalAccessException, InstantiationException, IOException, NoSuchMethodException, ScriptException { Module ret = load(moduleName); if (null == ret) // wasn't loaded ret = getLoaded(moduleName); ret.inAllChannels = true; return ret; } public Module load(Class<?> modClass) throws IllegalAccessException, InstantiationException { if (null == modClass) return null; System.out.print("Loading Module " + modClass.getName() + " ... "); Module module = null; // return null if module is already loaded if (null != getLoaded(modClass)) return null; module = (Module) modClass.newInstance(); bootModule(module); module.moduleName = modClass.getName().replace("goat.module.", ""); return module; } private void bootModule(Module module) { if (null == module) return; pool.execute(module); while (!module .isRunning()) { // wait to make sure module is running before adding it to loaded module // list try { Thread.sleep( 5); // short wait, it shouldn't take too long for the thread pool to start the module } catch (InterruptedException ie) { } } System.out.print("running ... "); bot.addModule(module); bot.addCommands(module.getCommands()); System.out.println("loaded."); } public Module loadInAllChannels(Class<?> modClass) throws IllegalAccessException, InstantiationException { Module ret = load(modClass); if (null != ret) ret.inAllChannels = true; return ret; } public boolean unload(String moduleName) { Module mod = getLoaded(moduleName); if (null == mod) return false; else { bot.removeModule(mod); mod.stopDispatcher(); } return true; } public Module getLoaded(int i) { return bot.getModules().get(i); } public Module getLoaded(Class<?> modClass) { Iterator<Module> it = bot.getModules().listIterator(); Module ret = null; if (!modClass.equals(goat.core.Module.class) && !modClass.equals(Object.class)) while (it.hasNext()) { Module mod = it.next(); if (modClass.isInstance(mod)) ret = mod; } return ret; } public Module getLoaded(String modName) { List<Module> mods = bot.getModules(); for (Module mod : mods) { if (mod.moduleName.equals(modName)) { return mod; } } return null; } /** * Build the list of all public classes in package goat.module. * * <p>This gets sort of ugly. It sure would be nice if the java reflection API could give us a * list of all classes in a package, wouldn't it? */ private void buildAllModulesList() { if (bot.isTesting()) return; Reflections reflections = new Reflections("goat"); Set<Class<? extends Module>> allModules = reflections.getSubTypesOf(Module.class); ArrayList<String> tempList = new ArrayList<String>(); for (Class<? extends Module> modClass : allModules) { tempList.add(modClass.getName()); } Collections.sort(tempList); System.out.println("Available Modules: "); for (String modName : tempList) { System.out.println(" " + modName); } System.out.println(); } public final String pyModDir = "src" + File.separatorChar + "main" + File.separatorChar + "python"; /** Looks in script dir(s) and assumes any files in there are modules. */ private void buildScriptModulesList() { // FIXME: only python mods dir atm, should include anything that // scriptDir() might return String path = pyModDir; File folder = new File(path); File[] listOfFiles = folder.listFiles(); System.out.println("Available script modules:"); for (File scriptModuleFile : listOfFiles) { System.out.println(" " + scriptModuleFile.getName()); allScriptModules.add(scriptModuleFile); } } public List<Class<? extends Module>> getAllModules() { return allModules; } public String scriptDir(String name) { String ret = "scripts"; if (name.endsWith(".py")) ret = pyModDir; return ret; } }