public void connect() { Statement statement = null; databaseType = foxbot.getConfig().getDatabaseType(); url = databaseType.equalsIgnoreCase("mysql") ? String.format( "jdbc:mysql://%s:%s/%s", foxbot.getConfig().getDatabaseHost(), foxbot.getConfig().getDatabasePort(), foxbot.getConfig().getDatabaseName()) : "jdbc:sqlite:data/bot.db"; try { if (databaseType.equalsIgnoreCase("sqlite")) { Class.forName("org.sqlite.JDBC"); } if (databaseType.equalsIgnoreCase("mysql")) { String user = foxbot.getConfig().getDatabaseUser(); String password = foxbot.getConfig().getDatabasePassword(); connection = DriverManager.getConnection(url, user, password); } else { connection = DriverManager.getConnection(url); } statement = connection.createStatement(); statement.setQueryTimeout(30); statement.executeUpdate( "CREATE TABLE IF NOT EXISTS tells (tell_time VARCHAR(32), sender VARCHAR(32), receiver VARCHAR(32), message VARCHAR(1024), used TINYINT)"); statement.executeUpdate( "CREATE TABLE IF NOT EXISTS bans (channel VARCHAR(64), target VARCHAR(32), hostmask VARCHAR(64), reason VARCHAR(1024), banner VARCHAR(32), ban_time BIGINT)"); statement.executeUpdate( "CREATE TABLE IF NOT EXISTS kicks (channel VARCHAR(64), target VARCHAR(32), hostmask VARCHAR(64), reason VARCHAR(1024), kicker VARCHAR(32), kick_time BIGINT)"); statement.executeUpdate( "CREATE TABLE IF NOT EXISTS mutes (channel VARCHAR(64), target VARCHAR(32), hostmask VARCHAR(64), reason VARCHAR(1024), muter VARCHAR(32), mute_time BIGINT)"); } catch (SQLException | ClassNotFoundException ex) { Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex); foxbot.disconnect(); } finally { try { if (statement != null) { statement.close(); } if (connection != null) { connection.close(); connection = null; } } catch (SQLException ex) { Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex); } } }
@Override public void execute(final MessageEvent event, final String[] args) { User sender = event.getUser(); if (args.length == 1) { if (args[0].equalsIgnoreCase("list")) { List<String> tells = foxbot.getDatabase().getTells(sender.getNick(), true); if (!tells.isEmpty()) { for (String tell : tells) { foxbot.sendNotice(sender, Utils.colourise(tell)); } return; } foxbot.sendNotice(sender, "No messages for you :<"); return; } if (args[0].equalsIgnoreCase("clean")) { foxbot.getDatabase().cleanTells(sender.getNick()); foxbot.sendNotice(sender, "Deleted all of your read messages."); return; } } if (args.length > 1) { String nick = args[0]; StringBuilder message = new StringBuilder(args[1]); for (int arg = 2; arg < args.length; arg++) { message.append(" ").append(args[arg]); } foxbot.getDatabase().addTell(sender.getNick(), nick, message.toString()); foxbot.sendNotice(sender, String.format("Tell added for %s", nick)); return; } foxbot.sendNotice( sender, String.format( "Wrong number of args! Use %stell <nick> <message> or %stell list", foxbot.getConfig().getCommandPrefix(), foxbot.getConfig().getCommandPrefix())); }
public void reconnect() { if (connection == null) { try { if (databaseType.equalsIgnoreCase("sqlite")) { Class.forName("org.sqlite.JDBC"); } if (databaseType.equalsIgnoreCase("mysql")) { String user = foxbot.getConfig().getDatabaseUser(); String password = foxbot.getConfig().getDatabasePassword(); connection = DriverManager.getConnection(url, user, password); } else { connection = DriverManager.getConnection(url); } } catch (SQLException | ClassNotFoundException ex) { Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex); foxbot.disconnect(); } } }
@Override public void execute(MessageEvent event, String[] args) { User sender = event.getUser(); Channel channel = event.getChannel(); if (args.length > 0) { StringBuilder command = new StringBuilder(); boolean verbose = args[0].equals("-v"); for (int i = verbose ? 1 : 0; i < args.length; i++) { command.append(args[i]).append(" "); } try { Process proc = runtime.exec(command.toString()); BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); // Prevent spam on long results int count = 0; String line; while ((line = stdInput.readLine()) != null) { if (!verbose && count >= 3) { channel.send().message("Max output reached. Use -v to show full output."); break; } if (!line.isEmpty()) { channel.send().message(line); count++; } } stdInput.close(); while ((line = stdError.readLine()) != null) { if (!verbose && count >= 3) { channel.send().message("Max output reached. Use -v to show full output."); break; } if (!line.isEmpty()) { channel.send().message(line); count++; } } stdError.close(); proc.destroy(); } catch (IOException ex) { foxbot.getLogger().error("Error occurred while executing system command", ex); } return; } sender .send() .notice( String.format( "Wrong number of args! Use %ssystem [-v] <command>", foxbot.getConfig().getCommandPrefix())); }
@Override public void execute(MessageEvent event, String[] args) { User sender = event.getUser(); Channel channel = event.getChannel(); if (args.length > 0) { String plugin = args[0].toLowerCase(); String url = String.format( "http://api.bukget.org/3/search/plugin_name/like/%s%s", plugin, (args.length) == 1 ? "" : ("?size=" + args[1])); Connection conn = Jsoup.connect(url).timeout(500).followRedirects(true).ignoreContentType(true); String json; try { json = conn.get().text(); } catch (IOException ex) { foxbot.log(ex); channel .send() .message( Utils.colourise( String.format( "(%s) &cAn error occurred while querying the Bukget API!", Utils.munge(sender.getNick())))); return; } if (json.equals("[]")) { channel .send() .message( Utils.colourise( String.format("(%s) &cNo results found!", Utils.munge(sender.getNick())))); return; } JSONArray jsonArray = new JSONArray(json); JSONObject found = null; for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String name = jsonObject.getString("plugin_name"); if (name.equalsIgnoreCase(plugin)) { found = jsonObject; break; } } if (found == null) { found = jsonArray.getJSONObject(0); } String name = found.getString("plugin_name"); String description = found.getString("description"); String pluginUrl = String.format("http://dev.bukkit.org/bukkit-plugins/%s/", found.getString("slug")); if (description.isEmpty()) { description = "No description"; } channel .send() .message( Utils.colourise( String.format( "(%s) &2Name:&r %s &2Description:&r %s &2URL:&r %s", Utils.munge(sender.getNick()), name, description, pluginUrl))); return; } foxbot.sendNotice( sender, String.format( "Wrong number of args! Use %sbukkitsearch <plugin>", foxbot.getConfig().getCommandPrefix())); }