netserver/HWProto.hs
changeset 896 93df8ac94382
parent 895 6aee2f335726
child 897 35d91fa3753b
equal deleted inserted replaced
895:6aee2f335726 896:93df8ac94382
     1 module HWProto where
     1 module HWProto where
     2 
     2 
     3 import IO
     3 import IO
       
     4 import Data.List
     4 import Data.Word
     5 import Data.Word
     5 import Miscutils
     6 import Miscutils
     6 import Maybe (fromMaybe)
     7 import Maybe (fromMaybe, fromJust)
     7 
     8 
     8 fromRoom :: String -> [ClientInfo] -> [ClientInfo]
     9 fromRoom :: String -> [ClientInfo] -> [ClientInfo]
     9 fromRoom roomName clients = filter (\cl -> roomName == room cl) clients
    10 fromRoom roomName clients = filter (\cl -> roomName == room cl) clients
    10 
    11 
    11 -- 'noInfo' clients state command handlers
    12 -- 'noInfo' clients state command handlers
    50 	handleCmd_noRoom client clients rooms ["CREATE", newRoom, ""]
    51 	handleCmd_noRoom client clients rooms ["CREATE", newRoom, ""]
    51 
    52 
    52 handleCmd_noRoom client clients rooms ("JOIN":roomName:roomPassword:[]) =
    53 handleCmd_noRoom client clients rooms ("JOIN":roomName:roomPassword:[]) =
    53 	if noRoom then
    54 	if noRoom then
    54 		(client, rooms, [client], ["WARNING", "There's no room with that name"])
    55 		(client, rooms, [client], ["WARNING", "There's no room with that name"])
       
    56 	else if roomPassword /= password (getRoom roomName) then
       
    57 		(client, rooms, [client], ["WARNING", "Wrong password"])
    55 	else
    58 	else
    56 		(client{room = roomName}, rooms, client : fromRoom roomName clients, ["JOIN", roomName, nick client])
    59 		(client{room = roomName}, rooms, client : fromRoom roomName clients, ["JOIN", roomName, nick client])
    57 	where
    60 	where
    58 		noRoom = null $ filter (\room -> roomName == name room) rooms
    61 		noRoom = null $ filter (\room -> roomName == name room) rooms
       
    62 		getRoom roomName = fromJust $ find (\room -> roomName == name room) rooms
    59 
    63 
    60 handleCmd_noRoom client clients rooms ("JOIN":roomName:[]) =
    64 handleCmd_noRoom client clients rooms ("JOIN":roomName:[]) =
    61 	handleCmd_noRoom client clients rooms ["JOIN", roomName, ""]
    65 	handleCmd_noRoom client clients rooms ["JOIN", roomName, ""]
    62 
    66 
    63 handleCmd_noRoom client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"])
    67 handleCmd_noRoom client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"])