AdminPunishment.java 11 KB

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