criss28282

Untitled

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