Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
BanCommands.c
Go to the documentation of this file.
1 // Ban Subcommands Args
3 {
10 }
11 
12 // Ban Create Args
13 enum SCR_EBanCreateArgs
14 {
15  EBCA_COMMAND = 0,
17  EBCA_ID,
20  EBCA_END
21 }
22 
23 // Ban Remove Args
24 enum SCR_EBanRemoveArgs
25 {
26  EBRA_COMMAND = 0,
28  EBRA_ID,
29  EBRA_END
30 }
31 
32 // Ban List Args
33 enum SCR_EBanListArgs
34 {
35  EBLA_COMMAND = 0,
37  EBLA_PAGE,
38  EBLA_END
39 }
40 
41 // Ban Server Command
42 class BanCommand: ScrServerCommand
43 {
44  protected ref StateBackendCallback m_Callback;
45  protected ref BanListPageParams m_Params;
46  protected BanServiceApi m_BanApi;
48 
49  const static int BAN_LIST_ITEMS_PER_PAGE_CHAT = 10;
50  const static int BAN_LIST_ITEMS_PER_PAGE_RCON = 25;
51 
52  protected int m_iBanPlayerId;
53  protected int m_iDuration;
54  protected int m_iPage;
55  protected string m_sBanReason = "";
56  protected string m_sPlayerName = "";
57 
58  // Handle Create Subcommand
59  //-----------------------------------------------------------------------------
60  protected ScrServerCmdResult CreateBan(array<string> argv, int playerId)
61  {
62  // All args except Reason are required
63  if (argv.Count() < SCR_EBanCreateArgs.EBCA_REASON)
64  return ScrServerCmdResult(string.Empty, EServerCmdResultType.PARAMETERS);
65 
66  // if lenght of id is 36 then consider it as Identity Id. Otherwise try to get number as Player Id
67  m_iBanPlayerId = 0;
68  if (argv[SCR_EBanCreateArgs.EBCA_ID].Length() != 36)
69  m_iBanPlayerId = argv[SCR_EBanCreateArgs.EBCA_ID].ToInt();
70 
71  // Get duration for ban in Int from String
72  m_iDuration = argv[SCR_EBanCreateArgs.EBCA_DURATION].ToInt(-1);
73  if (m_iDuration < 0)
74  return ScrServerCmdResult("Not Valid time value in seconds", EServerCmdResultType.ERR);
75 
76  // If there are some additional args then combine them into reason string
77  m_sBanReason = "";
78  if (argv.Count() > SCR_EBanCreateArgs.EBCA_REASON)
79  {
80  for (int i = SCR_EBanCreateArgs.EBCA_REASON, count = argv.Count() - 1; i < count; i++)
81  {
82  m_sBanReason += argv[i] + " ";
83  }
84  m_sBanReason += argv[argv.Count() - 1];
85  }
86  else
87  {
88  m_sBanReason = "No reason provided"
89  }
90 
91  // Create Ban for Player Id in session or directly on Identity Id
92  m_sPlayerName = "";
93  if (m_iBanPlayerId > 0)
94  {
95  m_sPlayerName = GetGame().GetPlayerManager().GetPlayerName(m_iBanPlayerId);
96  if (m_sPlayerName == "")
97  return ScrServerCmdResult("Player not found", EServerCmdResultType.ERR);
98  PrintFormat("Request Ban %1[%2] for %3 seconds. Reason: '%4'", m_sPlayerName, m_iBanPlayerId, m_iDuration, m_sBanReason);
99  bool success = m_BanApi.CreateBanPlayerId(m_Callback, m_iBanPlayerId, m_sBanReason, m_iDuration);
100  if (!success)
101  return ScrServerCmdResult("Failed to create ban.", EServerCmdResultType.ERR);
102  }
103  else
104  {
105  // We dont know name of player so use his identityId as replacement
106  m_sPlayerName = argv[SCR_EBanCreateArgs.EBCA_ID];
107  PrintFormat("Request Ban %1 for %2 seconds. Reason: '%3'", argv[SCR_EBanCreateArgs.EBCA_ID], m_iDuration, m_sBanReason);
108  bool success = m_BanApi.CreateBanIdentityId(m_Callback, argv[SCR_EBanCreateArgs.EBCA_ID], m_sBanReason, m_iDuration);
109  if (!success)
110  return ScrServerCmdResult("Failed to create ban.", EServerCmdResultType.ERR);
111  }
112  return ScrServerCmdResult(string.Empty, EServerCmdResultType.PENDING);
113  }
114 
115  // Handle Remove Subcommand
116  //-----------------------------------------------------------------------------
117  protected ScrServerCmdResult RemoveBan(array<string> argv, int playerId)
118  {
119  // All args are required
120  if (argv.Count() < SCR_EBanRemoveArgs.EBRA_END)
121  return ScrServerCmdResult(string.Empty, EServerCmdResultType.PARAMETERS);
122 
123  PrintFormat("Unban user with identity: '%1'", argv[SCR_EBanRemoveArgs.EBRA_ID]);
124  bool success = m_BanApi.RemoveBans(m_Callback, {argv[SCR_EBanRemoveArgs.EBRA_ID]});
125  if (!success)
126  return ScrServerCmdResult("Failed to remove ban.", EServerCmdResultType.ERR);
127  return ScrServerCmdResult(string.Empty, EServerCmdResultType.PENDING);
128  }
129 
130  // Handle List Subcommand
131  //-----------------------------------------------------------------------------
132  protected ScrServerCmdResult ListBans(array<string> argv, int playerId)
133  {
134  // Get which page was requested. Show first page if unspecified
135  if (argv.Count() < SCR_EBanListArgs.EBLA_END)
136  {
137  m_iPage = 1;
138  }
139  else
140  {
141  m_iPage = argv[SCR_EBanListArgs.EBLA_PAGE].ToInt();
142  if (m_iPage < 1)
143  return ScrServerCmdResult("Not Valid page number", EServerCmdResultType.ERR);
144  }
145 
146  m_Params = new BanListPageParams();
147 
148  // Set different page limits for chat and RCON
149  if (playerId == 0)
150  m_Params.limit = BAN_LIST_ITEMS_PER_PAGE_RCON;
151  else
152  m_Params.limit = BAN_LIST_ITEMS_PER_PAGE_CHAT;
153  m_Params.offset = m_Params.limit * (m_iPage - 1);
154  bool success = m_BanApi.RequestServerBanList(m_Callback, m_Params);
155  if (!success)
156  return ScrServerCmdResult("Failed to list bans.", EServerCmdResultType.ERR);
157  return ScrServerCmdResult("Processing ban list", EServerCmdResultType.PENDING);
158  }
159 
160  // Handle result for List Subcommand
161  //-----------------------------------------------------------------------------
163  {
164  // Check if there are any bans
165  if (m_BanApi.GetPageCount() == 0)
166  return ScrServerCmdResult("Server has no bans to list.", EServerCmdResultType.OK);
167 
168  // Error if requested page exceeded page count
169  if ((m_BanApi.GetPage() + 1) > m_BanApi.GetPageCount())
170  return ScrServerCmdResult("Page not found!", EServerCmdResultType.ERR);
171 
172  // Generate string resposne for list
173  ref array<OnlineBanListData> banList = new array<OnlineBanListData>;
174  m_BanApi.GetPageItems(banList);
175  string banListStr = "Total bans: " + m_BanApi.GetTotalItemCount() + " | Page: " + (m_BanApi.GetPage() + 1) +"/" + m_BanApi.GetPageCount() + "\n";
176  banListStr += "- Identity Id | Banned name\n";
177  foreach (OnlineBanListData ban : banList)
178  {
179  banListStr += string.Format("- %1 | %2\n", ban.identityId, ban.bannedName);
180  }
181 
182  return ScrServerCmdResult(banListStr, EServerCmdResultType.OK);
183  }
184 
185  // Command handler for RCON and Chat
186  //-----------------------------------------------------------------------------
187  protected ScrServerCmdResult HandleCommand(array<string> argv, int playerId = 0)
188  {
189  if (RplSession.Mode() != RplMode.Dedicated)
190  return ScrServerCmdResult("Command is supported only on Dedicated Servers", EServerCmdResultType.ERR);
191 
192  BackendApi bApi = GetGame().GetBackendApi();
193  if (!bApi)
194  return ScrServerCmdResult(string.Empty, EServerCmdResultType.ERR);
195  m_BanApi = bApi.GetBanServiceApi();
196  if (!m_BanApi)
197  return ScrServerCmdResult(string.Empty, EServerCmdResultType.ERR);
198 
200  if (argv.Count() > 1)
201  {
202  if (argv[1] == "create")
204  else if (argv[1] == "remove")
206  else if (argv[1] == "list")
208  }
209 
210 
211  if (m_eSubcommandArg == SCR_EBanSubcommandArg.EBSA_MISSING)
213 
214  m_Callback = new StateBackendCallback;
215 
216  switch(m_eSubcommandArg)
217  {
218  // Handle Help
219  case SCR_EBanSubcommandArg.EBSA_HELP:
220  return ScrServerCmdResult("Help for ban command. \n#ban create <playerId> <duration> <reason> \n#ban create <identityId> <duration> <reason> \n#ban remove <identityId>\n#ban list <page>\n\n- <duration> is in seconds and can be set to 0 for permanent.\n- <reason> is optional.", EServerCmdResultType.OK);
221 
222  // Handle Create
223  case SCR_EBanSubcommandArg.EBSA_CREATE:
224  return CreateBan(argv, playerId);
225 
226  // Handle Remove
227  case SCR_EBanSubcommandArg.EBSA_REMOVE:
228  return RemoveBan(argv, playerId);
229 
230  // Handle List
231  case SCR_EBanSubcommandArg.EBSA_LIST:
232  return ListBans(argv, playerId);
233  }
234  return ScrServerCmdResult(string.Empty, EServerCmdResultType.ERR);
235  }
236 
237  // Specify keyword of command
238  //-----------------------------------------------------------------------------
239  override string GetKeyword()
240  {
241  return "ban";
242  }
243 
244  // Run command server-side
245  //-----------------------------------------------------------------------------
246  override bool IsServerSide()
247  {
248  return true;
249  }
250 
251  // Set requirement to admin permission via RCON
252  //-----------------------------------------------------------------------------
253  override int RequiredRCONPermission()
254  {
255  return ERCONPermissions.PERMISSIONS_ADMIN;
256  }
257 
258  // Set requirement to be logged in administrator for chat command
259  //-----------------------------------------------------------------------------
260  override int RequiredChatPermission()
261  {
262  return EPlayerRole.ADMINISTRATOR;
263  }
264 
265  // Handle Chat command on server
266  //-----------------------------------------------------------------------------
267  override ref ScrServerCmdResult OnChatServerExecution(array<string> argv, int playerId)
268  {
269  return HandleCommand(argv, playerId);
270  }
271 
272  // Handle Chat command on client
273  //-----------------------------------------------------------------------------
274  override ref ScrServerCmdResult OnChatClientExecution(array<string> argv, int playerId)
275  {
276  return ScrServerCmdResult(string.Empty, EServerCmdResultType.OK);
277  }
278 
279  // Handle RCON command on server
280  //-----------------------------------------------------------------------------
281  override ref ScrServerCmdResult OnRCONExecution(array<string> argv)
282  {
283  return HandleCommand(argv);
284  }
285 
286  // Handle successful result in OnUpdate()
287  //-----------------------------------------------------------------------------
289  {
290  switch(m_eSubcommandArg)
291  {
292  // Handle Create Result
293  case SCR_EBanSubcommandArg.EBSA_CREATE:
294  if (m_iBanPlayerId > 0)
295  GetGame().GetPlayerManager().KickPlayer(m_iBanPlayerId, PlayerManagerKickReason.BAN);
296  return ScrServerCmdResult(string.Format("Player '%1' banned!", m_sPlayerName), EServerCmdResultType.OK);
297 
298  // Handle Remove Result
299  case SCR_EBanSubcommandArg.EBSA_REMOVE:
300  return ScrServerCmdResult("Ban removed!", EServerCmdResultType.OK);
301 
302  // Handle List Result
303  case SCR_EBanSubcommandArg.EBSA_LIST:
304  return ListBansResult();
305  }
306  return ScrServerCmdResult(string.Empty, EServerCmdResultType.OK);
307  }
308 
309  // Handle Pending command
310  //-----------------------------------------------------------------------------
312  {
313  switch(m_Callback.m_eState)
314  {
315  case EBackendCallbackState.EBCS_SUCCESS: return HandleSuccessfulResult();
316  case EBackendCallbackState.EBCS_PENDING: return ScrServerCmdResult(string.Empty, EServerCmdResultType.PENDING);
317  case EBackendCallbackState.EBCS_TIMEOUT: return ScrServerCmdResult("Timeout", EServerCmdResultType.ERR);
318  }
319  return ScrServerCmdResult(string.Empty, EServerCmdResultType.ERR);
320  }
321 }
322 
323 
324 // Kick Server Command
326 {
327  // Specify keyword of command
328  //-----------------------------------------------------------------------------
329  override string GetKeyword()
330  {
331  return "kick";
332  }
333 
334  // Run command server-side
335  //-----------------------------------------------------------------------------
336  override bool IsServerSide()
337  {
338  return true;
339  }
340 
341  // Set requirement to admin permission via RCON
342  //-----------------------------------------------------------------------------
343  override int RequiredRCONPermission()
344  {
345  return ERCONPermissions.PERMISSIONS_ADMIN;
346  }
347 
348  // Set requirement to be logged in administrator for chat command
349  //-----------------------------------------------------------------------------
350  override int RequiredChatPermission()
351  {
352  return EPlayerRole.ADMINISTRATOR;
353  }
354 
355  // Shared handle for kicking player for RCON and Chat command
356  //-----------------------------------------------------------------------------
357  protected ScrServerCmdResult KickPlayer(array<string> argv, int playerId = 0)
358  {
359  if (argv.Count() < 2)
360  return ScrServerCmdResult(string.Empty, EServerCmdResultType.PARAMETERS);
361 
362  int kickPlayerId = argv[1].ToInt();
363  if (kickPlayerId < 1)
364  return ScrServerCmdResult("Not Valid Player ID", EServerCmdResultType.ERR);
365 
366  string kickPlayerName = GetGame().GetPlayerManager().GetPlayerName(kickPlayerId);
367  if (kickPlayerName == "")
368  return ScrServerCmdResult("Player not found", EServerCmdResultType.ERR);
369 
370  PrintFormat("Kick %1[%2]", kickPlayerName, kickPlayerId);
371  GetGame().GetPlayerManager().KickPlayer(kickPlayerId, PlayerManagerKickReason.KICK);
372 
373  return ScrServerCmdResult(string.Format("Player '%1' kicked!", kickPlayerName), EServerCmdResultType.OK);
374  }
375 
376  // Handle Chat command on server
377  //-----------------------------------------------------------------------------
378  override ref ScrServerCmdResult OnChatServerExecution(array<string> argv, int playerId)
379  {
380  return KickPlayer(argv, playerId);
381  }
382 
383  // Handle Chat command on client
384  //-----------------------------------------------------------------------------
385  override ref ScrServerCmdResult OnChatClientExecution(array<string> argv, int playerId)
386  {
387  return ScrServerCmdResult(string.Empty, EServerCmdResultType.OK);
388  }
389 
390  // Handle RCON command on server
391  //-----------------------------------------------------------------------------
392  override ref ScrServerCmdResult OnRCONExecution(array<string> argv)
393  {
394  return KickPlayer(argv);
395  }
396 
397  // Handle Pending command
398  //-----------------------------------------------------------------------------
399  override ref ScrServerCmdResult OnUpdate()
400  {
401  return ScrServerCmdResult(string.Empty, EServerCmdResultType.OK);
402  }
403 
404 }
RemoveBan
protected ScrServerCmdResult RemoveBan(array< string > argv, int playerId)
Definition: BanCommands.c:117
RequiredRCONPermission
override int RequiredRCONPermission()
Definition: BanCommands.c:253
OnUpdate
override ref ScrServerCmdResult OnUpdate()
Definition: BanCommands.c:311
EBackendCallbackState
EBackendCallbackState
Definition: UploadSaveCommand.c:1
EPlayerRole
EPlayerRole
Definition: EPlayerRole.c:7
EBCA_REASON
enum SCR_EBanSubcommandArg EBCA_REASON
m_iDuration
protected int m_iDuration
Definition: BanCommands.c:53
OnRCONExecution
override ref ScrServerCmdResult OnRCONExecution(array< string > argv)
Definition: BanCommands.c:281
EBRA_COMMAND
enum SCR_EBanSubcommandArg EBRA_COMMAND
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
m_sBanReason
protected string m_sBanReason
Definition: BanCommands.c:55
HandleCommand
protected ScrServerCmdResult HandleCommand(array< string > argv, int playerId=0)
Definition: BanCommands.c:187
m_Callback
enum SCR_EBanSubcommandArg m_Callback
m_Params
protected ref BanListPageParams m_Params
Definition: BanCommands.c:45
RequiredChatPermission
override int RequiredChatPermission()
Definition: BanCommands.c:260
EBSA_HELP
@ EBSA_HELP
Definition: BanCommands.c:5
EBLA_SUBCOMMAND
enum SCR_EBanSubcommandArg EBLA_SUBCOMMAND
m_iBanPlayerId
protected int m_iBanPlayerId
Definition: BanCommands.c:52
GetKeyword
override string GetKeyword()
Definition: BanCommands.c:239
EBCA_COMMAND
enum SCR_EBanSubcommandArg EBCA_COMMAND
EBLA_PAGE
enum SCR_EBanSubcommandArg EBLA_PAGE
m_eSubcommandArg
protected SCR_EBanSubcommandArg m_eSubcommandArg
Definition: BanCommands.c:47
EBSA_MISSING
@ EBSA_MISSING
Definition: BanCommands.c:4
m_BanApi
protected BanServiceApi m_BanApi
Definition: BanCommands.c:46
PlayerManagerKickReason
PlayerManagerKickReason
Definition: PlayerManagerKickReason.c:12
ScrServerCmdResult
Definition: ScrServerCmdResult.c:7
EBSA_LIST
@ EBSA_LIST
Definition: BanCommands.c:8
EBSA_END
@ EBSA_END
Definition: BanCommands.c:9
EBRA_ID
enum SCR_EBanSubcommandArg EBRA_ID
ScrServerCommand
Definition: ScrServerCommand.c:7
m_iPage
protected int m_iPage
Definition: BanCommands.c:54
IsServerSide
override bool IsServerSide()
Definition: BanCommands.c:246
CreateBan
protected ScrServerCmdResult CreateBan(array< string > argv, int playerId)
Definition: BanCommands.c:60
m_sPlayerName
protected string m_sPlayerName
Definition: BanCommands.c:56
SCR_EBanSubcommandArg
SCR_EBanSubcommandArg
Definition: BanCommands.c:2
OnChatClientExecution
override ref ScrServerCmdResult OnChatClientExecution(array< string > argv, int playerId)
Definition: BanCommands.c:274
EBLA_COMMAND
enum SCR_EBanSubcommandArg EBLA_COMMAND
ListBans
protected ScrServerCmdResult ListBans(array< string > argv, int playerId)
Definition: BanCommands.c:132
EBSA_CREATE
@ EBSA_CREATE
Definition: BanCommands.c:6
EBRA_SUBCOMMAND
enum SCR_EBanSubcommandArg EBRA_SUBCOMMAND
KickCommand
Definition: BanCommands.c:325
HandleSuccessfulResult
protected ScrServerCmdResult HandleSuccessfulResult()
Definition: BanCommands.c:288
ListBansResult
protected ScrServerCmdResult ListBansResult()
Definition: BanCommands.c:162
EBSA_REMOVE
@ EBSA_REMOVE
Definition: BanCommands.c:7
EBCA_DURATION
enum SCR_EBanSubcommandArg EBCA_DURATION
EBCA_ID
enum SCR_EBanSubcommandArg EBCA_ID
OnChatServerExecution
override ref ScrServerCmdResult OnChatServerExecution(array< string > argv, int playerId)
Definition: BanCommands.c:267
EBCA_SUBCOMMAND
enum SCR_EBanSubcommandArg EBCA_SUBCOMMAND