netserver/HWProto.hs
author unc0rr
Sun, 04 May 2008 14:07:26 +0000
changeset 902 3cc10f0aae37
parent 901 2f5ce9a584f9
child 903 d4e5d8cbe449
permissions -rw-r--r--
Finish conversion
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
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
     9
-- 'noInfo' clients state command handlers
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    10
handleCmd_noInfo :: Handle -> [ClientInfo] -> [RoomInfo] -> [String] -> ([ClientInfo], [RoomInfo], [Handle], [String])
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    11
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    12
handleCmd_noInfo clhandle clients rooms ("NICK":newNick:[]) =
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    13
	if not . null $ nick client then
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    14
		(clients, rooms, [clhandle], ["ERROR", "The nick already chosen"])
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    15
	else if haveSameNick then
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    16
		(clients, rooms, [clhandle], ["WARNING", "Choose another nick"])
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    17
	else
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    18
		(modifyClient clhandle clients (\cl -> cl{nick = newNick}), rooms, [clhandle], ["NICK", newNick])
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    19
	where
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    20
		haveSameNick = not . null $ filter (\cl -> newNick == nick cl) clients
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    21
		client = clientByHandle clhandle clients
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    22
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    23
handleCmd_noInfo clhandle clients rooms ("PROTO":protoNum:[]) =
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    24
	if protocol client > 0 then
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    25
		(clients, rooms, [clhandle], ["ERROR", "Protocol number already known"])
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    26
	else if parsedProto == 0 then
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    27
		(clients, rooms, [clhandle], ["ERROR", "Bad input"])
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    28
	else
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    29
		(modifyClient clhandle clients (\cl -> cl{protocol = parsedProto}), rooms, [], [])
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    30
	where
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    31
		parsedProto = fromMaybe 0 (maybeRead protoNum :: Maybe Word16)
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    32
		client = clientByHandle clhandle clients
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    33
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    34
handleCmd_noInfo clhandle clients rooms _ = (clients, rooms, [clhandle], ["ERROR", "Bad command or incorrect parameter"])
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    35
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    36
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    37
-- 'noRoom' clients state command handlers
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    38
handleCmd_noRoom :: Handle -> [ClientInfo] -> [RoomInfo] -> [String] -> ([ClientInfo], [RoomInfo], [Handle], [String])
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    39
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    40
handleCmd_noRoom clhandle clients rooms ("CREATE":newRoom:roomPassword:[]) =
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    41
	if haveSameRoom then
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    42
		(clients, rooms, [clhandle], ["WARNING", "There's already a room with that name"])
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    43
	else
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    44
		(modifyClient clhandle clients (\cl -> cl{room = newRoom, isMaster = True}), (RoomInfo newRoom roomPassword):rooms, [clhandle], ["JOINS", nick client])
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    45
	where
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    46
		haveSameRoom = not . null $ filter (\room -> newRoom == name room) rooms
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    47
		client = clientByHandle clhandle clients
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    48
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    49
handleCmd_noRoom clhandle clients rooms ("CREATE":newRoom:[]) =
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    50
	handleCmd_noRoom clhandle clients rooms ["CREATE", newRoom, ""]
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    51
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    52
handleCmd_noRoom clhandle clients rooms ("JOIN":roomName:roomPassword:[]) =
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    53
	if noSuchRoom then
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    54
		(clients, rooms, [clhandle], ["WARNING", "There's no room with that name"])
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    55
	else if roomPassword /= password (roomByName roomName rooms) then
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    56
		(clients, rooms, [clhandle], ["WARNING", "Wrong password"])
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    57
	else
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    58
		(modifyClient clhandle clients (\cl -> cl{room = roomName}), rooms, clhandle : (fromRoomHandles roomName clients), ["JOINS", nick client])
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    59
	where
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    60
		noSuchRoom = null $ filter (\room -> roomName == name room) rooms
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    61
		client = clientByHandle clhandle clients
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    62
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    63
handleCmd_noRoom clhandle clients rooms ("JOIN":roomName:[]) =
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    64
	handleCmd_noRoom clhandle clients rooms ["JOIN", roomName, ""]
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    65
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    66
handleCmd_noRoom clhandle clients rooms _ = (clients, rooms, [clhandle], ["ERROR", "Bad command or incorrect parameter"])
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    67
897
35d91fa3753b 'In room' state stub
unc0rr
parents: 896
diff changeset
    68
-- 'inRoom' clients state command handlers
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    69
handleCmd_inRoom :: Handle -> [ClientInfo] -> [RoomInfo] -> [String] -> ([ClientInfo], [RoomInfo], [Handle], [String])
897
35d91fa3753b 'In room' state stub
unc0rr
parents: 896
diff changeset
    70
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    71
handleCmd_inRoom clhandle clients rooms _ = (clients, rooms, [clhandle], ["ERROR", "Bad command or incorrect parameter"])
897
35d91fa3753b 'In room' state stub
unc0rr
parents: 896
diff changeset
    72
898
344ba7dba23d Handle QUIT of the master: disconnect all roommates
unc0rr
parents: 897
diff changeset
    73
-- state-independent command handlers
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    74
handleCmd :: Handle -> [ClientInfo] -> [RoomInfo] -> [String] -> ([ClientInfo], [RoomInfo], [Handle], [String])
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    75
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    76
handleCmd clhandle clients rooms ("QUIT":xs) =
891
701f86df9b4c Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents: 890
diff changeset
    77
	if null (room client) then
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    78
		(clients, rooms, [clhandle], ["QUIT"])
898
344ba7dba23d Handle QUIT of the master: disconnect all roommates
unc0rr
parents: 897
diff changeset
    79
	else if isMaster client then
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    80
		(clients, filter (\rm -> room client /= name rm) rooms, roomMates, ["ROOMABANDONED"]) -- core disconnects clients on ROOMABANDONED command
891
701f86df9b4c Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents: 890
diff changeset
    81
	else
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    82
		(clients, rooms, roomMates, ["QUIT", nick client])
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    83
	where
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    84
		client = clientByHandle clhandle clients
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    85
		roomMates = fromRoomHandles (room client) clients
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    86
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    87
-- check state and call state-dependent commmand handlers
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    88
handleCmd clhandle clients rooms cmd =
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    89
	if null (nick client) || protocol client == 0 then
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    90
		handleCmd_noInfo clhandle clients rooms cmd
897
35d91fa3753b 'In room' state stub
unc0rr
parents: 896
diff changeset
    91
	else if null (room client) then
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    92
		handleCmd_noRoom clhandle clients rooms cmd
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    93
	else
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    94
		handleCmd_inRoom clhandle clients rooms cmd
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    95
	where
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    96
		client = clientByHandle clhandle clients