author | unc0rr |
Sat, 24 Sep 2011 00:00:57 +0400 | |
changeset 5996 | 2c72fe81dd37 |
parent 5426 | 109e9b5761c2 |
child 6012 | 6bac93097da3 |
permissions | -rw-r--r-- |
5184 | 1 |
{-# LANGUAGE OverloadedStrings #-} |
2 |
module Actions where |
|
3 |
||
4 |
import Control.Concurrent |
|
5 |
import qualified Data.Set as Set |
|
6 |
import qualified Data.Sequence as Seq |
|
7 |
import qualified Data.List as L |
|
8 |
import qualified Control.Exception as Exception |
|
9 |
import System.Log.Logger |
|
10 |
import Control.Monad |
|
11 |
import Data.Time |
|
12 |
import Data.Maybe |
|
13 |
import Control.Monad.Reader |
|
14 |
import Control.Monad.State.Strict |
|
15 |
import qualified Data.ByteString.Char8 as B |
|
16 |
import Control.DeepSeq |
|
17 |
import Data.Unique |
|
18 |
import Control.Arrow |
|
19 |
import Control.Exception |
|
5209
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
20 |
import System.Process |
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
21 |
import Network.Socket |
5184 | 22 |
----------------------------- |
5996
2c72fe81dd37
Convert boolean variable + a bunch of fields which make sense only while game is going on into Maybe + structure
unc0rr
parents:
5426
diff
changeset
|
23 |
import OfficialServer.GameReplayStore |
5184 | 24 |
import CoreTypes |
25 |
import Utils |
|
26 |
import ClientIO |
|
27 |
import ServerState |
|
28 |
import Consts |
|
29 |
import ConfigFile |
|
30 |
||
31 |
data Action = |
|
32 |
AnswerClients ![ClientChan] ![B.ByteString] |
|
33 |
| SendServerMessage |
|
34 |
| SendServerVars |
|
35 |
| MoveToRoom RoomIndex |
|
36 |
| MoveToLobby B.ByteString |
|
37 |
| RemoveTeam B.ByteString |
|
38 |
| RemoveRoom |
|
39 |
| UnreadyRoomClients |
|
40 |
| JoinLobby |
|
41 |
| ProtocolError B.ByteString |
|
42 |
| Warning B.ByteString |
|
43 |
| NoticeMessage Notice |
|
44 |
| ByeClient B.ByteString |
|
45 |
| KickClient ClientIndex |
|
46 |
| KickRoomClient ClientIndex |
|
47 |
| BanClient NominalDiffTime B.ByteString ClientIndex |
|
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
48 |
| BanIP B.ByteString NominalDiffTime B.ByteString |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
49 |
| BanList |
5184 | 50 |
| ChangeMaster |
51 |
| RemoveClientTeams ClientIndex |
|
52 |
| ModifyClient (ClientInfo -> ClientInfo) |
|
53 |
| ModifyClient2 ClientIndex (ClientInfo -> ClientInfo) |
|
54 |
| ModifyRoom (RoomInfo -> RoomInfo) |
|
55 |
| ModifyServerInfo (ServerInfo -> ServerInfo) |
|
56 |
| AddRoom B.ByteString B.ByteString |
|
57 |
| CheckRegistered |
|
58 |
| ClearAccountsCache |
|
59 |
| ProcessAccountInfo AccountInfo |
|
60 |
| AddClient ClientInfo |
|
61 |
| DeleteClient ClientIndex |
|
62 |
| PingAll |
|
63 |
| StatsAction |
|
5209
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
64 |
| RestartServer |
5184 | 65 |
| AddNick2Bans B.ByteString B.ByteString UTCTime |
66 |
| AddIP2Bans B.ByteString B.ByteString UTCTime |
|
67 |
| CheckBanned |
|
68 |
| SaveReplay |
|
69 |
||
70 |
||
71 |
type CmdHandler = [B.ByteString] -> Reader (ClientIndex, IRnC) [Action] |
|
72 |
||
73 |
instance NFData Action where |
|
74 |
rnf (AnswerClients chans msg) = chans `deepseq` msg `deepseq` () |
|
75 |
rnf a = a `seq` () |
|
76 |
||
77 |
instance NFData B.ByteString |
|
78 |
instance NFData (Chan a) |
|
79 |
||
80 |
||
81 |
othersChans :: StateT ServerState IO [ClientChan] |
|
82 |
othersChans = do |
|
83 |
cl <- client's id |
|
84 |
ri <- clientRoomA |
|
85 |
liftM (map sendChan . filter (/= cl)) $ roomClientsS ri |
|
86 |
||
87 |
processAction :: Action -> StateT ServerState IO () |
|
88 |
||
89 |
||
90 |
processAction (AnswerClients chans msg) = |
|
91 |
io $ mapM_ (`writeChan` (msg `deepseq` msg)) (chans `deepseq` chans) |
|
92 |
||
93 |
||
94 |
processAction SendServerMessage = do |
|
95 |
chan <- client's sendChan |
|
96 |
protonum <- client's clientProto |
|
97 |
si <- liftM serverInfo get |
|
98 |
let message = if protonum < latestReleaseVersion si then |
|
99 |
serverMessageForOldVersions si |
|
100 |
else |
|
101 |
serverMessage si |
|
102 |
processAction $ AnswerClients [chan] ["SERVER_MESSAGE", message] |
|
103 |
||
104 |
||
105 |
processAction SendServerVars = do |
|
106 |
chan <- client's sendChan |
|
107 |
si <- gets serverInfo |
|
108 |
io $ writeChan chan ("SERVER_VARS" : vars si) |
|
109 |
where |
|
110 |
vars si = [ |
|
111 |
"MOTD_NEW", serverMessage si, |
|
112 |
"MOTD_OLD", serverMessageForOldVersions si, |
|
113 |
"LATEST_PROTO", showB $ latestReleaseVersion si |
|
114 |
] |
|
115 |
||
116 |
||
117 |
processAction (ProtocolError msg) = do |
|
118 |
chan <- client's sendChan |
|
119 |
processAction $ AnswerClients [chan] ["ERROR", msg] |
|
120 |
||
121 |
||
122 |
processAction (Warning msg) = do |
|
123 |
chan <- client's sendChan |
|
124 |
processAction $ AnswerClients [chan] ["WARNING", msg] |
|
125 |
||
126 |
processAction (NoticeMessage n) = do |
|
127 |
chan <- client's sendChan |
|
128 |
processAction $ AnswerClients [chan] ["NOTICE", showB . fromEnum $ n] |
|
129 |
||
130 |
processAction (ByeClient msg) = do |
|
131 |
(Just ci) <- gets clientIndex |
|
132 |
ri <- clientRoomA |
|
133 |
||
134 |
chan <- client's sendChan |
|
135 |
clNick <- client's nick |
|
136 |
loggedIn <- client's logonPassed |
|
137 |
||
138 |
when (ri /= lobbyId) $ do |
|
139 |
processAction $ MoveToLobby ("quit: " `B.append` msg) |
|
140 |
return () |
|
141 |
||
142 |
clientsChans <- liftM (Prelude.map sendChan . Prelude.filter logonPassed) $! allClientsS |
|
143 |
io $ |
|
144 |
infoM "Clients" (show ci ++ " quits: " ++ B.unpack msg) |
|
145 |
||
146 |
processAction $ AnswerClients [chan] ["BYE", msg] |
|
147 |
when loggedIn $ processAction $ AnswerClients clientsChans ["LOBBY:LEFT", clNick, msg] |
|
148 |
||
149 |
s <- get |
|
150 |
put $! s{removedClients = ci `Set.insert` removedClients s} |
|
151 |
||
152 |
processAction (DeleteClient ci) = do |
|
153 |
io $ debugM "Clients" $ "DeleteClient: " ++ show ci |
|
154 |
||
155 |
rnc <- gets roomsClients |
|
156 |
io $ removeClient rnc ci |
|
157 |
||
158 |
s <- get |
|
159 |
put $! s{removedClients = ci `Set.delete` removedClients s} |
|
5209
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
160 |
|
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
161 |
sp <- gets (shutdownPending . serverInfo) |
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
162 |
cls <- allClientsS |
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
163 |
io $ when (sp && null cls) $ throwIO ShutdownException |
5184 | 164 |
|
165 |
processAction (ModifyClient f) = do |
|
166 |
(Just ci) <- gets clientIndex |
|
167 |
rnc <- gets roomsClients |
|
168 |
io $ modifyClient rnc f ci |
|
169 |
return () |
|
170 |
||
171 |
processAction (ModifyClient2 ci f) = do |
|
172 |
rnc <- gets roomsClients |
|
173 |
io $ modifyClient rnc f ci |
|
174 |
return () |
|
175 |
||
176 |
||
177 |
processAction (ModifyRoom f) = do |
|
178 |
rnc <- gets roomsClients |
|
179 |
ri <- clientRoomA |
|
180 |
io $ modifyRoom rnc f ri |
|
181 |
return () |
|
182 |
||
183 |
||
184 |
processAction (ModifyServerInfo f) = do |
|
185 |
modify (\s -> s{serverInfo = f $ serverInfo s}) |
|
186 |
si <- gets serverInfo |
|
187 |
io $ writeServerConfig si |
|
188 |
||
189 |
||
190 |
processAction (MoveToRoom ri) = do |
|
191 |
(Just ci) <- gets clientIndex |
|
192 |
rnc <- gets roomsClients |
|
193 |
||
194 |
io $ do |
|
195 |
modifyClient rnc (\cl -> cl{teamsInGame = 0, isReady = False, isMaster = False}) ci |
|
196 |
modifyRoom rnc (\r -> r{playersIn = playersIn r + 1}) ri |
|
197 |
moveClientToRoom rnc ri ci |
|
198 |
||
199 |
chans <- liftM (map sendChan) $ roomClientsS ri |
|
200 |
clNick <- client's nick |
|
201 |
||
202 |
processAction $ AnswerClients chans ["JOINED", clNick] |
|
203 |
||
204 |
||
205 |
processAction (MoveToLobby msg) = do |
|
206 |
(Just ci) <- gets clientIndex |
|
207 |
ri <- clientRoomA |
|
208 |
rnc <- gets roomsClients |
|
5996
2c72fe81dd37
Convert boolean variable + a bunch of fields which make sense only while game is going on into Maybe + structure
unc0rr
parents:
5426
diff
changeset
|
209 |
(gameProgress, playersNum) <- io $ room'sM rnc ((isJust . gameInfo) &&& playersIn) ri |
5184 | 210 |
ready <- client's isReady |
211 |
master <- client's isMaster |
|
212 |
-- client <- client's id |
|
213 |
clNick <- client's nick |
|
214 |
chans <- othersChans |
|
215 |
||
216 |
if master then |
|
217 |
if gameProgress && playersNum > 1 then |
|
218 |
mapM_ processAction [ChangeMaster, AnswerClients chans ["LEFT", clNick, msg], NoticeMessage AdminLeft, RemoveClientTeams ci] |
|
219 |
else |
|
220 |
processAction RemoveRoom |
|
221 |
else |
|
222 |
mapM_ processAction [AnswerClients chans ["LEFT", clNick, msg], RemoveClientTeams ci] |
|
223 |
||
224 |
-- when not removing room |
|
225 |
when (not master || (gameProgress && playersNum > 1)) . io $ do |
|
226 |
modifyRoom rnc (\r -> r{ |
|
227 |
playersIn = playersIn r - 1, |
|
228 |
readyPlayers = if ready then readyPlayers r - 1 else readyPlayers r |
|
229 |
}) ri |
|
230 |
moveClientToLobby rnc ci |
|
231 |
||
232 |
processAction ChangeMaster = do |
|
233 |
(Just ci) <- gets clientIndex |
|
234 |
ri <- clientRoomA |
|
235 |
rnc <- gets roomsClients |
|
236 |
newMasterId <- liftM (head . filter (/= ci)) . io $ roomClientsIndicesM rnc ri |
|
237 |
newMaster <- io $ client'sM rnc id newMasterId |
|
238 |
let newRoomName = nick newMaster |
|
239 |
mapM_ processAction [ |
|
240 |
ModifyRoom (\r -> r{masterID = newMasterId, name = newRoomName}), |
|
241 |
ModifyClient2 newMasterId (\c -> c{isMaster = True}), |
|
242 |
AnswerClients [sendChan newMaster] ["ROOM_CONTROL_ACCESS", "1"] |
|
243 |
] |
|
244 |
||
245 |
processAction (AddRoom roomName roomPassword) = do |
|
246 |
Just clId <- gets clientIndex |
|
247 |
rnc <- gets roomsClients |
|
248 |
proto <- io $ client'sM rnc clientProto clId |
|
249 |
||
250 |
let rm = newRoom{ |
|
251 |
masterID = clId, |
|
252 |
name = roomName, |
|
253 |
password = roomPassword, |
|
254 |
roomProto = proto |
|
255 |
} |
|
256 |
||
257 |
rId <- io $ addRoom rnc rm |
|
258 |
||
259 |
processAction $ MoveToRoom rId |
|
260 |
||
261 |
chans <- liftM (map sendChan) $! roomClientsS lobbyId |
|
262 |
||
263 |
mapM_ processAction [ |
|
264 |
AnswerClients chans ["ROOM", "ADD", roomName] |
|
265 |
, ModifyClient (\cl -> cl{isMaster = True}) |
|
266 |
] |
|
267 |
||
268 |
||
269 |
processAction RemoveRoom = do |
|
270 |
Just clId <- gets clientIndex |
|
271 |
rnc <- gets roomsClients |
|
272 |
ri <- io $ clientRoomM rnc clId |
|
273 |
roomName <- io $ room'sM rnc name ri |
|
274 |
others <- othersChans |
|
275 |
lobbyChans <- liftM (map sendChan) $! roomClientsS lobbyId |
|
276 |
||
277 |
mapM_ processAction [ |
|
278 |
AnswerClients lobbyChans ["ROOM", "DEL", roomName], |
|
279 |
AnswerClients others ["ROOMABANDONED", roomName] |
|
280 |
] |
|
281 |
||
282 |
io $ removeRoom rnc ri |
|
283 |
||
284 |
||
285 |
processAction (UnreadyRoomClients) = do |
|
286 |
rnc <- gets roomsClients |
|
287 |
ri <- clientRoomA |
|
288 |
roomPlayers <- roomClientsS ri |
|
289 |
roomClIDs <- io $ roomClientsIndicesM rnc ri |
|
290 |
pr <- client's clientProto |
|
291 |
processAction $ AnswerClients (map sendChan roomPlayers) $ notReadyMessage pr (map nick roomPlayers) |
|
292 |
io $ mapM_ (modifyClient rnc (\cl -> cl{isReady = False})) roomClIDs |
|
293 |
processAction $ ModifyRoom (\r -> r{readyPlayers = 0}) |
|
294 |
where |
|
295 |
notReadyMessage p nicks = if p < 38 then "NOT_READY" : nicks else "CLIENT_FLAGS" : "-r" : nicks |
|
296 |
||
297 |
||
298 |
processAction (RemoveTeam teamName) = do |
|
299 |
rnc <- gets roomsClients |
|
300 |
ri <- clientRoomA |
|
5996
2c72fe81dd37
Convert boolean variable + a bunch of fields which make sense only while game is going on into Maybe + structure
unc0rr
parents:
5426
diff
changeset
|
301 |
inGame <- io $ room'sM rnc (isJust . gameInfo) ri |
5184 | 302 |
chans <- othersChans |
303 |
if not $ inGame then |
|
304 |
mapM_ processAction [ |
|
305 |
AnswerClients chans ["REMOVE_TEAM", teamName], |
|
306 |
ModifyRoom (\r -> r{teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r}) |
|
307 |
] |
|
308 |
else |
|
309 |
mapM_ processAction [ |
|
310 |
AnswerClients chans ["EM", rmTeamMsg], |
|
311 |
ModifyRoom (\r -> r{ |
|
312 |
teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r, |
|
5996
2c72fe81dd37
Convert boolean variable + a bunch of fields which make sense only while game is going on into Maybe + structure
unc0rr
parents:
5426
diff
changeset
|
313 |
gameInfo = liftM (\g -> g{ |
2c72fe81dd37
Convert boolean variable + a bunch of fields which make sense only while game is going on into Maybe + structure
unc0rr
parents:
5426
diff
changeset
|
314 |
leftTeams = teamName : leftTeams g, |
2c72fe81dd37
Convert boolean variable + a bunch of fields which make sense only while game is going on into Maybe + structure
unc0rr
parents:
5426
diff
changeset
|
315 |
roundMsgs = roundMsgs g Seq.|> rmTeamMsg |
2c72fe81dd37
Convert boolean variable + a bunch of fields which make sense only while game is going on into Maybe + structure
unc0rr
parents:
5426
diff
changeset
|
316 |
}) $ gameInfo r |
5184 | 317 |
}) |
318 |
] |
|
319 |
where |
|
320 |
rmTeamMsg = toEngineMsg $ 'F' `B.cons` teamName |
|
321 |
||
322 |
||
323 |
processAction (RemoveClientTeams clId) = do |
|
324 |
rnc <- gets roomsClients |
|
325 |
||
326 |
removeTeamActions <- io $ do |
|
327 |
clNick <- client'sM rnc nick clId |
|
328 |
rId <- clientRoomM rnc clId |
|
329 |
roomTeams <- room'sM rnc teams rId |
|
330 |
return . Prelude.map (RemoveTeam . teamname) . Prelude.filter (\t -> teamowner t == clNick) $ roomTeams |
|
331 |
||
332 |
mapM_ processAction removeTeamActions |
|
333 |
||
334 |
||
335 |
||
336 |
processAction CheckRegistered = do |
|
337 |
(Just ci) <- gets clientIndex |
|
338 |
n <- client's nick |
|
339 |
h <- client's host |
|
340 |
p <- client's clientProto |
|
341 |
uid <- client's clUID |
|
342 |
haveSameNick <- liftM (not . null . tail . filter (\c -> nick c == n)) allClientsS |
|
343 |
if haveSameNick then |
|
344 |
if p < 38 then |
|
345 |
mapM_ processAction [ByeClient "Nickname is already in use", removeNick] |
|
346 |
else |
|
347 |
mapM_ processAction [NoticeMessage NickAlreadyInUse, removeNick] |
|
348 |
else |
|
349 |
do |
|
350 |
db <- gets (dbQueries . serverInfo) |
|
351 |
io $ writeChan db $ CheckAccount ci (hashUnique uid) n h |
|
352 |
return () |
|
353 |
where |
|
354 |
removeNick = ModifyClient (\c -> c{nick = ""}) |
|
355 |
||
356 |
||
357 |
processAction ClearAccountsCache = do |
|
358 |
dbq <- gets (dbQueries . serverInfo) |
|
359 |
io $ writeChan dbq ClearCache |
|
360 |
return () |
|
361 |
||
362 |
||
363 |
processAction (ProcessAccountInfo info) = |
|
364 |
case info of |
|
365 |
HasAccount passwd isAdmin -> do |
|
366 |
chan <- client's sendChan |
|
367 |
mapM_ processAction [AnswerClients [chan] ["ASKPASSWORD"], ModifyClient (\c -> c{webPassword = passwd, isAdministrator = isAdmin})] |
|
368 |
Guest -> |
|
369 |
processAction JoinLobby |
|
370 |
Admin -> do |
|
371 |
mapM_ processAction [ModifyClient (\cl -> cl{isAdministrator = True}), JoinLobby] |
|
372 |
chan <- client's sendChan |
|
373 |
processAction $ AnswerClients [chan] ["ADMIN_ACCESS"] |
|
374 |
||
375 |
||
376 |
processAction JoinLobby = do |
|
377 |
chan <- client's sendChan |
|
378 |
clientNick <- client's nick |
|
379 |
(lobbyNicks, clientsChans) <- liftM (unzip . Prelude.map (nick &&& sendChan) . Prelude.filter logonPassed) $! allClientsS |
|
380 |
mapM_ processAction $ |
|
381 |
AnswerClients clientsChans ["LOBBY:JOINED", clientNick] |
|
382 |
: AnswerClients [chan] ("LOBBY:JOINED" : clientNick : lobbyNicks) |
|
383 |
: [ModifyClient (\cl -> cl{logonPassed = True}), SendServerMessage] |
|
384 |
||
385 |
||
386 |
processAction (KickClient kickId) = do |
|
387 |
modify (\s -> s{clientIndex = Just kickId}) |
|
5214 | 388 |
clHost <- client's host |
389 |
currentTime <- io getCurrentTime |
|
390 |
mapM_ processAction [ |
|
391 |
AddIP2Bans clHost "60 seconds cooldown after kick" (addUTCTime 60 currentTime), |
|
392 |
ByeClient "Kicked" |
|
393 |
] |
|
5184 | 394 |
|
395 |
||
396 |
processAction (BanClient seconds reason banId) = do |
|
397 |
modify (\s -> s{clientIndex = Just banId}) |
|
398 |
clHost <- client's host |
|
399 |
currentTime <- io getCurrentTime |
|
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
400 |
let msg = B.concat ["Ban for ", B.pack . show $ seconds, " (", reason, ")"] |
5184 | 401 |
mapM_ processAction [ |
402 |
AddIP2Bans clHost msg (addUTCTime seconds currentTime) |
|
403 |
, KickClient banId |
|
404 |
] |
|
405 |
||
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
406 |
processAction (BanIP ip seconds reason) = do |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
407 |
currentTime <- io getCurrentTime |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
408 |
let msg = B.concat ["Ban for ", B.pack . show $ seconds, " (", reason, ")"] |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
409 |
processAction $ |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
410 |
AddIP2Bans ip msg (addUTCTime seconds currentTime) |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
411 |
|
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
412 |
processAction BanList = do |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
413 |
ch <- client's sendChan |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
414 |
bans <- gets (bans . serverInfo) |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
415 |
processAction $ |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
416 |
AnswerClients [ch] ["BANLIST", B.pack $ show bans] |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
417 |
|
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
418 |
|
5184 | 419 |
|
420 |
processAction (KickRoomClient kickId) = do |
|
421 |
modify (\s -> s{clientIndex = Just kickId}) |
|
422 |
ch <- client's sendChan |
|
423 |
mapM_ processAction [AnswerClients [ch] ["KICKED"], MoveToLobby "kicked"] |
|
424 |
||
425 |
||
426 |
processAction (AddClient cl) = do |
|
427 |
rnc <- gets roomsClients |
|
428 |
si <- gets serverInfo |
|
429 |
newClId <- io $ do |
|
430 |
ci <- addClient rnc cl |
|
431 |
_ <- Exception.mask (forkIO . clientRecvLoop (clientSocket cl) (coreChan si) (sendChan cl) ci) |
|
432 |
||
433 |
infoM "Clients" (show ci ++ ": New client. Time: " ++ show (connectTime cl)) |
|
434 |
||
435 |
return ci |
|
436 |
||
437 |
modify (\s -> s{clientIndex = Just newClId}) |
|
438 |
mapM_ processAction |
|
439 |
[ |
|
440 |
AnswerClients [sendChan cl] ["CONNECTED", "Hedgewars server http://www.hedgewars.org/", serverVersion] |
|
441 |
, CheckBanned |
|
442 |
, AddIP2Bans (host cl) "Reconnected too fast" (addUTCTime 10 $ connectTime cl) |
|
443 |
] |
|
444 |
||
445 |
||
446 |
processAction (AddNick2Bans n reason expiring) = do |
|
447 |
processAction $ ModifyServerInfo (\s -> s{bans = BanByNick n reason expiring : bans s}) |
|
448 |
||
449 |
processAction (AddIP2Bans ip reason expiring) = do |
|
450 |
(Just ci) <- gets clientIndex |
|
451 |
rc <- gets removedClients |
|
452 |
when (not $ ci `Set.member` rc) |
|
453 |
$ processAction $ ModifyServerInfo (\s -> s{bans = BanByIP ip reason expiring : bans s}) |
|
454 |
||
455 |
processAction CheckBanned = do |
|
456 |
clTime <- client's connectTime |
|
457 |
clNick <- client's nick |
|
458 |
clHost <- client's host |
|
459 |
si <- gets serverInfo |
|
460 |
let validBans = filter (checkNotExpired clTime) $ bans si |
|
461 |
let ban = L.find (checkBan clHost clNick) $ validBans |
|
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
462 |
mapM_ processAction $ |
5184 | 463 |
ModifyServerInfo (\s -> s{bans = validBans}) |
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
464 |
: [ByeClient (getBanReason $ fromJust ban) | isJust ban] |
5184 | 465 |
where |
466 |
checkNotExpired testTime (BanByIP _ _ time) = testTime `diffUTCTime` time <= 0 |
|
467 |
checkNotExpired testTime (BanByNick _ _ time) = testTime `diffUTCTime` time <= 0 |
|
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
468 |
checkBan ip _ (BanByIP bip _ _) = bip `B.isPrefixOf` ip |
5184 | 469 |
checkBan _ n (BanByNick bn _ _) = bn == n |
470 |
getBanReason (BanByIP _ msg _) = msg |
|
471 |
getBanReason (BanByNick _ msg _) = msg |
|
472 |
||
473 |
processAction PingAll = do |
|
474 |
rnc <- gets roomsClients |
|
475 |
io (allClientsM rnc) >>= mapM_ (kickTimeouted rnc) |
|
476 |
cis <- io $ allClientsM rnc |
|
477 |
chans <- io $ mapM (client'sM rnc sendChan) cis |
|
478 |
io $ mapM_ (modifyClient rnc (\cl -> cl{pingsQueue = pingsQueue cl + 1})) cis |
|
479 |
processAction $ AnswerClients chans ["PING"] |
|
480 |
where |
|
481 |
kickTimeouted rnc ci = do |
|
482 |
pq <- io $ client'sM rnc pingsQueue ci |
|
483 |
when (pq > 0) $ |
|
484 |
withStateT (\as -> as{clientIndex = Just ci}) $ |
|
485 |
processAction (ByeClient "Ping timeout") |
|
486 |
||
487 |
||
488 |
processAction StatsAction = do |
|
5211 | 489 |
si <- gets serverInfo |
490 |
when (not $ shutdownPending si) $ do |
|
5212
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
491 |
rnc <- gets roomsClients |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
492 |
(roomsNum, clientsNum) <- io $ withRoomsAndClients rnc st |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
493 |
io $ writeChan (dbQueries si) $ SendStats clientsNum (roomsNum - 1) |
5184 | 494 |
where |
495 |
st irnc = (length $ allRooms irnc, length $ allClients irnc) |
|
496 |
||
5212
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
497 |
processAction RestartServer = do |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
498 |
sp <- gets (shutdownPending . serverInfo) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
499 |
when (not sp) $ do |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
500 |
sock <- gets (fromJust . serverSocket . serverInfo) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
501 |
args <- gets (runArgs . serverInfo) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
502 |
io $ do |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
503 |
noticeM "Core" "Closing listening socket" |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
504 |
sClose sock |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
505 |
noticeM "Core" "Spawning new server" |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
506 |
_ <- createProcess (proc "./hedgewars-server" args) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
507 |
return () |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
508 |
processAction $ ModifyServerInfo (\s -> s{shutdownPending = True}) |
5184 | 509 |
|
510 |
processAction SaveReplay = do |
|
511 |
ri <- clientRoomA |
|
512 |
rnc <- gets roomsClients |
|
513 |
io $ do |
|
514 |
r <- room'sM rnc id ri |
|
515 |
saveReplay r |