package com.jotuntech.sketcher.server.command; import java.nio.ByteBuffer; 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 KickCommand implements Command { int targetKey; String reason; public KickCommand() { } public KickCommand(int targetKey, String reason) { this.targetKey = targetKey; this.reason = reason; } public int perform(Server server, Connection connection) { if(connection != null && connection.getUser() == null) { /** Signed in users only. */ return Connection.SEND_NONE; } /** Locate referenced connection */ Connection targetConnection = server.getConnectionMap().get(targetKey); if(targetConnection == null || targetConnection.getUser() == null) { connection.getSendQueue().offer(new CommandEntry(0, new ServerMessageCommand("The user you are trying to kick doesn't exist."))); return Connection.SEND_NONE; } if(connection != null) { /** Check access controller */ AccessController controller = server.getAccessController(); if(controller != null) { if(!controller.isKickAllowed(server, connection.getUser(), targetConnection.getUser())) { connection.getSendQueue().offer(new CommandEntry(0, new ServerMessageCommand("You are not allowed to do that."))); return Connection.SEND_NONE; } } } /** Remove from connection map */ server.getConnectionMap().remove(targetConnection); /** Set time of death */ targetConnection.setTimeOfDeath(System.currentTimeMillis() + 5000); /** Return and broadcast */ return Connection.SEND_ALL; } public void decode(ByteBuffer in) { targetKey = in.getInt(); StringBuffer reasonBuffer = new StringBuffer(); while(in.remaining() >= 2) { reasonBuffer.append(in.getChar()); } reason = reasonBuffer.toString(); } public void encode(ByteBuffer out) { out.putInt(targetKey); for(int i = 0; i < reason.length(); i++) { out.putChar(reason.charAt(i)); } } }