AdminBan.java 15 KB

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