AdminTable.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server 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 Server 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 com.l2jserver.gameserver.datatables;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Map.Entry;
  25. import java.util.logging.Level;
  26. import javolution.util.FastMap;
  27. import org.w3c.dom.NamedNodeMap;
  28. import org.w3c.dom.Node;
  29. import com.l2jserver.Config;
  30. import com.l2jserver.gameserver.engines.DocumentParser;
  31. import com.l2jserver.gameserver.model.L2AccessLevel;
  32. import com.l2jserver.gameserver.model.L2AdminCommandAccessRight;
  33. import com.l2jserver.gameserver.model.StatsSet;
  34. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  35. import com.l2jserver.gameserver.network.SystemMessageId;
  36. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  37. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  38. /**
  39. * @author UnAfraid
  40. */
  41. public class AdminTable extends DocumentParser
  42. {
  43. private static final Map<Integer, L2AccessLevel> _accessLevels = new HashMap<>();
  44. private static final Map<String, L2AdminCommandAccessRight> _adminCommandAccessRights = new HashMap<>();
  45. private static final FastMap<L2PcInstance, Boolean> _gmList = new FastMap<>();
  46. private int _highestLevel = 0;
  47. /**
  48. * Instantiates a new admin table.
  49. */
  50. protected AdminTable()
  51. {
  52. _gmList.shared();
  53. load();
  54. }
  55. @Override
  56. public void load()
  57. {
  58. _accessLevels.clear();
  59. _adminCommandAccessRights.clear();
  60. parseDatapackFile("config/accessLevels.xml");
  61. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + _accessLevels.size() + " Access Levels");
  62. parseDatapackFile("config/adminCommands.xml");
  63. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + _adminCommandAccessRights.size() + " Access Commands");
  64. }
  65. @Override
  66. protected void parseDocument()
  67. {
  68. NamedNodeMap attrs;
  69. Node attr;
  70. StatsSet set;
  71. L2AccessLevel level;
  72. L2AdminCommandAccessRight command;
  73. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  74. {
  75. if ("list".equalsIgnoreCase(n.getNodeName()))
  76. {
  77. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  78. {
  79. if ("access".equalsIgnoreCase(d.getNodeName()))
  80. {
  81. set = new StatsSet();
  82. attrs = d.getAttributes();
  83. for (int i = 0; i < attrs.getLength(); i++)
  84. {
  85. attr = attrs.item(i);
  86. set.set(attr.getNodeName(), attr.getNodeValue());
  87. }
  88. level = new L2AccessLevel(set);
  89. if (level.getLevel() > _highestLevel)
  90. {
  91. _highestLevel = level.getLevel();
  92. }
  93. _accessLevels.put(level.getLevel(), level);
  94. }
  95. else if ("admin".equalsIgnoreCase(d.getNodeName()))
  96. {
  97. set = new StatsSet();
  98. attrs = d.getAttributes();
  99. for (int i = 0; i < attrs.getLength(); i++)
  100. {
  101. attr = attrs.item(i);
  102. set.set(attr.getNodeName(), attr.getNodeValue());
  103. }
  104. command = new L2AdminCommandAccessRight(set);
  105. _adminCommandAccessRights.put(command.getAdminCommand(), command);
  106. }
  107. }
  108. }
  109. }
  110. }
  111. /**
  112. * Returns the access level by characterAccessLevel.
  113. * @param accessLevelNum as int
  114. * @return the access level instance by char access level
  115. */
  116. public L2AccessLevel getAccessLevel(int accessLevelNum)
  117. {
  118. if (accessLevelNum < 0)
  119. {
  120. return _accessLevels.get(-1);
  121. }
  122. else if (!_accessLevels.containsKey(accessLevelNum))
  123. {
  124. _accessLevels.put(accessLevelNum, new L2AccessLevel());
  125. }
  126. return _accessLevels.get(accessLevelNum);
  127. }
  128. /**
  129. * Gets the master access level.
  130. * @return the master access level
  131. */
  132. public L2AccessLevel getMasterAccessLevel()
  133. {
  134. return _accessLevels.get(_highestLevel);
  135. }
  136. /**
  137. * Checks for access level.
  138. * @param id the id
  139. * @return {@code true}, if successful, {@code false} otherwise
  140. */
  141. public boolean hasAccessLevel(int id)
  142. {
  143. return _accessLevels.containsKey(id);
  144. }
  145. /**
  146. * Checks for access.
  147. * @param adminCommand the admin command
  148. * @param accessLevel the access level
  149. * @return {@code true}, if successful, {@code false} otherwise
  150. */
  151. public boolean hasAccess(String adminCommand, L2AccessLevel accessLevel)
  152. {
  153. L2AdminCommandAccessRight acar = _adminCommandAccessRights.get(adminCommand);
  154. if (acar == null)
  155. {
  156. // Trying to avoid the spam for next time when the gm would try to use the same command
  157. if ((accessLevel.getLevel() > 0) && (accessLevel.getLevel() == _highestLevel))
  158. {
  159. acar = new L2AdminCommandAccessRight(adminCommand, true, accessLevel.getLevel());
  160. _adminCommandAccessRights.put(adminCommand, acar);
  161. _log.info(getClass().getSimpleName() + ": No rights defined for admin command " + adminCommand + " auto setting accesslevel: " + accessLevel.getLevel() + " !");
  162. }
  163. else
  164. {
  165. _log.info(getClass().getSimpleName() + ": No rights defined for admin command " + adminCommand + " !");
  166. return false;
  167. }
  168. }
  169. return acar.hasAccess(accessLevel);
  170. }
  171. /**
  172. * Require confirm.
  173. * @param command the command
  174. * @return {@code true}, if the command require confirmation, {@code false} otherwise
  175. */
  176. public boolean requireConfirm(String command)
  177. {
  178. L2AdminCommandAccessRight acar = _adminCommandAccessRights.get(command);
  179. if (acar == null)
  180. {
  181. _log.info(getClass().getSimpleName() + ": No rights defined for admin command " + command + ".");
  182. return false;
  183. }
  184. return acar.getRequireConfirm();
  185. }
  186. /**
  187. * Gets the all GMs.
  188. * @param includeHidden the include hidden
  189. * @return the all GMs
  190. */
  191. public List<L2PcInstance> getAllGms(boolean includeHidden)
  192. {
  193. final List<L2PcInstance> tmpGmList = new ArrayList<>();
  194. for (Entry<L2PcInstance, Boolean> entry : _gmList.entrySet())
  195. {
  196. if (includeHidden || !entry.getValue())
  197. {
  198. tmpGmList.add(entry.getKey());
  199. }
  200. }
  201. return tmpGmList;
  202. }
  203. /**
  204. * Gets the all GM names.
  205. * @param includeHidden the include hidden
  206. * @return the all GM names
  207. */
  208. public List<String> getAllGmNames(boolean includeHidden)
  209. {
  210. final List<String> tmpGmList = new ArrayList<>();
  211. for (Entry<L2PcInstance, Boolean> entry : _gmList.entrySet())
  212. {
  213. if (!entry.getValue())
  214. {
  215. tmpGmList.add(entry.getKey().getName());
  216. }
  217. else if (includeHidden)
  218. {
  219. tmpGmList.add(entry.getKey().getName() + " (invis)");
  220. }
  221. }
  222. return tmpGmList;
  223. }
  224. /**
  225. * Add a L2PcInstance player to the Set _gmList.
  226. * @param player the player
  227. * @param hidden the hidden
  228. */
  229. public void addGm(L2PcInstance player, boolean hidden)
  230. {
  231. if (Config.DEBUG)
  232. {
  233. _log.fine("added gm: " + player.getName());
  234. }
  235. _gmList.put(player, hidden);
  236. }
  237. /**
  238. * Delete a GM.
  239. * @param player the player
  240. */
  241. public void deleteGm(L2PcInstance player)
  242. {
  243. if (Config.DEBUG)
  244. {
  245. _log.fine("deleted gm: " + player.getName());
  246. }
  247. _gmList.remove(player);
  248. }
  249. /**
  250. * GM will be displayed on clients GM list.
  251. * @param player the player
  252. */
  253. public void showGm(L2PcInstance player)
  254. {
  255. if (_gmList.containsKey(player))
  256. {
  257. _gmList.put(player, false);
  258. }
  259. }
  260. /**
  261. * GM will no longer be displayed on clients GM list.
  262. * @param player the player
  263. */
  264. public void hideGm(L2PcInstance player)
  265. {
  266. if (_gmList.containsKey(player))
  267. {
  268. _gmList.put(player, true);
  269. }
  270. }
  271. /**
  272. * Checks if is GM online.
  273. * @param includeHidden the include hidden
  274. * @return true, if is GM online
  275. */
  276. public boolean isGmOnline(boolean includeHidden)
  277. {
  278. for (Entry<L2PcInstance, Boolean> entry : _gmList.entrySet())
  279. {
  280. if (includeHidden || !entry.getValue())
  281. {
  282. return true;
  283. }
  284. }
  285. return false;
  286. }
  287. /**
  288. * Send list to player.
  289. * @param player the player
  290. */
  291. public void sendListToPlayer(L2PcInstance player)
  292. {
  293. if (isGmOnline(player.isGM()))
  294. {
  295. player.sendPacket(SystemMessageId.GM_LIST);
  296. for (String name : getAllGmNames(player.isGM()))
  297. {
  298. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.GM_C1);
  299. sm.addString(name);
  300. player.sendPacket(sm);
  301. }
  302. }
  303. else
  304. {
  305. player.sendPacket(SystemMessageId.NO_GM_PROVIDING_SERVICE_NOW);
  306. }
  307. }
  308. /**
  309. * Broadcast to GMs.
  310. * @param packet the packet
  311. */
  312. public void broadcastToGMs(L2GameServerPacket packet)
  313. {
  314. for (L2PcInstance gm : getAllGms(true))
  315. {
  316. gm.sendPacket(packet);
  317. }
  318. }
  319. /**
  320. * Broadcast message to GMs.
  321. * @param message the message
  322. */
  323. public void broadcastMessageToGMs(String message)
  324. {
  325. for (L2PcInstance gm : getAllGms(true))
  326. {
  327. gm.sendMessage(message);
  328. }
  329. }
  330. /**
  331. * Gets the single instance of AdminTable.
  332. * @return AccessLevels: the one and only instance of this class<br>
  333. */
  334. public static AdminTable getInstance()
  335. {
  336. return SingletonHolder._instance;
  337. }
  338. private static class SingletonHolder
  339. {
  340. protected static final AdminTable _instance = new AdminTable();
  341. }
  342. }