package com.jotuntech.sketcher.server.command; import java.nio.ByteBuffer; import com.jotuntech.sketcher.common.Log; import com.jotuntech.sketcher.common.User; import com.jotuntech.sketcher.server.AccessController; import com.jotuntech.sketcher.server.Command; import com.jotuntech.sketcher.server.CommandEntry; import com.jotuntech.sketcher.server.Connection; import com.jotuntech.sketcher.server.Server; public class SayCommand implements Command { boolean isAction; String text; public SayCommand() { } public SayCommand(String text, boolean isAction) { this.isAction = isAction; this.text = text; } public int perform(Server server, Connection connection) { User user = connection.getUser(); if(user == null) { /** Signed in users only. */ return Connection.SEND_NONE; } if(user.isViewer()) { connection.getSendQueue().offer(new CommandEntry(0, new ServerMessageCommand("Chat is not permitted in viewer mode. Please sign in to participate."))); return Connection.SEND_NONE; } AccessController controller = server.getAccessController(); if(controller != null && !controller.isMessageAllowed(server, connection.getUser(), text)) { connection.getSendQueue().offer(new CommandEntry(0, new ServerMessageCommand("You are not allowed to say that."))); return Connection.SEND_NONE; } if(isAction) { Log.info("* " + connection.getUser().getName() + text); } else { Log.info("<" + connection.getUser().getName() + "> " + text); } return Connection.SEND_ALL; } public void decode(ByteBuffer in) { isAction = in.get() != 0 ? true : false; StringBuffer textBuffer = new StringBuffer(); while(in.remaining() >= 2) { textBuffer.append(in.getChar()); } text = textBuffer.toString(); } public void encode(ByteBuffer out) { out.put((byte) (isAction ? 0xFF : 0x00)); for(int i = 0; i < text.length(); i++) { out.putChar(text.charAt(i)); } } }