netserver/HWProto.hs
author unc0rr
Thu, 01 May 2008 14:30:12 +0000
changeset 894 2ca76a7f3121
parent 893 149244d86bf1
child 895 6aee2f335726
permissions -rw-r--r--
- Fixed some bugs - Introduce client protocol number field - Handle PROTO command
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
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
     8
handleCmd :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
     9
handleCmd_noInfo :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    10
handleCmd_noRoom :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String])
891
701f86df9b4c Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents: 890
diff changeset
    11
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    12
-- 'noInfo' clients state command handlers
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    13
handleCmd_noInfo client clients rooms ("NICK":newNick:[]) =
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    14
	if not . null $ nick client then
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    15
		(client, rooms, [client], ["ERROR", "The nick already chosen"])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    16
	else if haveSameNick then
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    17
		(client, rooms, [client], ["WARNING", "Choose another nick"])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    18
	else
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    19
		(client{nick = newNick}, rooms, [client], ["NICK", newNick])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    20
	where
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    21
		haveSameNick = not . null $ filter (\cl -> newNick == nick cl) clients
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    22
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    23
handleCmd_noInfo client clients rooms ("PROTO":protoNum:[]) =
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    24
	if protocol client > 0 then
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    25
		(client, rooms, [client], ["ERROR", "Protocol number already known"])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    26
	else if parsedProto == 0 then
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    27
		(client, rooms, [client], ["ERROR", "Bad input"])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    28
	else
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    29
		(client{protocol = parsedProto}, rooms, [], [])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    30
	where
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    31
		parsedProto = fromMaybe 0 (maybeRead protoNum :: Maybe Word16)
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    32
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    33
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    34
handleCmd_noInfo client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    35
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    36
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    37
-- 'noRoom' clients state command handlers
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    38
--handleCmd_noRoom client clients rooms ("CREATE":newRoom:[]) =
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    39
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    40
handleCmd_noRoom client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"])
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    41
	
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    42
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    43
handleCmd client clients rooms ("QUIT":xs) =
891
701f86df9b4c Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents: 890
diff changeset
    44
	if null (room client) then
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    45
		(client, rooms, [client], ["QUIT"])
891
701f86df9b4c Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents: 890
diff changeset
    46
	else
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    47
		(client, rooms, clients, ["QUIT", nick client])
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    48
891
701f86df9b4c Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents: 890
diff changeset
    49
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    50
handleCmd client clients rooms cmd =
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    51
	if null (nick client) || protocol client == 0 then
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    52
		handleCmd_noInfo client clients rooms cmd
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    53
	else
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    54
		handleCmd_noRoom client clients rooms cmd