author | unc0rr |
Mon, 21 Jul 2008 09:45:40 +0000 | |
changeset 1082 | 596b1dcdc1df |
parent 902 | 3cc10f0aae37 |
child 1083 | 3448dd03483f |
permissions | -rw-r--r-- |
849 | 1 |
module Miscutils where |
2 |
||
3 |
import IO |
|
4 |
import Control.Concurrent.STM |
|
894 | 5 |
import Data.Word |
6 |
import Data.Char |
|
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
7 |
import Data.List |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
8 |
import Maybe (fromJust) |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
9 |
|
849 | 10 |
|
851 | 11 |
data ClientInfo = |
1082 | 12 |
ClientInfo |
851 | 13 |
{ |
1082 | 14 |
chan :: TChan [String], |
851 | 15 |
handle :: Handle, |
16 |
nick :: String, |
|
894 | 17 |
protocol :: Word16, |
851 | 18 |
room :: String, |
19 |
isMaster :: Bool |
|
20 |
} |
|
21 |
||
1082 | 22 |
instance Eq ClientInfo where |
23 |
a1 == a2 = handle a1 == handle a2 |
|
24 |
||
851 | 25 |
data RoomInfo = |
26 |
RoomInfo |
|
27 |
{ |
|
28 |
name :: String, |
|
29 |
password :: String |
|
30 |
} |
|
31 |
||
1082 | 32 |
type ClientsTransform = [ClientInfo] -> [ClientInfo] |
33 |
type RoomsTransform = [RoomInfo] -> [RoomInfo] |
|
34 |
type HandlesSelector = ClientInfo -> [ClientInfo] -> [RoomInfo] -> [Handle] |
|
35 |
type CmdHandler = ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientsTransform, RoomsTransform, HandlesSelector, [String]) |
|
36 |
||
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
37 |
|
902 | 38 |
roomByName :: String -> [RoomInfo] -> RoomInfo |
39 |
roomByName roomName rooms = fromJust $ find (\room -> roomName == name room) rooms |
|
40 |
||
1082 | 41 |
tselect :: [ClientInfo] -> STM ([String], ClientInfo) |
42 |
tselect = foldl orElse retry . map (\ci -> (flip (,) ci) `fmap` readTChan (chan ci)) |
|
889 | 43 |
|
894 | 44 |
maybeRead :: Read a => String -> Maybe a |
45 |
maybeRead s = case reads s of |
|
46 |
[(x, rest)] | all isSpace rest -> Just x |
|
47 |
_ -> Nothing |
|
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
48 |
|
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
49 |
deleteBy2t :: (a -> b -> Bool) -> b -> [a] -> [a] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
50 |
deleteBy2t _ _ [] = [] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
51 |
deleteBy2t eq x (y:ys) = if y `eq` x then ys else y : deleteBy2t eq x ys |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
52 |
|
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
53 |
deleteFirstsBy2t :: (a -> b -> Bool) -> [a] -> [b] -> [a] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
54 |
deleteFirstsBy2t eq = foldl (flip (deleteBy2t eq)) |
1082 | 55 |
|
56 |
sameRoom :: HandlesSelector |
|
57 |
sameRoom client clients rooms = map handle $ filter (\ci -> room ci == room client) clients |
|
58 |
||
59 |
othersInRoom :: HandlesSelector |
|
60 |
othersInRoom client clients rooms = map handle $ filter (client /=) $ filter (\ci -> room ci == room client) clients |
|
61 |
||
62 |
fromRoom :: String -> HandlesSelector |
|
63 |
fromRoom roomName _ clients _ = map handle $ filter (\ci -> room ci == roomName) clients |
|
64 |
||
65 |
clientOnly :: HandlesSelector |
|
66 |
clientOnly client _ _ = [handle client] |
|
67 |
||
68 |
noChangeClients :: ClientsTransform |
|
69 |
noChangeClients a = a |
|
70 |
||
71 |
modifyClient :: ClientInfo -> ClientsTransform |
|
72 |
modifyClient client (cl:cls) = |
|
73 |
if cl == client then |
|
74 |
client : cls |
|
75 |
else |
|
76 |
cl : (modifyClient client cls) |
|
77 |
||
78 |
noChangeRooms :: RoomsTransform |
|
79 |
noChangeRooms a = a |
|
80 |
||
81 |
addRoom :: RoomInfo -> RoomsTransform |
|
82 |
addRoom room rooms = room:rooms |
|
83 |
||
84 |
removeRoom :: String -> RoomsTransform |
|
85 |
removeRoom roomname rooms = filter (\rm -> roomname /= name rm) rooms |
|
86 |
||
87 |
badCmd :: [String] |
|
88 |
badCmd = ["ERROR", "Bad command, state or incorrect parameter"] |