forked from greghaynes/xsbs
/
servercommands.py
332 lines (274 loc) · 9.47 KB
/
servercommands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""This file contains all the basic commands that the server needs to be playable, this does not handle '/' type requests from clients only server-side cubescript commands"""
import sbserver
import os
from datetime import timedelta, datetime
from hyperserv.events import eventHandler, triggerServerEvent
from hypershade.config import config
from hypershade.cubescript import systemCS, CSCommand, playerCS
from hypershade.usersession import UserSessionManager, PermissionError
from hypershade.util import ipLongToString, modeNumber, mastermodeNumber, formatCaller
from hypershade.files import openfile
from hypershade.bandatabase import bandatabase
class ServerError(Exception): pass
@CSCommand("vote")
def voteMap(caller,name,mode=None):
"""Places a vote for the map of choice. This can be used to call maps from /storage/maps."""
if mode is None:
mode=sbserver.gameMode()
triggerServerEvent("vote_map",[caller,modeNumber(mode),name])
@CSCommand("map","master")
def changeMap(caller,name,mode=None):
"""Same as the /map command."""
if mode is None:
mode=sbserver.gameMode()
mode=modeNumber(mode)
sbserver.setMap(name,mode)
#load the map if it exists and mode is coop
if modeNumber("coop")==mode:
try:
loadmap(caller,name)
except:
pass
return name
@CSCommand("master","master")
def setMaster(caller):
"""Makes the caller the master of the server."""
if(caller[0]=="ingame"):
return sbserver.setMaster(caller[1])
raise ServerError("You are not ingame.")
return
@CSCommand("admin","admin")
def setAdmin(caller):
"""Makes the caller the admin of the server."""
if(caller[0]=="ingame"):
return sbserver.setAdmin(caller[1])
raise ServerError("You are not ingame.")
return
@CSCommand("relinquish")
def relinquish(caller):
"""Gives up the level of power that the player has (master or admin)."""
if(caller[0]=="ingame"):
return sbserver.resetPrivilege(caller[1])
raise ServerError("You are not ingame.")
return
@CSCommand("kick","master")
def kick(caller,cn):
"""Kicks another player; however, this command does not work on players with higher permission. Kicking a player also gives them a 60 minute ban."""
cn=int(cn)
try:
UserSessionManager.checkPermissions(caller,UserSessionManager[("ingame",cn)][1]) #check if the other person is more privileged
except PermissionError:
triggerServerEvent("player_kick_failed",[caller,cn])
raise
ban(caller,sbserver.playerName(cn),"kicked by %s" % formatCaller(caller))
triggerServerEvent("player_kicked",[caller,cn])
return sbserver.playerKick(cn)
@CSCommand("sendto","master")
def sendto(caller,cn):
"""Forces the specified player to /getmap."""
cn=int(cn)
return sbserver.sendMapTo(cn)
@CSCommand("spectator")
def spectator(caller,boolean=None,cn=None):
"""Sets spectator for the given cn. If the cn is left off it applies to the caller."""
#empty args
if boolean is None:
boolean=1
boolean=int(boolean)
if cn is None:
if(caller[0]=="ingame"):
cn=caller[1]
else:
raise ServerError("You are not ingame. Please specify cn.")
cn=int(cn)
#check if it's a self call
if caller[1]==cn:
if boolean==0 and sbserver.masterMode()>=2:
UserSessionManager.checkPermissions(caller,"master")
spectatorHelpler(boolean,cn)
else:
UserSessionManager.checkPermissions(caller,"master")
spectatorHelpler(boolean,cn)
def spectatorHelpler(boolean,cn):
if boolean:
sbserver.spectate(cn)
else:
sbserver.unspectate(cn)
@CSCommand("editmute","master")
def editmute(caller,boolean=None,cn=None):
"""Sets an editmute on the given cn. If the cn is left off it applies to the caller."""
#empty args
if boolean is None:
boolean=1
if cn is None:
if(caller[0]=="ingame"):
cn=caller[1]
else:
raise ServerError("You are not ingame. Please specify cn.")
cn=int(cn)
boolean=int(boolean)
if boolean:
spectator(caller,0,cn)
sbserver.editMute(cn)
triggerServerEvent("player_editmuted",[cn])
else:
sbserver.editUnmute(cn)
triggerServerEvent("player_editunmuted",[cn])
@CSCommand("mastermode","master")
def masterMode(caller,name=None):
"""Changes the mastermode of the server."""
if name==None:
return sbserver.masterMode()
mastermode=mastermodeNumber(name)
if config["serverpublic"]=="1":
if mastermode>=2:
UserSessionManager.checkPermissions(caller,"trusted")
if config["serverpublic"]=="2":
if mastermode>=3:
UserSessionManager.checkPermissions(caller,"trusted")
return sbserver.setMasterMode(mastermode)
@CSCommand("who")
def who(caller,where="ingame"):
"""Gives info on all the people logged in to the server. This includes: name, cn and IP address. Use this in conjuction with #echo. Ex: #echo (who)"""
def cndetails(cn):
return sbserver.playerName(cn)+" (cn"+str(cn)+"/"+ipLongToString(sbserver.playerIpLong(cn))+")"
clientlist=[session[1] for session in UserSessionManager.keys() if session[0] == where]
if where=="ingame":
clientlist='; '.join(map(cndetails,clientlist))
if clientlist!="" or clientlist!=[]:
return str(clientlist)
else:
return "No clients for %s." % where
@CSCommand("list")
def listCommands(caller,which="hyperserv"):
"""Lists all commands available. Use this in conjuction with #echo. Ex: #echo (list)"""
if which=="all":
commands=systemCS.functions.keys()+systemCS.external.keys()
elif which=="cubescript":
commands=systemCS.functions.keys()
else:
commands=systemCS.external.keys()
commands.sort()
return ' '.join(commands)
@CSCommand("say","admin")
def say(caller,*what):
"""Causes the server to say the input, simialar to #notice, without "Notice from PlayerName: MESSAGE HERE"""
string=' '.join(map(str,what))
triggerServerEvent("say",[string])
return string
@CSCommand("echo")
def echo(caller,*what):
"""Displays the output from commands. Examples: listusersessions, list, user, who, whoami. Note that the command must be in parentheses. #echo (command)"""
string=' '.join(map(str,what))
triggerServerEvent("echo",[caller,string])
return string
@CSCommand("ban","trusted")
def ban(caller,who=None,reason="",time="60"):
"""Bans the person specified. If time is "perm","permanent","permanently","0" or 0 then the ban will be permanent. The default ban time is 60 minutes."""
if who is None:
return
try:
who=sbserver.playerName(int(who))
except ValueError:
pass
if time[-1]=="d":
time=int(time[:1])*1440
if time in ["perm","permanent","permanently","0",0]:
expires=None
else:
expires=datetime.utcnow()+timedelta(0,int(time)*60)
bandatabase[who]=(expires,reason)
@CSCommand("bans","master")
def bans(caller):
"""Returns all of the current bans in effect. Use this in conjuction with #echo. Ex: #echo (bans)"""
return bandatabase
@CSCommand("delban","trusted")
def delban(caller,who):
"""Deletes a username from the ban database."""
del bandatabase[who]
@CSCommand("clearbans", "master")
def clearbans(caller):
"""Clears all of the bans ending within the next hour."""
bandatabase.clearbans()
@CSCommand("minsleft")
def minsleft(caller,time=None):
"""Sets the amount of time remianing on the map. This can also be used in conjuction with #echo. Ex: #echo (minsleft)"""
if time is not None:
UserSessionManager.checkPermissions(caller,"trusted")
sbserver.setMinsRemaining(int(time))
return sbserver.minutesRemaining()/60 #todo, fix the api, it shouldn't return seconds...
@CSCommand("team")
def team(caller,*args):
"""Allows players to switch teams just like with /team."""
if(len(args)==1):
if(caller[0]=="ingame"):
cn=caller[1]
else:
raise ServerError("You are not ingame. Please specify cn.")
elif(len(args)==2):
cn=args[0]
else:
raise TypeError("team takes either 1 or 2 arguments.")
cn=int(cn)
teamname=args[-1]
if cn!=caller[1]:
UserSessionManager.checkPermissions(caller,"master")
sbserver.setTeam(cn,teamname)
sbserver.suicide(cn)
@CSCommand("mute","master")
def mute(caller,*args):
"""Sets a mute on the given cn. If the cn is left off it applies to the caller."""
if(len(args)==1):
boolean=1
cn=args[0]
elif(len(args)==2):
boolean=args[0]
cn=args[1]
else:
raise TypeError("mute takes either 1 or 2 arguments.")
boolean=int(boolean)
cn=int(cn)
triggerServerEvent("player_muted",[caller,boolean,cn])
@CSCommand("savemap","trusted")
def savemap(caller,name=None):
"""Saves the map to the server. Note that the map must be sent prior to saving."""
if name is None:
name=sbserver.mapName()
ogzfilename,ogz=openfile(os.path.join("maps",name+".ogz"),"wb")
mapdata=sbserver.getMapDataFile()
mapdata.seek(0)
ogz.write(mapdata.read())
ogz.close()
triggerServerEvent("savemap",[caller,name,ogzfilename])
@CSCommand("loadmap","master")
def loadmap(caller,name=None):
"""Loads a map from the server. This is automatically executed when you change to a map that the server has in storage."""
if name is None:
name=sbserver.mapName()
ogzfilename,ogz=openfile(os.path.join("maps",name+".ogz"),"rb")
mapdata=sbserver.getMapDataFile()
mapdata.seek(0)
mapdata.truncate(0)
mapdata.write(ogz.read())
mapdata.flush()
ogz.close()
triggerServerEvent("loadmap",[caller,name,ogzfilename])
def color(number, string):
return '\fs\f' + str(number) + string + '\fr'
@CSCommand("pm")
def CSserverPM(caller, cn, *what):
"""Allows players to pm other players."""
string=' '.join(map(str,what))
try:
cn = int(cn)
reciver = ("ingame", cn)
if caller[0] == "ingame":
namecn = " ("+str(caller[1])+"): "
else:
namecn = ""
string1 = formatCaller(caller) + namecn
newstring = color(3, string1+string)
playerCS.executeby(reciver,"echo \"%s\"" %newstring)
except ValueError:
triggerServerEvent("pm",[caller,cn,string])
return string