diff -r 2ca76a7f3121 -r 6aee2f335726 netserver/HWProto.hs --- a/netserver/HWProto.hs Thu May 01 14:30:12 2008 +0000 +++ b/netserver/HWProto.hs Thu May 01 15:14:32 2008 +0000 @@ -5,11 +5,12 @@ import Miscutils import Maybe (fromMaybe) -handleCmd :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String]) -handleCmd_noInfo :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String]) -handleCmd_noRoom :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String]) +fromRoom :: String -> [ClientInfo] -> [ClientInfo] +fromRoom roomName clients = filter (\cl -> roomName == room cl) clients -- 'noInfo' clients state command handlers +handleCmd_noInfo :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String]) + handleCmd_noInfo client clients rooms ("NICK":newNick:[]) = if not . null $ nick client then (client, rooms, [client], ["ERROR", "The nick already chosen"]) @@ -35,18 +36,42 @@ -- 'noRoom' clients state command handlers ---handleCmd_noRoom client clients rooms ("CREATE":newRoom:[]) = +handleCmd_noRoom :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String]) + +handleCmd_noRoom client clients rooms ("CREATE":newRoom:roomPassword:[]) = + if haveSameRoom then + (client, rooms, [client], ["WARNING", "There's already a room with that name"]) + else + (client{room = newRoom, isMaster = True}, (RoomInfo newRoom roomPassword):rooms, [client], ["JOIN", newRoom, nick client]) + where + haveSameRoom = not . null $ filter (\room -> newRoom == name room) rooms + +handleCmd_noRoom client clients rooms ("CREATE":newRoom:[]) = + handleCmd_noRoom client clients rooms ["CREATE", newRoom, ""] + +handleCmd_noRoom client clients rooms ("JOIN":roomName:roomPassword:[]) = + if noRoom then + (client, rooms, [client], ["WARNING", "There's no room with that name"]) + else + (client{room = roomName}, rooms, client : fromRoom roomName clients, ["JOIN", roomName, nick client]) + where + noRoom = null $ filter (\room -> roomName == name room) rooms + +handleCmd_noRoom client clients rooms ("JOIN":roomName:[]) = + handleCmd_noRoom client clients rooms ["JOIN", roomName, ""] handleCmd_noRoom client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"]) - + +-- state-independent comman handlers +handleCmd :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String]) handleCmd client clients rooms ("QUIT":xs) = if null (room client) then (client, rooms, [client], ["QUIT"]) else - (client, rooms, clients, ["QUIT", nick client]) + (client, rooms, fromRoom (room client) clients, ["QUIT", nick client]) - +-- check state and call state-dependent commmand handlers handleCmd client clients rooms cmd = if null (nick client) || protocol client == 0 then handleCmd_noInfo client clients rooms cmd