CommunityBoardHandler.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (C) 2004-2015 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.handler;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import java.util.concurrent.ConcurrentHashMap;
  23. import java.util.logging.Logger;
  24. import com.l2jserver.Config;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.network.SystemMessageId;
  27. import com.l2jserver.gameserver.util.Util;
  28. /**
  29. * Community Board handler.
  30. * @author Zoey76
  31. */
  32. public final class CommunityBoardHandler implements IHandler<IParseBoardHandler, String>
  33. {
  34. private static final Logger LOG = Logger.getLogger(CommunityBoardHandler.class.getName());
  35. /** The registered handlers. */
  36. private final Map<String, IParseBoardHandler> _datatable = new HashMap<>();
  37. /** The bypasses used by the players. */
  38. private final Map<Integer, String> _bypasses = new ConcurrentHashMap<>();
  39. protected CommunityBoardHandler()
  40. {
  41. // Prevent external initialization.
  42. }
  43. @Override
  44. public void registerHandler(IParseBoardHandler handler)
  45. {
  46. for (String cmd : handler.getCommunityBoardCommands())
  47. {
  48. _datatable.put(cmd.toLowerCase(), handler);
  49. }
  50. }
  51. @Override
  52. public synchronized void removeHandler(IParseBoardHandler handler)
  53. {
  54. for (String cmd : handler.getCommunityBoardCommands())
  55. {
  56. _datatable.remove(cmd.toLowerCase());
  57. }
  58. }
  59. @Override
  60. public IParseBoardHandler getHandler(String cmd)
  61. {
  62. for (IParseBoardHandler cb : _datatable.values())
  63. {
  64. for (String command : cb.getCommunityBoardCommands())
  65. {
  66. if (cmd.toLowerCase().startsWith(command.toLowerCase()))
  67. {
  68. return cb;
  69. }
  70. }
  71. }
  72. return null;
  73. }
  74. @Override
  75. public int size()
  76. {
  77. return _datatable.size();
  78. }
  79. /**
  80. * Verifies if the string is a registered community board command.
  81. * @param cmd the command to verify
  82. * @return {@code true} if the command has been registered, {@code false} otherwise
  83. */
  84. public boolean isCommunityBoardCommand(String cmd)
  85. {
  86. return getHandler(cmd) != null;
  87. }
  88. /**
  89. * Parses a community board command.
  90. * @param command the command
  91. * @param player the player
  92. */
  93. public void handleParseCommand(String command, L2PcInstance player)
  94. {
  95. if (player == null)
  96. {
  97. return;
  98. }
  99. if (!Config.ENABLE_COMMUNITY_BOARD)
  100. {
  101. player.sendPacket(SystemMessageId.CB_OFFLINE);
  102. return;
  103. }
  104. final IParseBoardHandler cb = getHandler(command);
  105. if (cb == null)
  106. {
  107. LOG.warning(CommunityBoardHandler.class.getSimpleName() + ": Couldn't find parse handler for command " + command + "!");
  108. return;
  109. }
  110. cb.parseCommunityBoardCommand(command, player);
  111. }
  112. /**
  113. * Writes a command into the client.
  114. * @param player the player
  115. * @param url the command URL
  116. * @param arg1 the first argument
  117. * @param arg2 the second argument
  118. * @param arg3 the third argument
  119. * @param arg4 the fourth argument
  120. * @param arg5 the fifth argument
  121. */
  122. public void handleWriteCommand(L2PcInstance player, String url, String arg1, String arg2, String arg3, String arg4, String arg5)
  123. {
  124. if (player == null)
  125. {
  126. return;
  127. }
  128. if (!Config.ENABLE_COMMUNITY_BOARD)
  129. {
  130. player.sendPacket(SystemMessageId.CB_OFFLINE);
  131. return;
  132. }
  133. String cmd = "";
  134. switch (url)
  135. {
  136. case "Topic":
  137. {
  138. cmd = "_bbstop";
  139. break;
  140. }
  141. case "Post":
  142. {
  143. cmd = "_bbspos"; // TODO: Implement.
  144. break;
  145. }
  146. case "Region":
  147. {
  148. cmd = "_bbsloc";
  149. break;
  150. }
  151. case "Notice":
  152. {
  153. cmd = "_bbsclan";
  154. break;
  155. }
  156. default:
  157. {
  158. separateAndSend("<html><body><br><br><center>The command: " + url + " is not implemented yet.</center><br><br></body></html>", player);
  159. return;
  160. }
  161. }
  162. final IParseBoardHandler cb = getHandler(cmd);
  163. if (cb == null)
  164. {
  165. LOG.warning(CommunityBoardHandler.class.getSimpleName() + ": Couldn't find write handler for command " + cmd + "!");
  166. return;
  167. }
  168. if (!(cb instanceof IWriteBoardHandler))
  169. {
  170. LOG.warning(CommunityBoardHandler.class.getSimpleName() + ": " + cb.getClass().getSimpleName() + " doesn't implement write!");
  171. return;
  172. }
  173. ((IWriteBoardHandler) cb).writeCommunityBoardCommand(player, arg1, arg2, arg3, arg4, arg5);
  174. }
  175. /**
  176. * Sets the last bypass used by the player.
  177. * @param player the player
  178. * @param title the title
  179. * @param bypass the bypass
  180. */
  181. public void addBypass(L2PcInstance player, String title, String bypass)
  182. {
  183. _bypasses.put(player.getObjectId(), title + "&" + bypass);
  184. }
  185. /**
  186. * Removes the last bypass used by the player.
  187. * @param player the player
  188. * @return the last bypass used
  189. */
  190. public String removeBypass(L2PcInstance player)
  191. {
  192. return _bypasses.remove(player.getObjectId());
  193. }
  194. /**
  195. * Separates and send an HTML into multiple packets, to display into the community board.<br>
  196. * The limit is 16383 characters.
  197. * @param html the HTML to send
  198. * @param player the player
  199. */
  200. public static void separateAndSend(String html, L2PcInstance player)
  201. {
  202. Util.sendCBHtml(player, html);
  203. }
  204. public static CommunityBoardHandler getInstance()
  205. {
  206. return SingletonHolder._instance;
  207. }
  208. private static class SingletonHolder
  209. {
  210. protected static final CommunityBoardHandler _instance = new CommunityBoardHandler();
  211. }
  212. }