AdminBan.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Copyright (C) 2004-2013 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J DataPack is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package handlers.admincommandhandlers;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.SQLException;
  23. import java.util.StringTokenizer;
  24. import com.l2jserver.Config;
  25. import com.l2jserver.L2DatabaseFactory;
  26. import com.l2jserver.gameserver.LoginServerThread;
  27. import com.l2jserver.gameserver.communitybbs.Manager.RegionBBSManager;
  28. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  29. import com.l2jserver.gameserver.model.L2World;
  30. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  31. import com.l2jserver.gameserver.network.SystemMessageId;
  32. import com.l2jserver.gameserver.util.GMAudit;
  33. /**
  34. * This class handles following admin commands: - ban_acc <account_name> = changes account access level to -1 and logs him off. If no account is specified target's account is used. - ban_char <char_name> = changes a characters access level to -1 and logs him off. If no character is specified target
  35. * is used. - ban_chat <char_name> <duration> = chat bans a character for the specified duration. If no name is specified the target is chat banned indefinitely. - unban_acc <account_name> = changes account access level to 0. - unban_char <char_name> = changes specified characters access level to 0.
  36. * - unban_chat <char_name> = lifts chat ban from specified player. If no player name is specified current target is used. - jail charname [penalty_time] = jails character. Time specified in minutes. For ever if no time is specified. - unjail charname = Unjails player, teleport him to Floran.
  37. * @version $Revision: 1.1.6.3 $ $Date: 2005/04/11 10:06:06 $ con.close() change by Zoey76 24/02/2011
  38. */
  39. public class AdminBan implements IAdminCommandHandler
  40. {
  41. private static final String[] ADMIN_COMMANDS =
  42. {
  43. "admin_ban", // returns ban commands
  44. "admin_ban_acc",
  45. "admin_ban_char",
  46. "admin_ban_chat",
  47. "admin_unban", // returns unban commands
  48. "admin_unban_acc",
  49. "admin_unban_char",
  50. "admin_unban_chat",
  51. "admin_jail",
  52. "admin_unjail"
  53. };
  54. @Override
  55. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  56. {
  57. StringTokenizer st = new StringTokenizer(command);
  58. st.nextToken();
  59. String player = "";
  60. int duration = -1;
  61. L2PcInstance targetPlayer = null;
  62. if (st.hasMoreTokens())
  63. {
  64. player = st.nextToken();
  65. targetPlayer = L2World.getInstance().getPlayer(player);
  66. if (st.hasMoreTokens())
  67. {
  68. try
  69. {
  70. duration = Integer.parseInt(st.nextToken());
  71. }
  72. catch (NumberFormatException nfe)
  73. {
  74. activeChar.sendMessage("Invalid number format used: " + nfe);
  75. return false;
  76. }
  77. }
  78. }
  79. else
  80. {
  81. if ((activeChar.getTarget() != null) && activeChar.getTarget().isPlayer())
  82. {
  83. targetPlayer = activeChar.getTarget().getActingPlayer();
  84. }
  85. }
  86. if ((targetPlayer != null) && targetPlayer.equals(activeChar))
  87. {
  88. activeChar.sendPacket(SystemMessageId.CANNOT_USE_ON_YOURSELF);
  89. return false;
  90. }
  91. if (command.startsWith("admin_ban ") || command.equalsIgnoreCase("admin_ban"))
  92. {
  93. activeChar.sendMessage("Available ban commands: //ban_acc, //ban_char, //ban_chat");
  94. return false;
  95. }
  96. else if (command.startsWith("admin_ban_acc"))
  97. {
  98. // May need to check usage in admin_ban_menu as well.
  99. if ((targetPlayer == null) && player.isEmpty())
  100. {
  101. activeChar.sendMessage("Usage: //ban_acc <account_name> (if none, target char's account gets banned)");
  102. return false;
  103. }
  104. else if (targetPlayer == null)
  105. {
  106. LoginServerThread.getInstance().sendAccessLevel(player, -1);
  107. activeChar.sendMessage("Ban request sent for account " + player);
  108. auditAction(command, activeChar, player);
  109. }
  110. else
  111. {
  112. targetPlayer.setPunishLevel(L2PcInstance.PunishLevel.ACC, 0);
  113. activeChar.sendMessage("Account " + targetPlayer.getAccountName() + " banned.");
  114. auditAction(command, activeChar, targetPlayer.getAccountName());
  115. }
  116. }
  117. else if (command.startsWith("admin_ban_char"))
  118. {
  119. if ((targetPlayer == null) && player.isEmpty())
  120. {
  121. activeChar.sendMessage("Usage: //ban_char <char_name> (if none, target char is banned)");
  122. return false;
  123. }
  124. auditAction(command, activeChar, (targetPlayer == null ? player : targetPlayer.getName()));
  125. return changeCharAccessLevel(targetPlayer, player, activeChar, -1);
  126. }
  127. else if (command.startsWith("admin_ban_chat"))
  128. {
  129. if ((targetPlayer == null) && player.isEmpty())
  130. {
  131. activeChar.sendMessage("Usage: //ban_chat <char_name> [penalty_minutes]");
  132. return false;
  133. }
  134. if (targetPlayer != null)
  135. {
  136. if (targetPlayer.getPunishLevel().value() > 0)
  137. {
  138. activeChar.sendMessage(targetPlayer.getName() + " is already jailed or banned.");
  139. return false;
  140. }
  141. String banLengthStr = "";
  142. targetPlayer.setPunishLevel(L2PcInstance.PunishLevel.CHAT, duration);
  143. if (duration > 0)
  144. {
  145. banLengthStr = " for " + duration + " minutes";
  146. }
  147. activeChar.sendMessage(targetPlayer.getName() + " is now chat banned" + banLengthStr + ".");
  148. auditAction(command, activeChar, targetPlayer.getName());
  149. }
  150. else
  151. {
  152. banChatOfflinePlayer(activeChar, player, duration, true);
  153. auditAction(command, activeChar, player);
  154. }
  155. }
  156. else if (command.startsWith("admin_unban_chat"))
  157. {
  158. if ((targetPlayer == null) && player.isEmpty())
  159. {
  160. activeChar.sendMessage("Usage: //unban_chat <char_name>");
  161. return false;
  162. }
  163. if (targetPlayer != null)
  164. {
  165. if (targetPlayer.isChatBanned())
  166. {
  167. targetPlayer.setPunishLevel(L2PcInstance.PunishLevel.NONE, 0);
  168. activeChar.sendMessage(targetPlayer.getName() + "'s chat ban has now been lifted.");
  169. auditAction(command, activeChar, targetPlayer.getName());
  170. }
  171. else
  172. {
  173. activeChar.sendMessage(targetPlayer.getName() + " is not currently chat banned.");
  174. }
  175. }
  176. else
  177. {
  178. banChatOfflinePlayer(activeChar, player, 0, false);
  179. auditAction(command, activeChar, player);
  180. }
  181. }
  182. else if (command.startsWith("admin_unban ") || command.equalsIgnoreCase("admin_unban"))
  183. {
  184. activeChar.sendMessage("Available unban commands: //unban_acc, //unban_char, //unban_chat");
  185. return false;
  186. }
  187. else if (command.startsWith("admin_unban_acc"))
  188. {
  189. // Need to check admin_unban_menu command as well in AdminMenu.java handler.
  190. if (targetPlayer != null)
  191. {
  192. activeChar.sendMessage(targetPlayer.getName() + " is currently online so must not be banned.");
  193. return false;
  194. }
  195. else if (!player.isEmpty())
  196. {
  197. LoginServerThread.getInstance().sendAccessLevel(player, 0);
  198. activeChar.sendMessage("Unban request sent for account " + player);
  199. auditAction(command, activeChar, player);
  200. }
  201. else
  202. {
  203. activeChar.sendMessage("Usage: //unban_acc <account_name>");
  204. return false;
  205. }
  206. }
  207. else if (command.startsWith("admin_unban_char"))
  208. {
  209. if ((targetPlayer == null) && player.isEmpty())
  210. {
  211. activeChar.sendMessage("Usage: //unban_char <char_name>");
  212. return false;
  213. }
  214. else if (targetPlayer != null)
  215. {
  216. activeChar.sendMessage(targetPlayer.getName() + " is currently online so must not be banned.");
  217. return false;
  218. }
  219. else
  220. {
  221. auditAction(command, activeChar, player);
  222. return changeCharAccessLevel(null, player, activeChar, 0);
  223. }
  224. }
  225. else if (command.startsWith("admin_jail"))
  226. {
  227. if ((targetPlayer == null) && player.isEmpty())
  228. {
  229. activeChar.sendMessage("Usage: //jail <charname> [penalty_minutes] (if no name is given, selected target is jailed indefinitely)");
  230. return false;
  231. }
  232. if (targetPlayer != null)
  233. {
  234. if (targetPlayer.isFlyingMounted())
  235. {
  236. targetPlayer.untransform();
  237. }
  238. targetPlayer.setPunishLevel(L2PcInstance.PunishLevel.JAIL, duration);
  239. activeChar.sendMessage("Character " + targetPlayer.getName() + " jailed for " + (duration > 0 ? duration + " minutes." : "ever!"));
  240. auditAction(command, activeChar, targetPlayer.getName());
  241. }
  242. else
  243. {
  244. jailOfflinePlayer(activeChar, player, duration);
  245. auditAction(command, activeChar, player);
  246. }
  247. }
  248. else if (command.startsWith("admin_unjail"))
  249. {
  250. if ((targetPlayer == null) && player.isEmpty())
  251. {
  252. activeChar.sendMessage("Usage: //unjail <charname> (If no name is given target is used)");
  253. return false;
  254. }
  255. else if (targetPlayer != null)
  256. {
  257. targetPlayer.setPunishLevel(L2PcInstance.PunishLevel.NONE, 0);
  258. activeChar.sendMessage("Character " + targetPlayer.getName() + " removed from jail");
  259. auditAction(command, activeChar, targetPlayer.getName());
  260. }
  261. else
  262. {
  263. unjailOfflinePlayer(activeChar, player);
  264. auditAction(command, activeChar, player);
  265. }
  266. }
  267. return true;
  268. }
  269. private void auditAction(String fullCommand, L2PcInstance activeChar, String target)
  270. {
  271. if (!Config.GMAUDIT)
  272. {
  273. return;
  274. }
  275. String[] command = fullCommand.split(" ");
  276. GMAudit.auditGMAction(activeChar.getName() + " [" + activeChar.getObjectId() + "]", command[0], (target.isEmpty() ? "no-target" : target), (command.length > 2 ? command[2] : ""));
  277. }
  278. private void banChatOfflinePlayer(L2PcInstance activeChar, String name, int delay, boolean ban)
  279. {
  280. int level = 0;
  281. long value = 0;
  282. if (ban)
  283. {
  284. level = L2PcInstance.PunishLevel.CHAT.value();
  285. value = (delay > 0 ? delay * 60000L : 60000);
  286. }
  287. else
  288. {
  289. level = L2PcInstance.PunishLevel.NONE.value();
  290. value = 0;
  291. }
  292. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  293. {
  294. PreparedStatement statement = con.prepareStatement("UPDATE characters SET punish_level=?, punish_timer=? WHERE char_name=?");
  295. statement.setInt(1, level);
  296. statement.setLong(2, value);
  297. statement.setString(3, name);
  298. statement.execute();
  299. int count = statement.getUpdateCount();
  300. statement.close();
  301. if (count == 0)
  302. {
  303. activeChar.sendMessage("Character not found!");
  304. }
  305. else if (ban)
  306. {
  307. activeChar.sendMessage("Character " + name + " chat-banned for " + (delay > 0 ? delay + " minutes." : "ever!"));
  308. }
  309. else
  310. {
  311. activeChar.sendMessage("Character " + name + "'s chat-banned lifted");
  312. }
  313. }
  314. catch (SQLException se)
  315. {
  316. activeChar.sendMessage("SQLException while chat-banning player");
  317. if (Config.DEBUG)
  318. {
  319. se.printStackTrace();
  320. }
  321. }
  322. }
  323. private void jailOfflinePlayer(L2PcInstance activeChar, String name, int delay)
  324. {
  325. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  326. {
  327. PreparedStatement statement = con.prepareStatement("UPDATE characters SET x=?, y=?, z=?, punish_level=?, punish_timer=? WHERE char_name=?");
  328. statement.setInt(1, -114356);
  329. statement.setInt(2, -249645);
  330. statement.setInt(3, -2984);
  331. statement.setInt(4, L2PcInstance.PunishLevel.JAIL.value());
  332. statement.setLong(5, (delay > 0 ? delay * 60000L : 0));
  333. statement.setString(6, name);
  334. statement.execute();
  335. int count = statement.getUpdateCount();
  336. statement.close();
  337. if (count == 0)
  338. {
  339. activeChar.sendMessage("Character not found!");
  340. }
  341. else
  342. {
  343. activeChar.sendMessage("Character " + name + " jailed for " + (delay > 0 ? delay + " minutes." : "ever!"));
  344. }
  345. }
  346. catch (SQLException se)
  347. {
  348. activeChar.sendMessage("SQLException while jailing player");
  349. if (Config.DEBUG)
  350. {
  351. se.printStackTrace();
  352. }
  353. }
  354. }
  355. private void unjailOfflinePlayer(L2PcInstance activeChar, String name)
  356. {
  357. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  358. {
  359. PreparedStatement statement = con.prepareStatement("UPDATE characters SET x=?, y=?, z=?, punish_level=?, punish_timer=? WHERE char_name=?");
  360. statement.setInt(1, 17836);
  361. statement.setInt(2, 170178);
  362. statement.setInt(3, -3507);
  363. statement.setInt(4, 0);
  364. statement.setLong(5, 0);
  365. statement.setString(6, name);
  366. statement.execute();
  367. int count = statement.getUpdateCount();
  368. statement.close();
  369. if (count == 0)
  370. {
  371. activeChar.sendMessage("Character not found!");
  372. }
  373. else
  374. {
  375. activeChar.sendMessage("Character " + name + " removed from jail");
  376. }
  377. }
  378. catch (SQLException se)
  379. {
  380. activeChar.sendMessage("SQLException while jailing player");
  381. if (Config.DEBUG)
  382. {
  383. se.printStackTrace();
  384. }
  385. }
  386. }
  387. private boolean changeCharAccessLevel(L2PcInstance targetPlayer, String player, L2PcInstance activeChar, int lvl)
  388. {
  389. if (targetPlayer != null)
  390. {
  391. targetPlayer.setAccessLevel(lvl);
  392. targetPlayer.sendMessage("Your character has been banned. Goodbye.");
  393. targetPlayer.logout();
  394. RegionBBSManager.getInstance().changeCommunityBoard();
  395. activeChar.sendMessage("The character " + targetPlayer.getName() + " has now been banned.");
  396. }
  397. else
  398. {
  399. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  400. {
  401. PreparedStatement statement = con.prepareStatement("UPDATE characters SET accesslevel=? WHERE char_name=?");
  402. statement.setInt(1, lvl);
  403. statement.setString(2, player);
  404. statement.execute();
  405. int count = statement.getUpdateCount();
  406. statement.close();
  407. if (count == 0)
  408. {
  409. activeChar.sendMessage("Character not found or access level unaltered.");
  410. return false;
  411. }
  412. activeChar.sendMessage(player + " now has an access level of " + lvl);
  413. }
  414. catch (SQLException se)
  415. {
  416. activeChar.sendMessage("SQLException while changing character's access level");
  417. if (Config.DEBUG)
  418. {
  419. se.printStackTrace();
  420. }
  421. return false;
  422. }
  423. }
  424. return true;
  425. }
  426. @Override
  427. public String[] getAdminCommandList()
  428. {
  429. return ADMIN_COMMANDS;
  430. }
  431. }