AdminTable.java 9.6 KB

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