Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ProcessingManager.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.parser.irc;
  23. import com.dmdirc.parser.common.ParserError;
  24. import com.dmdirc.parser.events.NumericEvent;
  25. import com.dmdirc.parser.irc.processors.IRCProcessor;
  26. import com.dmdirc.parser.irc.processors.Process001;
  27. import com.dmdirc.parser.irc.processors.Process004005;
  28. import com.dmdirc.parser.irc.processors.Process464;
  29. import com.dmdirc.parser.irc.processors.ProcessAccount;
  30. import com.dmdirc.parser.irc.processors.ProcessAway;
  31. import com.dmdirc.parser.irc.processors.ProcessCap;
  32. import com.dmdirc.parser.irc.processors.ProcessInvite;
  33. import com.dmdirc.parser.irc.processors.ProcessJoin;
  34. import com.dmdirc.parser.irc.processors.ProcessKick;
  35. import com.dmdirc.parser.irc.processors.ProcessList;
  36. import com.dmdirc.parser.irc.processors.ProcessListModes;
  37. import com.dmdirc.parser.irc.processors.ProcessMOTD;
  38. import com.dmdirc.parser.irc.processors.ProcessMessage;
  39. import com.dmdirc.parser.irc.processors.ProcessMode;
  40. import com.dmdirc.parser.irc.processors.ProcessNames;
  41. import com.dmdirc.parser.irc.processors.ProcessNick;
  42. import com.dmdirc.parser.irc.processors.ProcessNickInUse;
  43. import com.dmdirc.parser.irc.processors.ProcessNoticeAuth;
  44. import com.dmdirc.parser.irc.processors.ProcessPart;
  45. import com.dmdirc.parser.irc.processors.ProcessQuit;
  46. import com.dmdirc.parser.irc.processors.ProcessTopic;
  47. import com.dmdirc.parser.irc.processors.ProcessWallops;
  48. import com.dmdirc.parser.irc.processors.ProcessWho;
  49. import java.util.Date;
  50. import java.util.HashMap;
  51. import java.util.Map;
  52. /**
  53. * IRC Parser Processing Manager.
  54. * Manages adding/removing/calling processing stuff.
  55. */
  56. public class ProcessingManager {
  57. /** Reference to the parser object that owns this ProcessingManager. */
  58. private final IRCParser parser;
  59. /** Hashtable used to store the different types of IRCProcessor known. */
  60. private final Map<String, IRCProcessor> processHash = new HashMap<>();
  61. /**
  62. * Constructor to create a ProcessingManager.
  63. *
  64. * @param parser IRCParser that owns this Processing Manager
  65. * @param prefixModeManager The manager to use to access prefix modes.
  66. * @param userModeManager Mode manager to use for user modes.
  67. * @param chanModeManager Mode manager to use for channel modes.
  68. */
  69. public ProcessingManager(final IRCParser parser, final PrefixModeManager prefixModeManager,
  70. final ModeManager userModeManager, final ModeManager chanModeManager) {
  71. this.parser = parser;
  72. //------------------------------------------------
  73. // Add processors
  74. //------------------------------------------------
  75. // NOTICE AUTH
  76. addProcessor(new ProcessNoticeAuth(parser, this));
  77. // 001
  78. addProcessor(new Process001(parser, this));
  79. // 004
  80. // 005
  81. addProcessor(new Process004005(parser, this));
  82. // 464
  83. addProcessor(new Process464(parser, this));
  84. // 301
  85. // 305
  86. // 306
  87. addProcessor(new ProcessAway(parser, this));
  88. // 352
  89. addProcessor(new ProcessWho(parser, this));
  90. // INVITE
  91. addProcessor(new ProcessInvite(parser, this));
  92. // JOIN
  93. addProcessor(new ProcessJoin(parser, prefixModeManager, userModeManager, chanModeManager,
  94. this));
  95. // KICK
  96. addProcessor(new ProcessKick(parser, this));
  97. // PRIVMSG
  98. // NOTICE
  99. addProcessor(new ProcessMessage(parser, prefixModeManager, this));
  100. // MODE
  101. // 324
  102. addProcessor(new ProcessMode(parser, prefixModeManager, userModeManager, chanModeManager,
  103. this));
  104. // 372
  105. // 375
  106. // 376
  107. // 422
  108. addProcessor(new ProcessMOTD(parser, this));
  109. // 353
  110. // 366
  111. addProcessor(new ProcessNames(parser, prefixModeManager, userModeManager, this));
  112. // 433
  113. addProcessor(new ProcessNickInUse(parser, this));
  114. // NICK
  115. addProcessor(new ProcessNick(parser, this));
  116. // PART
  117. addProcessor(new ProcessPart(parser, this));
  118. // QUIT
  119. addProcessor(new ProcessQuit(parser, this));
  120. // TOPIC
  121. // 332
  122. // 333
  123. addProcessor(new ProcessTopic(parser, this));
  124. // 344
  125. // 345
  126. // 346
  127. // 347
  128. // 348
  129. // 349
  130. // 367
  131. // 368
  132. addProcessor(new ProcessListModes(parser, this));
  133. // WALLOPS
  134. addProcessor(new ProcessWallops(parser, this));
  135. // 321
  136. // 322
  137. // 323
  138. addProcessor(new ProcessList(parser, this));
  139. // CAP
  140. addProcessor(new ProcessCap(parser, this));
  141. // ACCOUNT
  142. addProcessor(new ProcessAccount(parser, this));
  143. }
  144. /**
  145. * Debugging Data to the console.
  146. */
  147. private void doDebug(final String line, final Object... args) {
  148. parser.callDebugInfo(IRCParser.DEBUG_PROCESSOR, line, args);
  149. }
  150. /**
  151. * Add new Process type.
  152. *
  153. * @param processor IRCProcessor subclass for the processor.
  154. */
  155. public void addProcessor(final IRCProcessor processor) {
  156. // handles() returns a String array of all the tokens
  157. // that this processor will parse.
  158. addProcessor(processor.handles(), processor);
  159. }
  160. /**
  161. * Add a processor to tokens not-specified in the handles() reply.
  162. *
  163. * @param processor IRCProcessor subclass for the processor.
  164. * @param handles String Array of tokens to add this processor as a hadler for
  165. */
  166. public void addProcessor(final String[] handles, final IRCProcessor processor) {
  167. doDebug("Adding processor: " + processor.getName());
  168. for (String handle : handles) {
  169. if (processHash.containsKey(handle.toLowerCase())) {
  170. // New Processors take priority over old ones
  171. processHash.remove(handle.toLowerCase());
  172. }
  173. doDebug("\t Added handler for: " + handle);
  174. processHash.put(handle.toLowerCase(), processor);
  175. }
  176. }
  177. /**
  178. * Remove a Process type.
  179. *
  180. * @param processor IRCProcessor subclass for the processor.
  181. */
  182. public void delProcessor(final IRCProcessor processor) {
  183. IRCProcessor testProcessor;
  184. doDebug("Deleting processor: " + processor.getName());
  185. for (String elementName : processHash.keySet()) {
  186. doDebug("\t Checking handler for: " + elementName);
  187. testProcessor = processHash.get(elementName);
  188. if (testProcessor.getName().equalsIgnoreCase(processor.getName())) {
  189. doDebug("\t Removed handler for: " + elementName);
  190. processHash.remove(elementName);
  191. }
  192. }
  193. }
  194. /**
  195. * Get the processor used for a specified token.
  196. *
  197. * @param sParam Type of line to process ("005", "PRIVMSG" etc)
  198. * @return IRCProcessor for the given param.
  199. * @throws ProcessorNotFoundException if no processer exists for the param
  200. */
  201. public IRCProcessor getProcessor(final String sParam) throws ProcessorNotFoundException {
  202. if (processHash.containsKey(sParam.toLowerCase())) {
  203. return processHash.get(sParam.toLowerCase());
  204. } else {
  205. throw new ProcessorNotFoundException("No processors will handle " + sParam);
  206. }
  207. }
  208. /**
  209. * Process a Line.
  210. *
  211. * @param sParam Type of line to process ("005", "PRIVMSG" etc)
  212. * @param token IRCTokenised line to process
  213. * @throws ProcessorNotFoundException exception if no processors exists to handle the line
  214. */
  215. public void process(final String sParam, final String... token) throws ProcessorNotFoundException {
  216. process(new Date(), sParam, token);
  217. }
  218. /**
  219. * Process a Line.
  220. *
  221. * @param date Date of line.
  222. * @param sParam Type of line to process ("005", "PRIVMSG" etc)
  223. * @param token IRCTokenised line to process
  224. * @throws ProcessorNotFoundException exception if no processors exists to handle the line
  225. */
  226. public void process(final Date date, final String sParam, final String... token) throws ProcessorNotFoundException {
  227. IRCProcessor messageProcessor = null;
  228. try {
  229. messageProcessor = getProcessor(sParam);
  230. if (messageProcessor instanceof TimestampedIRCProcessor) {
  231. ((TimestampedIRCProcessor)messageProcessor).process(date, sParam, token);
  232. } else {
  233. messageProcessor.process(sParam, token);
  234. }
  235. } catch (ProcessorNotFoundException p) {
  236. throw p;
  237. } catch (Exception e) {
  238. final ParserError ei = new ParserError(ParserError.ERROR_ERROR,
  239. "Exception in Processor. [" + messageProcessor + "]: "
  240. + e.getMessage(), parser.getLastLine());
  241. ei.setException(e);
  242. parser.callErrorInfo(ei);
  243. } finally {
  244. // Try to call callNumeric. We don't want this to work if sParam is a non
  245. // integer param, hense the empty catch
  246. try {
  247. callNumeric(Integer.parseInt(sParam), token);
  248. } catch (NumberFormatException e) {
  249. }
  250. }
  251. }
  252. /**
  253. * Callback to all objects implementing the onNumeric Callback.
  254. *
  255. * @param numeric What numeric is this for
  256. * @param token IRC Tokenised line
  257. */
  258. protected void callNumeric(final int numeric, final String... token) {
  259. parser.getCallbackManager().publish(new NumericEvent(parser, new Date(), numeric, token));
  260. }
  261. }