commandparser.java
来自「google公司的用Java写的一个聊天软件的原代码」· Java 代码 · 共 617 行 · 第 1/2 页
JAVA
617 行
/*************************************************************************** * Copyright 2006-2008 by Christian Ihle * * kontakt@usikkert.net * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/package net.usikkert.kouchat.misc;import java.io.File;import java.util.Date;import java.util.List;import net.usikkert.kouchat.Constants;import net.usikkert.kouchat.net.FileReceiver;import net.usikkert.kouchat.net.FileSender;import net.usikkert.kouchat.ui.UserInterface;import net.usikkert.kouchat.util.Tools;/** * Parses and executes commands. A command starts with a slash, and can * have arguments. * * @author Christian Ihle */public class CommandParser{ private final Controller controller; private final UserInterface ui; private final MessageController msgController; private final NickDTO me; /** * Constructor. * * @param controller The controller. * @param ui The user interface. */ public CommandParser( final Controller controller, final UserInterface ui ) { this.controller = controller; this.ui = ui; msgController = ui.getMessageController(); me = Settings.getSettings().getMe(); } /** * Command: <em>/topic <optional new topic></em>. * Prints the current topic if no arguments are supplied, * or changes the topic. To remove the topic, use a space as the argument. * * @param args Nothing, or the new topic. */ private void cmdTopic( final String args ) { if ( args.length() == 0 ) { TopicDTO topic = controller.getTopic(); if ( topic.getTopic().equals( "" ) ) { msgController.showSystemMessage( "No topic set" ); } else { String date = Tools.dateToString( new Date( topic.getTime() ), "HH:mm:ss, dd. MMM. yy" ); msgController.showSystemMessage( "Topic is: " + topic.getTopic() + " (set by " + topic.getNick() + " at " + date + ")" ); } } else { fixTopic( args ); } } /** * Command: <em>/away <away message></em>. * Set status to away. * * @param args The away message. */ private void cmdAway( final String args ) { if ( me.isAway() ) { msgController.showSystemMessage( "/away - you are already away: '" + me.getAwayMsg() + "'" ); } else { if ( args.trim().length() == 0 ) { msgController.showSystemMessage( "/away - missing argument <away message>" ); } else { try { controller.changeAwayStatus( me.getCode(), true, args.trim() ); controller.sendAwayMessage(); ui.changeAway( true ); msgController.showSystemMessage( "You went away: " + me.getAwayMsg() ); } catch ( final CommandException e ) { msgController.showSystemMessage( e.getMessage() ); } } } } /** * Command: <em>/back</em>. * Set status to not away. */ private void cmdBack() { if ( me.isAway() ) { try { controller.changeAwayStatus( me.getCode(), false, "" ); controller.sendBackMessage(); ui.changeAway( false ); msgController.showSystemMessage( "You came back" ); } catch ( final CommandException e ) { msgController.showSystemMessage( e.getMessage() ); } } else { msgController.showSystemMessage( "/back - you are not away" ); } } /** * Command: <em>/clear</em>. * Clear all the text from the chat. */ private void cmdClear() { ui.clearChat(); } /** * Command: <em>/about</em>. * Show information about the application. */ private void cmdAbout() { msgController.showSystemMessage( "This is " + Constants.APP_NAME + " v" + Constants.APP_VERSION + ", by " + Constants.AUTHOR_NAME + " - " + Constants.AUTHOR_MAIL + " - " + Constants.APP_WEB ); } /** * Command: <em>/help</em>. * Shows a list of commands. */ private void cmdHelp() { showCommands(); } /** * Command: <em>/whois <nick></em>. * Show information about a user. * * @param args The user to show information about. */ private void cmdWhois( final String args ) { if ( args.trim().length() == 0 ) { msgController.showSystemMessage( "/whois - missing argument <nick>" ); } else { String[] argsArray = args.split( "\\s" ); String nick = argsArray[1].trim(); NickDTO user = controller.getNick( nick ); if ( user == null ) { msgController.showSystemMessage( "/whois - no such user '" + nick + "'" ); } else { String info = "/whois - " + user.getNick(); if ( user.isAway() ) info += " (Away)"; info += ":\nIP address: " + user.getIpAddress() + "\nClient: " + user.getClient() + "\nOperating System: " + user.getOperatingSystem() + "\nOnline: " + Tools.howLongFromNow( user.getLogonTime() ); if ( user.isAway() ) info += "\nAway message: " + user.getAwayMsg(); msgController.showSystemMessage( info ); } } } /** * Command: <em>/send <nick> <file></em>. * Send a file to a user. * * @param args First argument is the user to send to, and the second is * the file to send to the user. */ private void cmdSend( final String args ) { String[] argsArray = args.split( "\\s" ); if ( argsArray.length <= 2 ) { msgController.showSystemMessage( "/send - missing arguments <nick> <file>" ); } else { String nick = argsArray[1]; NickDTO user = controller.getNick( nick ); if ( user != me ) { if ( user == null ) { msgController.showSystemMessage( "/send - no such user '" + nick + "'" ); } else { String file = ""; for ( int i = 2; i < argsArray.length; i++ ) { file += argsArray[i] + " "; } file = file.trim(); File sendFile = new File( file ); if ( sendFile.exists() && sendFile.isFile() ) { sendFile( user, sendFile ); } else { msgController.showSystemMessage( "/send - no such file '" + file + "'" ); } } } else { msgController.showSystemMessage( "/send - no point in doing that!" ); } } } /** * Command: <em>/msg <nick> <msg></em>. * Send a private message to a user. * * @param args The first argument is the user to send to, and the * second is the private message to the user. */ private void cmdMsg( final String args ) { String[] argsArray = args.split( "\\s" ); if ( argsArray.length <= 2 ) { msgController.showSystemMessage( "/msg - missing arguments <nick> <msg>" ); } else
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?