commandparser.java

来自「google公司的用Java写的一个聊天软件的原代码」· Java 代码 · 共 617 行 · 第 1/2 页

JAVA
617
字号
		{			String nick = argsArray[1];			NickDTO user = controller.getNick( nick );			if ( user == null )			{				msgController.showSystemMessage( "/msg - no such user '" + nick + "'" );			}			else if ( user == me )			{				msgController.showSystemMessage( "/msg - no point in doing that!" );			}			else if ( user.getPrivateChatPort() == 0 )			{				msgController.showSystemMessage( "/msg - " + user.getNick() + " can't receive private chat messages" );			}			else			{				String privmsg = "";				for ( int i = 2; i < argsArray.length; i++ )				{					privmsg += argsArray[i] + " ";				}				privmsg = privmsg.trim();				try				{					controller.sendPrivateMessage( privmsg, user.getIpAddress(), user.getPrivateChatPort(), user.getCode() );					msgController.showPrivateOwnMessage( user, privmsg );				}				catch ( final CommandException e )				{					msgController.showSystemMessage( e.getMessage() );				}			}		}	}	/**	 * Command: <em>/nick &lt;new nick&gt;</em>.	 * Changes your nick name.	 *	 * @param args The nick to change to.	 */	private void cmdNick( final String args )	{		if ( args.trim().length() == 0 )		{			msgController.showSystemMessage( "/nick - missing argument <nick>" );		}		else		{			String[] argsArray = args.split( "\\s" );			String nick = argsArray[1].trim();			if ( !nick.equals( me.getNick() ) )			{				if ( controller.isNickInUse( nick ) )				{					msgController.showSystemMessage( "/nick - '" + nick + "' is in use by someone else" );				}				else if ( !Tools.isValidNick( nick ) )				{					msgController.showSystemMessage( "/nick - '" + nick + "' is not a valid nick name. (1-10 letters)" );				}				else				{					try					{						controller.changeMyNick( nick );						msgController.showSystemMessage( "You changed nick to " + me.getNick() );						ui.showTopic();					}					catch ( final CommandException e )					{						msgController.showSystemMessage( e.getMessage() );					}				}			}			else			{				msgController.showSystemMessage( "/nick - you are already called '" + nick + "'" );			}		}	}	/**	 * Command: <em>/names</em>.	 * Shows a list of connected users.	 */	private void cmdNames()	{		NickList list = controller.getNickList();		String nickList = "";		for ( int i = 0; i < list.size(); i++ )		{			NickDTO nick = list.get( i );			nickList += nick.getNick();			if ( i < list.size() - 1 )				nickList += ", ";		}		msgController.showSystemMessage( "Users: " + nickList );	}	/**	 * Command: <em>/transfers</em>.	 * Shows a list of all transfers and their status.	 */	private void cmdTransfers()	{		List<FileSender> fsList = controller.getTransferList().getFileSenders();		List<FileReceiver> frList = controller.getTransferList().getFileReceivers();		String senders = "Sending:";		String receivers = "\nReceiving:";		for ( FileSender fs : fsList )		{			senders += "\n" + fs.getFileName() + " [" + Tools.byteToString( fs.getFileSize() ) + "] (" + fs.getPercent() + "%) to " + fs.getNick().getNick();		}		for ( FileReceiver fr : frList )		{			receivers += "\n" + fr.getFileName() + " [" + Tools.byteToString( fr.getFileSize() ) + "] (" + fr.getPercent() + "%) from " + fr.getNick().getNick();		}		msgController.showSystemMessage( "File transfers:\n" + senders + receivers );	}	/**	 * Command: <em>//&lt;text&gt;</em>.	 * Sends the text as a message, instead of parsing it as a command.	 *	 * @param line The text starting with a slash.	 */	private void cmdSlash( final String line )	{		String message = line.replaceFirst( "/", "" );		try		{			controller.sendChatMessage( message );			msgController.showOwnMessage( message );		}		catch ( final CommandException e )		{			msgController.showSystemMessage( e.getMessage() );		}	}	/**	 * Command: <em>/'anything'</em>.	 * The command was not recognized by the parser.	 *	 * @param command The unknown command.	 */	private void cmdUnknown( final String command )	{		msgController.showSystemMessage( "Unknown command '" + command + "'. Type /help for a list of commands" );	}	/**	 * Updates the topic. If the new topic is empty, the topic will be removed.	 *	 * @param newTopic The new topic to use.	 */	public void fixTopic( final String newTopic )	{		TopicDTO topic = controller.getTopic();		String trimTopic = newTopic.trim();		if ( !trimTopic.equals( topic.getTopic().trim() ) )		{			try			{				controller.changeTopic( trimTopic );				if ( trimTopic.length() > 0 )					msgController.showSystemMessage( "You changed the topic to: " + trimTopic );				else					msgController.showSystemMessage( "You removed the topic" );				ui.showTopic();			}			catch ( final CommandException e )			{				msgController.showSystemMessage( e.getMessage() );			}		}	}	/**	 * Sends a file to a user.	 *	 * @param user The user to send to.	 * @param file The file to send to the user.	 */	public void sendFile( final NickDTO user, final File file )	{		try		{			controller.sendFile( user.getCode(), file.length(), file.hashCode(), file.getName() );			FileSender fileSend = new FileSender( user, file );			ui.showTransfer( fileSend );			controller.getTransferList().addFileSender( fileSend );			String size = Tools.byteToString( file.length() );			msgController.showSystemMessage( "Trying to send the file "					+ file.getName() + " [" + size + "] to " + user.getNick() );		}		catch ( final CommandException e )		{			msgController.showSystemMessage( e.getMessage() );		}	}	/**	 * Shows a list of all the supported commands, with a short description.	 */	public void showCommands()	{		msgController.showSystemMessage( Constants.APP_NAME + " commands:\n"				+ "/help - show this help message\n"				+ "/about - information about " + Constants.APP_NAME + "\n"				+ "/clear - clear all the text from the chat\n"				+ "/whois <nick> - show information about a user\n"				+ "/names - show the user list\n"				+ "/nick <new nick> - changes your nick name\n"				+ "/away <away message> - set status to away\n"				+ "/back - set status to not away\n"				+ "/send <nick> <file> - send a file to a user\n"				+ "/msg <nick> <msg> - send a private message to a user\n"				+ "/transfers - shows a list of all transfers and their status\n"				+ "/topic <optional new topic> - prints the current topic, or changes the topic\n"				+ "//<text> - send the text as a normal message, with a single slash" );	}	/**	 * Parses the line to split the command from the arguments.	 * The command is then checked against valid options and redirected	 * to the appropriate method.	 *	 * @param line The command in its raw form.	 */	public void parse( final String line )	{		String command = "";		if ( line.contains( " " ) )			command = line.substring( 1, line.indexOf( ' ' ) );		else			command = line.substring( 1, line.length() );		if ( command.length() > 0 )		{			String args = line.replaceFirst( "/" + command, "" );			if ( command.equals( "topic" ) )				cmdTopic( args );			else if ( command.equals( "away" ) )				cmdAway( args );			else if ( command.equals( "back" ) )				cmdBack();			else if ( command.equals( "clear" ) )				cmdClear();			else if ( command.equals( "about" ) )				cmdAbout();			else if ( command.equals( "help" ) )				cmdHelp();			else if ( command.equals( "whois" ) )				cmdWhois( args );			else if ( command.equals( "send" ) )				cmdSend( args );			else if ( command.equals( "msg" ) )				cmdMsg( args );			else if ( command.equals( "nick" ) )				cmdNick( args );			else if ( command.equals( "names" ) )				cmdNames();			else if ( command.equals( "transfers" ) )				cmdTransfers();			else if ( command.startsWith( "/" ) )				cmdSlash( line );			else				cmdUnknown( command );		}		else			cmdUnknown( command );	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?