AdminBan.java 14 KB

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