Browse Source

Explicit isOP field

Tankernn 8 years ago
parent
commit
270321b1f6

+ 3 - 0
.gitignore

@@ -1,5 +1,7 @@
 .settings/
 .metadata/
+.classpath
+.project
 bin/
 logs/
 *.class
@@ -16,3 +18,4 @@ logs/
 
 # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
 hs_err_pid*
+/target/

+ 4 - 0
src/main/java/server/Client.java

@@ -25,6 +25,7 @@ public class Client implements ActionListener {
 	public Socket sock;
 	
 	public String[] permissions;
+	public boolean isOP = false;
 	
 	int messLastPeriod = 0;
 	Timer timer = new Timer(800, this);
@@ -119,6 +120,9 @@ public class Client implements ActionListener {
 	}
 	
 	public boolean hasPermission(String commandPermission) {
+		if (this.isOP)
+			return true;
+		
 		for (int i = 0; i < permissions.length; i++) {
 			if (commandPermission.startsWith(permissions[i].replace(".*", ".")) || commandPermission.equalsIgnoreCase(permissions[i]) || permissions[i].equalsIgnoreCase("*"))
 				return true;

+ 8 - 0
src/main/java/server/LocalClient.java

@@ -1,6 +1,7 @@
 package server;
 
 import java.io.BufferedReader;
+import java.io.IOException;
 import java.io.InputStreamReader;
 
 import common.MessagePacket;
@@ -11,6 +12,8 @@ public class LocalClient extends Client {
 	public LocalClient() { //Constructor for local client, the server, with full permissions
 		in = new BufferedReader(new InputStreamReader(System.in));
 		
+		this.isOP = true;
+		
 		username = "SERVER";
 		permissions = new String[] {"*"};
 		
@@ -20,6 +23,11 @@ public class LocalClient extends Client {
 	@Override
 	public void disconnect() {
 		readuser.interrupt();
+		try {
+			in.close();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
 	}
 	
 	@Override

+ 45 - 0
src/main/java/server/command/GiveOP.java

@@ -0,0 +1,45 @@
+package server.command;
+
+import common.Command;
+import common.MessagePacket;
+import common.MessagePacket.MessageType;
+import server.Client;
+import server.Server;
+
+public class GiveOP extends Command {
+
+	@Override
+	public void execute(String[] args, Client caller) throws Exception {
+		try {
+			Client target = Server.getUserByName(args[0]).get();
+			target.isOP = true;
+			target.send(new MessagePacket("You are now OP.", MessageType.INFO));
+		} catch (NullPointerException ex) {
+			caller.send(new MessagePacket("No such user: " + args[0], MessageType.WARNING));
+		}
+	}
+
+	@Override
+	public String getName() {
+		return "op";
+	}
+
+	@Override
+	public String getPermission() {
+		// TODO Auto-generated method stub
+		return "server.op";
+	}
+
+	@Override
+	public String getDescription() {
+		// TODO Auto-generated method stub
+		return "Give a client OP permissions.";
+	}
+
+	@Override
+	public int getMinArgNumber() {
+		// TODO Auto-generated method stub
+		return 1;
+	}
+
+}