author | unc0rr |
Thu, 01 May 2008 15:14:32 +0000 | |
changeset 895 | 6aee2f335726 |
parent 894 | 2ca76a7f3121 |
child 896 | 93df8ac94382 |
permissions | -rw-r--r-- |
890 | 1 |
module HWProto where |
2 |
||
3 |
import IO |
|
894 | 4 |
import Data.Word |
890 | 5 |
import Miscutils |
894 | 6 |
import Maybe (fromMaybe) |
890 | 7 |
|
895 | 8 |
fromRoom :: String -> [ClientInfo] -> [ClientInfo] |
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 | 11 |
-- 'noInfo' clients state command handlers |
895 | 12 |
handleCmd_noInfo :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String]) |
13 |
||
894 | 14 |
handleCmd_noInfo client clients rooms ("NICK":newNick:[]) = |
15 |
if not . null $ nick client then |
|
16 |
(client, rooms, [client], ["ERROR", "The nick already chosen"]) |
|
17 |
else if haveSameNick then |
|
18 |
(client, rooms, [client], ["WARNING", "Choose another nick"]) |
|
19 |
else |
|
20 |
(client{nick = newNick}, rooms, [client], ["NICK", newNick]) |
|
21 |
where |
|
22 |
haveSameNick = not . null $ filter (\cl -> newNick == nick cl) clients |
|
23 |
||
24 |
handleCmd_noInfo client clients rooms ("PROTO":protoNum:[]) = |
|
25 |
if protocol client > 0 then |
|
26 |
(client, rooms, [client], ["ERROR", "Protocol number already known"]) |
|
27 |
else if parsedProto == 0 then |
|
28 |
(client, rooms, [client], ["ERROR", "Bad input"]) |
|
29 |
else |
|
30 |
(client{protocol = parsedProto}, rooms, [], []) |
|
31 |
where |
|
32 |
parsedProto = fromMaybe 0 (maybeRead protoNum :: Maybe Word16) |
|
33 |
||
34 |
||
35 |
handleCmd_noInfo client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"]) |
|
36 |
||
37 |
||
38 |
-- 'noRoom' clients state command handlers |
|
895 | 39 |
handleCmd_noRoom :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String]) |
40 |
||
41 |
handleCmd_noRoom client clients rooms ("CREATE":newRoom:roomPassword:[]) = |
|
42 |
if haveSameRoom then |
|
43 |
(client, rooms, [client], ["WARNING", "There's already a room with that name"]) |
|
44 |
else |
|
45 |
(client{room = newRoom, isMaster = True}, (RoomInfo newRoom roomPassword):rooms, [client], ["JOIN", newRoom, nick client]) |
|
46 |
where |
|
47 |
haveSameRoom = not . null $ filter (\room -> newRoom == name room) rooms |
|
48 |
||
49 |
handleCmd_noRoom client clients rooms ("CREATE":newRoom:[]) = |
|
50 |
handleCmd_noRoom client clients rooms ["CREATE", newRoom, ""] |
|
51 |
||
52 |
handleCmd_noRoom client clients rooms ("JOIN":roomName:roomPassword:[]) = |
|
53 |
if noRoom then |
|
54 |
(client, rooms, [client], ["WARNING", "There's no room with that name"]) |
|
55 |
else |
|
56 |
(client{room = roomName}, rooms, client : fromRoom roomName clients, ["JOIN", roomName, nick client]) |
|
57 |
where |
|
58 |
noRoom = null $ filter (\room -> roomName == name room) rooms |
|
59 |
||
60 |
handleCmd_noRoom client clients rooms ("JOIN":roomName:[]) = |
|
61 |
handleCmd_noRoom client clients rooms ["JOIN", roomName, ""] |
|
894 | 62 |
|
63 |
handleCmd_noRoom client _ rooms _ = (client, rooms, [client], ["ERROR", "Bad command or incorrect parameter"]) |
|
895 | 64 |
|
65 |
-- state-independent comman handlers |
|
66 |
handleCmd :: ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientInfo, [RoomInfo], [ClientInfo], [String]) |
|
893 | 67 |
|
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 | 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 | 72 |
(client, rooms, fromRoom (room client) clients, ["QUIT", nick client]) |
893 | 73 |
|
895 | 74 |
-- check state and call state-dependent commmand handlers |
894 | 75 |
handleCmd client clients rooms cmd = |
76 |
if null (nick client) || protocol client == 0 then |
|
77 |
handleCmd_noInfo client clients rooms cmd |
|
893 | 78 |
else |
894 | 79 |
handleCmd_noRoom client clients rooms cmd |