/**
   * This method is responsible for the creation of the Client UI.
   *
   * @param args[0] The host to connect to.
   */
  public static void main(String[] args) {
    // Get loginId
    String id = "";
    String pw = "";
    try {
      id = args[0];
    } catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("ERROR - No login ID specified. Connection aborted.");
      System.exit(0);
    }
    try {
      pw = args[1];
    } catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("ERROR - Password not specified. Connection aborted.");
      System.exit(0);
    }
    // Get host
    String host = "";
    try {
      host = args[2];
    } catch (ArrayIndexOutOfBoundsException e) {
      host = "localhost";
    }

    // Get port
    int port = 0;
    try {
      port = Integer.parseInt(args[3]);
    } catch (Throwable t) {
      port = DEFAULT_PORT; // Set port to 5555
    }

    ClientConsole chat = new ClientConsole(id, pw, host, port);
    chat.accept(); // Wait for console data
  }
Exemple #2
0
 // @SuppressWarnings("static-access")
 public void runCmd(String[] cmd) {
   // String cmdName = cmd[0].toLowerCase().trim();
   String msg;
   int target;
   switch (cmd[0].toLowerCase().trim()) {
     case "votekick":
       ChatServer.sendOne(
           ChatServer.getClientID(name),
           Colors.DARKRED + "/vote and /votekick are unimplemented.");
       break;
       /*if (cmd.length != 2) {
       	ChatServer.sendOne(ChatServer.getClientID(name), Colors.DARKRED + "votekick syntax: /votekick <user>");
       	break;
       }
       target = ChatServer.getClientID(cmd[1]); //get id of user name
       String starter = name;
       if (target == -1) {
       	ChatServer.sendOne(ChatServer.getClientID(name), Colors.DARKRED + "User '" + cmd[1] + "' is not online");;
       } else {
       	startVote("kick", ChatServer.getClientID(starter), target, "kick");
       	vote(ChatServer.getClientID(starter), 1); //voter also votes yes
       }
       break;*/
     case "vote":
       ChatServer.sendOne(
           ChatServer.getClientID(name),
           Colors.DARKRED + "/vote and /votekick are unimplemented.");
       break;
       /*if (cmd.length != 2) {
       	ChatServer.sendOne(ChatServer.getClientID(name), Colors.DARKRED + "vote syntax: /vote <yes | no>");
       	break;
       }
       int voter = ChatServer.getClientID(name);
       if (cmd[1].equalsIgnoreCase("yes")) {
       	ChatServer.vote(voter, 1);
       } else if (cmd[1].equalsIgnoreCase("no")) {
       	ChatServer.vote(voter, 0);
       } else if (cmd[1].equalsIgnoreCase("end")) {
       	ChatServer.sendOne(ChatServer.getClientID(name), Colors.DARKRED + "Ending current vote and tallying results.");
       } else {
       	ChatServer.sendOne(ChatServer.getClientID(name), Colors.DARKRED + "Please vote \"yes\" or \"no\"");
       }
       break;*/
     case "me":
     case "emote":
       if (cmd.length <= 1) {
         ChatServer.sendOne(
             ChatServer.getClientID(name), Colors.DARKRED + "me syntax: /me &lt;message&gt;");
         break;
       }
       msg = "";
       for (int i = 1; i < cmd.length; i++) {
         msg += cmd[i] + " ";
       }
       ChatServer.sendAll(Colors.WHITE + "> " + Colors.GREEN + name + Colors.WHITE + " " + msg);
       break;
     case "msg":
     case "message":
       if (cmd.length < 3) {
         ChatServer.sendOne(
             ChatServer.getClientID(name),
             Colors.DARKRED + "msg syntax: /msg &lt;user&gt; &lt;message&gt;");
         break;
       }
       msg = "";
       for (int i = 2; i < cmd.length; i++) {
         msg += cmd[i] + " ";
       }
       target = ChatServer.getClientID(cmd[1]);
       lastUserPMd = target;
       if (target == -1) {
         ChatServer.sendOne(
             ChatServer.getClientID(name), Colors.DARKRED + "User '" + cmd[1] + "' is not online");
       } else {
         // ChatServer.sendOne(ChatServer.getClientID(name), target, Colors.PINK + "" + msg);
         ChatServer.sendOne(target, Colors.PINK + "" + name + " -> you: " + msg);
         ChatServer.sendOne(
             ChatServer.getClientID(name), Colors.PINK + "me -> " + cmd[1] + ": " + msg);
         ChatServer.sendMods(Colors.PINK + "" + name + " -> " + cmd[1] + ": " + msg);
       }
       break;
     case "r":
     case "reply":
       if (cmd.length < 2) {
         ChatServer.sendOne(
             ChatServer.getClientID(name), Colors.DARKRED + "reply syntax: /r &lt;message&gt;");
         break;
       }
       if (lastUserPMd == -1) {
         ChatServer.sendOne(
             ChatServer.getClientID(name), Colors.DARKRED + "You have not messaged anyone yet");
         break;
       }
       msg = "";
       for (int i = 1; i < cmd.length; i++) {
         msg += cmd[i] + " ";
       }
       target = lastUserPMd;
       if (target == -1) {
         ChatServer.sendOne(
             ChatServer.getClientID(name),
             Colors.DARKRED + "User '" + ChatServer.getClientName(target) + "' is not online");
         ;
       } else {
         // ChatServer.sendOne(ChatServer.getClientID(name), target, Colors.PINK + "" + msg);
         ChatServer.sendOne(target, Colors.PINK + "" + name + " -> you: " + msg);
         ChatServer.sendOne(
             ChatServer.getClientID(name),
             Colors.PINK + "me -> " + ChatServer.getClientName(target) + ": " + msg);
         ChatServer.sendMods(
             Colors.PINK + "" + name + " -> " + ChatServer.getClientName(target) + ": " + msg);
       }
       break;
     case "help":
     case "?":
       if (cmd.length != 1) {
         ChatServer.sendOne(ChatServer.getClientID(name), Colors.DARKRED + "help syntax: /help");
         break;
       }
       String cmds = "";
       if (getRank() > 0) cmds += "/me /logout /msg /r /who /help /ping ";
       if (getRank() > 1) cmds += "/kick /broadcast /mute ";
       if (getRank() > 2) cmds += "/stop /kickall /pex ";
       ChatServer.sendOne(
           ChatServer.getClientID(name), Colors.LIGHTGREEN + "Available commands: " + cmds);
       break;
     case "who":
     case "list":
       if (cmd.length != 1) {
         ChatServer.sendOne(
             ChatServer.getClientID(name), Colors.DARKRED + "user list syntax: /list");
         break;
       }
       ChatServer.sendOne(
           ChatServer.getClientID(name),
           Colors.LIGHTGREEN + "Users online: " + ChatServer.getClientsOnline());
       break;
     case "logout":
     case "logoff":
     case "dc":
       leave();
       break;
     case "ping":
     case "pong":
       if (cmd.length == 1) {
         ChatServer.sendOne(ChatServer.getClientID(name), "Pong!");
       } else {
         String str = "";
         for (int i = 1; i < cmd.length; i++) {
           str += cmd[i] + " ";
         }
         ChatServer.sendOne(ChatServer.getClientID(name), str);
       }
       break;
     case "stop": // admin
       if (getRank() < 3) {
         ChatServer.sendOne(
             ChatServer.getClientID(name),
             Colors.RED + "You do not have permission to use this command");
         break;
       }
       if (cmd.length != 1) {
         ChatServer.sendOne(
             ChatServer.getClientID(name), Colors.DARKRED + "ChatServer stop syntax: /stop");
         break;
       }
       ChatServer.sendOne(ChatServer.getClientID(name), Colors.RED + "Server is shutting down...");
       System.exit(0);
       break;
     case "kick": // mod
       if (getRank() < 2) {
         ChatServer.sendOne(
             ChatServer.getClientID(name),
             Colors.RED + "You do not have permission to use this command");
         break;
       }
       if (cmd.length < 2) {
         ChatServer.sendOne(
             ChatServer.getClientID(name),
             Colors.DARKRED + "kick syntax: /kick &lt;user &lt;reason&gt;&gt;");
         break;
       }
       if (ChatServer.getClientID(cmd[1]) == -1) {
         ChatServer.sendOne(
             ChatServer.getClientID(name), Colors.DARKRED + "User '" + cmd[1] + "' is not online");
         ;
       } else {
         if (cmd.length == 2) {
           ChatServer.kick(ChatServer.getClientID(name), ChatServer.getClientID(cmd[1]));
         } else if (cmd.length > 2) {
           msg = "";
           for (int i = 2; i < cmd.length; i++) {
             msg += cmd[i] + " ";
           }
           ChatServer.kick(ChatServer.getClientID(cmd[1]), msg);
         }
       }
       break;
     case "kickall": // admin
       if (getRank() < 3) {
         ChatServer.sendOne(
             ChatServer.getClientID(name),
             Colors.RED + "You do not have permission to use this command");
         break;
       }
       if (cmd.length != 1) {
         ChatServer.sendOne(
             ChatServer.getClientID(name), Colors.DARKRED + "kickall syntax: /kickall");
         break;
       }
       // ChatServer.kickAll();
       ChatServer.pushToChat(Colors.DARKRED + "kickall command is unimplemented");
       break;
     case "mute": // mod
       if (getRank() < 2) {
         ChatServer.sendOne(
             ChatServer.getClientID(name),
             Colors.RED + "You do not have permission to use this command");
         break;
       }
       if (cmd.length < 2) {
         ChatServer.sendOne(
             ChatServer.getClientID(name), Colors.DARKRED + "mute syntax: /mute &lt;user&gt;");
         break;
       }
       if (ChatServer.getClientID(cmd[1]) == -1) {
         ChatServer.sendOne(
             ChatServer.getClientID(name), Colors.DARKRED + "User '" + cmd[1] + "' is not online");
       }
       ChatServer.mute(ChatServer.getClientID(cmd[1]));
       ChatServer.sendOne(
           ChatServer.getClientID(cmd[1]),
           Colors.RED
               + "You have been "
               + ((ChatServer.getClient(cmd[1]).isMuted()) ? "muted" : "unmuted"));
       ChatServer.sendOne(
           ChatServer.getClientID(name),
           Colors.RED
               + ""
               + cmd[1]
               + " has been "
               + ((ChatServer.getClient(cmd[1]).isMuted()) ? "muted" : "unmuted"));
       break;
     case "broadcast": // mod
     case "bc":
       if (getRank() < 2) {
         ChatServer.sendOne(
             ChatServer.getClientID(name),
             Colors.RED + "You do not have permission to use this command");
         break;
       }
       if (cmd.length < 2) {
         ChatServer.sendOne(
             ChatServer.getClientID(name),
             Colors.DARKRED + "broadcast syntax: /broadcast &lt;message&gt;");
         break;
       }
       msg = "";
       for (int i = 1; i < cmd.length; i++) {
         msg += cmd[i] + " ";
       }
       ChatServer.sendAll(
           Colors.DARKRED
               + "["
               + Colors.LIGHTGREEN
               + "Broadcast"
               + Colors.DARKRED
               + "]"
               + Colors.LIGHTGREEN
               + Colors.BOLD
               + " "
               + msg);
       break;
     case "pex": // admin
     case "rank":
       if (getRank() < 3) {
         ChatServer.sendOne(
             ChatServer.getClientID(name),
             Colors.RED + "You do not have permission to use this command");
         break;
       }
       if (cmd.length < 2 || cmd.length > 4) { // /pex promote user rank
         ChatServer.sendOne(
             ChatServer.getClientID(name),
             Colors.DARKRED
                 + "permissions syntax: /pex &lt;promote | demote&gt; &lt;user&gt; or /pex set &lt;user &lt;user | mod | admin&gt;&gt;");
         break;
       }
       if (cmd[2].equals(name)) {
         ChatServer.sendOne(
             ChatServer.getClientID(name), Colors.DARKRED + "You cannot promote/demote yourself");
         break;
       }
       if (ChatServer.getClientID(cmd[2]) == -1) {
         ChatServer.sendOne(
             ChatServer.getClientID(name), Colors.DARKRED + "User '" + cmd[2] + "' is not online");
       }
       switch (cmd[1].toLowerCase().trim()) {
         case "set":
           if (cmd.length != 4) {
             ChatServer.sendOne(
                 ChatServer.getClientID(name),
                 Colors.DARKRED
                     + "permissions syntax: /pex set &lt;user &lt;user | mod | admin&gt;&gt;");
           } else {
             switch (cmd[3].toLowerCase().trim()) {
               case "mod":
               case "moderator":
                 ChatServer.promoteClient(ChatServer.getClientID(cmd[2]), 2);
                 ChatServer.sendOne(
                     ChatServer.getClientID(cmd[2]), "You have been moved to moderator");
                 ChatServer.sendOne(
                     ChatServer.getClientID(name),
                     Colors.PINK + "Changed " + cmd[2] + " to moderator");
                 break;
               case "admin":
               case "administrator":
                 ChatServer.promoteClient(ChatServer.getClientID(cmd[2]), 3);
                 ChatServer.sendOne(
                     ChatServer.getClientID(cmd[2]), "You have been moved to administrator");
                 ChatServer.sendOne(
                     ChatServer.getClientID(name),
                     Colors.PINK + "Changed " + cmd[2] + " to administrator");
                 break;
               case "user":
                 ChatServer.promoteClient(ChatServer.getClientID(cmd[2]), 1);
                 ChatServer.sendOne(ChatServer.getClientID(cmd[2]), "You have been moved to user");
                 ChatServer.sendOne(
                     ChatServer.getClientID(name), Colors.PINK + "Changed " + cmd[2] + " to user");
                 break;
               default:
                 ChatServer.sendOne(
                     ChatServer.getClientID(name),
                     Colors.DARKRED
                         + "permissions syntax: /pex set &lt;user &lt;user | mod | admin&gt;&gt;");
                 break;
             }
           }
           break;
         case "promote":
           if (cmd.length != 3) {
             ChatServer.sendOne(
                 ChatServer.getClientID(name),
                 Colors.DARKRED
                     + "permissions syntax: /pex &lt;promote | demote&gt; &lt;user&gt;");
           } else {
             ChatServer.promoteClient(ChatServer.getClientID(cmd[2]));
             ChatServer.sendOne(ChatServer.getClientID(cmd[2]), "You have been promoted");
             ChatServer.sendOne(ChatServer.getClientID(name), Colors.PINK + "Promoted " + cmd[2]);
           }
           break;
         case "demote":
           if (cmd.length != 3) {
             ChatServer.sendOne(
                 ChatServer.getClientID(name),
                 Colors.DARKRED
                     + "permissions syntax: /pex &lt;promote | demote&gt; &lt;user&gt;");
           } else {
             ChatServer.demoteClient(ChatServer.getClientID(cmd[2]));
             ChatServer.sendOne(ChatServer.getClientID(cmd[2]), "You have been demoted");
             ChatServer.sendOne(ChatServer.getClientID(name), Colors.PINK + "Demoted " + cmd[2]);
           }
           break;
         default:
           ChatServer.sendOne(
               ChatServer.getClientID(name),
               Colors.DARKRED
                   + "permissions syntax: /pex &lt;promote | demote&gt; &lt;user&gt; or /pex set &lt;user &lt;user | mod | admin&gt;&gt;");
           break;
       }
       break;
     default:
       ChatServer.sendOne(
           ChatServer.getClientID(name),
           Colors.DARKRED + "Unknown command. Type /help for a list of commands.");
       break;
   }
 }
 public void handleMessageFromClientUI(Object message) {
   String parsed = "";
   if (!((Message) message).message.startsWith("#") && isConnected()) {
     super.handleMessageFromClientUI(message);
     return;
   }
   {
     parsed = ((Message) message).message.substring(1);
     if (parsed.equals("quit")) {
       try {
         closeConnection();
       } catch (IOException ex) {
         ex.printStackTrace();
         System.exit(1);
       }
       System.exit(0);
     } else if (parsed.equals("logoff")) {
       try {
         closeConnection();
       } catch (IOException ex) {
         ex.printStackTrace();
         System.exit(1);
       }
     } else if (parsed.equals("login")) {
       if (isConnected()) {
         System.out.println("Error: Already connected");
         return;
       }
       try {
         openConnection();
       } catch (IOException ex) {
         System.out.println("Error: " + ex.getMessage());
         ex.printStackTrace();
       }
     } else if (parsed.startsWith("sethost")) {
       if (isConnected()) {
         System.out.println("Error: Already connected");
         return;
       } else {
         String host = "";
         try {
           host = parsed.split(" ")[1];
         } catch (ArrayIndexOutOfBoundsException ex) {
           System.out.println("Error: Must specify host");
           return;
         }
         setHost(host);
       }
     } else if (parsed.startsWith("setport")) {
       if (isConnected()) {
         System.out.println("Error: Already connected");
         return;
       }
       int port = 0;
       try {
         port = Integer.parseInt(parsed.split(" ")[1]);
       } catch (ArrayIndexOutOfBoundsException ex) {
         System.out.println("Error: Must specify port");
         return;
       }
       setPort(port);
     } else if (parsed.equals("getport")) {
       System.out.println("Port: " + getPort());
     } else if (parsed.equals("gethost")) {
       System.out.println("Host: " + getHost());
     }
   }
 }
 protected void connectionException(Exception exception) {
   System.out.println("Connection exception: " + exception.getMessage());
   exception.printStackTrace();
   System.exit(1);
 }