Search in sources :

Example 1 with CommandArgument

use of pcl.lc.irc.entryClasses.CommandArgument in project LanteaBot by PC-Logix.

the class Weather method initHook.

@Override
protected void initHook() {
    local_command = new Command("weather", new CommandArgumentParser(1, new CommandArgument(ArgumentTypes.STRING, "Location"))) {

        @Override
        public CommandChainStateObject onExecuteSuccess(Command command, String nick, String target, GenericMessageEvent event, String params) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
            Helper.sendMessage(target, getWeather(this.argumentParser.getArgument("Location")));
            return new CommandChainStateObject();
        }
    };
    local_command.registerAlias("w");
    local_command.setHelpText("Returns weather data for the supplied postal code, or Place name");
    IRCBot.registerCommand(local_command);
}
Also used : Command(pcl.lc.irc.entryClasses.Command) XPathExpressionException(javax.xml.xpath.XPathExpressionException) CommandArgument(pcl.lc.irc.entryClasses.CommandArgument) CommandArgumentParser(pcl.lc.irc.entryClasses.CommandArgumentParser) CommandChainStateObject(pcl.lc.utils.CommandChainStateObject) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) GenericMessageEvent(org.pircbotx.hooks.types.GenericMessageEvent) SAXException(org.xml.sax.SAXException)

Example 2 with CommandArgument

use of pcl.lc.irc.entryClasses.CommandArgument in project LanteaBot by PC-Logix.

the class Hiss method initCommands.

private void initCommands() {
    local_command = new Command("hiss", new CommandArgumentParser(1, new CommandArgument(ArgumentTypes.STRING))) {

        @Override
        public CommandChainStateObject onExecuteSuccess(Command command, String nick, String target, GenericMessageEvent event, String params) {
            String str = this.argumentParser.getArgument(0);
            if (str == null || str.equals("")) {
                Helper.sendMessage(target, "Snek?", nick);
            } else {
                Helper.sendMessage(target, str.replaceAll("s", "ss").replaceAll("S", "SS"));
            }
            return new CommandChainStateObject();
        }
    };
    local_command.registerAlias("snake");
    local_command.registerAlias("snek");
    local_command.setHelpText("Sneks the text");
}
Also used : Command(pcl.lc.irc.entryClasses.Command) CommandArgument(pcl.lc.irc.entryClasses.CommandArgument) CommandArgumentParser(pcl.lc.irc.entryClasses.CommandArgumentParser) CommandChainStateObject(pcl.lc.utils.CommandChainStateObject) GenericMessageEvent(org.pircbotx.hooks.types.GenericMessageEvent)

Example 3 with CommandArgument

use of pcl.lc.irc.entryClasses.CommandArgument in project LanteaBot by PC-Logix.

the class Seen method initHook.

@Override
protected void initHook() {
    local_command = new Command("seen", new CommandArgumentParser(1, new CommandArgument(ArgumentTypes.STRING, "Nick"))) {

        @Override
        public CommandChainStateObject onExecuteSuccess(Command command, String nick, String target, GenericMessageEvent event, ArrayList<String> params) throws Exception {
            String dest;
            if (event.getClass().getName().equals("org.pircbotx.hooks.events.MessageEvent")) {
                dest = target;
            } else {
                dest = "query";
            }
            PreparedStatement getSeen = Database.getPreparedStatement("getLastSeen");
            String targetNick = this.argumentParser.getArgument("Nick");
            getSeen.setString(1, targetNick.toLowerCase());
            ResultSet results = getSeen.executeQuery();
            if (results.next()) {
                if (dest.equals("query")) {
                    event.respond(targetNick + " was last seen " + formatTime(System.currentTimeMillis() - results.getLong(1)) + "ago. Saying: " + ((results.getString(2).isEmpty()) ? "No Record" : results.getString(2)));
                } else {
                    Helper.sendMessage(dest, targetNick + " was last seen " + formatTime(System.currentTimeMillis() - results.getLong(1)) + "ago. " + ((results.getString(2) == null) ? "No Record" : results.getString(2)));
                }
            } else {
                if (dest.equals("query")) {
                    event.respond(targetNick + " has not been seen");
                } else {
                    event.getBot().sendIRC().message(dest, targetNick + " has not been seen");
                }
            }
            return new CommandChainStateObject();
        }
    };
    local_command.setHelpText("Tells you the last time a user was active.  Active means they sent a message");
    IRCBot.registerCommand(local_command);
    Database.addStatement("CREATE TABLE IF NOT EXISTS LastSeen(user PRIMARY KEY, timestamp, doing)");
    Database.addUpdateQuery(4, "ALTER TABLE LastSeen ADD doing DEFAULT NULL");
    Database.addPreparedStatement("updateLastSeen", "REPLACE INTO LastSeen(user, timestamp, doing) VALUES (?, ?, ?);");
    Database.addPreparedStatement("getLastSeen", "SELECT timestamp, doing FROM LastSeen WHERE LOWER(user) = ? GROUP BY LOWER(user) ORDER BY timestamp desc");
// I Have NO idea where these came from, or why they are here.
// Database.addPreparedStatement("updateInfo","REPLACE INTO Info(key, data, doing) VALUES (?, ?, ?);");
// Database.addPreparedStatement("getInfo","SELECT data FROM Info WHERE key = ?;");
// Database.addPreparedStatement("getInfoAll","SELECT key, data FROM Info;");
// Database.addPreparedStatement("removeInfo","DELETE FROM Info WHERE key = ?;");
}
Also used : Command(pcl.lc.irc.entryClasses.Command) CommandArgument(pcl.lc.irc.entryClasses.CommandArgument) ResultSet(java.sql.ResultSet) CommandArgumentParser(pcl.lc.irc.entryClasses.CommandArgumentParser) CommandChainStateObject(pcl.lc.utils.CommandChainStateObject) PreparedStatement(java.sql.PreparedStatement) GenericMessageEvent(org.pircbotx.hooks.types.GenericMessageEvent)

Example 4 with CommandArgument

use of pcl.lc.irc.entryClasses.CommandArgument in project LanteaBot by PC-Logix.

the class LookUp method initHook.

@Override
protected void initHook() {
    local_command_lookup = new Command("lookup", new CommandArgumentParser(1, new CommandArgument(ArgumentTypes.STRING, "Address"), new CommandArgument(ArgumentTypes.STRING, "RecordType"))) {

        @Override
        public CommandChainStateObject onExecuteSuccess(Command command, String nick, String target, GenericMessageEvent event, ArrayList<String> params) {
            String address = this.argumentParser.getArgument("Address");
            try {
                InetAddress inetAddress;
                String recordType = this.argumentParser.getArgument("RecordType");
                if (recordType == null)
                    recordType = "A";
                // if first character is a digit then assume is an address
                if (Character.isDigit(address.charAt(0))) {
                    // convert address from string representation to byte array
                    byte[] b = new byte[4];
                    String[] bytes = address.split("[.]");
                    for (int i = 0; i < bytes.length; i++) {
                        b[i] = new Integer(bytes[i]).byteValue();
                    }
                    // get Internet Address of this host address
                    inetAddress = InetAddress.getByAddress(b);
                } else {
                    // get Internet Address of this host name
                    inetAddress = InetAddress.getByName(address);
                }
                // get the default initial Directory Context
                InitialDirContext iDirC = new InitialDirContext();
                // get the DNS records for inetAddress
                Attributes attributes = iDirC.getAttributes("dns:/" + inetAddress.getHostName());
                // get an enumeration of the attributes and print them out
                NamingEnumeration attributeEnumeration = attributes.getAll();
                while (attributeEnumeration.hasMore()) {
                    String record = "" + attributeEnumeration.next();
                    String[] thisType = record.split(":", 2);
                    if (thisType[0].equalsIgnoreCase(recordType) || recordType.equalsIgnoreCase("any")) {
                        Helper.sendMessage(target, "" + record);
                    }
                }
                attributeEnumeration.close();
            } catch (UnknownHostException exception) {
                Helper.sendMessage(target, "ERROR: No Internet Address for '" + address + "'");
            } catch (NamingException exception) {
                Helper.sendMessage(target, "ERROR: No DNS record for '" + address + "'");
                Helper.sendMessage(target, exception.getExplanation() + " Resolved: " + exception.getResolvedName() + " Unresolved: " + exception.getRemainingName());
                exception.printStackTrace();
            }
            // Helper.sendMessage(target, output.replace(params.get(0) + "/", " ").replaceAll("((?::0\\b){2,}):?(?!\\S*\\b\\1:0\\b)(\\S*)", "::$2"), nick);
            return new CommandChainStateObject();
        }
    };
    local_command_lookup.setHelpText("Returns DNS information");
    local_command_rdns = new Command("rdns") {

        @Override
        public CommandChainStateObject onExecuteSuccess(Command command, String nick, String target, GenericMessageEvent event, ArrayList<String> params) throws UnknownHostException {
            InetAddress addr = InetAddress.getByName(params.get(0));
            String host = addr.getCanonicalHostName();
            String output = "Reverse DNS Info for " + params.get(0) + " " + host;
            Helper.sendMessage(target, output, nick);
            return new CommandChainStateObject();
        }
    };
    local_command_rdns.setHelpText("Returns Reverse DNS information");
    IRCBot.registerCommand(local_command_lookup);
    IRCBot.registerCommand(local_command_rdns);
}
Also used : UnknownHostException(java.net.UnknownHostException) CommandArgument(pcl.lc.irc.entryClasses.CommandArgument) Attributes(javax.naming.directory.Attributes) CommandArgumentParser(pcl.lc.irc.entryClasses.CommandArgumentParser) NamingEnumeration(javax.naming.NamingEnumeration) InitialDirContext(javax.naming.directory.InitialDirContext) GenericMessageEvent(org.pircbotx.hooks.types.GenericMessageEvent) Command(pcl.lc.irc.entryClasses.Command) CommandChainStateObject(pcl.lc.utils.CommandChainStateObject) NamingException(javax.naming.NamingException) InetAddress(java.net.InetAddress)

Example 5 with CommandArgument

use of pcl.lc.irc.entryClasses.CommandArgument in project LanteaBot by PC-Logix.

the class MCInfo method initCommands.

private void initCommands() {
    local_command = new Command("mcinfo", new CommandArgumentParser(1, new CommandArgument(ArgumentTypes.STRING, "Address"), new CommandArgument(ArgumentTypes.INTEGER, "Port"))) {

        @Override
        public CommandChainStateObject onExecuteSuccess(Command command, String nick, String target, GenericMessageEvent event, ArrayList<String> params) throws IOException {
            String server = this.argumentParser.getArgument("Address");
            int port = this.argumentParser.getInt("Port");
            if (port <= 0)
                port = 25565;
            System.out.println(server + ":" + port);
            MinecraftPingReply data;
            try {
                data = new MinecraftPing().getPing(server, port);
                Helper.sendMessage(target, "Server info: Version: " + data.getVersion() + " MOTD: " + data.getMotd() + " Players: " + data.getOnlinePlayers() + "/" + data.getMaxPlayers(), nick);
            } catch (UnknownHostException e) {
                Helper.sendMessage(target, "Server could not be found.", nick);
            } catch (SocketTimeoutException e) {
                Helper.sendMessage(target, "Connection timed out on port " + port + ". " + ((port == 25565) ? "Does the server use a custom port?" : "Did you specify the right port?"), nick);
            }
            return new CommandChainStateObject();
        }
    };
    local_command.setHelpText("Returns information about a MC Server");
}
Also used : UnknownHostException(java.net.UnknownHostException) CommandArgument(pcl.lc.irc.entryClasses.CommandArgument) CommandArgumentParser(pcl.lc.irc.entryClasses.CommandArgumentParser) MinecraftPingReply(pcl.lc.utils.mcping.MinecraftPingReply) IOException(java.io.IOException) MinecraftPing(pcl.lc.utils.mcping.MinecraftPing) GenericMessageEvent(org.pircbotx.hooks.types.GenericMessageEvent) SocketTimeoutException(java.net.SocketTimeoutException) Command(pcl.lc.irc.entryClasses.Command) CommandChainStateObject(pcl.lc.utils.CommandChainStateObject)

Aggregations

CommandArgument (pcl.lc.irc.entryClasses.CommandArgument)35 CommandArgumentParser (pcl.lc.irc.entryClasses.CommandArgumentParser)35 GenericMessageEvent (org.pircbotx.hooks.types.GenericMessageEvent)34 Command (pcl.lc.irc.entryClasses.Command)34 CommandChainStateObject (pcl.lc.utils.CommandChainStateObject)30 PreparedStatement (java.sql.PreparedStatement)8 ResultSet (java.sql.ResultSet)8 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 JSONException (org.json.JSONException)3 URL (java.net.URL)2 UnknownHostException (java.net.UnknownHostException)2 SQLException (java.sql.SQLException)2 DecimalFormat (java.text.DecimalFormat)2 List (java.util.List)2 Random (java.util.Random)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 Matcher (java.util.regex.Matcher)2