Esempio n. 1
0
  private final String readMSG(final int len)
      throws IOException, InterruptedException, MessagingNetworkException {
    if (len > 65000)
      ServerConnection.throwProtocolViolated("incoming message is too long: " + len + " bytes");
    byte[] b = new byte[len];
    InputStream is = getInputStream();
    synchronized (is) {
      long abortTime =
          System.currentTimeMillis() + 1000 * MSNMessagingNetwork.REQPARAM_SOCKET_TIMEOUT_SECONDS;
      int ofs = 0;

      while (ofs < len) {
        if (Thread.currentThread().isInterrupted()) throw new InterruptedIOException();
        int read = is.read(b, ofs, len - ofs);
        if (read < 0) read = 0;
        ofs += read;
        if (System.currentTimeMillis() > abortTime) throw new IOException("connection timed out");
        /*
        if (len >= buffer.length)
        {
          ...
          return ...;
        }
        int pos = findCRLF();
        if (pos != -1) break;
        fill(is, abortTime);
        */
      }

      String msg = new String(b, 0, len, "UTF-8");
      return msg;
    }
  }
Esempio n. 2
0
  public final String readCommand(byte[] b)
      throws IOException, InterruptedException, MessagingNetworkException {
    InputStream is = getInputStream();
    synchronized (is) {
      long abortTime =
          System.currentTimeMillis() + 1000 * MSNMessagingNetwork.REQPARAM_SOCKET_TIMEOUT_SECONDS;
      int ofs = 0;
      boolean d = false;
      for (; ; ) {
        if (Thread.currentThread().isInterrupted()) throw new InterruptedIOException();
        int by = is.read();
        if (by == -1) throw new IOException("unexpected EOF");
        if (by == 10 && d) break;
        d = (by == 13);
        if (ofs < b.length) {
          b[ofs++] = (byte) by;
        }
        if (System.currentTimeMillis() > abortTime) throw new IOException("connection timed out");
        /*
        if (len >= buffer.length)
        {
          ...
          return ...;
        }
        int pos = findCRLF();
        if (pos != -1) break;
        fill(is, abortTime);
        */
      }
      if (b[ofs - 1] == 13) --ofs;

      String line = new String(b, 0, ofs, "ASCII");

      if (StringUtil.startsWith(line, "MSG")) {
        StringTokenizer st = new StringTokenizer(line);
        String len_s = null;
        while (st.hasMoreTokens()) {
          len_s = st.nextToken();
        }
        if (len_s == null) throw new AssertException("len_s is null");
        int len;
        try {
          len = Integer.parseInt(len_s);
        } catch (NumberFormatException ex) {
          ServerConnection.throwProtocolViolated("MSG length must be int");
          len = 0;
        }
        String msg = readMSG(len);
        line = line + "\r\n" + msg;
      }
      if (Defines.DEBUG && CAT.isDebugEnabled()) CAT.debug("S: " + line);
      return line;
    }
  }
Esempio n. 3
0
 /**
  * sends a invite to the server
  *
  * @param message the message to be sent
  */
 private void sendInvite(String to, String message) {
   if (server != null) {
     server.writeObject(new SD_Invite(to, message));
   }
 }
Esempio n. 4
0
 /**
  * sends a whisper to the server
  *
  * @param message the message to be sent
  */
 private void sendWhisper(String to, String message) {
   if (server != null) {
     server.writeObject(new SD_Whisper(to, message));
   }
 }
Esempio n. 5
0
 /**
  * sends a new message to the server
  *
  * @param message the message to be sent
  */
 private void sendChat(String message) {
   if (server != null) {
     server.writeObject(new SD_Chat(null, message));
   }
 }
Esempio n. 6
0
  /**
   * Method to parse a command and perform the specified action
   *
   * @param cmd the command that the user typed
   * @return a status indicator to tell if the command was valid
   */
  public boolean parseCommand(String cmd) {
    cmd = cmd.intern();
    if (cmd == "help" || cmd == "?") {
      String commands =
          "Listing  available commands:\n"
              + "<pre>\\help or \\? \t\t- display this message<br>"
              + "\\admin &lt;passphrase&gt; \t- become an admin<br>"
              + "\\create &lt;channel&gt; [password]\t- create a new channel with optional password<br>"
              + "\\join &lt;channel&gt;  [password]\t- join channel with optional password<br>"
              + "\\disconnect \t\t- disconnect from server<br>"
              + "\\whisper &lt;user&gt; &lt;message&gt; \t- whisper to a user<br>"
              + "\\private &lt;user&gt; \t- start a private message session<br>"
              + "\\ignore &lt;user&gt; \t- ignores a user<br>"
              + "\\clearignore \t\t- clear list of ignores<br>"
              + "\\reconnect \t\t- attempt to reconnect to server<br>"
              + "\\rename &lt;new name&gt; \t- change your username<br>"
              + "\\invite &lt;user&gt; \t- invite user to join at your channel<br>";
      if (admin) {
        commands +=
            "\\kick &lt;user&gt; \t\t- kick user from room<br>"
                + "\\logstart \t\t- start logging sessoin<br>"
                + "\\logstop \t\t- stop logging session<br>";
      }
      commands += "up or down \t\t- cycle your chat history</pre>";
      sendText("server", commands, false);
      return true;
    } else if (cmd == "disconnect") {
      if (server != null) {
        stop();
      }
      return true;
    } else if (cmd == "reconnect") {
      if (username == null) {
        error(
            "username still invalid. use \\rename &lt;new name&gt; to chose a new name and reconnect");
      } else {
        if (server != null) {
          stop();
        }
        server = new ServerConnection(this);
      }
      return true;
    } else if (cmd == "clearignore") {
      ignores.clear();
      updateList();
      serverMessage("ignore list cleared");
      return true;
    } else if (cmd.startsWith("whisper")) {
      int start = cmd.indexOf(' ');
      if (start < 0) {
        error("usage: \\whisper &lt;user&gt; &lt;message&gt;");
        return false;
      }
      cmd = cmd.substring(start + 1);
      start = cmd.indexOf(' ');
      if (start < 0) {
        error("usage: \\whisper &lt;user&gt; &lt;message&gt;");
        return false;
      }
      String un = cmd.substring(0, start);
      if (username.equals(un) || !users.contains(un)) {
        error("invalid user");
        return false;
      }
      String message = cmd.substring(start + 1);
      if (message.length() < 1) {
        error("usage: \\whisper &lt;user&gt; &lt;message&gt;");
      }
      sendWhisper(un, message);
      sendText(username, message, true);
      return true;
    } else if (cmd.startsWith("invite")) {
      String channel = (String) cboChannels.getSelectedItem();
      int start = cmd.indexOf(' ');
      if (start < 0) {
        error("usage: \\invite &lt;user&gt;");
        return false;
      }

      String un = cmd.substring(start + 1);

      if ((un.length() < 1) || (un.length() > 10) || (!un.matches("[\\w_-]+?"))) {
        error(un + " invalid user");
        return false;
      }

      if (username.equals(un)) {
        error("You cannot invite youself");
        return false;
      }

      if (users.contains(un)) {
        error(un + "  is already in your Channel");
        return false;
      }
      String message = un + " has been invited to join a channel " + channel;
      sendInvite(un, message);
      sendText(username, message, false);
      return true;

    } else if (cmd.startsWith("private")) {
      int start = cmd.indexOf(' ');
      if (start < 0) {
        error("usage: \\private &lt;user&gt;");
        return false;
      }
      String un = cmd.substring(start + 1);
      if (username.equals(un)) {
        error("cannot private message yourself");
        return false;
      }
      privates.newPrivate(un);
      return true;
    } else if (cmd.startsWith("rename")) {
      int start = cmd.indexOf(' ');
      if (start < 0) {
        error("usage: \\rename &lt;newname&gt;");
        return false;
      }
      String newName = cmd.substring(start + 1);
      if ((newName.length() < 1)
          || (newName.length()) > 10
          || (newName.equals("server"))
          || (!newName.matches("[\\w_-]+?"))) {
        error("invalid name");
        return false;
      }
      if (!server.connected) {
        server = new ServerConnection(this);
      }
      if (username == null) {
        username = newName;
        server.writeObject(new SD_UserAdd(newName));
      } else {
        rename(username, newName);
        username = newName;
        server.writeObject(new SD_Rename(null, username));
      }
      return true;
    } else if (cmd.startsWith("ignore")) {
      int start = cmd.indexOf(' ');
      if (start < 0) {
        error("usage: \\ignre &lt;user&gt;");
        return false;
      }
      ignore(cmd.substring(start + 1), false);
      return true;
    } else if (cmd.startsWith("join")) {
      int start = cmd.indexOf(' ');
      if (start < 0) {
        error("usage: \\join &lt;channel&gt; [password]");
        return false;
      }
      String name = cmd.substring(start + 1);
      String pass = null;
      start = name.indexOf(' ');
      if (start > 0) {
        pass = name.substring(start + 1);
        name = name.substring(0, start);
      }
      server.writeObject(new SD_Channel(false, name, pass));
      return true;
    } else if (cmd.startsWith("create")) {
      int start = cmd.indexOf(' ');
      if (start < 0) {
        error("usage: \\create &lt;channel&gt; [password]");
        return false;
      }
      String name = cmd.substring(start + 1);
      String pass = null;
      start = name.indexOf(' ');
      if (start > 0) {
        pass = name.substring(start + 1);
        name = name.substring(0, start);
      }
      server.writeObject(new SD_Channel(true, name, pass));
      return true;
      /*		} else if (cmd.startsWith("proxy")) {
      int start = cmd.indexOf(' ');
      if (start < 0) {
      	error("usage: \\proxy &lt;host&gt; &lt;port&gt;");
      	return false;
      }
      String phost = cmd.substring(start+1);
      start = phost.indexOf(' ');
      if (start < 0) {
      	error("usage: \\proxy &lt;host&gt; &lt;port&gt;");
      	return false;
      }
      String pport = phost.substring(start+1);
      phost = phost.substring(0, start);
      Properties systemSettings = System.getProperties();
      systemSettings.put("proxySet", "true");
      systemSettings.put("proxyHost", phost);
      systemSettings.put("proxyPort", pport);
      System.setProperties(systemSettings);
      serverMessage("Using " + phost + ":" + pport + " as proxy server<br>you can type \\reconnect to reconnect with this proxy");
      return true; */
    } else if (cmd.startsWith("admin")) {
      int start = cmd.indexOf(' ');
      if (start < 0) {
        error("usage: \\admin &lt;password&gt;");
        return false;
      }
      String pass = cmd.substring(start + 1);
      server.writeObject(new SD_AdminAdd(pass));
      return true;
    } else if (admin && cmd.startsWith("kick")) {
      int start = cmd.indexOf(' ');
      if (start < 0) {
        error("usage: \\kick &lt;user&gt;");
        return false;
      }
      String un = cmd.substring(start + 1);
      if (un.equals(username)) {
        error("cannot kick yourself");
        return false;
      }
      server.writeObject(new SD_Kick(un));
      return true;
    } else if (admin && cmd == "logstart") {
      server.writeObject(new SD_Log(true));
      return true;
    } else if (admin && cmd == "logstop") {
      server.writeObject(new SD_Log());
      return true;
    }
    error("unrecognized command, type \\help for help");
    return false;
  }
Esempio n. 7
0
 public void stop() {
   if (server != null) server._writeObject(new SD_UserDel(null)); // to bypass the queue
 }
Esempio n. 8
0
 public void sendPrivate(String name, String message) {
   server.writeObject(new SD_Private(name, message));
 }