public void onTrigger( String channel, String sender, String login, String hostname, String message, String trigger) { String toNick = null; String msg = null; try { toNick = message.substring(0, message.indexOf(' ')); msg = message.substring(message.indexOf(' ')); } catch (IndexOutOfBoundsException ioobe) { bot.msg(channel, "Bogus message format: try !" + TRIGGER + " <nick> <message>."); return; } for (User user : bot.getUsers(channel)) { if (user.equals(toNick)) { bot.msg(channel, toNick + " is here right now, you dumbass!"); return; } } saveTell(channel, sender, toNick, msg); }
/** * Sends this factoid to the channel * * @param sender The nick of the sender */ private void send(String sender) { if (isMessage()) { bot.msg(getChannel(), getReply().replace("$sender", sender), true); } else { // TODO - action evades spam, and all the local sendMessage routines bot.sendAction(getChannel(), getReply().replace("$sender", sender)); } }
private void tell(String channel, String toNick) { List<TellItem> items = JWorm.getWith( TellItem.class, "where `to`='" + toNick + "' and `channel`='" + channel + "'"); for (TellItem item : items) { StringBuilder message = new StringBuilder(); message .append(item.getTo()) .append(": ") .append(item.getFrom()) .append(" told me to tell you this: ") .append(item.getMessage()); bot.msg(channel, message.toString()); item.delete(); } }
private void saveTell(String channel, String fromNick, String toNick, String msg) { TellItem newItem = new TellItem(fromNick, toNick, msg, new Date().getTime(), channel); newItem.insert(); bot.msg(channel, "I'll tell " + toNick + " this: " + msg); }
public void onTrigger( String channel, String sender, String login, String hostname, String message, String trigger) { if (trigger.equals(TRIGGER_ADD)) { // Trying to add a new factoid String type; String factoidTrigger, reply; if (message.contains(SEPARATOR_MESSAGE)) { type = "message"; factoidTrigger = message.substring(0, message.indexOf(SEPARATOR_MESSAGE)); reply = message.substring(message.indexOf(SEPARATOR_MESSAGE) + SEPARATOR_MESSAGE.length()); } else if (message.contains(SEPARATOR_ACTION)) { type = "action"; factoidTrigger = message.substring(0, message.indexOf(SEPARATOR_ACTION)); reply = message.substring(message.indexOf(SEPARATOR_ACTION) + SEPARATOR_ACTION.length()); } else { // If it's neither a message nor an action bot.msg(channel, "What? Don't give me that nonsense, " + sender + "."); return; } if (find(channel, factoidTrigger, false).size() != 0) { bot.msg(channel, "But, " + sender + ", " + factoidTrigger + "."); return; } // First add the new item to the SQL db try { sqlHandler.insert( "INSERT INTO " + FACTOID_TABLE + " (`type`, `trigger`, `reply`, `author`, `channel`) VALUES ('" + type + "', '" + factoidTrigger + "', '" + reply + "', '" + sender + "', '" + channel + "');"); } catch (SQLException e) { System.err.println("Factoid insertion: SQL Exception: " + e); } // Then add it to memory factoids.add(new FactoidItem(type.equals("message"), factoidTrigger, reply, sender, channel)); bot.msg(channel, "OK, " + sender + "."); } else if (trigger.equals(TRIGGER_DEL)) { // Trying to remove a factoid List<FactoidItem> factoids = find(channel, message, false); if (factoids.size() == 0) { bot.msg(channel, sender + ", I can't remember " + message + " in the first place."); } else if (factoids.size() != 1) { bot.msg( channel, "I actually have " + factoids.size() + " such factoids, how did that happen? " + "Please remove them manually and fix this bug."); System.err.println("More than one factoid exists with '" + message + "' as trigger:"); for (FactoidItem factoid : factoids) { System.err.println(factoid.toString()); } } else { // First remove it from the SQL db try { if (sqlHandler.delete( "DELETE FROM " + FACTOID_TABLE + " WHERE `trigger` = '" + message + "' AND `channel` = '" + channel + "';") == 0) { System.err.println( "Factoid deletion warning: Item was found in local arraylist, but not in SQL DB!"); bot.msg( channel, "OMG inconsistency; I have the factoid in memory but not in the SQL db."); return; } } catch (SQLException e) { bot.msg(channel, "You should know that I caught an SQL exception."); System.err.println("Factoid deletion: SQL Exception!"); e.printStackTrace(); } // Then remove it from memory this.factoids.remove(factoids.get(0)); bot.msg(channel, "I no longer know of this " + message + " that you speak of."); } } else if (trigger.equals(TRIGGER_MAIN)) { // Trying to view data about a factoid List<FactoidItem> factoids = find(channel, message, false); if (factoids.size() == 0) { bot.msg(channel, sender + ", I do not know of this " + message + " that you speak of."); } else { for (FactoidItem factoid : factoids) { bot.msg(channel, factoid.toString()); } } } else if (trigger.equals(TRIGGER_RANDOM)) { try { Object[] row = sqlHandler.selectSingle( "SELECT reply FROM " + FACTOID_TABLE + " WHERE channel= '?' ORDER BY RAND() LIMIT 1;", Arrays.asList(new String[] {channel})); if (row.length > 0) { bot.msg(channel, (String) ((Object[]) row[0])[0]); } else { bot.msg(channel, "No factoids are added"); } } catch (SQLException e) { e.printStackTrace(); } } else if (trigger.equals(TRIGGER_FOR)) { List<FactoidItem> factoids = find(channel, message, true); if (factoids.size() == 0) { bot.msg(channel, "Sorry, that expression doesn't ring any bell."); } else { for (FactoidItem factoid : factoids) { bot.msg(channel, factoid.toString()); } } } }