author | unc0rr |
Sat, 16 Feb 2013 23:28:16 +0400 | |
changeset 8507 | f4475782cf45 |
parent 8482 | 5656a73fe3c3 |
child 8509 | eda9f2106d8d |
permissions | -rw-r--r-- |
8403
fbc6e7602e05
- Allow server admins to use DELEGATE even when not room owner
unc0rr
parents:
8401
diff
changeset
|
1 |
{-# LANGUAGE CPP, OverloadedStrings, ScopedTypeVariables #-} |
7766 | 2 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
5184 | 3 |
module Actions where |
4 |
||
5 |
import Control.Concurrent |
|
6 |
import qualified Data.Set as Set |
|
8403
fbc6e7602e05
- Allow server admins to use DELEGATE even when not room owner
unc0rr
parents:
8401
diff
changeset
|
7 |
import qualified Data.Map as Map |
5184 | 8 |
import qualified Data.List as L |
9 |
import qualified Control.Exception as Exception |
|
10 |
import System.Log.Logger |
|
11 |
import Control.Monad |
|
12 |
import Data.Time |
|
13 |
import Data.Maybe |
|
14 |
import Control.Monad.Reader |
|
15 |
import Control.Monad.State.Strict |
|
16 |
import qualified Data.ByteString.Char8 as B |
|
17 |
import Control.DeepSeq |
|
18 |
import Data.Unique |
|
19 |
import Control.Arrow |
|
20 |
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
|
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 |
----------------------------- |
7766 | 24 |
#if defined(OFFICIAL_SERVER) |
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
|
25 |
import OfficialServer.GameReplayStore |
7766 | 26 |
#endif |
5184 | 27 |
import CoreTypes |
28 |
import Utils |
|
29 |
import ClientIO |
|
30 |
import ServerState |
|
31 |
import Consts |
|
32 |
import ConfigFile |
|
6068 | 33 |
import EngineInteraction |
5184 | 34 |
|
35 |
data Action = |
|
36 |
AnswerClients ![ClientChan] ![B.ByteString] |
|
37 |
| SendServerMessage |
|
38 |
| SendServerVars |
|
39 |
| MoveToRoom RoomIndex |
|
40 |
| MoveToLobby B.ByteString |
|
41 |
| RemoveTeam B.ByteString |
|
6753
e95b1f62d0de
Don't remove client's teams from teams list on "ROUNDFINISHED 0", just send team removal message to others.
unc0rr
parents:
6733
diff
changeset
|
42 |
| SendTeamRemovalMessage B.ByteString |
5184 | 43 |
| RemoveRoom |
6758
26bf919aeb57
Oh, should also check for game finish when player quits without ROUNDFINISHED message: small refactoring, not tested at all
unc0rr
parents:
6756
diff
changeset
|
44 |
| FinishGame |
5184 | 45 |
| UnreadyRoomClients |
46 |
| JoinLobby |
|
47 |
| ProtocolError B.ByteString |
|
48 |
| Warning B.ByteString |
|
49 |
| NoticeMessage Notice |
|
50 |
| ByeClient B.ByteString |
|
51 |
| KickClient ClientIndex |
|
52 |
| KickRoomClient ClientIndex |
|
53 |
| BanClient NominalDiffTime B.ByteString ClientIndex |
|
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
54 |
| BanIP B.ByteString NominalDiffTime B.ByteString |
8154 | 55 |
| BanNick B.ByteString NominalDiffTime B.ByteString |
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
56 |
| BanList |
7748 | 57 |
| Unban B.ByteString |
8247 | 58 |
| ChangeMaster (Maybe ClientIndex) |
8438
64ac58abd02a
Don't resend "team quit" message when client closes engine, then quits room:
unc0rr
parents:
8422
diff
changeset
|
59 |
| RemoveClientTeams |
5184 | 60 |
| ModifyClient (ClientInfo -> ClientInfo) |
61 |
| ModifyClient2 ClientIndex (ClientInfo -> ClientInfo) |
|
7757
c20e6c80e249
Don't accept ROUNDFINISHED message twice. Fixes game hangs when half of teams quit game.
unc0rr
parents:
7748
diff
changeset
|
62 |
| ModifyRoomClients (ClientInfo -> ClientInfo) |
5184 | 63 |
| ModifyRoom (RoomInfo -> RoomInfo) |
64 |
| ModifyServerInfo (ServerInfo -> ServerInfo) |
|
65 |
| AddRoom B.ByteString B.ByteString |
|
7921
6b074de32bea
Send ROOM UPD message when team is added/deleted from room, and when game starts or finishes
unc0rr
parents:
7898
diff
changeset
|
66 |
| SendUpdateOnThisRoom |
5184 | 67 |
| CheckRegistered |
68 |
| ClearAccountsCache |
|
69 |
| ProcessAccountInfo AccountInfo |
|
70 |
| AddClient ClientInfo |
|
71 |
| DeleteClient ClientIndex |
|
72 |
| PingAll |
|
73 |
| StatsAction |
|
5209
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
74 |
| RestartServer |
5184 | 75 |
| AddNick2Bans B.ByteString B.ByteString UTCTime |
76 |
| AddIP2Bans B.ByteString B.ByteString UTCTime |
|
8239 | 77 |
| CheckBanned Bool |
5184 | 78 |
| SaveReplay |
8403
fbc6e7602e05
- Allow server admins to use DELEGATE even when not room owner
unc0rr
parents:
8401
diff
changeset
|
79 |
| Stats |
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8476
diff
changeset
|
80 |
| CheckRecord |
8507 | 81 |
| CheckFailed B.ByteString |
82 |
| CheckSuccess [B.ByteString] |
|
5184 | 83 |
|
84 |
||
85 |
type CmdHandler = [B.ByteString] -> Reader (ClientIndex, IRnC) [Action] |
|
86 |
||
87 |
instance NFData Action where |
|
88 |
rnf (AnswerClients chans msg) = chans `deepseq` msg `deepseq` () |
|
89 |
rnf a = a `seq` () |
|
90 |
||
8452 | 91 |
instance NFData B.ByteString |
5184 | 92 |
instance NFData (Chan a) |
93 |
||
94 |
||
95 |
othersChans :: StateT ServerState IO [ClientChan] |
|
96 |
othersChans = do |
|
97 |
cl <- client's id |
|
98 |
ri <- clientRoomA |
|
99 |
liftM (map sendChan . filter (/= cl)) $ roomClientsS ri |
|
100 |
||
101 |
processAction :: Action -> StateT ServerState IO () |
|
102 |
||
103 |
||
104 |
processAction (AnswerClients chans msg) = |
|
105 |
io $ mapM_ (`writeChan` (msg `deepseq` msg)) (chans `deepseq` chans) |
|
106 |
||
107 |
||
108 |
processAction SendServerMessage = do |
|
109 |
chan <- client's sendChan |
|
110 |
protonum <- client's clientProto |
|
111 |
si <- liftM serverInfo get |
|
112 |
let message = if protonum < latestReleaseVersion si then |
|
113 |
serverMessageForOldVersions si |
|
114 |
else |
|
115 |
serverMessage si |
|
116 |
processAction $ AnswerClients [chan] ["SERVER_MESSAGE", message] |
|
117 |
||
118 |
||
119 |
processAction SendServerVars = do |
|
120 |
chan <- client's sendChan |
|
121 |
si <- gets serverInfo |
|
122 |
io $ writeChan chan ("SERVER_VARS" : vars si) |
|
123 |
where |
|
124 |
vars si = [ |
|
125 |
"MOTD_NEW", serverMessage si, |
|
126 |
"MOTD_OLD", serverMessageForOldVersions si, |
|
127 |
"LATEST_PROTO", showB $ latestReleaseVersion si |
|
128 |
] |
|
129 |
||
130 |
||
131 |
processAction (ProtocolError msg) = do |
|
132 |
chan <- client's sendChan |
|
133 |
processAction $ AnswerClients [chan] ["ERROR", msg] |
|
134 |
||
135 |
||
136 |
processAction (Warning msg) = do |
|
137 |
chan <- client's sendChan |
|
138 |
processAction $ AnswerClients [chan] ["WARNING", msg] |
|
139 |
||
140 |
processAction (NoticeMessage n) = do |
|
141 |
chan <- client's sendChan |
|
142 |
processAction $ AnswerClients [chan] ["NOTICE", showB . fromEnum $ n] |
|
143 |
||
144 |
processAction (ByeClient msg) = do |
|
145 |
(Just ci) <- gets clientIndex |
|
146 |
ri <- clientRoomA |
|
147 |
||
148 |
chan <- client's sendChan |
|
149 |
clNick <- client's nick |
|
8372
3c193ec03e09
Logon procedure for checkers, introduce invisible clients
unc0rr
parents:
8371
diff
changeset
|
150 |
loggedIn <- client's isVisible |
5184 | 151 |
|
152 |
when (ri /= lobbyId) $ do |
|
153 |
processAction $ MoveToLobby ("quit: " `B.append` msg) |
|
154 |
return () |
|
155 |
||
8372
3c193ec03e09
Logon procedure for checkers, introduce invisible clients
unc0rr
parents:
8371
diff
changeset
|
156 |
clientsChans <- liftM (Prelude.map sendChan . Prelude.filter isVisible) $! allClientsS |
5184 | 157 |
io $ |
158 |
infoM "Clients" (show ci ++ " quits: " ++ B.unpack msg) |
|
159 |
||
160 |
when loggedIn $ processAction $ AnswerClients clientsChans ["LOBBY:LEFT", clNick, msg] |
|
161 |
||
8158 | 162 |
mapM_ processAction |
7465
c2dcf97ca664
Okay, this is workaround over ping timeouts problem on the server. Could make server crash if recieve thread wakes up after second ping timeout event.
unc0rr
parents:
7351
diff
changeset
|
163 |
[ |
c2dcf97ca664
Okay, this is workaround over ping timeouts problem on the server. Could make server crash if recieve thread wakes up after second ping timeout event.
unc0rr
parents:
7351
diff
changeset
|
164 |
AnswerClients [chan] ["BYE", msg] |
8372
3c193ec03e09
Logon procedure for checkers, introduce invisible clients
unc0rr
parents:
8371
diff
changeset
|
165 |
, ModifyClient (\c -> c{nick = "", isVisible = False}) -- this will effectively hide client from others while he isn't deleted from list |
7465
c2dcf97ca664
Okay, this is workaround over ping timeouts problem on the server. Could make server crash if recieve thread wakes up after second ping timeout event.
unc0rr
parents:
7351
diff
changeset
|
166 |
] |
c2dcf97ca664
Okay, this is workaround over ping timeouts problem on the server. Could make server crash if recieve thread wakes up after second ping timeout event.
unc0rr
parents:
7351
diff
changeset
|
167 |
|
5184 | 168 |
s <- get |
169 |
put $! s{removedClients = ci `Set.insert` removedClients s} |
|
170 |
||
171 |
processAction (DeleteClient ci) = do |
|
172 |
io $ debugM "Clients" $ "DeleteClient: " ++ show ci |
|
173 |
||
174 |
rnc <- gets roomsClients |
|
175 |
io $ removeClient rnc ci |
|
176 |
||
177 |
s <- get |
|
178 |
put $! s{removedClients = ci `Set.delete` removedClients s} |
|
7321
57bd4f201401
- Try sending remove message in 'finally' as a last resort
unc0rr
parents:
7126
diff
changeset
|
179 |
|
5209
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
180 |
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
|
181 |
cls <- allClientsS |
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
182 |
io $ when (sp && null cls) $ throwIO ShutdownException |
5184 | 183 |
|
184 |
processAction (ModifyClient f) = do |
|
185 |
(Just ci) <- gets clientIndex |
|
186 |
rnc <- gets roomsClients |
|
187 |
io $ modifyClient rnc f ci |
|
188 |
return () |
|
189 |
||
190 |
processAction (ModifyClient2 ci f) = do |
|
191 |
rnc <- gets roomsClients |
|
192 |
io $ modifyClient rnc f ci |
|
193 |
return () |
|
194 |
||
7757
c20e6c80e249
Don't accept ROUNDFINISHED message twice. Fixes game hangs when half of teams quit game.
unc0rr
parents:
7748
diff
changeset
|
195 |
processAction (ModifyRoomClients f) = do |
c20e6c80e249
Don't accept ROUNDFINISHED message twice. Fixes game hangs when half of teams quit game.
unc0rr
parents:
7748
diff
changeset
|
196 |
rnc <- gets roomsClients |
c20e6c80e249
Don't accept ROUNDFINISHED message twice. Fixes game hangs when half of teams quit game.
unc0rr
parents:
7748
diff
changeset
|
197 |
ri <- clientRoomA |
c20e6c80e249
Don't accept ROUNDFINISHED message twice. Fixes game hangs when half of teams quit game.
unc0rr
parents:
7748
diff
changeset
|
198 |
roomClIDs <- io $ roomClientsIndicesM rnc ri |
c20e6c80e249
Don't accept ROUNDFINISHED message twice. Fixes game hangs when half of teams quit game.
unc0rr
parents:
7748
diff
changeset
|
199 |
io $ mapM_ (modifyClient rnc f) roomClIDs |
c20e6c80e249
Don't accept ROUNDFINISHED message twice. Fixes game hangs when half of teams quit game.
unc0rr
parents:
7748
diff
changeset
|
200 |
|
5184 | 201 |
|
202 |
processAction (ModifyRoom f) = do |
|
203 |
rnc <- gets roomsClients |
|
204 |
ri <- clientRoomA |
|
205 |
io $ modifyRoom rnc f ri |
|
206 |
return () |
|
207 |
||
208 |
||
209 |
processAction (ModifyServerInfo f) = do |
|
210 |
modify (\s -> s{serverInfo = f $ serverInfo s}) |
|
211 |
si <- gets serverInfo |
|
212 |
io $ writeServerConfig si |
|
213 |
||
214 |
||
215 |
processAction (MoveToRoom ri) = do |
|
216 |
(Just ci) <- gets clientIndex |
|
217 |
rnc <- gets roomsClients |
|
218 |
||
219 |
io $ do |
|
7898 | 220 |
modifyClient rnc (\cl -> cl{teamsInGame = 0, isReady = False, isMaster = False, isInGame = False}) ci |
5184 | 221 |
modifyRoom rnc (\r -> r{playersIn = playersIn r + 1}) ri |
222 |
moveClientToRoom rnc ri ci |
|
223 |
||
224 |
chans <- liftM (map sendChan) $ roomClientsS ri |
|
225 |
clNick <- client's nick |
|
226 |
||
227 |
processAction $ AnswerClients chans ["JOINED", clNick] |
|
228 |
||
229 |
||
230 |
processAction (MoveToLobby msg) = do |
|
231 |
(Just ci) <- gets clientIndex |
|
232 |
ri <- clientRoomA |
|
233 |
rnc <- gets roomsClients |
|
7766 | 234 |
playersNum <- io $ room'sM rnc playersIn ri |
5184 | 235 |
master <- client's isMaster |
236 |
-- client <- client's id |
|
237 |
clNick <- client's nick |
|
238 |
chans <- othersChans |
|
239 |
||
240 |
if master then |
|
7521 | 241 |
if playersNum > 1 then |
8438
64ac58abd02a
Don't resend "team quit" message when client closes engine, then quits room:
unc0rr
parents:
8422
diff
changeset
|
242 |
mapM_ processAction [ChangeMaster Nothing, NoticeMessage AdminLeft, RemoveClientTeams, AnswerClients chans ["LEFT", clNick, msg]] |
5184 | 243 |
else |
244 |
processAction RemoveRoom |
|
245 |
else |
|
8438
64ac58abd02a
Don't resend "team quit" message when client closes engine, then quits room:
unc0rr
parents:
8422
diff
changeset
|
246 |
mapM_ processAction [RemoveClientTeams, AnswerClients chans ["LEFT", clNick, msg]] |
5184 | 247 |
|
248 |
-- when not removing room |
|
7351
34efdd1f230f
- Check ready status only after deleting player's teams (should fix the bug when you're unable to start game)
unc0rr
parents:
7321
diff
changeset
|
249 |
ready <- client's isReady |
7521 | 250 |
when (not master || playersNum > 1) . io $ do |
5184 | 251 |
modifyRoom rnc (\r -> r{ |
252 |
playersIn = playersIn r - 1, |
|
253 |
readyPlayers = if ready then readyPlayers r - 1 else readyPlayers r |
|
254 |
}) ri |
|
255 |
moveClientToLobby rnc ci |
|
256 |
||
7710
fd5bcbd698a5
- Keep track of room name so correct name is displayed when you become room admin
unc0rr
parents:
7682
diff
changeset
|
257 |
|
8247 | 258 |
processAction (ChangeMaster delegateId)= do |
5184 | 259 |
(Just ci) <- gets clientIndex |
7710
fd5bcbd698a5
- Keep track of room name so correct name is displayed when you become room admin
unc0rr
parents:
7682
diff
changeset
|
260 |
proto <- client's clientProto |
5184 | 261 |
ri <- clientRoomA |
262 |
rnc <- gets roomsClients |
|
8247 | 263 |
newMasterId <- liftM (\ids -> fromMaybe (last . filter (/= ci) $ ids) delegateId) . io $ roomClientsIndicesM rnc ri |
5184 | 264 |
newMaster <- io $ client'sM rnc id newMasterId |
6733 | 265 |
oldRoomName <- io $ room'sM rnc name ri |
7682 | 266 |
oldMaster <- client's nick |
8245 | 267 |
kicked <- client's isKickedFromServer |
7668
4cb423f42105
Show who is the room admin on join (no tested, also I don't like how it is done via server warnings, but it seems there's no other solution compatible with .17)
unc0rr
parents:
7664
diff
changeset
|
268 |
thisRoomChans <- liftM (map sendChan) $ roomClientsS ri |
8245 | 269 |
let newRoomName = if (proto < 42) || kicked then nick newMaster else oldRoomName |
5184 | 270 |
mapM_ processAction [ |
7775 | 271 |
ModifyRoom (\r -> r{masterID = newMasterId |
272 |
, name = newRoomName |
|
273 |
, isRestrictedJoins = False |
|
274 |
, isRestrictedTeams = False |
|
8232 | 275 |
, isRegisteredOnly = False |
7775 | 276 |
, readyPlayers = if isReady newMaster then readyPlayers r else readyPlayers r + 1}) |
277 |
, ModifyClient2 newMasterId (\c -> c{isMaster = True, isReady = True}) |
|
7682 | 278 |
, AnswerClients [sendChan newMaster] ["ROOM_CONTROL_ACCESS", "1"] |
279 |
, AnswerClients thisRoomChans ["CLIENT_FLAGS", "-h", oldMaster] |
|
7775 | 280 |
, AnswerClients thisRoomChans ["CLIENT_FLAGS", "+hr", nick newMaster] |
5184 | 281 |
] |
282 |
||
7766 | 283 |
newRoom' <- io $ room'sM rnc id ri |
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
|
284 |
chans <- liftM (map sendChan) $! sameProtoClientsS proto |
7972 | 285 |
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
|
286 |
|
7321
57bd4f201401
- Try sending remove message in 'finally' as a last resort
unc0rr
parents:
7126
diff
changeset
|
287 |
|
5184 | 288 |
processAction (AddRoom roomName roomPassword) = do |
289 |
Just clId <- gets clientIndex |
|
290 |
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
|
291 |
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
|
292 |
n <- client's nick |
5184 | 293 |
|
294 |
let rm = newRoom{ |
|
295 |
masterID = clId, |
|
296 |
name = roomName, |
|
297 |
password = roomPassword, |
|
298 |
roomProto = proto |
|
299 |
} |
|
300 |
||
301 |
rId <- io $ addRoom rnc rm |
|
302 |
||
303 |
processAction $ MoveToRoom rId |
|
304 |
||
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
|
305 |
chans <- liftM (map sendChan) $! sameProtoClientsS proto |
5184 | 306 |
|
307 |
mapM_ processAction [ |
|
7945
4006d77e1a28
Send notification about 1 player in room on room creation
unc0rr
parents:
7926
diff
changeset
|
308 |
AnswerClients chans ("ROOM" : "ADD" : roomInfo n rm{playersIn = 1}) |
5184 | 309 |
] |
310 |
||
311 |
||
312 |
processAction RemoveRoom = do |
|
313 |
Just clId <- gets clientIndex |
|
314 |
rnc <- gets roomsClients |
|
315 |
ri <- io $ clientRoomM rnc clId |
|
316 |
roomName <- io $ room'sM rnc name ri |
|
317 |
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
|
318 |
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
|
319 |
chans <- liftM (map sendChan) $! sameProtoClientsS proto |
5184 | 320 |
|
321 |
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
|
322 |
AnswerClients chans ["ROOM", "DEL", roomName], |
5184 | 323 |
AnswerClients others ["ROOMABANDONED", roomName] |
324 |
] |
|
325 |
||
326 |
io $ removeRoom rnc ri |
|
327 |
||
328 |
||
7921
6b074de32bea
Send ROOM UPD message when team is added/deleted from room, and when game starts or finishes
unc0rr
parents:
7898
diff
changeset
|
329 |
processAction SendUpdateOnThisRoom = do |
6b074de32bea
Send ROOM UPD message when team is added/deleted from room, and when game starts or finishes
unc0rr
parents:
7898
diff
changeset
|
330 |
Just clId <- gets clientIndex |
6b074de32bea
Send ROOM UPD message when team is added/deleted from room, and when game starts or finishes
unc0rr
parents:
7898
diff
changeset
|
331 |
proto <- client's clientProto |
6b074de32bea
Send ROOM UPD message when team is added/deleted from room, and when game starts or finishes
unc0rr
parents:
7898
diff
changeset
|
332 |
rnc <- gets roomsClients |
6b074de32bea
Send ROOM UPD message when team is added/deleted from room, and when game starts or finishes
unc0rr
parents:
7898
diff
changeset
|
333 |
ri <- io $ clientRoomM rnc clId |
6b074de32bea
Send ROOM UPD message when team is added/deleted from room, and when game starts or finishes
unc0rr
parents:
7898
diff
changeset
|
334 |
rm <- io $ room'sM rnc id ri |
7926
550083f61a0e
oops, fix incorrect room owner name in ROOM UPD command again
unc0rr
parents:
7924
diff
changeset
|
335 |
n <- io $ client'sM rnc nick (masterID rm) |
7921
6b074de32bea
Send ROOM UPD message when team is added/deleted from room, and when game starts or finishes
unc0rr
parents:
7898
diff
changeset
|
336 |
chans <- liftM (map sendChan) $! sameProtoClientsS proto |
7924
351f970c60e1
oops, fix incorrect room owner name in ROOM UPD command
unc0rr
parents:
7921
diff
changeset
|
337 |
processAction $ AnswerClients chans ("ROOM" : "UPD" : name rm : roomInfo n rm) |
7921
6b074de32bea
Send ROOM UPD message when team is added/deleted from room, and when game starts or finishes
unc0rr
parents:
7898
diff
changeset
|
338 |
|
6b074de32bea
Send ROOM UPD message when team is added/deleted from room, and when game starts or finishes
unc0rr
parents:
7898
diff
changeset
|
339 |
|
6758
26bf919aeb57
Oh, should also check for game finish when player quits without ROUNDFINISHED message: small refactoring, not tested at all
unc0rr
parents:
6756
diff
changeset
|
340 |
processAction UnreadyRoomClients = do |
5184 | 341 |
ri <- clientRoomA |
342 |
roomPlayers <- roomClientsS ri |
|
343 |
pr <- client's clientProto |
|
7757
c20e6c80e249
Don't accept ROUNDFINISHED message twice. Fixes game hangs when half of teams quit game.
unc0rr
parents:
7748
diff
changeset
|
344 |
mapM_ processAction [ |
7775 | 345 |
AnswerClients (map sendChan roomPlayers) $ notReadyMessage pr . map nick . filter (not . isMaster) $ roomPlayers |
346 |
, ModifyRoomClients (\cl -> cl{isReady = isMaster cl}) |
|
347 |
, ModifyRoom (\r -> r{readyPlayers = 1}) |
|
7757
c20e6c80e249
Don't accept ROUNDFINISHED message twice. Fixes game hangs when half of teams quit game.
unc0rr
parents:
7748
diff
changeset
|
348 |
] |
5184 | 349 |
where |
350 |
notReadyMessage p nicks = if p < 38 then "NOT_READY" : nicks else "CLIENT_FLAGS" : "-r" : nicks |
|
351 |
||
7321
57bd4f201401
- Try sending remove message in 'finally' as a last resort
unc0rr
parents:
7126
diff
changeset
|
352 |
|
6758
26bf919aeb57
Oh, should also check for game finish when player quits without ROUNDFINISHED message: small refactoring, not tested at all
unc0rr
parents:
6756
diff
changeset
|
353 |
processAction FinishGame = do |
26bf919aeb57
Oh, should also check for game finish when player quits without ROUNDFINISHED message: small refactoring, not tested at all
unc0rr
parents:
6756
diff
changeset
|
354 |
rnc <- gets roomsClients |
26bf919aeb57
Oh, should also check for game finish when player quits without ROUNDFINISHED message: small refactoring, not tested at all
unc0rr
parents:
6756
diff
changeset
|
355 |
ri <- clientRoomA |
26bf919aeb57
Oh, should also check for game finish when player quits without ROUNDFINISHED message: small refactoring, not tested at all
unc0rr
parents:
6756
diff
changeset
|
356 |
thisRoomChans <- liftM (map sendChan) $ roomClientsS ri |
7321
57bd4f201401
- Try sending remove message in 'finally' as a last resort
unc0rr
parents:
7126
diff
changeset
|
357 |
answerRemovedTeams <- io $ |
7126
8daa5c8e84c0
Bring leftTeams back (with a fix) as it is apparently needed for spectators.
unc0rr
parents:
7124
diff
changeset
|
358 |
room'sM rnc (map (\t -> AnswerClients thisRoomChans ["REMOVE_TEAM", t]) . leftTeams . fromJust . gameInfo) ri |
7321
57bd4f201401
- Try sending remove message in 'finally' as a last resort
unc0rr
parents:
7126
diff
changeset
|
359 |
|
57bd4f201401
- Try sending remove message in 'finally' as a last resort
unc0rr
parents:
7126
diff
changeset
|
360 |
mapM_ processAction $ |
7124 | 361 |
SaveReplay |
7126
8daa5c8e84c0
Bring leftTeams back (with a fix) as it is apparently needed for spectators.
unc0rr
parents:
7124
diff
changeset
|
362 |
: ModifyRoom |
6758
26bf919aeb57
Oh, should also check for game finish when player quits without ROUNDFINISHED message: small refactoring, not tested at all
unc0rr
parents:
6756
diff
changeset
|
363 |
(\r -> r{ |
26bf919aeb57
Oh, should also check for game finish when player quits without ROUNDFINISHED message: small refactoring, not tested at all
unc0rr
parents:
6756
diff
changeset
|
364 |
gameInfo = Nothing, |
26bf919aeb57
Oh, should also check for game finish when player quits without ROUNDFINISHED message: small refactoring, not tested at all
unc0rr
parents:
6756
diff
changeset
|
365 |
readyPlayers = 0 |
26bf919aeb57
Oh, should also check for game finish when player quits without ROUNDFINISHED message: small refactoring, not tested at all
unc0rr
parents:
6756
diff
changeset
|
366 |
} |
26bf919aeb57
Oh, should also check for game finish when player quits without ROUNDFINISHED message: small refactoring, not tested at all
unc0rr
parents:
6756
diff
changeset
|
367 |
) |
7126
8daa5c8e84c0
Bring leftTeams back (with a fix) as it is apparently needed for spectators.
unc0rr
parents:
7124
diff
changeset
|
368 |
: UnreadyRoomClients |
7921
6b074de32bea
Send ROOM UPD message when team is added/deleted from room, and when game starts or finishes
unc0rr
parents:
7898
diff
changeset
|
369 |
: SendUpdateOnThisRoom |
8241
b15f165c080c
Send "ROUND_FINISHED" to room clients when server thinks so
unc0rr
parents:
8239
diff
changeset
|
370 |
: AnswerClients thisRoomChans ["ROUND_FINISHED"] |
7126
8daa5c8e84c0
Bring leftTeams back (with a fix) as it is apparently needed for spectators.
unc0rr
parents:
7124
diff
changeset
|
371 |
: answerRemovedTeams |
5184 | 372 |
|
7321
57bd4f201401
- Try sending remove message in 'finally' as a last resort
unc0rr
parents:
7126
diff
changeset
|
373 |
|
6753
e95b1f62d0de
Don't remove client's teams from teams list on "ROUNDFINISHED 0", just send team removal message to others.
unc0rr
parents:
6733
diff
changeset
|
374 |
processAction (SendTeamRemovalMessage teamName) = do |
e95b1f62d0de
Don't remove client's teams from teams list on "ROUNDFINISHED 0", just send team removal message to others.
unc0rr
parents:
6733
diff
changeset
|
375 |
chans <- othersChans |
e95b1f62d0de
Don't remove client's teams from teams list on "ROUNDFINISHED 0", just send team removal message to others.
unc0rr
parents:
6733
diff
changeset
|
376 |
mapM_ processAction [ |
e95b1f62d0de
Don't remove client's teams from teams list on "ROUNDFINISHED 0", just send team removal message to others.
unc0rr
parents:
6733
diff
changeset
|
377 |
AnswerClients chans ["EM", rmTeamMsg], |
e95b1f62d0de
Don't remove client's teams from teams list on "ROUNDFINISHED 0", just send team removal message to others.
unc0rr
parents:
6733
diff
changeset
|
378 |
ModifyRoom (\r -> r{ |
e95b1f62d0de
Don't remove client's teams from teams list on "ROUNDFINISHED 0", just send team removal message to others.
unc0rr
parents:
6733
diff
changeset
|
379 |
gameInfo = liftM (\g -> g{ |
7124 | 380 |
teamsInGameNumber = teamsInGameNumber g - 1 |
8369 | 381 |
, roundMsgs = rmTeamMsg : roundMsgs g |
6753
e95b1f62d0de
Don't remove client's teams from teams list on "ROUNDFINISHED 0", just send team removal message to others.
unc0rr
parents:
6733
diff
changeset
|
382 |
}) $ gameInfo r |
e95b1f62d0de
Don't remove client's teams from teams list on "ROUNDFINISHED 0", just send team removal message to others.
unc0rr
parents:
6733
diff
changeset
|
383 |
}) |
e95b1f62d0de
Don't remove client's teams from teams list on "ROUNDFINISHED 0", just send team removal message to others.
unc0rr
parents:
6733
diff
changeset
|
384 |
] |
7321
57bd4f201401
- Try sending remove message in 'finally' as a last resort
unc0rr
parents:
7126
diff
changeset
|
385 |
|
6758
26bf919aeb57
Oh, should also check for game finish when player quits without ROUNDFINISHED message: small refactoring, not tested at all
unc0rr
parents:
6756
diff
changeset
|
386 |
rnc <- gets roomsClients |
26bf919aeb57
Oh, should also check for game finish when player quits without ROUNDFINISHED message: small refactoring, not tested at all
unc0rr
parents:
6756
diff
changeset
|
387 |
ri <- clientRoomA |
26bf919aeb57
Oh, should also check for game finish when player quits without ROUNDFINISHED message: small refactoring, not tested at all
unc0rr
parents:
6756
diff
changeset
|
388 |
gi <- io $ room'sM rnc gameInfo ri |
8422 | 389 |
when (0 == teamsInGameNumber (fromJust gi)) $ |
7321
57bd4f201401
- Try sending remove message in 'finally' as a last resort
unc0rr
parents:
7126
diff
changeset
|
390 |
processAction FinishGame |
6753
e95b1f62d0de
Don't remove client's teams from teams list on "ROUNDFINISHED 0", just send team removal message to others.
unc0rr
parents:
6733
diff
changeset
|
391 |
where |
e95b1f62d0de
Don't remove client's teams from teams list on "ROUNDFINISHED 0", just send team removal message to others.
unc0rr
parents:
6733
diff
changeset
|
392 |
rmTeamMsg = toEngineMsg $ 'F' `B.cons` teamName |
7321
57bd4f201401
- Try sending remove message in 'finally' as a last resort
unc0rr
parents:
7126
diff
changeset
|
393 |
|
57bd4f201401
- Try sending remove message in 'finally' as a last resort
unc0rr
parents:
7126
diff
changeset
|
394 |
|
5184 | 395 |
processAction (RemoveTeam teamName) = do |
8438
64ac58abd02a
Don't resend "team quit" message when client closes engine, then quits room:
unc0rr
parents:
8422
diff
changeset
|
396 |
(Just ci) <- gets clientIndex |
5184 | 397 |
rnc <- gets roomsClients |
398 |
ri <- clientRoomA |
|
8438
64ac58abd02a
Don't resend "team quit" message when client closes engine, then quits room:
unc0rr
parents:
8422
diff
changeset
|
399 |
inGame <- io $ do |
64ac58abd02a
Don't resend "team quit" message when client closes engine, then quits room:
unc0rr
parents:
8422
diff
changeset
|
400 |
r <- room'sM rnc (isJust . gameInfo) ri |
64ac58abd02a
Don't resend "team quit" message when client closes engine, then quits room:
unc0rr
parents:
8422
diff
changeset
|
401 |
c <- client'sM rnc isInGame ci |
64ac58abd02a
Don't resend "team quit" message when client closes engine, then quits room:
unc0rr
parents:
8422
diff
changeset
|
402 |
return $ r && c |
5184 | 403 |
chans <- othersChans |
7321
57bd4f201401
- Try sending remove message in 'finally' as a last resort
unc0rr
parents:
7126
diff
changeset
|
404 |
mapM_ processAction $ |
7126
8daa5c8e84c0
Bring leftTeams back (with a fix) as it is apparently needed for spectators.
unc0rr
parents:
7124
diff
changeset
|
405 |
ModifyRoom (\r -> r{ |
8daa5c8e84c0
Bring leftTeams back (with a fix) as it is apparently needed for spectators.
unc0rr
parents:
7124
diff
changeset
|
406 |
teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r |
8daa5c8e84c0
Bring leftTeams back (with a fix) as it is apparently needed for spectators.
unc0rr
parents:
7124
diff
changeset
|
407 |
, gameInfo = liftM (\g -> g{leftTeams = teamName : leftTeams g}) $ gameInfo r |
8daa5c8e84c0
Bring leftTeams back (with a fix) as it is apparently needed for spectators.
unc0rr
parents:
7124
diff
changeset
|
408 |
}) |
7947 | 409 |
: SendUpdateOnThisRoom |
7124 | 410 |
: AnswerClients chans ["REMOVE_TEAM", teamName] |
411 |
: [SendTeamRemovalMessage teamName | inGame] |
|
5184 | 412 |
|
413 |
||
8438
64ac58abd02a
Don't resend "team quit" message when client closes engine, then quits room:
unc0rr
parents:
8422
diff
changeset
|
414 |
processAction RemoveClientTeams = do |
64ac58abd02a
Don't resend "team quit" message when client closes engine, then quits room:
unc0rr
parents:
8422
diff
changeset
|
415 |
(Just ci) <- gets clientIndex |
5184 | 416 |
rnc <- gets roomsClients |
417 |
||
418 |
removeTeamActions <- io $ do |
|
8438
64ac58abd02a
Don't resend "team quit" message when client closes engine, then quits room:
unc0rr
parents:
8422
diff
changeset
|
419 |
rId <- clientRoomM rnc ci |
5184 | 420 |
roomTeams <- room'sM rnc teams rId |
8438
64ac58abd02a
Don't resend "team quit" message when client closes engine, then quits room:
unc0rr
parents:
8422
diff
changeset
|
421 |
return . Prelude.map (RemoveTeam . teamname) . Prelude.filter (\t -> teamownerId t == ci) $ roomTeams |
5184 | 422 |
|
423 |
mapM_ processAction removeTeamActions |
|
424 |
||
425 |
||
426 |
||
427 |
processAction CheckRegistered = do |
|
428 |
(Just ci) <- gets clientIndex |
|
429 |
n <- client's nick |
|
430 |
h <- client's host |
|
431 |
p <- client's clientProto |
|
8371 | 432 |
checker <- client's isChecker |
5184 | 433 |
uid <- client's clUID |
8371 | 434 |
-- allow multiple checker logins |
435 |
haveSameNick <- liftM (not . null . tail . filter (\c -> (not $ isChecker c) && caseInsensitiveCompare (nick c) n)) allClientsS |
|
8476 | 436 |
if (not checker) && haveSameNick then |
5184 | 437 |
if p < 38 then |
8401
87410ae372f6
Server messages localization using Qt's l10n subsystem:
unc0rr
parents:
8396
diff
changeset
|
438 |
processAction $ ByeClient $ loc "Nickname is already in use" |
5184 | 439 |
else |
8239 | 440 |
processAction $ NoticeMessage NickAlreadyInUse |
5184 | 441 |
else |
442 |
do |
|
443 |
db <- gets (dbQueries . serverInfo) |
|
444 |
io $ writeChan db $ CheckAccount ci (hashUnique uid) n h |
|
445 |
return () |
|
446 |
||
447 |
processAction ClearAccountsCache = do |
|
448 |
dbq <- gets (dbQueries . serverInfo) |
|
449 |
io $ writeChan dbq ClearCache |
|
450 |
return () |
|
451 |
||
452 |
||
8189 | 453 |
processAction (ProcessAccountInfo info) = do |
5184 | 454 |
case info of |
455 |
HasAccount passwd isAdmin -> do |
|
8189 | 456 |
b <- isBanned |
8372
3c193ec03e09
Logon procedure for checkers, introduce invisible clients
unc0rr
parents:
8371
diff
changeset
|
457 |
c <- client's isChecker |
3c193ec03e09
Logon procedure for checkers, introduce invisible clients
unc0rr
parents:
8371
diff
changeset
|
458 |
when (not b) $ (if c then checkerLogin else playerLogin) passwd isAdmin |
8189 | 459 |
Guest -> do |
460 |
b <- isBanned |
|
461 |
when (not b) $ |
|
462 |
processAction JoinLobby |
|
5184 | 463 |
Admin -> do |
464 |
mapM_ processAction [ModifyClient (\cl -> cl{isAdministrator = True}), JoinLobby] |
|
465 |
chan <- client's sendChan |
|
466 |
processAction $ AnswerClients [chan] ["ADMIN_ACCESS"] |
|
8189 | 467 |
where |
468 |
isBanned = do |
|
8239 | 469 |
processAction $ CheckBanned False |
8189 | 470 |
liftM B.null $ client's nick |
8401
87410ae372f6
Server messages localization using Qt's l10n subsystem:
unc0rr
parents:
8396
diff
changeset
|
471 |
checkerLogin _ False = processAction $ ByeClient $ loc "No checker rights" |
8372
3c193ec03e09
Logon procedure for checkers, introduce invisible clients
unc0rr
parents:
8371
diff
changeset
|
472 |
checkerLogin p True = do |
3c193ec03e09
Logon procedure for checkers, introduce invisible clients
unc0rr
parents:
8371
diff
changeset
|
473 |
wp <- client's webPassword |
3c193ec03e09
Logon procedure for checkers, introduce invisible clients
unc0rr
parents:
8371
diff
changeset
|
474 |
processAction $ |
8401
87410ae372f6
Server messages localization using Qt's l10n subsystem:
unc0rr
parents:
8396
diff
changeset
|
475 |
if wp == p then ModifyClient $ \c -> c{logonPassed = True} else ByeClient $ loc "Authentication failed" |
8372
3c193ec03e09
Logon procedure for checkers, introduce invisible clients
unc0rr
parents:
8371
diff
changeset
|
476 |
playerLogin p a = do |
3c193ec03e09
Logon procedure for checkers, introduce invisible clients
unc0rr
parents:
8371
diff
changeset
|
477 |
chan <- client's sendChan |
3c193ec03e09
Logon procedure for checkers, introduce invisible clients
unc0rr
parents:
8371
diff
changeset
|
478 |
mapM_ processAction [AnswerClients [chan] ["ASKPASSWORD"], ModifyClient (\c -> c{webPassword = p, isAdministrator = a})] |
5184 | 479 |
|
480 |
processAction JoinLobby = do |
|
481 |
chan <- client's sendChan |
|
482 |
clientNick <- client's nick |
|
7498
86984f6fa1b9
Introduce 'a' and 'u' client flags to mark admins and authenticated users
unc0rr
parents:
7465
diff
changeset
|
483 |
isAuthenticated <- liftM (not . B.null) $ client's webPassword |
86984f6fa1b9
Introduce 'a' and 'u' client flags to mark admins and authenticated users
unc0rr
parents:
7465
diff
changeset
|
484 |
isAdmin <- client's isAdministrator |
8372
3c193ec03e09
Logon procedure for checkers, introduce invisible clients
unc0rr
parents:
8371
diff
changeset
|
485 |
loggedInClients <- liftM (Prelude.filter isVisible) $! allClientsS |
7498
86984f6fa1b9
Introduce 'a' and 'u' client flags to mark admins and authenticated users
unc0rr
parents:
7465
diff
changeset
|
486 |
let (lobbyNicks, clientsChans) = unzip . L.map (nick &&& sendChan) $ loggedInClients |
86984f6fa1b9
Introduce 'a' and 'u' client flags to mark admins and authenticated users
unc0rr
parents:
7465
diff
changeset
|
487 |
let authenticatedNicks = L.map nick . L.filter (not . B.null . webPassword) $ loggedInClients |
86984f6fa1b9
Introduce 'a' and 'u' client flags to mark admins and authenticated users
unc0rr
parents:
7465
diff
changeset
|
488 |
let adminsNicks = L.map nick . L.filter isAdministrator $ loggedInClients |
86984f6fa1b9
Introduce 'a' and 'u' client flags to mark admins and authenticated users
unc0rr
parents:
7465
diff
changeset
|
489 |
let clFlags = B.concat . L.concat $ [["u" | isAuthenticated], ["a" | isAdmin]] |
86984f6fa1b9
Introduce 'a' and 'u' client flags to mark admins and authenticated users
unc0rr
parents:
7465
diff
changeset
|
490 |
mapM_ processAction . concat $ [ |
86984f6fa1b9
Introduce 'a' and 'u' client flags to mark admins and authenticated users
unc0rr
parents:
7465
diff
changeset
|
491 |
[AnswerClients clientsChans ["LOBBY:JOINED", clientNick]] |
86984f6fa1b9
Introduce 'a' and 'u' client flags to mark admins and authenticated users
unc0rr
parents:
7465
diff
changeset
|
492 |
, [AnswerClients [chan] ("LOBBY:JOINED" : clientNick : lobbyNicks)] |
86984f6fa1b9
Introduce 'a' and 'u' client flags to mark admins and authenticated users
unc0rr
parents:
7465
diff
changeset
|
493 |
, [AnswerClients [chan] ("CLIENT_FLAGS" : "+u" : authenticatedNicks) | not $ null authenticatedNicks] |
86984f6fa1b9
Introduce 'a' and 'u' client flags to mark admins and authenticated users
unc0rr
parents:
7465
diff
changeset
|
494 |
, [AnswerClients [chan] ("CLIENT_FLAGS" : "+a" : adminsNicks) | not $ null adminsNicks] |
86984f6fa1b9
Introduce 'a' and 'u' client flags to mark admins and authenticated users
unc0rr
parents:
7465
diff
changeset
|
495 |
, [AnswerClients (chan : clientsChans) ["CLIENT_FLAGS", B.concat["+" , clFlags], clientNick] | not $ B.null clFlags] |
8372
3c193ec03e09
Logon procedure for checkers, introduce invisible clients
unc0rr
parents:
8371
diff
changeset
|
496 |
, [ModifyClient (\cl -> cl{logonPassed = True, isVisible = True})] |
7498
86984f6fa1b9
Introduce 'a' and 'u' client flags to mark admins and authenticated users
unc0rr
parents:
7465
diff
changeset
|
497 |
, [SendServerMessage] |
86984f6fa1b9
Introduce 'a' and 'u' client flags to mark admins and authenticated users
unc0rr
parents:
7465
diff
changeset
|
498 |
] |
5184 | 499 |
|
500 |
||
501 |
processAction (KickClient kickId) = do |
|
502 |
modify (\s -> s{clientIndex = Just kickId}) |
|
5214 | 503 |
clHost <- client's host |
504 |
currentTime <- io getCurrentTime |
|
505 |
mapM_ processAction [ |
|
8401
87410ae372f6
Server messages localization using Qt's l10n subsystem:
unc0rr
parents:
8396
diff
changeset
|
506 |
AddIP2Bans clHost (loc "60 seconds cooldown after kick") (addUTCTime 60 currentTime) |
8245 | 507 |
, ModifyClient (\c -> c{isKickedFromServer = True}) |
508 |
, ByeClient "Kicked" |
|
5214 | 509 |
] |
5184 | 510 |
|
511 |
||
512 |
processAction (BanClient seconds reason banId) = do |
|
513 |
modify (\s -> s{clientIndex = Just banId}) |
|
514 |
clHost <- client's host |
|
515 |
currentTime <- io getCurrentTime |
|
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
516 |
let msg = B.concat ["Ban for ", B.pack . show $ seconds, " (", reason, ")"] |
5184 | 517 |
mapM_ processAction [ |
518 |
AddIP2Bans clHost msg (addUTCTime seconds currentTime) |
|
519 |
, KickClient banId |
|
520 |
] |
|
521 |
||
8245 | 522 |
|
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
523 |
processAction (BanIP ip seconds reason) = do |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
524 |
currentTime <- io getCurrentTime |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
525 |
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
|
526 |
processAction $ |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
527 |
AddIP2Bans ip msg (addUTCTime seconds currentTime) |
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
528 |
|
8245 | 529 |
|
8154 | 530 |
processAction (BanNick n seconds reason) = do |
531 |
currentTime <- io getCurrentTime |
|
532 |
let msg = |
|
533 |
if seconds > 60 * 60 * 24 * 365 then |
|
534 |
B.concat ["Permanent ban (", reason, ")"] |
|
535 |
else |
|
536 |
B.concat ["Ban for ", B.pack . show $ seconds, " (", reason, ")"] |
|
537 |
processAction $ |
|
538 |
AddNick2Bans n msg (addUTCTime seconds currentTime) |
|
539 |
||
8245 | 540 |
|
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
541 |
processAction BanList = do |
8156 | 542 |
time <- io $ getCurrentTime |
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
543 |
ch <- client's sendChan |
8156 | 544 |
b <- gets (B.intercalate "\n" . concatMap (ban2Str time) . bans . serverInfo) |
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
545 |
processAction $ |
7766 | 546 |
AnswerClients [ch] ["BANLIST", b] |
8156 | 547 |
where |
548 |
ban2Str time (BanByIP b r t) = ["I", b, r, B.pack . show $ t `diffUTCTime` time] |
|
549 |
ban2Str time (BanByNick b r t) = ["N", b, r, B.pack . show $ t `diffUTCTime` time] |
|
7321
57bd4f201401
- Try sending remove message in 'finally' as a last resort
unc0rr
parents:
7126
diff
changeset
|
550 |
|
8245 | 551 |
|
7748 | 552 |
processAction (Unban entry) = do |
8156 | 553 |
processAction $ ModifyServerInfo (\s -> s{bans = filter (not . f) $ bans s}) |
7748 | 554 |
where |
555 |
f (BanByIP bip _ _) = bip == entry |
|
556 |
f (BanByNick bn _ _) = bn == entry |
|
5184 | 557 |
|
8245 | 558 |
|
5184 | 559 |
processAction (KickRoomClient kickId) = do |
560 |
modify (\s -> s{clientIndex = Just kickId}) |
|
561 |
ch <- client's sendChan |
|
8401
87410ae372f6
Server messages localization using Qt's l10n subsystem:
unc0rr
parents:
8396
diff
changeset
|
562 |
mapM_ processAction [AnswerClients [ch] ["KICKED"], MoveToLobby $ loc "kicked"] |
5184 | 563 |
|
564 |
||
565 |
processAction (AddClient cl) = do |
|
566 |
rnc <- gets roomsClients |
|
567 |
si <- gets serverInfo |
|
568 |
newClId <- io $ do |
|
569 |
ci <- addClient rnc cl |
|
570 |
_ <- Exception.mask (forkIO . clientRecvLoop (clientSocket cl) (coreChan si) (sendChan cl) ci) |
|
571 |
||
572 |
infoM "Clients" (show ci ++ ": New client. Time: " ++ show (connectTime cl)) |
|
573 |
||
574 |
return ci |
|
575 |
||
576 |
modify (\s -> s{clientIndex = Just newClId}) |
|
577 |
mapM_ processAction |
|
578 |
[ |
|
579 |
AnswerClients [sendChan cl] ["CONNECTED", "Hedgewars server http://www.hedgewars.org/", serverVersion] |
|
8239 | 580 |
, CheckBanned True |
6809 | 581 |
, AddIP2Bans (host cl) "Reconnected too fast" (addUTCTime 10 $ connectTime cl) |
5184 | 582 |
] |
583 |
||
584 |
||
585 |
processAction (AddNick2Bans n reason expiring) = do |
|
586 |
processAction $ ModifyServerInfo (\s -> s{bans = BanByNick n reason expiring : bans s}) |
|
587 |
||
588 |
processAction (AddIP2Bans ip reason expiring) = do |
|
589 |
(Just ci) <- gets clientIndex |
|
590 |
rc <- gets removedClients |
|
591 |
when (not $ ci `Set.member` rc) |
|
592 |
$ processAction $ ModifyServerInfo (\s -> s{bans = BanByIP ip reason expiring : bans s}) |
|
593 |
||
8239 | 594 |
processAction (CheckBanned byIP) = do |
5184 | 595 |
clTime <- client's connectTime |
596 |
clNick <- client's nick |
|
597 |
clHost <- client's host |
|
598 |
si <- gets serverInfo |
|
599 |
let validBans = filter (checkNotExpired clTime) $ bans si |
|
8239 | 600 |
let ban = L.find (checkBan byIP clHost clNick) $ validBans |
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
601 |
mapM_ processAction $ |
5184 | 602 |
ModifyServerInfo (\s -> s{bans = validBans}) |
5426
109e9b5761c2
Implement command for banning by ip and a command for bans list
unc0rr
parents:
5214
diff
changeset
|
603 |
: [ByeClient (getBanReason $ fromJust ban) | isJust ban] |
5184 | 604 |
where |
605 |
checkNotExpired testTime (BanByIP _ _ time) = testTime `diffUTCTime` time <= 0 |
|
606 |
checkNotExpired testTime (BanByNick _ _ time) = testTime `diffUTCTime` time <= 0 |
|
8239 | 607 |
checkBan True ip _ (BanByIP bip _ _) = bip `B.isPrefixOf` ip |
608 |
checkBan False _ n (BanByNick bn _ _) = caseInsensitiveCompare bn n |
|
609 |
checkBan _ _ _ _ = False |
|
5184 | 610 |
getBanReason (BanByIP _ msg _) = msg |
611 |
getBanReason (BanByNick _ msg _) = msg |
|
612 |
||
613 |
processAction PingAll = do |
|
614 |
rnc <- gets roomsClients |
|
615 |
io (allClientsM rnc) >>= mapM_ (kickTimeouted rnc) |
|
616 |
cis <- io $ allClientsM rnc |
|
617 |
chans <- io $ mapM (client'sM rnc sendChan) cis |
|
618 |
io $ mapM_ (modifyClient rnc (\cl -> cl{pingsQueue = pingsQueue cl + 1})) cis |
|
619 |
processAction $ AnswerClients chans ["PING"] |
|
620 |
where |
|
621 |
kickTimeouted rnc ci = do |
|
622 |
pq <- io $ client'sM rnc pingsQueue ci |
|
7465
c2dcf97ca664
Okay, this is workaround over ping timeouts problem on the server. Could make server crash if recieve thread wakes up after second ping timeout event.
unc0rr
parents:
7351
diff
changeset
|
623 |
when (pq > 0) $ do |
5184 | 624 |
withStateT (\as -> as{clientIndex = Just ci}) $ |
8401
87410ae372f6
Server messages localization using Qt's l10n subsystem:
unc0rr
parents:
8396
diff
changeset
|
625 |
processAction (ByeClient $ loc "Ping timeout") |
7600
31a177d2856c
Disable workaround, as it still makes server crash and hung clients are hidden from players anyway
unc0rr
parents:
7537
diff
changeset
|
626 |
-- when (pq > 1) $ |
31a177d2856c
Disable workaround, as it still makes server crash and hung clients are hidden from players anyway
unc0rr
parents:
7537
diff
changeset
|
627 |
-- processAction $ DeleteClient ci -- smth went wrong with client io threads, issue DeleteClient here |
5184 | 628 |
|
629 |
||
630 |
processAction StatsAction = do |
|
5211 | 631 |
si <- gets serverInfo |
632 |
when (not $ shutdownPending si) $ do |
|
5212
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
633 |
rnc <- gets roomsClients |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
634 |
(roomsNum, clientsNum) <- io $ withRoomsAndClients rnc st |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
635 |
io $ writeChan (dbQueries si) $ SendStats clientsNum (roomsNum - 1) |
5184 | 636 |
where |
637 |
st irnc = (length $ allRooms irnc, length $ allClients irnc) |
|
638 |
||
7321
57bd4f201401
- Try sending remove message in 'finally' as a last resort
unc0rr
parents:
7126
diff
changeset
|
639 |
processAction RestartServer = do |
5212
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
640 |
sp <- gets (shutdownPending . serverInfo) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
641 |
when (not sp) $ do |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
642 |
sock <- gets (fromJust . serverSocket . serverInfo) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
643 |
args <- gets (runArgs . serverInfo) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
644 |
io $ do |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
645 |
noticeM "Core" "Closing listening socket" |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
646 |
sClose sock |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
647 |
noticeM "Core" "Spawning new server" |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
648 |
_ <- createProcess (proc "./hedgewars-server" args) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
649 |
return () |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
650 |
processAction $ ModifyServerInfo (\s -> s{shutdownPending = True}) |
5184 | 651 |
|
8403
fbc6e7602e05
- Allow server admins to use DELEGATE even when not room owner
unc0rr
parents:
8401
diff
changeset
|
652 |
processAction Stats = do |
fbc6e7602e05
- Allow server admins to use DELEGATE even when not room owner
unc0rr
parents:
8401
diff
changeset
|
653 |
cls <- allClientsS |
8452 | 654 |
rms <- allRoomsS |
655 |
let clientsMap = Map.fromListWith (+) . map (\c -> (clientProto c, 1 :: Int)) $ cls |
|
656 |
let roomsMap = Map.fromListWith (+) . map (\c -> (roomProto c, 1 :: Int)) . filter ((/=) 0 . roomProto) $ rms |
|
657 |
let keys = Map.keysSet clientsMap `Set.union` Map.keysSet roomsMap |
|
658 |
let versionsStats = B.concat . ((:) "<table border=1>") . (flip (++) ["</table>"]) |
|
659 |
. concatMap (\p -> [ |
|
660 |
"<tr><td>", protoNumber2ver p |
|
661 |
, "</td><td>", showB $ Map.findWithDefault 0 p clientsMap |
|
662 |
, "</td><td>", showB $ Map.findWithDefault 0 p roomsMap |
|
663 |
, "</td></tr>"]) |
|
664 |
. Set.toList $ keys |
|
665 |
processAction $ Warning versionsStats |
|
666 |
||
8403
fbc6e7602e05
- Allow server admins to use DELEGATE even when not room owner
unc0rr
parents:
8401
diff
changeset
|
667 |
|
6012 | 668 |
#if defined(OFFICIAL_SERVER) |
5184 | 669 |
processAction SaveReplay = do |
670 |
ri <- clientRoomA |
|
671 |
rnc <- gets roomsClients |
|
8371 | 672 |
|
5184 | 673 |
io $ do |
674 |
r <- room'sM rnc id ri |
|
675 |
saveReplay r |
|
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8476
diff
changeset
|
676 |
|
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8476
diff
changeset
|
677 |
|
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8476
diff
changeset
|
678 |
processAction CheckRecord = do |
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8476
diff
changeset
|
679 |
p <- client's clientProto |
8482 | 680 |
c <- client's sendChan |
681 |
l <- io $ loadReplay (fromIntegral p) |
|
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8476
diff
changeset
|
682 |
when (not $ null l) $ |
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8476
diff
changeset
|
683 |
processAction $ AnswerClients [c] ("REPLAY" : l) |
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8476
diff
changeset
|
684 |
|
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8476
diff
changeset
|
685 |
|
8507 | 686 |
processAction CheckRecord = do |
687 |
p <- client's clientProto |
|
688 |
c <- client's sendChan |
|
689 |
l <- io $ loadReplay (fromIntegral p) |
|
690 |
when (not $ null l) $ |
|
691 |
processAction $ AnswerClients [c] ("REPLAY" : l) |
|
692 |
||
693 |
processAction (CheckFailed msg) = return () |
|
694 |
||
695 |
processAction (CheckSuccess info) = return () |
|
696 |
||
6012 | 697 |
#else |
698 |
processAction SaveReplay = return () |
|
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8476
diff
changeset
|
699 |
processAction CheckRecord = return () |
8507 | 700 |
processAction (CheckFailed _) = return () |
701 |
processAction (CheckSuccess _) = return () |
|
6012 | 702 |
#endif |