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