netserver/HWProto.hs
author unc0rr
Thu, 01 May 2008 15:14:32 +0000
changeset 895 6aee2f335726
parent 894 2ca76a7f3121
child 896 93df8ac94382
permissions -rw-r--r--
- Remove old hwserv code - Introduce rooms (CREATE and JOIN commands handling)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
890
1d8c4a5ec622 - Improve server core
unc0rr
parents:
diff changeset
     1
module HWProto where
1d8c4a5ec622 - Improve server core
unc0rr
parents:
diff changeset
     2
1d8c4a5ec622 - Improve server core
unc0rr
parents:
diff changeset
     3
import IO
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
     4
import Data.Word
890
1d8c4a5ec622 - Improve server core
unc0rr
parents:
diff changeset
     5
import Miscutils
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
     6
import Maybe (fromMaybe)
890
1d8c4a5ec622 - Improve server core
unc0rr
parents:
diff changeset
     7
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
     8
fromRoom :: String -> [ClientInfo] -> [ClientInfo]
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
     9
fromRoom roomName clients = filter (\cl -> roomName == room cl) clients
891
701f86df9b4c Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents: 890
diff changeset
    10
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    11
-- 'noInfo' clients state command handlers
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    12
handleCmd_noInfo :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    13
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    14
handleCmd_noInfo client clients rooms ("NICK":newNick:[]) =
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    15
	if not . null $ nick client then
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    16
		(client, rooms, [client], ["ERROR", "The nick already chosen"])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    17
	else if haveSameNick then
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    18
		(client, rooms, [client], ["WARNING", "Choose another nick"])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    19
	else
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    20
		(client{nick = newNick}, rooms, [client], ["NICK", newNick])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    21
	where
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    22
		haveSameNick = not . null $ filter (\cl -> newNick == nick cl) clients
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    23
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    24
handleCmd_noInfo client clients rooms ("PROTO":protoNum:[]) =
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    25
	if protocol client > 0 then
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    26
		(client, rooms, [client], ["ERROR", "Protocol number already known"])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    27
	else if parsedProto == 0 then
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    28
		(client, rooms, [client], ["ERROR", "Bad input"])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    29
	else
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    30
		(client{protocol = parsedProto}, rooms, [], [])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    31
	where
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    32
		parsedProto = fromMaybe 0 (maybeRead protoNum :: Maybe Word16)
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    33
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    34
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    35
handleCmd_noInfo client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    36
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    37
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    38
-- 'noRoom' clients state command handlers
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    39
handleCmd_noRoom :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    40
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    41
handleCmd_noRoom client clients rooms ("CREATE":newRoom:roomPassword:[]) =
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    42
	if haveSameRoom then
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    43
		(client, rooms, [client], ["WARNING", "There's already a room with that name"])
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    44
	else
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    45
		(client{room = newRoom, isMaster = True}, (RoomInfo newRoom roomPassword):rooms, [client], ["JOIN", newRoom, nick client])
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    46
	where
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    47
		haveSameRoom = not . null $ filter (\room -> newRoom == name room) rooms
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    48
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    49
handleCmd_noRoom client clients rooms ("CREATE":newRoom:[]) =
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    50
	handleCmd_noRoom client clients rooms ["CREATE", newRoom, ""]
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    51
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    52
handleCmd_noRoom client clients rooms ("JOIN":roomName:roomPassword:[]) =
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    53
	if noRoom then
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    54
		(client, rooms, [client], ["WARNING", "There's no room with that name"])
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    55
	else
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    56
		(client{room = roomName}, rooms, client : fromRoom roomName clients, ["JOIN", roomName, nick client])
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    57
	where
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    58
		noRoom = null $ filter (\room -> roomName == name room) rooms
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    59
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    60
handleCmd_noRoom client clients rooms ("JOIN":roomName:[]) =
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    61
	handleCmd_noRoom client clients rooms ["JOIN", roomName, ""]
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    62
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    63
handleCmd_noRoom client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"])
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    64
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    65
-- state-independent comman handlers	
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    66
handleCmd :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    67
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    68
handleCmd client clients rooms ("QUIT":xs) =
891
701f86df9b4c Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents: 890
diff changeset
    69
	if null (room client) then
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    70
		(client, rooms, [client], ["QUIT"])
891
701f86df9b4c Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents: 890
diff changeset
    71
	else
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    72
		(client, rooms, fromRoom (room client) clients, ["QUIT", nick client])
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    73
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    74
-- check state and call state-dependent commmand handlers
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    75
handleCmd client clients rooms cmd =
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    76
	if null (nick client) || protocol client == 0 then
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    77
		handleCmd_noInfo client clients rooms cmd
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    78
	else
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    79
		handleCmd_noRoom client clients rooms cmd