/** * creates a new channel, if the channel exists it is removed (this method doubles as a * removeChannel) * * @param name the name of the channel */ public void newChannel(String name, boolean pass) { if (channels.containsKey(name)) { channels.remove(name); cboChannels.removeItem(name); } else { channels.put(name, new Boolean(pass)); cboChannels.addItem(name); } }
/** cleans up a connection by removing all user from all maintained lists */ public void close() { error("Connection to server was lost"); admin = false; users.clear(); afks.clear(); ignores.clear(); admins.clear(); channels.clear(); cboChannels.removeAllItems(); updateList(); }
/** * 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 <passphrase> \t- become an admin<br>" + "\\create <channel> [password]\t- create a new channel with optional password<br>" + "\\join <channel> [password]\t- join channel with optional password<br>" + "\\disconnect \t\t- disconnect from server<br>" + "\\whisper <user> <message> \t- whisper to a user<br>" + "\\private <user> \t- start a private message session<br>" + "\\ignore <user> \t- ignores a user<br>" + "\\clearignore \t\t- clear list of ignores<br>" + "\\reconnect \t\t- attempt to reconnect to server<br>" + "\\rename <new name> \t- change your username<br>" + "\\invite <user> \t- invite user to join at your channel<br>"; if (admin) { commands += "\\kick <user> \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 <new name> 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 <user> <message>"); return false; } cmd = cmd.substring(start + 1); start = cmd.indexOf(' '); if (start < 0) { error("usage: \\whisper <user> <message>"); 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 <user> <message>"); } 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 <user>"); 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 <user>"); 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 <newname>"); 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 <user>"); 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 <channel> [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 <channel> [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 <host> <port>"); return false; } String phost = cmd.substring(start+1); start = phost.indexOf(' '); if (start < 0) { error("usage: \\proxy <host> <port>"); 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 <password>"); 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 <user>"); 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; }
/** Initializes the graphical components */ public void init() { username = getParameter("username"); if (username == null) { username = JOptionPane.showInputDialog( this, "Please enter a username", "Login", JOptionPane.QUESTION_MESSAGE); } try { PORT = Integer.valueOf(getParameter("port")).intValue(); } catch (NumberFormatException e) { PORT = 42412; } URL url = getDocumentBase(); site = url.getHost(); locationURL = "http://" + site + ":" + url.getPort() + "/" + url.getPath(); setSize(615, 362); c = getContentPane(); c.setBackground(new Color(224, 224, 224)); if (site == null || locationURL == null) { c.add(new JLabel("ERROR: did not recieve needed data from page")); } myAction = new MyAction(); myKeyListener = new MyKeyListener(); myMouseListener = new MyMouseListener(); myHyperlinkListener = new MyHyperlinkListener(); c.setLayout(null); cboChannels = new JComboBox(); cboChannels.setBounds(5, 5, 150, 24); butChannel = new JButton("Join"); butChannel.setToolTipText("Join channel"); butChannel.addActionListener(myAction); butChannel.setBounds(160, 5, 60, 24); butCreate = new JButton("Create"); butCreate.setToolTipText("Create new channel"); butCreate.addActionListener(myAction); butCreate.setBounds(230, 5, 100, 24); butCreate.setEnabled(false); butInvite = new JButton("Invite"); butInvite.setToolTipText("Invite Friend"); butInvite.addActionListener(myAction); butInvite.setBounds(340, 5, 80, 24); mainChat = new ChatPane(this); textScroller = new JScrollPane( mainChat, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); textScroller.setBounds(5, 34, 500, 270); userList = new JList(); userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); userList.setCellRenderer(new MyCellRenderer()); userList.setBackground(new Color(249, 249, 250)); JScrollPane userScroller = new JScrollPane(userList); userScroller.setBounds(510, 34, 100, 297); messageText = new JTextField(); messageText.setBounds(5, 309, 500, 22); messageText.setColumns(10); messageText.setBackground(new Color(249, 249, 250)); JMenuItem item; popup = new JPopupMenu("test"); popup.add("whisper").addActionListener(myAction); popup.add("private message").addActionListener(myAction); popup.add("ignore").addActionListener(myAction); popup.add("clear ignore list").addActionListener(myAction); conNo = new ImageIcon(getURL("images/connect_no.gif")); conYes = new ImageIcon(getURL("images/connect_established.gif")); secNo = new ImageIcon(getURL("images/decrypted.gif")); secYes = new ImageIcon(getURL("images/encrypted.gif")); conIcon = new JLabel(conNo); conIcon.setBorder(new EtchedBorder()); secIcon = new JLabel(secNo); secIcon.setBorder(new EtchedBorder()); conIcon.setBounds(563, 334, 22, 22); secIcon.setBounds(588, 334, 22, 22); bottomText = new JLabel( "<html><body><font color=#445577><b>" + "LlamaChat " + VERSION + "</b></font> © " + "<a href=\"" + linkURL + "\">Joseph Monti</a> 2002-2003" + "</body></html>"); bottomText.setBounds(5, 336, 500, 20); c.add(cboChannels); c.add(butChannel); c.add(butCreate); c.add(butInvite); c.add(textScroller); c.add(userScroller); c.add(messageText); c.add(conIcon); c.add(secIcon); c.add(bottomText); userList.addMouseListener(myMouseListener); messageText.addKeyListener(myKeyListener); bottomText.addMouseListener(myMouseListener); users = new ArrayList(); ignores = new ArrayList(5); afks = new ArrayList(5); admins = new ArrayList(5); history = new CommandHistory(10); admin = false; channels = new Hashtable(); privates = new PrivateMsg(this); showUserStatus = false; myColors[0] = new Color(200, 0, 0); myColors[1] = new Color(0, 150, 0); myColors[2] = new Color(0, 0, 200); rect = new Rectangle(0, 0, 1, 1); String opening = "<font color=#333333>" + "==================================<br>" + "Welcome to LlamaChat " + VERSION + "<br>" + "If you need assistance, type \\help<br>" + "Enjoy your stay!<br>" + "Maestria Aplicada en Redes<br>" + "==================================<br></font>"; HTMLDocument doc = (HTMLDocument) mainChat.getDocument(); HTMLEditorKit kit = (HTMLEditorKit) mainChat.getEditorKit(); try { kit.insertHTML(doc, doc.getLength(), opening, 0, 0, HTML.Tag.FONT); } catch (Throwable t) { t.printStackTrace(System.out); } // validate the name if (!username.matches("[\\w_-]+?")) { error( "username contains invalid characters, changing to " + "'invalid' for now. " + "Type \\rename to chose a new name"); username = "******"; } if (username.length() > 10) { username = username.substring(0, 10); error("username too long, changed to " + username); } connect(); }