netserver/HWProto.hs
author unc0rr
Thu, 01 May 2008 15:55:03 +0000
changeset 898 344ba7dba23d
parent 897 35d91fa3753b
child 899 36f91881e83f
permissions -rw-r--r--
Handle QUIT of the master: disconnect all roommates
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
890
1d8c4a5ec622 - Improve server core
unc0rr
parents:
diff changeset
     1
module HWProto where
1d8c4a5ec622 - Improve server core
unc0rr
parents:
diff changeset
     2
1d8c4a5ec622 - Improve server core
unc0rr
parents:
diff changeset
     3
import IO
896
93df8ac94382 Handle password parameter on JOIN
unc0rr
parents: 895
diff changeset
     4
import Data.List
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
     5
import Data.Word
890
1d8c4a5ec622 - Improve server core
unc0rr
parents:
diff changeset
     6
import Miscutils
896
93df8ac94382 Handle password parameter on JOIN
unc0rr
parents: 895
diff changeset
     7
import Maybe (fromMaybe, fromJust)
890
1d8c4a5ec622 - Improve server core
unc0rr
parents:
diff changeset
     8
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
     9
fromRoom :: String -> [ClientInfo] -> [ClientInfo]
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    10
fromRoom roomName clients = filter (\cl -> roomName == room cl) clients
891
701f86df9b4c Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents: 890
diff changeset
    11
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    12
-- 'noInfo' clients state command handlers
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    13
handleCmd_noInfo :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    14
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    15
handleCmd_noInfo client clients rooms ("NICK":newNick:[]) =
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    16
	if not . null $ nick client then
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    17
		(client, rooms, [client], ["ERROR", "The nick already chosen"])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    18
	else if haveSameNick then
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    19
		(client, rooms, [client], ["WARNING", "Choose another nick"])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    20
	else
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    21
		(client{nick = newNick}, rooms, [client], ["NICK", newNick])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    22
	where
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    23
		haveSameNick = not . null $ filter (\cl -> newNick == nick cl) clients
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    24
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    25
handleCmd_noInfo client clients rooms ("PROTO":protoNum:[]) =
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    26
	if protocol client > 0 then
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    27
		(client, rooms, [client], ["ERROR", "Protocol number already known"])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    28
	else if parsedProto == 0 then
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    29
		(client, rooms, [client], ["ERROR", "Bad input"])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    30
	else
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    31
		(client{protocol = parsedProto}, rooms, [], [])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    32
	where
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    33
		parsedProto = fromMaybe 0 (maybeRead protoNum :: Maybe Word16)
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    34
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    35
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    36
handleCmd_noInfo client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    37
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    38
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    39
-- 'noRoom' clients state command handlers
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    40
handleCmd_noRoom :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    41
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    42
handleCmd_noRoom client clients rooms ("CREATE":newRoom:roomPassword:[]) =
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    43
	if haveSameRoom then
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    44
		(client, rooms, [client], ["WARNING", "There's already a room with that name"])
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    45
	else
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    46
		(client{room = newRoom, isMaster = True}, (RoomInfo newRoom roomPassword):rooms, [client], ["JOIN", newRoom, nick client])
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    47
	where
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    48
		haveSameRoom = not . null $ filter (\room -> newRoom == name room) rooms
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    49
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    50
handleCmd_noRoom client clients rooms ("CREATE":newRoom:[]) =
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    51
	handleCmd_noRoom client clients rooms ["CREATE", newRoom, ""]
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    52
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    53
handleCmd_noRoom client clients rooms ("JOIN":roomName:roomPassword:[]) =
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    54
	if noRoom then
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    55
		(client, rooms, [client], ["WARNING", "There's no room with that name"])
896
93df8ac94382 Handle password parameter on JOIN
unc0rr
parents: 895
diff changeset
    56
	else if roomPassword /= password (getRoom roomName) then
93df8ac94382 Handle password parameter on JOIN
unc0rr
parents: 895
diff changeset
    57
		(client, rooms, [client], ["WARNING", "Wrong password"])
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    58
	else
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    59
		(client{room = roomName}, rooms, client : fromRoom roomName clients, ["JOIN", roomName, nick client])
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    60
	where
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    61
		noRoom = null $ filter (\room -> roomName == name room) rooms
896
93df8ac94382 Handle password parameter on JOIN
unc0rr
parents: 895
diff changeset
    62
		getRoom roomName = fromJust $ find (\room -> roomName == name room) rooms
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    63
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    64
handleCmd_noRoom client clients rooms ("JOIN":roomName:[]) =
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    65
	handleCmd_noRoom client clients rooms ["JOIN", roomName, ""]
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    66
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    67
handleCmd_noRoom client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"])
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    68
897
35d91fa3753b 'In room' state stub
unc0rr
parents: 896
diff changeset
    69
-- 'inRoom' clients state command handlers
35d91fa3753b 'In room' state stub
unc0rr
parents: 896
diff changeset
    70
handleCmd_inRoom :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
35d91fa3753b 'In room' state stub
unc0rr
parents: 896
diff changeset
    71
35d91fa3753b 'In room' state stub
unc0rr
parents: 896
diff changeset
    72
handleCmd_inRoom client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"])
35d91fa3753b 'In room' state stub
unc0rr
parents: 896
diff changeset
    73
898
344ba7dba23d Handle QUIT of the master: disconnect all roommates
unc0rr
parents: 897
diff changeset
    74
-- state-independent command handlers
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    75
handleCmd :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    76
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    77
handleCmd client clients rooms ("QUIT":xs) =
891
701f86df9b4c Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents: 890
diff changeset
    78
	if null (room client) then
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    79
		(client, rooms, [client], ["QUIT"])
898
344ba7dba23d Handle QUIT of the master: disconnect all roommates
unc0rr
parents: 897
diff changeset
    80
	else if isMaster client then
344ba7dba23d Handle QUIT of the master: disconnect all roommates
unc0rr
parents: 897
diff changeset
    81
		(client, filter (\rm -> room client /= name rm) rooms, fromRoom (room client) clients, ["ROOMABANDONED"])
891
701f86df9b4c Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents: 890
diff changeset
    82
	else
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    83
		(client, rooms, fromRoom (room client) clients, ["QUIT", nick client])
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    84
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    85
-- check state and call state-dependent commmand handlers
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    86
handleCmd client clients rooms cmd =
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    87
	if null (nick client) || protocol client == 0 then
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    88
		handleCmd_noInfo client clients rooms cmd
897
35d91fa3753b 'In room' state stub
unc0rr
parents: 896
diff changeset
    89
	else if null (room client) then
35d91fa3753b 'In room' state stub
unc0rr
parents: 896
diff changeset
    90
		handleCmd_noRoom client clients rooms cmd
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    91
	else
897
35d91fa3753b 'In room' state stub
unc0rr
parents: 896
diff changeset
    92
		handleCmd_inRoom client clients rooms cmd