author | nemo |
Wed, 10 Aug 2011 17:20:21 -0400 | |
changeset 5539 | 7bb3d679e648 |
parent 5426 | 109e9b5761c2 |
child 5996 | 2c72fe81dd37 |
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 |
|
20 |
import OfficialServer.GameReplayStore |
|
5209
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
21 |
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
|
22 |
import Network.Socket |
5184 | 23 |
----------------------------- |
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 |
|
209 |
(gameProgress, playersNum) <- io $ room'sM rnc (gameinprogress &&& playersIn) ri |
|
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 |
|
301 |
inGame <- io $ room'sM rnc gameinprogress ri |
|
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, |
|
313 |
leftTeams = teamName : leftTeams r, |
|
314 |
roundMsgs = roundMsgs r Seq.|> rmTeamMsg |
|
315 |
}) |
|
316 |
] |
|
317 |
where |
|
318 |
rmTeamMsg = toEngineMsg $ 'F' `B.cons` teamName |
|
319 |
||
320 |
||
321 |
processAction (RemoveClientTeams clId) = do |
|
322 |
rnc <- gets roomsClients |
|
323 |
||
324 |
removeTeamActions <- io $ do |
|
325 |
clNick <- client'sM rnc nick clId |
|
326 |
rId <- clientRoomM rnc clId |
|
327 |
roomTeams <- room'sM rnc teams rId |
|
328 |
return . Prelude.map (RemoveTeam . teamname) . Prelude.filter (\t -> teamowner t == clNick) $ roomTeams |
|
329 |
||
330 |
mapM_ processAction removeTeamActions |
|
331 |
||
332 |
||
333 |
||
334 |
processAction CheckRegistered = do |
|
335 |
(Just ci) <- gets clientIndex |
|
336 |
n <- client's nick |
|
337 |
h <- client's host |
|
338 |
p <- client's clientProto |
|
339 |
uid <- client's clUID |
|
340 |
haveSameNick <- liftM (not . null . tail . filter (\c -> nick c == n)) allClientsS |
|
341 |
if haveSameNick then |
|
342 |
if p < 38 then |
|
343 |
mapM_ processAction [ByeClient "Nickname is already in use", removeNick] |
|
344 |
else |
|
345 |
mapM_ processAction [NoticeMessage NickAlreadyInUse, removeNick] |
|
346 |
else |
|
347 |
do |
|
348 |
db <- gets (dbQueries . serverInfo) |
|
349 |
io $ writeChan db $ CheckAccount ci (hashUnique uid) n h |
|
350 |
return () |
|
351 |
where |
|
352 |
removeNick = ModifyClient (\c -> c{nick = ""}) |
|
353 |
||
354 |
||
355 |
processAction ClearAccountsCache = do |
|
356 |
dbq <- gets (dbQueries . serverInfo) |
|
357 |
io $ writeChan dbq ClearCache |
|
358 |
return () |
|
359 |
||
360 |
||
361 |
processAction (ProcessAccountInfo info) = |
|
362 |
case info of |
|
363 |
HasAccount passwd isAdmin -> do |
|
364 |
chan <- client's sendChan |
|
365 |
mapM_ processAction [AnswerClients [chan] ["ASKPASSWORD"], ModifyClient (\c -> c{webPassword = passwd, isAdministrator = isAdmin})] |
|
366 |
Guest -> |
|
367 |
processAction JoinLobby |
|
368 |
Admin -> do |
|
369 |
mapM_ processAction [ModifyClient (\cl -> cl{isAdministrator = True}), JoinLobby] |
|
370 |
chan <- client's sendChan |
|
371 |
processAction $ AnswerClients [chan] ["ADMIN_ACCESS"] |
|
372 |
||
373 |
||
374 |
processAction JoinLobby = do |
|
375 |
chan <- client's sendChan |
|
376 |
clientNick <- client's nick |
|
377 |
(lobbyNicks, clientsChans) <- liftM (unzip . Prelude.map (nick &&& sendChan) . Prelude.filter logonPassed) $! allClientsS |
|
378 |
mapM_ processAction $ |
|
379 |
AnswerClients clientsChans ["LOBBY:JOINED", clientNick] |
|
380 |
: AnswerClients [chan] ("LOBBY:JOINED" : clientNick : lobbyNicks) |
|
381 |
: [ModifyClient (\cl -> cl{logonPassed = True}), SendServerMessage] |
|
382 |
||
383 |
||
384 |
processAction (KickClient kickId) = do |
|
385 |
modify (\s -> s{clientIndex = Just kickId}) |
|
5214 | 386 |
clHost <- client's host |
387 |
currentTime <- io getCurrentTime |
|
388 |
mapM_ processAction [ |
|
389 |
AddIP2Bans clHost "60 seconds cooldown after kick" (addUTCTime 60 currentTime), |
|
390 |
ByeClient "Kicked" |
|
391 |
] |
|
5184 | 392 |
|
393 |
||
394 |
processAction (BanClient seconds reason banId) = do |
|
395 |
modify (\s -> s{clientIndex = Just banId}) |
|
396 |
clHost <- client's host |
|
397 |
currentTime <- io getCurrentTime |
|
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
398 |
let msg = B.concat ["Ban for ", B.pack . show $ seconds, " (", reason, ")"] |
5184 | 399 |
mapM_ processAction [ |
400 |
AddIP2Bans clHost msg (addUTCTime seconds currentTime) |
|
401 |
, KickClient banId |
|
402 |
] |
|
403 |
||
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
404 |
processAction (BanIP ip seconds reason) = do |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
405 |
currentTime <- io getCurrentTime |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
406 |
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
|
407 |
processAction $ |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
408 |
AddIP2Bans ip msg (addUTCTime seconds currentTime) |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
409 |
|
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
410 |
processAction BanList = do |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
411 |
ch <- client's sendChan |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
412 |
bans <- gets (bans . serverInfo) |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
413 |
processAction $ |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
414 |
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
|
415 |
|
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
416 |
|
5184 | 417 |
|
418 |
processAction (KickRoomClient kickId) = do |
|
419 |
modify (\s -> s{clientIndex = Just kickId}) |
|
420 |
ch <- client's sendChan |
|
421 |
mapM_ processAction [AnswerClients [ch] ["KICKED"], MoveToLobby "kicked"] |
|
422 |
||
423 |
||
424 |
processAction (AddClient cl) = do |
|
425 |
rnc <- gets roomsClients |
|
426 |
si <- gets serverInfo |
|
427 |
newClId <- io $ do |
|
428 |
ci <- addClient rnc cl |
|
429 |
_ <- Exception.mask (forkIO . clientRecvLoop (clientSocket cl) (coreChan si) (sendChan cl) ci) |
|
430 |
||
431 |
infoM "Clients" (show ci ++ ": New client. Time: " ++ show (connectTime cl)) |
|
432 |
||
433 |
return ci |
|
434 |
||
435 |
modify (\s -> s{clientIndex = Just newClId}) |
|
436 |
mapM_ processAction |
|
437 |
[ |
|
438 |
AnswerClients [sendChan cl] ["CONNECTED", "Hedgewars server http://www.hedgewars.org/", serverVersion] |
|
439 |
, CheckBanned |
|
440 |
, AddIP2Bans (host cl) "Reconnected too fast" (addUTCTime 10 $ connectTime cl) |
|
441 |
] |
|
442 |
||
443 |
||
444 |
processAction (AddNick2Bans n reason expiring) = do |
|
445 |
processAction $ ModifyServerInfo (\s -> s{bans = BanByNick n reason expiring : bans s}) |
|
446 |
||
447 |
processAction (AddIP2Bans ip reason expiring) = do |
|
448 |
(Just ci) <- gets clientIndex |
|
449 |
rc <- gets removedClients |
|
450 |
when (not $ ci `Set.member` rc) |
|
451 |
$ processAction $ ModifyServerInfo (\s -> s{bans = BanByIP ip reason expiring : bans s}) |
|
452 |
||
453 |
processAction CheckBanned = do |
|
454 |
clTime <- client's connectTime |
|
455 |
clNick <- client's nick |
|
456 |
clHost <- client's host |
|
457 |
si <- gets serverInfo |
|
458 |
let validBans = filter (checkNotExpired clTime) $ bans si |
|
459 |
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
|
460 |
mapM_ processAction $ |
5184 | 461 |
ModifyServerInfo (\s -> s{bans = validBans}) |
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
462 |
: [ByeClient (getBanReason $ fromJust ban) | isJust ban] |
5184 | 463 |
where |
464 |
checkNotExpired testTime (BanByIP _ _ time) = testTime `diffUTCTime` time <= 0 |
|
465 |
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
|
466 |
checkBan ip _ (BanByIP bip _ _) = bip `B.isPrefixOf` ip |
5184 | 467 |
checkBan _ n (BanByNick bn _ _) = bn == n |
468 |
getBanReason (BanByIP _ msg _) = msg |
|
469 |
getBanReason (BanByNick _ msg _) = msg |
|
470 |
||
471 |
processAction PingAll = do |
|
472 |
rnc <- gets roomsClients |
|
473 |
io (allClientsM rnc) >>= mapM_ (kickTimeouted rnc) |
|
474 |
cis <- io $ allClientsM rnc |
|
475 |
chans <- io $ mapM (client'sM rnc sendChan) cis |
|
476 |
io $ mapM_ (modifyClient rnc (\cl -> cl{pingsQueue = pingsQueue cl + 1})) cis |
|
477 |
processAction $ AnswerClients chans ["PING"] |
|
478 |
where |
|
479 |
kickTimeouted rnc ci = do |
|
480 |
pq <- io $ client'sM rnc pingsQueue ci |
|
481 |
when (pq > 0) $ |
|
482 |
withStateT (\as -> as{clientIndex = Just ci}) $ |
|
483 |
processAction (ByeClient "Ping timeout") |
|
484 |
||
485 |
||
486 |
processAction StatsAction = do |
|
5211 | 487 |
si <- gets serverInfo |
488 |
when (not $ shutdownPending si) $ do |
|
5212
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
489 |
rnc <- gets roomsClients |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
490 |
(roomsNum, clientsNum) <- io $ withRoomsAndClients rnc st |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
491 |
io $ writeChan (dbQueries si) $ SendStats clientsNum (roomsNum - 1) |
5184 | 492 |
where |
493 |
st irnc = (length $ allRooms irnc, length $ allClients irnc) |
|
494 |
||
5212
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
495 |
processAction RestartServer = do |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
496 |
sp <- gets (shutdownPending . serverInfo) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
497 |
when (not sp) $ do |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
498 |
sock <- gets (fromJust . serverSocket . serverInfo) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
499 |
args <- gets (runArgs . serverInfo) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
500 |
io $ do |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
501 |
noticeM "Core" "Closing listening socket" |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
502 |
sClose sock |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
503 |
noticeM "Core" "Spawning new server" |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
504 |
_ <- createProcess (proc "./hedgewars-server" args) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
505 |
return () |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
506 |
processAction $ ModifyServerInfo (\s -> s{shutdownPending = True}) |
5184 | 507 |
|
508 |
processAction SaveReplay = do |
|
509 |
ri <- clientRoomA |
|
510 |
rnc <- gets roomsClients |
|
511 |
io $ do |
|
512 |
r <- room'sM rnc id ri |
|
513 |
saveReplay r |