示例#1
0
文件: Bot.java 项目: cmsd2/elsie
 public void respondToIrcEvent(IIrcEvent event) {
   log.info("Responding to event " + event);
   IIrcMessage command = event.getIRCMessage();
   if (command.getCommand().equalsIgnoreCase("PING")) {
     consecutiveErrors = 0;
     log.info("Sending pong");
     String self = hostname;
     if (command.getEscapedParams() != null && command.getEscapedParams().length() > 0)
       self = command.getEscapedParams();
     enqueueCommand(irc.pong(self));
   } else if (command.getCommand().equalsIgnoreCase("ERROR")) {
     consecutiveErrors++;
     if (consecutiveErrors > 1) {
       nextServer();
     }
     reconnect("IRC Error!");
   } else if (command.getCommand().equals("433")) { // nick collision
     log.info("Nick collision. Cycling nicks");
     currentNick++;
     if (currentNick == nicks.size()) {
       currentNick = 0;
     }
     myNick = (String) nicks.get(currentNick);
     enqueueCommand(irc.nick(myNick));
     log.info("(Re)Joining channels");
     for (IChannel c : channels) {
       c.join();
     }
   } else if (command.getCommand().equalsIgnoreCase("CTCP_VERSION")) {
     enqueueCommand(irc.ctcpVersion(command.getPrefixNick(), description, version, environment));
   } else if (command.getCommand().equalsIgnoreCase("CTCP_PING")) {
     enqueueCommand(irc.ctcpPing(command.getPrefixNick(), command.getEscapedParams()));
   } else if (command.getCommand().equalsIgnoreCase("CTCP_TIME")) {
     String dateTime = df.format(new Date(System.currentTimeMillis()));
     enqueueCommand(irc.ctcpTime(command.getPrefixNick(), dateTime));
   }
 }
示例#2
0
文件: Bot.java 项目: cmsd2/elsie
  public void run() {
    List<IIrcMessage> command;

    long lastIRCEvent = System.currentTimeMillis();

    boolean successful = false;
    currentServer--;
    do {
      log.info("Connecting");
      sendErrorEvent("Bot.run", "problem", "Connecting...");
      currentServer++;
      if (currentServer == servers.size()) {
        currentServer = 0;
      }
      successful = connect();
    } while (successful == false);

    while (true) {
      try {
        sleep(pauseTime);

        while (receiver.ready()) {
          command = receive();

          lastIRCEvent = System.currentTimeMillis();

          for (IIrcMessage msg : command) {
            sendIRCEvent(msg);
            if ((msg.getCommand().equalsIgnoreCase("PRIVMSG")
                    & msg.getEscapedParams().matches(myNick + ":? +.*"))
                || (msg.getCommand().equalsIgnoreCase("PRIVMSG")
                    & msg.isPrivate()
                    & !msg.getPrefixNick().equalsIgnoreCase(myNick))) {
              String temp = msg.getEscapedParams().replaceFirst(myNick + ":? +", "");
              String[] botCmd = temp.split(" +");
              sendBotEvent(msg.getPrefixNick(), botCmd, msg.isPrivate());
            } else if (msg.getCommand().equals("001")) {
              registered = true;
              for (IChannel c : channels) {
                c.join();
              }
            }

            sendMessages();
            sendCommands();
          }
        }
        if (System.currentTimeMillis() - lastIRCEvent > 500000) {
          lastIRCEvent = System.currentTimeMillis();
          sendErrorEvent("Bot.run", "problem", "Timeout (no data for 500 seconds).");
          sendIRCEvent(
              new IrcMessage(
                  "QUIT",
                  myNick + "!" + myNick + "@" + hostname,
                  null,
                  "Timeout (no data for 500 seconds).",
                  myNick,
                  myNick + "@" + hostname,
                  false));
          reconnect("Timeout (no data for 500 seconds)");
        }

        sendMessages();
        sendCommands();
      } catch (SocketTimeoutException e) {
        log.error("socket timeout while running", e);
        sendErrorEvent("Bot.run", "SocketTimeoutException", e.getMessage());
        reconnect("Read timeout.");
      } catch (IOException e) {
        log.error("ioexception while running", e);
        sendErrorEvent("Bot.run", "IOException", e.getMessage());
        reconnect("Read error.");
      } catch (InterruptedException e) {
        log.error("interrupted while running", e);
        sendErrorEvent("Bot.run", "InterruptedException", e.getMessage());
      }
    }
  }