String tmp = modeReceived.substring(0, 1); if (tmp.compareToIgnoreCase("-") == 0 || tmp.compareToIgnoreCase("+") == 0) { String name = modeReceived.substring(3); SortedListModel model = channel.getUserNameList(); if (model.contains("!" + name)) { channel.updateUserArea("!" + name, "remove"); } else if (model.contains("@" + name)) { channel.updateUserArea("@" + name, "remove"); } else if (model.contains("%" + name)) { channel.updateUserArea("%" + name, "remove"); } else if (model.contains("+" + name)) { channel.updateUserArea("+" + name, "remove"); } else if (model.contains(name)) { channel.updateUserArea(name, "remove"); } if (tmp.compareToIgnoreCase("+") == 0) { tmp = modeReceived.substring(1, 2); if (tmp.compareToIgnoreCase("v") == 0) {
char plusMinus = modeReceived.charAt(0); String tmp = modeReceived.substring(1, modeReceived.indexOf(" ")); String names = modeReceived.substring(modeReceived.indexOf(" ") + 1); String splits[] = names.split(" "); if (plusMinus == '-' || plusMinus == '+') { for (int i = 0; i < splits.length; i++) { String name = splits[i]; SortedListModel model = channel. getUserNameList(); if (model.contains("!" + name)) { channel.updateUserArea("!" + name, "remove"); } else if (model.contains("@" + name)) { channel.updateUserArea("@" + name, "remove"); } else if (model.contains("%" + name)) { channel.updateUserArea("%" + name, "remove"); } else if (model.contains("+" + name)) {
    public void parseFromServer(String lineFromServer) throws IOException {        myParser parser = new myParser(lineFromServer);        String command = parser.getCommand();        if (command.equals("PING")) {            parseSendToCommand("PONG :" + parser.getTrailing());            pong = true;        }        else if (command.equals("JOIN")) {            String channelName = parser.getTrailing();            if (channelName.startsWith("#")) {                channelName = channelName.substring(1);                int indexOfChannel = findTab(tabbedPane, "#" + channelName);                if (indexOfChannel == -1) { // Channel Tab doesn't exist                    ChannelPanel channel = addChannel(tabbedPane, channelName);                    indexOfChannel = findTab(tabbedPane, "#" + channelName);                    tabbedPane.setSelectedIndex(indexOfChannel);                    channel.updateTextArea("*** Now talking in: " +                                           parser.getTrailing());                    if (tabbedPane.getSelectedIndex() !=                        findTab(tabbedPane, "#" + channelName)) {                        tabbedPane.setBackgroundAt(findTab(tabbedPane,                            "#" + channelName),                            Color.red);                    }                    tabbedPane.revalidate();                }                else { // channelName tab exists!                    ChannelPanel channel = (ChannelPanel) tabbedPane.                        getComponentAt(                        indexOfChannel);                    channel.updateTextArea("---> JOIN: " + parser.getNick() +                                           " (" +                                           parser.getUser() + "@" +                                           parser.getHost() + ")");                    if (tabbedPane.getSelectedIndex() != indexOfChannel) {                        tabbedPane.setBackgroundAt(indexOfChannel, Color.red);                    }                    tabbedPane.revalidate();                    channel.updateUserArea(parser.getNick(), "add");                }            }        }        else if (command.equals("PRIVMSG")) {            // handle PRIVMSG            String destination = parser.getMiddle();            if (destination.startsWith("#")) {                // it's a channel!                String channelName = destination.substring(1);// MSG Received....!                int indexOfChannel = findTab(tabbedPane, destination);                if (indexOfChannel != -1) { // A connection/tab already exists!                    Component aComponent = tabbedPane.getComponentAt(                        indexOfChannel);                    if (aComponent instanceof ChannelPanel) {                        String trailing;                        String nick;                        nick = parser.getNick();                        trailing = parser.getTrailing();                        if (trailing.indexOf(1) != -1) { //is this a CTCP                            //remove \001 from Beginning and End of CTCP msg                            trailing = trailing.substring(1,                                trailing.length() - 1);                            ( (ChannelPanel) aComponent).updateTextArea(                                formatNickname("[" + nick + "] ") + trailing);                        }                        if (nick.equals(nickname)) {                            //((ChannelPanel)aComponent).textArea.setDisabledTextColor(new Color(255,0,0));                            ( (ChannelPanel) aComponent).updateTextArea(                                formatNickname("<" + nick + "> ") + trailing);                        }                        else {                            //((ChannelPanel)aComponent).textArea.setDisabledTextColor(new Color(0,0,255));                            ( (ChannelPanel) aComponent).updateTextArea(                                formatNickname("<" + nick + "> ") + trailing);                        }                        if (tabbedPane.getSelectedIndex() != indexOfChannel) {                            tabbedPane.setBackgroundAt(indexOfChannel,                                Color.red);                        }                        tabbedPane.revalidate();                    }                }                else { // A connection/tab doesn't exists, so create one!                    ChannelPanel channel = addChannel(tabbedPane, channelName);                    channel.updateTextArea(                        formatNickname("<" + parser.getNick() + "> ") + "> " +                        parser.getTrailing());                    if (tabbedPane.getSelectedIndex() !=                        findTab(tabbedPane, "#" + channelName)) {                        tabbedPane.setBackgroundAt(findTab(tabbedPane,                            "#" + channelName),                            Color.red);                    }                    tabbedPane.revalidate();                }            } //if (destination.startsWith("#"))            else { //if (!destination.equalsIgnoreCase(getNickname()))                // it's from a user                int indexOfUser = findTab(tabbedPane, parser.getNick());                if (indexOfUser == -1) {                    UserPanel userPanel = addUser(tabbedPane, parser.getNick()); //if tab doesnt exist                    // Add the real username@hostname in title of tabbedPane                    userPanel.setTitleArea(parser.getNick() + "!" +                                           parser.getUser() +                                           "@" + parser.getHost());                }                indexOfUser = findTab(tabbedPane, parser.getNick());                if (indexOfUser != -1) { // A user connection/tab already exists!                    Component aComponent = tabbedPane.getComponentAt(                        indexOfUser);                    UserPanel userPanel = (UserPanel) aComponent;                    if (aComponent instanceof UserPanel) {                        String ctcpCommand;                        String trailing;                        char firstOne = 1;                        trailing = parser.getTrailing();                        if (trailing.indexOf(1) != -1) { //is this a CTCP                            if (trailing.indexOf(" ") != -1) {                                ctcpCommand = trailing.substring(1,                                    trailing.indexOf(" "));                            }                            else {                                ctcpCommand = trailing.substring(1,                                    trailing.indexOf(1, 2));                            }                            if (ctcpCommand.equalsIgnoreCase("PING")) {                                parseSendToCommand("NOTICE " + parser.getNick() +                                    " " +                                    trailing);                            }                            else if (ctcpCommand.equalsIgnoreCase("VERSION")) {                                String versionReply = "NOTICE " +                                    parser.getNick() + " " +                                    firstOne + ctcpCommand +                                    " Using ajPlugin IRC:v" + parent.getVersion() + ":Java" +                                    firstOne;                                parseSendToCommand(versionReply);                            }                            userPanel.updateTextArea(                                formatNickname("  CTCP " + ctcpCommand +                                               " received from " +                                               parser.getNick()));                        } // if (trailing.indexOf(1) != -1)  //is this a CTCP                        else { // regular privmsg                            userPanel.updateTextArea(                                formatNickname("<" + parser.getNick() + "> ") +                                trailing);                        }                        if (tabbedPane.getSelectedIndex() != indexOfUser) {                            tabbedPane.setBackgroundAt(indexOfUser, Color.red);                        }                        tabbedPane.revalidate();                    } //if (aComponent instanceof UserPanel)                } // if (indexOfUser != -1)            } // else (Msg from a user..)        } //else if (command.equals("PRIVMSG"))        else if (command.equals("PART")) {            // Let's grab the channel name            String channelName = parser.getParams();            int index = channelName.indexOf("#");            int index2 = channelName.indexOf(":");            if (index2 != -1) {                channelName = channelName.substring(index, index2 - 1);            }            else {                channelName = channelName.substring(index);            }            if (channelName.startsWith("#")) {                channelName = channelName.substring(1);                int indexOfChannel = findTab(tabbedPane, "#" + channelName);                if (indexOfChannel != -1) { // Tab doesn't exist. Do nothing                    Component aComponent = tabbedPane.getComponentAt(                        indexOfChannel);                    if (aComponent instanceof ChannelPanel) {                        ( (ChannelPanel) aComponent).updateTextArea(                            "<--- PART: " +                            parser.getNick());                        ( (ChannelPanel) aComponent).updateUserArea(parser.                            getNick(),                            "remove");                    }                    // Ok, let's remove the tab if the user who left the channel is you                    if (parser.getNick().equals(getNickname())) {                        tabbedPane.removeTabAt(indexOfChannel);                    }                }            }        }        else if (command.equals("QUIT")) {            //??????????????????????????            //Find what channel user is on and direct Quit msg to correct channel(s)            tabUpdate("Init Window",                      parser.getNick() + " has quit (" + parser.getTrailing() +                      ")");        }        else if (command.equals("NICK")) {            // handle NICK --- When a user changes its nickname            int index = findTab(tabbedPane, parser.getNick());            if (index != -1) {                // setting the user tab title to changed nickname                tabbedPane.setTitleAt(index, parser.getTrailing());                // Let's change the name of the UserPanel alsow                UserPanel temp = (UserPanel) tabbedPane.getComponentAt(index);                temp.setName(parser.getTrailing());            }            if (parser.getNick().equalsIgnoreCase(getNickname())) {                setNickname(parser.getTrailing());            }            // When a user changes his/her name, his name in every            // UserPanel or ChannelPanel should be changed too.            // So, we are doing it now --- here            String oldNickname = parser.getNick();            String newNickname = parser.getTrailing();            changeNickname(oldNickname, newNickname);        }        else if (command.equals("NOTICE")) {            // handle NOTICE ( arranger: savoir de qui elle vient et la mettre au bon endroit)            tabUpdate("Init Window",                      "NOTICE FROM: " + parser.getNick() + " ---> " +                      parser.getTrailing());        }        else if (command.equals("KICK")) {            // handle KICK            String kickedNick;            String kickedChannel;            String temp;            temp = parser.getMiddle();            kickedChannel = temp.substring(0, temp.indexOf(" "));            kickedNick = temp.substring(temp.indexOf(" ") + 1, temp.length());            int indexOfChannel = findTab(tabbedPane, kickedChannel);            ChannelPanel channel = (ChannelPanel) tabbedPane.getComponentAt(                indexOfChannel);            if (! (kickedNick.equals(getNickname()))) {                channel.updateTextArea("<- KICK: " + kickedNick + " by " +                                       parser.getNick() + " (" +                                       parser.getTrailing() +                                       ")");                channel.updateUserArea(kickedNick, "remove");            }            else { // You have been kicked...                tabbedPane.removeTabAt(indexOfChannel);                tabUpdate("Init Window",                          "You have been kicked from " + kickedChannel + " by " +                          parser.getNick() + " because " + parser.getTrailing());            }        }        else if (command.equals("001") || // RPL_WELCOME                 command.equals("002") || // RPL_YOURHOST                 command.equals("003") || // RPL_CREATED                 command.equals("004")) { // RPL_MYINFO            // successful registration            tabUpdate("Init Window", parser.getTrailing());        }        else if (command.equals("250") ||                 command.equals("251") ||                 command.equals("253") ||                 command.equals("255") ||                 command.equals("256") ||                 command.equals("257") ||                 command.equals("258") ||                 command.equals("259") ||                 command.equals("265") ||                 command.equals("266")                 ) {            tabUpdate("Init Window", parser.getTrailing());        }//252 & 254 = num IRCOP and Total Channels        else if (command.equals("252")) {            String numThing;            numThing = lineFromServer.substring(lineFromServer.indexOf("252") +                                                5 +                                                nickname.length(),                                                lineFromServer.indexOf(":", 2));            tabUpdate("Init Window", numThing + " " + parser.getTrailing());        }        else if (command.equals("254")) {            String numThing;            numThing = lineFromServer.substring(lineFromServer.indexOf("254") +                                                5 +                                                nickname.length(),                                                lineFromServer.indexOf(":", 2));            tabUpdate("Init Window", numThing + " " + parser.getTrailing());        }//channel full, invite only or need a key        else if (command.equals("471") ||                 command.equals("473") ||                 command.equals("475")                 ) {            String channel;            channel = lineFromServer.substring(lineFromServer.indexOf("#"),                                               lineFromServer.indexOf(":", 2));            tabUpdate("Init Window",                      parser.getTrailing() + " (" + channel + ")");        }        /*        else if (command.equals("371") ||                        command.equals("372") ||                        command.equals("374") ||                        command.equals("375") ||                        command.equals("376")                )                {         tabUpdate("Init Window", "371-376 " + parser.getTrailing());                    tabUpdate("Init Window", "Skipping MOTD!");                }*/        else if (command.equals("311") // RPL_WHOISUSER                 ) {            // Ok, let's see how we can process PROCESS            StringTokenizer st = new StringTokenizer(parser.getParams(),                " \r\n");            //StringTokenizer st = new StringTokenizer(parser.getParams(), " ");            String whoNick = "",                whoUser = "",                whoHost = "",                whoMode = "",                whoRealName = "";            for (int i = 0; st.hasMoreTokens(); ) {                String aToken = st.nextToken();                if (i == 1) {                    whoNick = aToken;                }                else if (i == 2) {                    whoUser = aToken;                }                else if (i == 3) {                    whoHost = aToken;                }                else if (i == 4) {                    whoMode = aToken;                }                else if (i == 5) {                    // vrifier!!!!!!!!!!!!!!                    String paramsWhois = parser.getParams();                    whoRealName = paramsWhois.substring(paramsWhois.indexOf(                        aToken) + 1,                        paramsWhois.length());                    //whoRealName = aToken.substring(1);                }                i++;            }            // Let's show the messages in the Init Window            tabUpdate("Init Window", "Whois info for " + whoNick);            tabUpdate("Init Window",                      "  " + whoNick + "!" + whoUser + "@" + whoHost + " " +                      whoRealName);        }        else if (command.equals("312")) {            String whoNick = "",                whoServer = "",                whoServerInfo = "";            StringTokenizer st = new StringTokenizer(parser.getParams(),                " \r\n");            for (int i = 0; st.hasMoreTokens(); ) {                String aToken = st.nextToken();                if (i == 1) {                    whoNick = aToken;                }                else if (i == 2) {                    whoServer = aToken;                }                else if (i == 3) {                    // vrifier!!!!!!!!!!!!!! ainsi que whois realname                    String paramsWhois = parser.getParams();                    whoServerInfo = paramsWhois.substring(paramsWhois.indexOf(                        aToken) + 1,                        paramsWhois.length());                    //whoServerInfo = aToken.substring(aToken.length());                }                i++;            }            // Let's update the Init Window            tabUpdate("Init Window",                      "  Server: " + whoServer + " " + whoServerInfo);        }        else if (command.equals("313")) {            tabUpdate("Init Window", "  Operator: " + lineFromServer);        }        else if (command.equals("317")) {            String seconds = "",                trailing = "";            trailing = parser.getTrailing();            StringTokenizer st = new StringTokenizer(parser.getParams(),                " \r\n");            for (int i = 0; st.hasMoreTokens(); ) {                String aToken = st.nextToken();                if (i == 2) {                    seconds = aToken;                    break;                }                i++;            }            tabUpdate("Init Window", " Idle time: " + seconds + " " + trailing);        }        else if (command.equals("318")) {            tabUpdate("Init Window", parser.getTrailing());        }        else if (command.equals("319")) {            tabUpdate("Init Window", "  Channels: " + parser.getTrailing());        }        else if (command.equals("301")) { // RPL_AWAY            StringTokenizer st = new StringTokenizer(parser.getParams(),                " \r\n");            String awayNickname = "";            for (int i = 0; st.hasMoreTokens(); ) {                String aToken = st.nextToken();                if (i == 1) {                    awayNickname = aToken;                    break;                }                i++;            }            tabUpdate("Init Window",                      awayNickname + " is away for \"" + parser.getTrailing() +                      "\"");        }        else if (command.equals("305")) { // RPL_UNAWAY            ;        }        else if (command.equals("333")) { // RPL_UNAWAY            ;        }// used or not ???????        else if (command.equals("332") // RPL_TOPIC                 ) {            String topicChannelName = "";            StringTokenizer st = new StringTokenizer(parser.getParams(),                " \r\n");            for (int i = 0; st.hasMoreTokens(); ) {                String aToken = st.nextToken();                if (i == 1) {                    topicChannelName = aToken;                    break;                }                i++;            }            int indexOfChannel = findTab(tabbedPane, topicChannelName);            if (indexOfChannel != -1) {                Component aComponent = tabbedPane.getComponentAt(indexOfChannel);                if (aComponent instanceof ChannelPanel) {                    ( (ChannelPanel) aComponent).setTitleArea(parser.                        getTrailing());                }            }        }        else if (command.equals("TOPIC")                 ) {            String topicChannelName = "";            StringTokenizer st = new StringTokenizer(parser.getParams(),                " \r\n");            if (st.hasMoreTokens()) {                topicChannelName = st.nextToken();            }            int indexOfChannel = findTab(tabbedPane, topicChannelName);            if (indexOfChannel != -1) {                Component aComponent = tabbedPane.getComponentAt(indexOfChannel);                if (aComponent instanceof ChannelPanel) {                    ( (ChannelPanel) aComponent).setTitleArea(parser.                        getTrailing());                    ( (ChannelPanel) aComponent).updateTextArea("TOPIC: " +                        parser.getTrailing() + "(" + parser.getNick() + ")");                }            }        }        else if (command.equals("353") //                 ) {            String channelName = parser.getMiddle();            int index = channelName.lastIndexOf("#");            channelName = channelName.substring(index);            // Find the tab of channelName            int indexOfChannel = findTab(tabbedPane, channelName);            // If the channelName tab exists!            if (indexOfChannel != -1) {                Component aComponent = tabbedPane.getComponentAt(indexOfChannel);                if (aComponent instanceof ChannelPanel) {                    // All the usernames are in trailing!                    String trailing = parser.getTrailing();                    // let's split those usernames with StringTokenizer                    StringTokenizer st = new StringTokenizer(trailing, " \r\n");                    // How many users are there?                    int totalTokens = st.countTokens();                    for (int i = 0; i < totalTokens; i++) {                        // a username/nickname                        String tempnickname = st.nextToken();                        // Add that username to channelPanel's usernameBox                        ( (ChannelPanel) aComponent).updateUserArea(                            tempnickname, "add");                    }                }            }            else { // channelName tab doesn't exist!                ;            }        }        else if (command.equals("404")) {            tabUpdate("Init Window",                      "ERROR: Cannot send msg to channel! " + lineFromServer);        }        else if (command.equals("421")) {            tabUpdate("Init Window",                      "ERROR: Command not found! " + lineFromServer);        }        else if (command.equals("442")) {            tabUpdate("Init Window",                      "ERROR: You're not on that channel! " + lineFromServer);        }        else if (command.equals("443")) {            tabUpdate("Init Window",                      "ERROR: User is already on that channel! " +                      lineFromServer);        }        else if (command.equals("461")) {            tabUpdate("Init Window",                      "ERROR: Need more params for this command! " +                      lineFromServer);        }        else if (command.equals("482")) {            tabUpdate("Init Window",                      "ERROR: You must be channel operator for this! " +                      lineFromServer);        }        else if (command.equals("433")) {            tabUpdate("Init Window",                      nickname + " already in use " + parser.getTrailing());            String newNickname;            newNickname = JOptionPane.showInputDialog(nicknameString,                nickname);            if (newNickname != null) {                nickname = newNickname;                Component aComponent = tabbedPane.getComponentAt(0);                ( (InitPanel) aComponent).setTitleArea(newNickname +                    " connected to: " +                    host + ":" + port);                parseSendToCommand("NICK " + newNickname);            }        }        else {            if (lineFromServer.indexOf("MODE") != -1) {                String nickCommand;                String modeReceiver;                String modeReceived;                String temp;                String channelsMode;                temp = lineFromServer.substring(1, lineFromServer.indexOf(" "));                nickCommand = temp;                if (temp.indexOf("!") != -1) {                    nickCommand = temp.substring(0, temp.indexOf("!"));                }                temp = lineFromServer.substring(lineFromServer.indexOf(" ",                    lineFromServer.indexOf(" ") + 1));                temp = temp.substring(temp.indexOf(" "));// User mode                if (temp.indexOf(":") != -1) {                    modeReceiver = temp.substring(0, temp.indexOf(":") - 1);                    modeReceived = temp.substring(temp.indexOf(":") + 1);                    tabUpdate("Init Window",                              nickCommand + " sets MODE: " + modeReceived + " " +                              modeReceiver);                }// Channel mode                else {                    channelsMode = temp.substring(1, temp.indexOf(" ", 1));                    modeReceived = temp.substring(temp.indexOf(" ", 1) + 1,                                                  temp.length());                    int indexOfChannel = findTab(tabbedPane, channelsMode);                    if (indexOfChannel != -1) {                        Component aComponent = tabbedPane.getComponentAt(                            indexOfChannel);                        if (aComponent instanceof ChannelPanel) {                            ChannelPanel channel = (ChannelPanel) aComponent;                            channel.updateTextArea(nickCommand + " sets MODE: " +                                modeReceived);                            String tmp = modeReceived.substring(0, 1);                            if (tmp.compareToIgnoreCase("-") == 0                                || tmp.compareToIgnoreCase("+") == 0) {                                String name = modeReceived.substring(3);                                SortedListModel model = channel.getUserNameList();                                if (model.contains("!" + name)) {                                    channel.updateUserArea("!" + name, "remove");                                }                                else if (model.contains("@" +                                    name)) {                                    channel.updateUserArea("@" + name, "remove");                                }                                else if (model.contains("%" +                                    name)) {                                    channel.updateUserArea("%" + name, "remove");                                }                                else if (model.contains("+" +                                    name)) {                                    channel.updateUserArea("+" + name, "remove");                                }                                else if (model.contains(name)) {                                    channel.updateUserArea(name, "remove");                                }                                if (tmp.compareToIgnoreCase("+") == 0) {                                    tmp = modeReceived.substring(1, 2);                                    if (tmp.compareToIgnoreCase("v") == 0) {                                        channel.updateUserArea("+" + name,                                            "add");                                    }                                    else if (tmp.compareToIgnoreCase("a") == 0) {                                        channel.updateUserArea("!" + name,                                            "add");                                    }                                    else if (tmp.compareToIgnoreCase("o") == 0) {                                        channel.updateUserArea("@" + name,                                            "add");                                    }                                    else if (tmp.compareToIgnoreCase("h") == 0) {                                        channel.updateUserArea("%" + name,                                            "add");                                    }                                }                                else if (tmp.compareToIgnoreCase("-") == 0) {                                    channel.updateUserArea(name, "add");                                }                            }                        }                    }                }            }            else if (lineFromServer.indexOf(" 366 ") != -1) {                ;            }            else {                tabUpdate("Init Window", lineFromServer);            }        }    }