netserver/HWProto.hs
changeset 894 2ca76a7f3121
parent 893 149244d86bf1
child 895 6aee2f335726
equal deleted inserted replaced
893:149244d86bf1 894:2ca76a7f3121
     1 module HWProto where
     1 module HWProto where
     2 
     2 
     3 import IO
     3 import IO
       
     4 import Data.Word
     4 import Miscutils
     5 import Miscutils
       
     6 import Maybe (fromMaybe)
     5 
     7 
     6 handleCmd :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
     8 handleCmd :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
       
     9 handleCmd_noInfo :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
       
    10 handleCmd_noRoom :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
     7 
    11 
       
    12 -- 'noInfo' clients state command handlers
       
    13 handleCmd_noInfo client clients rooms ("NICK":newNick:[]) =
       
    14 	if not . null $ nick client then
       
    15 		(client, rooms, [client], ["ERROR", "The nick already chosen"])
       
    16 	else if haveSameNick then
       
    17 		(client, rooms, [client], ["WARNING", "Choose another nick"])
       
    18 	else
       
    19 		(client{nick = newNick}, rooms, [client], ["NICK", newNick])
       
    20 	where
       
    21 		haveSameNick = not . null $ filter (\cl -> newNick == nick cl) clients
       
    22 
       
    23 handleCmd_noInfo client clients rooms ("PROTO":protoNum:[]) =
       
    24 	if protocol client > 0 then
       
    25 		(client, rooms, [client], ["ERROR", "Protocol number already known"])
       
    26 	else if parsedProto == 0 then
       
    27 		(client, rooms, [client], ["ERROR", "Bad input"])
       
    28 	else
       
    29 		(client{protocol = parsedProto}, rooms, [], [])
       
    30 	where
       
    31 		parsedProto = fromMaybe 0 (maybeRead protoNum :: Maybe Word16)
       
    32 
       
    33 
       
    34 handleCmd_noInfo client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"])
       
    35 
       
    36 
       
    37 -- 'noRoom' clients state command handlers
       
    38 --handleCmd_noRoom client clients rooms ("CREATE":newRoom:[]) =
       
    39 
       
    40 handleCmd_noRoom client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"])
       
    41 	
     8 
    42 
     9 handleCmd client clients rooms ("QUIT":xs) =
    43 handleCmd client clients rooms ("QUIT":xs) =
    10 	if null (room client) then
    44 	if null (room client) then
    11 		(client, rooms, [client], ["QUIT"])
    45 		(client, rooms, [client], ["QUIT"])
    12 	else
    46 	else
    13 		(client, rooms, clients, ["QUIT", nick client])
    47 		(client, rooms, clients, ["QUIT", nick client])
    14 
    48 
    15 
    49 
    16 handleCmd client clients rooms ("NICK":newNick:[]) =
    50 handleCmd client clients rooms cmd =
    17 	if not . null $ nick client then
    51 	if null (nick client) || protocol client == 0 then
    18 		(client, rooms, [client], ["ERROR", "The nick already chosen"])
    52 		handleCmd_noInfo client clients rooms cmd
    19 	else if haveSameNick then
       
    20 		(client, rooms, [client], ["ERROR", "Choose another nick"])
       
    21 	else
    53 	else
    22 		(client{nick = newNick}, rooms, [client], ["NICK", newNick])
    54 		handleCmd_noRoom client clients rooms cmd
    23 	where
       
    24 		haveSameNick = not . null $ filter (\cl -> newNick == nick cl) clients
       
    25 
       
    26 
       
    27 handleCmd client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command"])