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