netserver/HWProto.hs
changeset 895 6aee2f335726
parent 894 2ca76a7f3121
child 896 93df8ac94382
equal deleted inserted replaced
894:2ca76a7f3121 895:6aee2f335726
     3 import IO
     3 import IO
     4 import Data.Word
     4 import Data.Word
     5 import Miscutils
     5 import Miscutils
     6 import Maybe (fromMaybe)
     6 import Maybe (fromMaybe)
     7 
     7 
     8 handleCmd :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
     8 fromRoom :: String -> [ClientInfo] -> [ClientInfo]
     9 handleCmd_noInfo :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
     9 fromRoom roomName clients = filter (\cl -> roomName == room cl) clients
    10 handleCmd_noRoom :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
       
    11 
    10 
    12 -- 'noInfo' clients state command handlers
    11 -- 'noInfo' clients state command handlers
       
    12 handleCmd_noInfo :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
       
    13 
    13 handleCmd_noInfo client clients rooms ("NICK":newNick:[]) =
    14 handleCmd_noInfo client clients rooms ("NICK":newNick:[]) =
    14 	if not . null $ nick client then
    15 	if not . null $ nick client then
    15 		(client, rooms, [client], ["ERROR", "The nick already chosen"])
    16 		(client, rooms, [client], ["ERROR", "The nick already chosen"])
    16 	else if haveSameNick then
    17 	else if haveSameNick then
    17 		(client, rooms, [client], ["WARNING", "Choose another nick"])
    18 		(client, rooms, [client], ["WARNING", "Choose another nick"])
    33 
    34 
    34 handleCmd_noInfo client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"])
    35 handleCmd_noInfo client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"])
    35 
    36 
    36 
    37 
    37 -- 'noRoom' clients state command handlers
    38 -- 'noRoom' clients state command handlers
    38 --handleCmd_noRoom client clients rooms ("CREATE":newRoom:[]) =
    39 handleCmd_noRoom :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
       
    40 
       
    41 handleCmd_noRoom client clients rooms ("CREATE":newRoom:roomPassword:[]) =
       
    42 	if haveSameRoom then
       
    43 		(client, rooms, [client], ["WARNING", "There's already a room with that name"])
       
    44 	else
       
    45 		(client{room = newRoom, isMaster = True}, (RoomInfo newRoom roomPassword):rooms, [client], ["JOIN", newRoom, nick client])
       
    46 	where
       
    47 		haveSameRoom = not . null $ filter (\room -> newRoom == name room) rooms
       
    48 
       
    49 handleCmd_noRoom client clients rooms ("CREATE":newRoom:[]) =
       
    50 	handleCmd_noRoom client clients rooms ["CREATE", newRoom, ""]
       
    51 
       
    52 handleCmd_noRoom client clients rooms ("JOIN":roomName:roomPassword:[]) =
       
    53 	if noRoom then
       
    54 		(client, rooms, [client], ["WARNING", "There's no room with that name"])
       
    55 	else
       
    56 		(client{room = roomName}, rooms, client : fromRoom roomName clients, ["JOIN", roomName, nick client])
       
    57 	where
       
    58 		noRoom = null $ filter (\room -> roomName == name room) rooms
       
    59 
       
    60 handleCmd_noRoom client clients rooms ("JOIN":roomName:[]) =
       
    61 	handleCmd_noRoom client clients rooms ["JOIN", roomName, ""]
    39 
    62 
    40 handleCmd_noRoom client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"])
    63 handleCmd_noRoom client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"])
    41 	
    64 
       
    65 -- state-independent comman handlers	
       
    66 handleCmd :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
    42 
    67 
    43 handleCmd client clients rooms ("QUIT":xs) =
    68 handleCmd client clients rooms ("QUIT":xs) =
    44 	if null (room client) then
    69 	if null (room client) then
    45 		(client, rooms, [client], ["QUIT"])
    70 		(client, rooms, [client], ["QUIT"])
    46 	else
    71 	else
    47 		(client, rooms, clients, ["QUIT", nick client])
    72 		(client, rooms, fromRoom (room client) clients, ["QUIT", nick client])
    48 
    73 
    49 
    74 -- check state and call state-dependent commmand handlers
    50 handleCmd client clients rooms cmd =
    75 handleCmd client clients rooms cmd =
    51 	if null (nick client) || protocol client == 0 then
    76 	if null (nick client) || protocol client == 0 then
    52 		handleCmd_noInfo client clients rooms cmd
    77 		handleCmd_noInfo client clients rooms cmd
    53 	else
    78 	else
    54 		handleCmd_noRoom client clients rooms cmd
    79 		handleCmd_noRoom client clients rooms cmd