diff -r 149244d86bf1 -r 2ca76a7f3121 netserver/HWProto.hs --- a/netserver/HWProto.hs Wed Apr 30 20:48:12 2008 +0000 +++ b/netserver/HWProto.hs Thu May 01 14:30:12 2008 +0000 @@ -1,10 +1,44 @@ module HWProto where import IO +import Data.Word 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]) +-- 'noInfo' clients state command handlers +handleCmd_noInfo client clients rooms ("NICK":newNick:[]) = + if not . null $ nick client then + (client, rooms, [client], ["ERROR", "The nick already chosen"]) + else if haveSameNick then + (client, rooms, [client], ["WARNING", "Choose another nick"]) + else + (client{nick = newNick}, rooms, [client], ["NICK", newNick]) + where + haveSameNick = not . null $ filter (\cl -> newNick == nick cl) clients + +handleCmd_noInfo client clients rooms ("PROTO":protoNum:[]) = + if protocol client > 0 then + (client, rooms, [client], ["ERROR", "Protocol number already known"]) + else if parsedProto == 0 then + (client, rooms, [client], ["ERROR", "Bad input"]) + else + (client{protocol = parsedProto}, rooms, [], []) + where + parsedProto = fromMaybe 0 (maybeRead protoNum :: Maybe Word16) + + +handleCmd_noInfo client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"]) + + +-- 'noRoom' clients state command handlers +--handleCmd_noRoom client clients rooms ("CREATE":newRoom:[]) = + +handleCmd_noRoom client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"]) + handleCmd client clients rooms ("QUIT":xs) = if null (room client) then @@ -13,15 +47,8 @@ (client, rooms, clients, ["QUIT", nick client]) -handleCmd client clients rooms ("NICK":newNick:[]) = - if not . null $ nick client then - (client, rooms, [client], ["ERROR", "The nick already chosen"]) - else if haveSameNick then - (client, rooms, [client], ["ERROR", "Choose another nick"]) +handleCmd client clients rooms cmd = + if null (nick client) || protocol client == 0 then + handleCmd_noInfo client clients rooms cmd else - (client{nick = newNick}, rooms, [client], ["NICK", newNick]) - where - haveSameNick = not . null $ filter (\cl -> newNick == nick cl) clients - - -handleCmd client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command"]) + handleCmd_noRoom client clients rooms cmd