AdminPunishment.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Copyright (C) 2004-2015 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.net.InetAddress;
  21. import java.net.UnknownHostException;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Date;
  24. import java.util.StringTokenizer;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.gameserver.cache.HtmCache;
  29. import com.l2jserver.gameserver.data.sql.impl.CharNameTable;
  30. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  31. import com.l2jserver.gameserver.instancemanager.PunishmentManager;
  32. import com.l2jserver.gameserver.model.L2World;
  33. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  34. import com.l2jserver.gameserver.model.punishment.PunishmentAffect;
  35. import com.l2jserver.gameserver.model.punishment.PunishmentTask;
  36. import com.l2jserver.gameserver.model.punishment.PunishmentType;
  37. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  38. import com.l2jserver.gameserver.util.GMAudit;
  39. import com.l2jserver.gameserver.util.Util;
  40. /**
  41. * @author UnAfraid
  42. */
  43. public class AdminPunishment implements IAdminCommandHandler
  44. {
  45. private static final Logger _log = Logger.getLogger(AdminPunishment.class.getName());
  46. private static final String[] ADMIN_COMMANDS =
  47. {
  48. "admin_punishment",
  49. "admin_punishment_add",
  50. "admin_punishment_remove",
  51. "admin_ban_acc",
  52. "admin_unban_acc",
  53. "admin_ban_chat",
  54. "admin_unban_chat",
  55. "admin_ban_char",
  56. "admin_unban_char",
  57. "admin_jail",
  58. "admin_unjail"
  59. };
  60. private static SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
  61. @Override
  62. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  63. {
  64. final StringTokenizer st = new StringTokenizer(command, " ");
  65. if (!st.hasMoreTokens())
  66. {
  67. return false;
  68. }
  69. final String cmd = st.nextToken();
  70. switch (cmd)
  71. {
  72. case "admin_punishment":
  73. {
  74. if (!st.hasMoreTokens())
  75. {
  76. String content = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/admin/punishment.htm");
  77. if (content != null)
  78. {
  79. content = content.replaceAll("%punishments%", Util.implode(PunishmentType.values(), ";"));
  80. content = content.replaceAll("%affects%", Util.implode(PunishmentAffect.values(), ";"));
  81. activeChar.sendPacket(new NpcHtmlMessage(0, 1, content));
  82. }
  83. else
  84. {
  85. _log.log(Level.WARNING, getClass().getSimpleName() + ": data/html/admin/punishment.htm is missing");
  86. }
  87. }
  88. else
  89. {
  90. final String subcmd = st.nextToken();
  91. switch (subcmd)
  92. {
  93. case "info":
  94. {
  95. String key = st.hasMoreTokens() ? st.nextToken() : null;
  96. String af = st.hasMoreTokens() ? st.nextToken() : null;
  97. String name = key;
  98. if ((key == null) || (af == null))
  99. {
  100. activeChar.sendMessage("Not enough data specified!");
  101. break;
  102. }
  103. final PunishmentAffect affect = PunishmentAffect.getByName(af);
  104. if (affect == null)
  105. {
  106. activeChar.sendMessage("Incorrect value specified for affect type!");
  107. break;
  108. }
  109. // Swap the name of the character with it's id.
  110. if (affect == PunishmentAffect.CHARACTER)
  111. {
  112. key = findCharId(key);
  113. }
  114. String content = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/admin/punishment-info.htm");
  115. if (content != null)
  116. {
  117. StringBuilder sb = new StringBuilder();
  118. for (PunishmentType type : PunishmentType.values())
  119. {
  120. if (PunishmentManager.getInstance().hasPunishment(key, affect, type))
  121. {
  122. long expiration = PunishmentManager.getInstance().getPunishmentExpiration(key, affect, type);
  123. String expire = "never";
  124. if (expiration > 0)
  125. {
  126. // Synchronize date formatter since its not thread safe.
  127. synchronized (DATE_FORMATTER)
  128. {
  129. expire = DATE_FORMATTER.format(new Date(expiration));
  130. }
  131. }
  132. sb.append("<tr><td><font color=\"LEVEL\">" + type + "</font></td><td>" + expire + "</td><td><a action=\"bypass -h admin_punishment_remove " + name + " " + affect + " " + type + "\">Remove</a></td></tr>");
  133. }
  134. }
  135. content = content.replaceAll("%player_name%", name);
  136. content = content.replaceAll("%punishments%", sb.toString());
  137. content = content.replaceAll("%affects%", Util.implode(PunishmentAffect.values(), ";"));
  138. content = content.replaceAll("%affect_type%", affect.name());
  139. activeChar.sendPacket(new NpcHtmlMessage(0, 1, content));
  140. }
  141. else
  142. {
  143. _log.log(Level.WARNING, getClass().getSimpleName() + ": data/html/admin/punishment-info.htm is missing");
  144. }
  145. break;
  146. }
  147. case "player":
  148. {
  149. L2PcInstance target = null;
  150. if (st.hasMoreTokens())
  151. {
  152. final String playerName = st.nextToken();
  153. if (playerName.isEmpty() && ((activeChar.getTarget() == null) || !activeChar.getTarget().isPlayer()))
  154. {
  155. return useAdminCommand("admin_punishment", activeChar);
  156. }
  157. target = L2World.getInstance().getPlayer(playerName);
  158. }
  159. if ((target == null) && ((activeChar.getTarget() == null) || !activeChar.getTarget().isPlayer()))
  160. {
  161. activeChar.sendMessage("You must target player!");
  162. break;
  163. }
  164. if (target == null)
  165. {
  166. target = activeChar.getTarget().getActingPlayer();
  167. }
  168. String content = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/admin/punishment-player.htm");
  169. if (content != null)
  170. {
  171. content = content.replaceAll("%player_name%", target.getName());
  172. content = content.replaceAll("%punishments%", Util.implode(PunishmentType.values(), ";"));
  173. content = content.replaceAll("%acc%", target.getAccountName());
  174. content = content.replaceAll("%char%", target.getName());
  175. content = content.replaceAll("%ip%", target.getIPAddress());
  176. activeChar.sendPacket(new NpcHtmlMessage(0, 1, content));
  177. }
  178. else
  179. {
  180. _log.log(Level.WARNING, getClass().getSimpleName() + ": data/html/admin/punishment-player.htm is missing");
  181. }
  182. break;
  183. }
  184. }
  185. }
  186. break;
  187. }
  188. case "admin_punishment_add":
  189. {
  190. // Add new punishment
  191. String key = st.hasMoreTokens() ? st.nextToken() : null;
  192. String af = st.hasMoreTokens() ? st.nextToken() : null;
  193. String t = st.hasMoreTokens() ? st.nextToken() : null;
  194. String exp = st.hasMoreTokens() ? st.nextToken() : null;
  195. String reason = st.hasMoreTokens() ? st.nextToken() : null;
  196. // Let's grab the other part of the reason if there is..
  197. if (reason != null)
  198. {
  199. while (st.hasMoreTokens())
  200. {
  201. reason += " " + st.nextToken();
  202. }
  203. if (!reason.isEmpty())
  204. {
  205. reason = reason.replaceAll("\\$", "\\\\\\$");
  206. reason = reason.replaceAll("\r\n", "<br1>");
  207. reason = reason.replace("<", "&lt;");
  208. reason = reason.replace(">", "&gt;");
  209. }
  210. }
  211. String name = key;
  212. if ((key == null) || (af == null) || (t == null) || (exp == null) || (reason == null))
  213. {
  214. activeChar.sendMessage("Please fill all the fields!");
  215. break;
  216. }
  217. if (!Util.isDigit(exp) && !exp.equals("-1"))
  218. {
  219. activeChar.sendMessage("Incorrect value specified for expiration time!");
  220. break;
  221. }
  222. long expirationTime = Integer.parseInt(exp);
  223. if (expirationTime > 0)
  224. {
  225. expirationTime = System.currentTimeMillis() + (expirationTime * 60 * 1000);
  226. }
  227. final PunishmentAffect affect = PunishmentAffect.getByName(af);
  228. final PunishmentType type = PunishmentType.getByName(t);
  229. if ((affect == null) || (type == null))
  230. {
  231. activeChar.sendMessage("Incorrect value specified for affect/punishment type!");
  232. break;
  233. }
  234. // Swap the name of the character with it's id.
  235. if (affect == PunishmentAffect.CHARACTER)
  236. {
  237. key = findCharId(key);
  238. }
  239. else if (affect == PunishmentAffect.IP)
  240. {
  241. try
  242. {
  243. InetAddress addr = InetAddress.getByName(key);
  244. if (addr.isLoopbackAddress())
  245. {
  246. throw new UnknownHostException("You cannot ban any local address!");
  247. }
  248. else if (Config.GAME_SERVER_HOSTS.contains(addr.getHostAddress()))
  249. {
  250. throw new UnknownHostException("You cannot ban your gameserver's address!");
  251. }
  252. }
  253. catch (UnknownHostException e)
  254. {
  255. activeChar.sendMessage("You've entered an incorrect IP address!");
  256. activeChar.sendMessage(e.getMessage());
  257. break;
  258. }
  259. }
  260. // Check if we already put the same punishment on that guy ^^
  261. if (PunishmentManager.getInstance().hasPunishment(key, affect, type))
  262. {
  263. activeChar.sendMessage("Target is already affected by that punishment.");
  264. break;
  265. }
  266. // Punish him!
  267. PunishmentManager.getInstance().startPunishment(new PunishmentTask(key, affect, type, expirationTime, reason, activeChar.getName()));
  268. activeChar.sendMessage("Punishment " + type.name() + " have been applied to: " + affect + " " + name + "!");
  269. GMAudit.auditGMAction(activeChar.getName() + " [" + activeChar.getObjectId() + "]", cmd, affect.name(), name);
  270. return useAdminCommand("admin_punishment info " + name + " " + affect.name(), activeChar);
  271. }
  272. case "admin_punishment_remove":
  273. {
  274. // Remove punishment.
  275. String key = st.hasMoreTokens() ? st.nextToken() : null;
  276. String af = st.hasMoreTokens() ? st.nextToken() : null;
  277. String t = st.hasMoreTokens() ? st.nextToken() : null;
  278. String name = key;
  279. if ((key == null) || (af == null) || (t == null))
  280. {
  281. activeChar.sendMessage("Not enough data specified!");
  282. break;
  283. }
  284. final PunishmentAffect affect = PunishmentAffect.getByName(af);
  285. final PunishmentType type = PunishmentType.getByName(t);
  286. if ((affect == null) || (type == null))
  287. {
  288. activeChar.sendMessage("Incorrect value specified for affect/punishment type!");
  289. break;
  290. }
  291. // Swap the name of the character with it's id.
  292. if (affect == PunishmentAffect.CHARACTER)
  293. {
  294. key = findCharId(key);
  295. }
  296. if (!PunishmentManager.getInstance().hasPunishment(key, affect, type))
  297. {
  298. activeChar.sendMessage("Target is not affected by that punishment!");
  299. break;
  300. }
  301. PunishmentManager.getInstance().stopPunishment(key, affect, type);
  302. activeChar.sendMessage("Punishment " + type.name() + " have been stopped to: " + affect + " " + name + "!");
  303. GMAudit.auditGMAction(activeChar.getName() + " [" + activeChar.getObjectId() + "]", cmd, affect.name(), name);
  304. return useAdminCommand("admin_punishment info " + name + " " + affect.name(), activeChar);
  305. }
  306. case "admin_ban_char":
  307. {
  308. if (st.hasMoreTokens())
  309. {
  310. return useAdminCommand(String.format("admin_punishment_add %s %s %s %s %s", st.nextToken(), PunishmentAffect.CHARACTER, PunishmentType.BAN, 0, "Banned by admin"), activeChar);
  311. }
  312. }
  313. case "admin_unban_char":
  314. {
  315. if (st.hasMoreTokens())
  316. {
  317. return useAdminCommand(String.format("admin_punishment_remove %s %s %s", st.nextToken(), PunishmentAffect.CHARACTER, PunishmentType.BAN), activeChar);
  318. }
  319. }
  320. case "admin_ban_acc":
  321. {
  322. if (st.hasMoreTokens())
  323. {
  324. return useAdminCommand(String.format("admin_punishment_add %s %s %s %s %s", st.nextToken(), PunishmentAffect.ACCOUNT, PunishmentType.BAN, 0, "Banned by admin"), activeChar);
  325. }
  326. }
  327. case "admin_unban_acc":
  328. {
  329. if (st.hasMoreTokens())
  330. {
  331. return useAdminCommand(String.format("admin_punishment_remove %s %s %s", st.nextToken(), PunishmentAffect.ACCOUNT, PunishmentType.BAN), activeChar);
  332. }
  333. }
  334. case "admin_ban_chat":
  335. {
  336. if (st.hasMoreTokens())
  337. {
  338. return useAdminCommand(String.format("admin_punishment_add %s %s %s %s %s", st.nextToken(), PunishmentAffect.CHARACTER, PunishmentType.CHAT_BAN, 0, "Chat banned by admin"), activeChar);
  339. }
  340. }
  341. case "admin_unban_chat":
  342. {
  343. if (st.hasMoreTokens())
  344. {
  345. return useAdminCommand(String.format("admin_punishment_remove %s %s %s", st.nextToken(), PunishmentAffect.CHARACTER, PunishmentType.CHAT_BAN), activeChar);
  346. }
  347. }
  348. case "admin_jail":
  349. {
  350. if (st.hasMoreTokens())
  351. {
  352. return useAdminCommand(String.format("admin_punishment_add %s %s %s %s %s", st.nextToken(), PunishmentAffect.CHARACTER, PunishmentType.JAIL, 0, "Jailed by admin"), activeChar);
  353. }
  354. }
  355. case "admin_unjail":
  356. {
  357. if (st.hasMoreTokens())
  358. {
  359. return useAdminCommand(String.format("admin_punishment_remove %s %s %s", st.nextToken(), PunishmentAffect.CHARACTER, PunishmentType.JAIL), activeChar);
  360. }
  361. }
  362. }
  363. return true;
  364. }
  365. private static final String findCharId(String key)
  366. {
  367. int charId = CharNameTable.getInstance().getIdByName(key);
  368. if (charId > 0) // Yeah its a char name!
  369. {
  370. return Integer.toString(charId);
  371. }
  372. return key;
  373. }
  374. @Override
  375. public String[] getAdminCommandList()
  376. {
  377. return ADMIN_COMMANDS;
  378. }
  379. }