summaryrefslogtreecommitdiff
path: root/src/Server/CommandsTable.java
blob: 38d179933b589ff657b336fdbad14569c2f62f22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package Server;
import java.io.IOException;
import java.util.Hashtable;

public class CommandsTable
{
	private Hashtable<String, Command> table;
	public CommandsTable()
	{
		this.table = new Hashtable<String, Command>();
		this.table.put("NICK", new CommandNick());
		this.table.put("USER", new CommandUser());
		this.table.put("QUIT", new CommandQuit());
	}
	
	public void runCommand (Client client , String commandName , String args)
	{
		//FIXME: convert to upper case
		Command command = this.table.get(commandName);
		
		if (command == null)
		{
			System.err.println("Missing command <" + commandName + ">.");
			command = new CommandBad();
		}
		command.run(client, args);
	}
	
}


abstract class Command 
{
	abstract public void run(Client client, String args);
	
	public void println(Client client, String str)
	{
		try {
			client.println(":me " + str);
		} catch (IOException e) {
			System.err.println("Failed to print to client socket: <" + str + "> (" + e + ")");
		}
	}
}

class CommandNick extends Command
{
	public CommandNick() {}
	
	public void run(Client client, String args)
	{
		client.setNick(args);
		// FIXME: print reply
	}
}

class CommandUser extends Command
{
	public void run(Client client, String args)
	{
		//client.setNick(args);
		this.println(client, "001 " + client.getNick() + " :Welcome to Dor's ircd");
	}
}

class CommandQuit extends Command
{
	public CommandQuit() {}
	
	public void run(Client client, String args)
	{
		client.disconect();
		
	}
}

class CommandBad extends Command
{
	public void run(Client client, String args)
	{
		this.println(client, "421 " + client.getNick() + " " + args + " Unknown command.");
	}
}