AdminTable.java 9.5 KB

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