netserver/HWProto.hs
changeset 1320 bffc7262e25e
parent 1317 13cf8c5a7428
child 1321 d7dc4e86201e
equal deleted inserted replaced
1319:fedaae857a50 1320:bffc7262e25e
     2 
     2 
     3 import IO
     3 import IO
     4 import Data.List
     4 import Data.List
     5 import Data.Word
     5 import Data.Word
     6 import Miscutils
     6 import Miscutils
     7 import Maybe (fromMaybe, fromJust)
     7 import Maybe
     8 import qualified Data.Map as Map
     8 import qualified Data.Map as Map
     9 
     9 
    10 answerBadCmd = [(clientOnly, ["ERROR", "Bad command, state or incorrect parameter"])]
    10 answerBadCmd = [(clientOnly, ["ERROR", "Bad command, state or incorrect parameter"])]
    11 answerNotMaster = [(clientOnly, ["ERROR", "You cannot configure room parameters"])]
    11 answerNotMaster = [(clientOnly, ["ERROR", "You cannot configure room parameters"])]
    12 answerQuit = [(clientOnly, ["off"])]
    12 answerQuit = [(clientOnly, ["off"])]
    59 	else if haveSameNick then
    59 	else if haveSameNick then
    60 		(noChangeClients, noChangeRooms, answerNickChooseAnother)
    60 		(noChangeClients, noChangeRooms, answerNickChooseAnother)
    61 	else
    61 	else
    62 		(modifyClient client{nick = newNick}, noChangeRooms, answerNick newNick)
    62 		(modifyClient client{nick = newNick}, noChangeRooms, answerNick newNick)
    63 	where
    63 	where
    64 		haveSameNick = not . null $ filter (\cl -> newNick == nick cl) clients
    64 		haveSameNick = isJust $ find (\cl -> newNick == nick cl) clients
    65 
    65 
    66 handleCmd_noInfo client _ _ ["PROTO", protoNum] =
    66 handleCmd_noInfo client _ _ ["PROTO", protoNum] =
    67 	if protocol client > 0 then
    67 	if protocol client > 0 then
    68 		(noChangeClients, noChangeRooms, answerProtocolKnown)
    68 		(noChangeClients, noChangeRooms, answerProtocolKnown)
    69 	else if parsedProto == 0 then
    69 	else if parsedProto == 0 then
    85 	if haveSameRoom then
    85 	if haveSameRoom then
    86 		(noChangeClients, noChangeRooms, answerRoomExists)
    86 		(noChangeClients, noChangeRooms, answerRoomExists)
    87 	else
    87 	else
    88 		(modifyClient client{room = newRoom, isMaster = True}, addRoom (RoomInfo newRoom roomPassword (protocol client) [] Map.empty), answerJoined $ nick client)
    88 		(modifyClient client{room = newRoom, isMaster = True}, addRoom (RoomInfo newRoom roomPassword (protocol client) [] Map.empty), answerJoined $ nick client)
    89 	where
    89 	where
    90 		haveSameRoom = not . null $ filter (\room -> newRoom == name room) rooms
    90 		haveSameRoom = isJust $ find (\room -> newRoom == name room) rooms
    91 
    91 
    92 handleCmd_noRoom client clients rooms ["CREATE", newRoom] =
    92 handleCmd_noRoom client clients rooms ["CREATE", newRoom] =
    93 	handleCmd_noRoom client clients rooms ["CREATE", newRoom, ""]
    93 	handleCmd_noRoom client clients rooms ["CREATE", newRoom, ""]
    94 	
    94 	
    95 handleCmd_noRoom client clients rooms ["JOIN", roomName, roomPassword] =
    95 handleCmd_noRoom client clients rooms ["JOIN", roomName, roomPassword] =
    98 	else if roomPassword /= password joinRoom then
    98 	else if roomPassword /= password joinRoom then
    99 		(noChangeClients, noChangeRooms, answerWrongPassword)
    99 		(noChangeClients, noChangeRooms, answerWrongPassword)
   100 	else
   100 	else
   101 		(modifyClient client{room = roomName}, noChangeRooms, (answerJoined $ nick client) ++ answerNicks ++ answerFullConfig joinRoom)
   101 		(modifyClient client{room = roomName}, noChangeRooms, (answerJoined $ nick client) ++ answerNicks ++ answerFullConfig joinRoom)
   102 	where
   102 	where
   103 		noSuchRoom = null $ filter (\room -> roomName == name room) rooms
   103 		noSuchRoom = isNothing $ find (\room -> roomName == name room) rooms
   104 		answerNicks = [(clientOnly, ["JOINED"] ++ (map nick $ filter (\ci -> room ci == roomName) clients))]
   104 		answerNicks = [(clientOnly, ["JOINED"] ++ (map nick $ filter (\ci -> room ci == roomName) clients))]
   105 		joinRoom = roomByName roomName rooms
   105 		joinRoom = roomByName roomName rooms
   106 
   106 
   107 handleCmd_noRoom client clients rooms ["JOIN", roomName] =
   107 handleCmd_noRoom client clients rooms ["JOIN", roomName] =
   108 	handleCmd_noRoom client clients rooms ["JOIN", roomName, ""]
   108 	handleCmd_noRoom client clients rooms ["JOIN", roomName, ""]