Example #1
0
  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);
  }
Example #2
0
 /**
  * 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));
   }
 }
Example #3
0
 public void onNickChange(String oldNick, String login, String hostname, String newNick) {
   for (String chan : bot.getChannels()) {
     for (User user : bot.getUsers(chan)) {
       if (user.equals(newNick)) {
         tell(chan, newNick);
       }
     }
   }
 }
Example #4
0
  public void onMessage(
      String channel, String sender, String login, String hostname, String message) {
    if (message.matches("^(\\w+):.+") && !message.matches("(\\w+)://.+")) {
      String toNick = message.substring(0, message.indexOf(':'));
      String msg = message.substring(message.indexOf(':') + 1, message.length());

      List<SeenUser> seens = JWorm.get(SeenUser.class);
      boolean save = false;
      for (SeenUser s : seens) {
        if (s.getNicks().contains(toNick)) {
          save = true;
          break;
        }
      }

      for (User user : bot.getUsers(channel)) {
        if (user.equals(toNick)) {
          return;
        }
      }

      if (save) {
        saveTell(channel, sender, toNick, msg);
      }
    }
  }
Example #5
0
 public Factoid(ModuleHandler moduleHandler) {
   // Load up all existing factoids from sql
   try {
     sqlHandler = SQLHandler.getSQLHandler();
     List<Object[]> rows =
         sqlHandler.select(
             "SELECT `type`, `trigger`, `reply`, `author`, `channel`  FROM "
                 + FACTOID_TABLE
                 + ";");
     for (Object[] row : rows) {
       boolean message = row[0].equals("message");
       factoids.add(
           new FactoidItem(
               message, (String) row[1], (String) row[2], (String) row[3], (String) row[4]));
     }
     moduleHandler.addTriggerListener(TRIGGER_MAIN, this);
     moduleHandler.addTriggerListener(TRIGGER_ADD, this);
     moduleHandler.addTriggerListener(TRIGGER_DEL, this);
     moduleHandler.addTriggerListener(TRIGGER_RANDOM, this);
     moduleHandler.addTriggerListener(TRIGGER_FOR, this);
     moduleHandler.addMessageListener(this);
     moduleHandler.registerHelp(
         TRIGGER_HELP,
         "Factoid: Make me say or do \"reply\" when someone says \"trigger\".\n"
             + "  "
             + Grouphug.MAIN_TRIGGER
             + TRIGGER_ADD
             + " trigger <say> reply\n"
             + "  "
             + Grouphug.MAIN_TRIGGER
             + TRIGGER_ADD
             + " trigger <do> something\n"
             + "  "
             + Grouphug.MAIN_TRIGGER
             + TRIGGER_DEL
             + " trigger\n"
             + "  "
             + Grouphug.MAIN_TRIGGER
             + TRIGGER_MAIN
             + " trigger      - show information about a factoid\n"
             + "  "
             + Grouphug.MAIN_TRIGGER
             + TRIGGER_RANDOM
             + "        - trigger a random factoid\n"
             + "  "
             + Grouphug.MAIN_TRIGGER
             + TRIGGER_FOR
             + " <expression> - show what factoid, if any, that is "
             + "triggered by that expression\n"
             + " - The string \"$sender\" will be replaced with the nick of the one triggering the factoid.\n"
             + " - A star (*) can be any string of characters.\n"
             + " - Regex can be used, but remember that * is replaced with .*");
     bot = Grouphug.getInstance();
   } catch (SQLUnavailableException ex) {
     System.err.println("Factoid startup: SQL is unavailable!");
   } catch (SQLException e) {
     System.err.println("Factoid startup: SQL Exception: " + e);
   }
 }
Example #6
0
 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();
   }
 }
Example #7
0
 public Tell(ModuleHandler moduleHandler) {
   if (SQL.isAvailable()) {
     bot = Grouphug.getInstance();
     moduleHandler.addTriggerListener(TRIGGER, this);
     moduleHandler.addJoinListener(this);
     moduleHandler.addNickChangeListener(this);
     moduleHandler.addMessageListener(this);
     moduleHandler.registerHelp(
         TRIGGER_HELP,
         "Tell: Tell something to someone who's not here when they eventually join\n"
             + "  "
             + Grouphug.MAIN_TRIGGER
             + TRIGGER
             + "<nick> <message>\n");
   } else {
     System.err.println("Tell module disabled: SQL is unavailable.");
   }
 }
Example #8
0
 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);
 }
Example #9
0
  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());
        }
      }
    }
  }