author | koda |
Sun, 29 Jan 2012 02:48:03 +0100 | |
changeset 6612 | 8fd6bb239a1e |
parent 6541 | 08ed346ed341 |
child 6733 | 5abbc345a82f |
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 |
|
239 |
let newRoomName = nick newMaster |
|
240 |
mapM_ processAction [ |
|
241 |
ModifyRoom (\r -> r{masterID = newMasterId, name = newRoomName}), |
|
242 |
ModifyClient2 newMasterId (\c -> c{isMaster = True}), |
|
243 |
AnswerClients [sendChan newMaster] ["ROOM_CONTROL_ACCESS", "1"] |
|
244 |
] |
|
245 |
||
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
|
246 |
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
|
247 |
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
|
248 |
chans <- liftM (map sendChan) $! sameProtoClientsS proto |
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 |
processAction $ AnswerClients chans ("ROOM" : "ADD" : roomInfo (nick newMaster) newRoom) |
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
|
250 |
|
5184 | 251 |
processAction (AddRoom roomName roomPassword) = do |
252 |
Just clId <- gets clientIndex |
|
253 |
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
|
254 |
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
|
255 |
n <- client's nick |
5184 | 256 |
|
257 |
let rm = newRoom{ |
|
258 |
masterID = clId, |
|
259 |
name = roomName, |
|
260 |
password = roomPassword, |
|
261 |
roomProto = proto |
|
262 |
} |
|
263 |
||
264 |
rId <- io $ addRoom rnc rm |
|
265 |
||
266 |
processAction $ MoveToRoom rId |
|
267 |
||
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
|
268 |
chans <- liftM (map sendChan) $! sameProtoClientsS proto |
5184 | 269 |
|
270 |
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
|
271 |
AnswerClients chans ("ROOM" : "ADD" : roomInfo n rm) |
5184 | 272 |
, ModifyClient (\cl -> cl{isMaster = True}) |
273 |
] |
|
274 |
||
275 |
||
276 |
processAction RemoveRoom = do |
|
277 |
Just clId <- gets clientIndex |
|
278 |
rnc <- gets roomsClients |
|
279 |
ri <- io $ clientRoomM rnc clId |
|
280 |
roomName <- io $ room'sM rnc name ri |
|
281 |
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
|
282 |
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
|
283 |
chans <- liftM (map sendChan) $! sameProtoClientsS proto |
5184 | 284 |
|
285 |
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
|
286 |
AnswerClients chans ["ROOM", "DEL", roomName], |
5184 | 287 |
AnswerClients others ["ROOMABANDONED", roomName] |
288 |
] |
|
289 |
||
290 |
io $ removeRoom rnc ri |
|
291 |
||
292 |
||
293 |
processAction (UnreadyRoomClients) = do |
|
294 |
rnc <- gets roomsClients |
|
295 |
ri <- clientRoomA |
|
296 |
roomPlayers <- roomClientsS ri |
|
297 |
roomClIDs <- io $ roomClientsIndicesM rnc ri |
|
298 |
pr <- client's clientProto |
|
299 |
processAction $ AnswerClients (map sendChan roomPlayers) $ notReadyMessage pr (map nick roomPlayers) |
|
300 |
io $ mapM_ (modifyClient rnc (\cl -> cl{isReady = False})) roomClIDs |
|
301 |
processAction $ ModifyRoom (\r -> r{readyPlayers = 0}) |
|
302 |
where |
|
303 |
notReadyMessage p nicks = if p < 38 then "NOT_READY" : nicks else "CLIENT_FLAGS" : "-r" : nicks |
|
304 |
||
305 |
||
306 |
processAction (RemoveTeam teamName) = do |
|
307 |
rnc <- gets roomsClients |
|
308 |
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
|
309 |
inGame <- io $ room'sM rnc (isJust . gameInfo) ri |
5184 | 310 |
chans <- othersChans |
311 |
if not $ inGame then |
|
312 |
mapM_ processAction [ |
|
313 |
AnswerClients chans ["REMOVE_TEAM", teamName], |
|
314 |
ModifyRoom (\r -> r{teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r}) |
|
315 |
] |
|
316 |
else |
|
317 |
mapM_ processAction [ |
|
318 |
AnswerClients chans ["EM", rmTeamMsg], |
|
319 |
ModifyRoom (\r -> r{ |
|
320 |
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
|
321 |
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
|
322 |
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
|
323 |
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
|
324 |
}) $ gameInfo r |
5184 | 325 |
}) |
326 |
] |
|
327 |
where |
|
328 |
rmTeamMsg = toEngineMsg $ 'F' `B.cons` teamName |
|
329 |
||
330 |
||
331 |
processAction (RemoveClientTeams clId) = do |
|
332 |
rnc <- gets roomsClients |
|
333 |
||
334 |
removeTeamActions <- io $ do |
|
335 |
clNick <- client'sM rnc nick clId |
|
336 |
rId <- clientRoomM rnc clId |
|
337 |
roomTeams <- room'sM rnc teams rId |
|
338 |
return . Prelude.map (RemoveTeam . teamname) . Prelude.filter (\t -> teamowner t == clNick) $ roomTeams |
|
339 |
||
340 |
mapM_ processAction removeTeamActions |
|
341 |
||
342 |
||
343 |
||
344 |
processAction CheckRegistered = do |
|
345 |
(Just ci) <- gets clientIndex |
|
346 |
n <- client's nick |
|
347 |
h <- client's host |
|
348 |
p <- client's clientProto |
|
349 |
uid <- client's clUID |
|
6191 | 350 |
haveSameNick <- liftM (not . null . tail . filter (\c -> caseInsensitiveCompare (nick c) n)) allClientsS |
5184 | 351 |
if haveSameNick then |
352 |
if p < 38 then |
|
353 |
mapM_ processAction [ByeClient "Nickname is already in use", removeNick] |
|
354 |
else |
|
355 |
mapM_ processAction [NoticeMessage NickAlreadyInUse, removeNick] |
|
356 |
else |
|
357 |
do |
|
358 |
db <- gets (dbQueries . serverInfo) |
|
359 |
io $ writeChan db $ CheckAccount ci (hashUnique uid) n h |
|
360 |
return () |
|
361 |
where |
|
362 |
removeNick = ModifyClient (\c -> c{nick = ""}) |
|
363 |
||
364 |
||
365 |
processAction ClearAccountsCache = do |
|
366 |
dbq <- gets (dbQueries . serverInfo) |
|
367 |
io $ writeChan dbq ClearCache |
|
368 |
return () |
|
369 |
||
370 |
||
371 |
processAction (ProcessAccountInfo info) = |
|
372 |
case info of |
|
373 |
HasAccount passwd isAdmin -> do |
|
374 |
chan <- client's sendChan |
|
375 |
mapM_ processAction [AnswerClients [chan] ["ASKPASSWORD"], ModifyClient (\c -> c{webPassword = passwd, isAdministrator = isAdmin})] |
|
376 |
Guest -> |
|
377 |
processAction JoinLobby |
|
378 |
Admin -> do |
|
379 |
mapM_ processAction [ModifyClient (\cl -> cl{isAdministrator = True}), JoinLobby] |
|
380 |
chan <- client's sendChan |
|
381 |
processAction $ AnswerClients [chan] ["ADMIN_ACCESS"] |
|
382 |
||
383 |
||
384 |
processAction JoinLobby = do |
|
385 |
chan <- client's sendChan |
|
386 |
clientNick <- client's nick |
|
387 |
(lobbyNicks, clientsChans) <- liftM (unzip . Prelude.map (nick &&& sendChan) . Prelude.filter logonPassed) $! allClientsS |
|
388 |
mapM_ processAction $ |
|
389 |
AnswerClients clientsChans ["LOBBY:JOINED", clientNick] |
|
390 |
: AnswerClients [chan] ("LOBBY:JOINED" : clientNick : lobbyNicks) |
|
391 |
: [ModifyClient (\cl -> cl{logonPassed = True}), SendServerMessage] |
|
392 |
||
393 |
||
394 |
processAction (KickClient kickId) = do |
|
395 |
modify (\s -> s{clientIndex = Just kickId}) |
|
5214 | 396 |
clHost <- client's host |
397 |
currentTime <- io getCurrentTime |
|
398 |
mapM_ processAction [ |
|
399 |
AddIP2Bans clHost "60 seconds cooldown after kick" (addUTCTime 60 currentTime), |
|
400 |
ByeClient "Kicked" |
|
401 |
] |
|
5184 | 402 |
|
403 |
||
404 |
processAction (BanClient seconds reason banId) = do |
|
405 |
modify (\s -> s{clientIndex = Just banId}) |
|
406 |
clHost <- client's host |
|
407 |
currentTime <- io getCurrentTime |
|
5426
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, ")"] |
5184 | 409 |
mapM_ processAction [ |
410 |
AddIP2Bans clHost msg (addUTCTime seconds currentTime) |
|
411 |
, KickClient banId |
|
412 |
] |
|
413 |
||
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
414 |
processAction (BanIP ip seconds reason) = do |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
415 |
currentTime <- io getCurrentTime |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
416 |
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
|
417 |
processAction $ |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
418 |
AddIP2Bans ip msg (addUTCTime seconds currentTime) |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
419 |
|
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
420 |
processAction BanList = do |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
421 |
ch <- client's sendChan |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
422 |
bans <- gets (bans . serverInfo) |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
423 |
processAction $ |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
424 |
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
|
425 |
|
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
426 |
|
5184 | 427 |
|
428 |
processAction (KickRoomClient kickId) = do |
|
429 |
modify (\s -> s{clientIndex = Just kickId}) |
|
430 |
ch <- client's sendChan |
|
431 |
mapM_ processAction [AnswerClients [ch] ["KICKED"], MoveToLobby "kicked"] |
|
432 |
||
433 |
||
434 |
processAction (AddClient cl) = do |
|
435 |
rnc <- gets roomsClients |
|
436 |
si <- gets serverInfo |
|
437 |
newClId <- io $ do |
|
438 |
ci <- addClient rnc cl |
|
439 |
_ <- Exception.mask (forkIO . clientRecvLoop (clientSocket cl) (coreChan si) (sendChan cl) ci) |
|
440 |
||
441 |
infoM "Clients" (show ci ++ ": New client. Time: " ++ show (connectTime cl)) |
|
442 |
||
443 |
return ci |
|
444 |
||
445 |
modify (\s -> s{clientIndex = Just newClId}) |
|
446 |
mapM_ processAction |
|
447 |
[ |
|
448 |
AnswerClients [sendChan cl] ["CONNECTED", "Hedgewars server http://www.hedgewars.org/", serverVersion] |
|
449 |
, CheckBanned |
|
450 |
, AddIP2Bans (host cl) "Reconnected too fast" (addUTCTime 10 $ connectTime cl) |
|
451 |
] |
|
452 |
||
453 |
||
454 |
processAction (AddNick2Bans n reason expiring) = do |
|
455 |
processAction $ ModifyServerInfo (\s -> s{bans = BanByNick n reason expiring : bans s}) |
|
456 |
||
457 |
processAction (AddIP2Bans ip reason expiring) = do |
|
458 |
(Just ci) <- gets clientIndex |
|
459 |
rc <- gets removedClients |
|
460 |
when (not $ ci `Set.member` rc) |
|
461 |
$ processAction $ ModifyServerInfo (\s -> s{bans = BanByIP ip reason expiring : bans s}) |
|
462 |
||
463 |
processAction CheckBanned = do |
|
464 |
clTime <- client's connectTime |
|
465 |
clNick <- client's nick |
|
466 |
clHost <- client's host |
|
467 |
si <- gets serverInfo |
|
468 |
let validBans = filter (checkNotExpired clTime) $ bans si |
|
469 |
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
|
470 |
mapM_ processAction $ |
5184 | 471 |
ModifyServerInfo (\s -> s{bans = validBans}) |
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
472 |
: [ByeClient (getBanReason $ fromJust ban) | isJust ban] |
5184 | 473 |
where |
474 |
checkNotExpired testTime (BanByIP _ _ time) = testTime `diffUTCTime` time <= 0 |
|
475 |
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
|
476 |
checkBan ip _ (BanByIP bip _ _) = bip `B.isPrefixOf` ip |
5184 | 477 |
checkBan _ n (BanByNick bn _ _) = bn == n |
478 |
getBanReason (BanByIP _ msg _) = msg |
|
479 |
getBanReason (BanByNick _ msg _) = msg |
|
480 |
||
481 |
processAction PingAll = do |
|
482 |
rnc <- gets roomsClients |
|
483 |
io (allClientsM rnc) >>= mapM_ (kickTimeouted rnc) |
|
484 |
cis <- io $ allClientsM rnc |
|
485 |
chans <- io $ mapM (client'sM rnc sendChan) cis |
|
486 |
io $ mapM_ (modifyClient rnc (\cl -> cl{pingsQueue = pingsQueue cl + 1})) cis |
|
487 |
processAction $ AnswerClients chans ["PING"] |
|
488 |
where |
|
489 |
kickTimeouted rnc ci = do |
|
490 |
pq <- io $ client'sM rnc pingsQueue ci |
|
491 |
when (pq > 0) $ |
|
492 |
withStateT (\as -> as{clientIndex = Just ci}) $ |
|
493 |
processAction (ByeClient "Ping timeout") |
|
494 |
||
495 |
||
496 |
processAction StatsAction = do |
|
5211 | 497 |
si <- gets serverInfo |
498 |
when (not $ shutdownPending si) $ do |
|
5212
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
499 |
rnc <- gets roomsClients |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
500 |
(roomsNum, clientsNum) <- io $ withRoomsAndClients rnc st |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
501 |
io $ writeChan (dbQueries si) $ SendStats clientsNum (roomsNum - 1) |
5184 | 502 |
where |
503 |
st irnc = (length $ allRooms irnc, length $ allClients irnc) |
|
504 |
||
5212
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
505 |
processAction RestartServer = do |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
506 |
sp <- gets (shutdownPending . serverInfo) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
507 |
when (not sp) $ do |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
508 |
sock <- gets (fromJust . serverSocket . serverInfo) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
509 |
args <- gets (runArgs . serverInfo) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
510 |
io $ do |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
511 |
noticeM "Core" "Closing listening socket" |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
512 |
sClose sock |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
513 |
noticeM "Core" "Spawning new server" |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
514 |
_ <- createProcess (proc "./hedgewars-server" args) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
515 |
return () |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
516 |
processAction $ ModifyServerInfo (\s -> s{shutdownPending = True}) |
5184 | 517 |
|
6012 | 518 |
#if defined(OFFICIAL_SERVER) |
5184 | 519 |
processAction SaveReplay = do |
520 |
ri <- clientRoomA |
|
521 |
rnc <- gets roomsClients |
|
522 |
io $ do |
|
523 |
r <- room'sM rnc id ri |
|
524 |
saveReplay r |
|
6012 | 525 |
#else |
526 |
processAction SaveReplay = return () |
|
527 |
#endif |