Exemple #1
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);
   }
 }
Exemple #2
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());
        }
      }
    }
  }