コード例 #1
0
ファイル: Bot.java プロジェクト: swatarianess/IRCbot
  private void connect() throws IOException {
    socket = new Socket(host, port);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

    writeLine("NICK :" + nick);
    writeLine("USER " + user + " * * :" + realName);

    String[] args;
    String prefix;
    String command;
    String target;
    String message;

    String sender;

    while ((line = in.readLine()) != null) {
      System.out.println("<" + line);
      if (line.startsWith("PING")) {
        if (count < 2) {
          sendPrivmsg(Master, "We just got pinged!");
          ++count;
        }
        writeLine(line.replace("PING", "PONG"));
      }

      args = line.split(" ");
      if (line.startsWith(":")) { // a prefix will always contain a leading colon
        prefix = args[0].substring(1);
        command = args[1];
        target = args[2];

        sender = prefix.split("!")[0];

        if (command.equals("001")) {
          writeLine("JOIN :" + channel);

        } else if (command.equals("PRIVMSG")) { // We got messaged!
          // to parse the message, we will need the
          // index of the second colon in the line
          message = line.substring(line.indexOf(":", 1) + 1);

          if (message.equalsIgnoreCase("!hello")) {
            if (target.equalsIgnoreCase(nick)) target = sender;

            Matcher m = p.matcher(line);
            if (m.find()) Person = m.group(1);

            if (Person.equals("Inf3cti0us")) { // TODO Find why this is broken?
              sendPrivmsg(target, "Greetings Master " + Person);
            } else {
              sendPrivmsg(target, "Hey " + Person + "..");
            }
          }

          if (message.equalsIgnoreCase("Wau?")) {
            if (target.equalsIgnoreCase(nick)) target = sender;
            Matcher m = p.matcher(line);
            if (m.find()) sendPrivmsg(target, "Not a robot ;/ " + m.group(1));
          }

          if (message.equalsIgnoreCase("!help")) {

            if (target.equalsIgnoreCase(nick)) {
              target = sender;
              Matcher m = p.matcher(line);
              if (m.find()) sendPrivmsg(target, "No help for you " + m.group(1));
            }
          }
        }
        if (command.equals("433")) {
          // Nickname is already in use. Add random Numbers :)
          writeLine("NICK :" + nick + RandomNumber());
          writeLine("USER " + user + " * * :" + realName);
        }
        if (command.equals("439")) { // TODO something interesting here?
          System.out.println("Waiting for processing connection..");
        }
        if (command.equals("KICK")) {
          sendPrivmsg(target, "Sorry, I'll behave better this time!");
          sendPrivmsg(target, " ACTION sits in the naughty corner");
        }
      }
    }
  }