You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProcessingManager.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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));
  77. // 001
  78. addProcessor(new Process001(parser));
  79. // 004
  80. // 005
  81. addProcessor(new Process004005(parser));
  82. // 464
  83. addProcessor(new Process464(parser));
  84. // 301
  85. // 305
  86. // 306
  87. addProcessor(new ProcessAway(parser));
  88. // 352
  89. addProcessor(new ProcessWho(parser));
  90. // INVITE
  91. addProcessor(new ProcessInvite(parser));
  92. // JOIN
  93. addProcessor(new ProcessJoin(parser, prefixModeManager, userModeManager, chanModeManager));
  94. // KICK
  95. addProcessor(new ProcessKick(parser));
  96. // PRIVMSG
  97. // NOTICE
  98. addProcessor(new ProcessMessage(parser, prefixModeManager));
  99. // MODE
  100. // 324
  101. addProcessor(new ProcessMode(parser, prefixModeManager, userModeManager, chanModeManager));
  102. // 372
  103. // 375
  104. // 376
  105. // 422
  106. addProcessor(new ProcessMOTD(parser));
  107. // 353
  108. // 366
  109. addProcessor(new ProcessNames(parser, prefixModeManager, userModeManager));
  110. // 433
  111. addProcessor(new ProcessNickInUse(parser));
  112. // NICK
  113. addProcessor(new ProcessNick(parser));
  114. // PART
  115. addProcessor(new ProcessPart(parser));
  116. // QUIT
  117. addProcessor(new ProcessQuit(parser));
  118. // TOPIC
  119. // 332
  120. // 333
  121. addProcessor(new ProcessTopic(parser));
  122. // 344
  123. // 345
  124. // 346
  125. // 347
  126. // 348
  127. // 349
  128. // 367
  129. // 368
  130. addProcessor(new ProcessListModes(parser));
  131. // WALLOPS
  132. addProcessor(new ProcessWallops(parser));
  133. // 321
  134. // 322
  135. // 323
  136. addProcessor(new ProcessList(parser));
  137. // CAP
  138. addProcessor(new ProcessCap(parser));
  139. // ACCOUNT
  140. addProcessor(new ProcessAccount(parser));
  141. }
  142. /**
  143. * Debugging Data to the console.
  144. */
  145. private void doDebug(final String line, final Object... args) {
  146. parser.callDebugInfo(IRCParser.DEBUG_PROCESSOR, line, args);
  147. }
  148. /**
  149. * Add new Process type.
  150. *
  151. * @param processor IRCProcessor subclass for the processor.
  152. */
  153. public void addProcessor(final IRCProcessor processor) {
  154. // handles() returns a String array of all the tokens
  155. // that this processor will parse.
  156. addProcessor(processor.handles(), processor);
  157. }
  158. /**
  159. * Add a processor to tokens not-specified in the handles() reply.
  160. *
  161. * @param processor IRCProcessor subclass for the processor.
  162. * @param handles String Array of tokens to add this processor as a hadler for
  163. */
  164. public void addProcessor(final String[] handles, final IRCProcessor processor) {
  165. doDebug("Adding processor: " + processor.getName());
  166. for (String handle : handles) {
  167. if (processHash.containsKey(handle.toLowerCase())) {
  168. // New Processors take priority over old ones
  169. processHash.remove(handle.toLowerCase());
  170. }
  171. doDebug("\t Added handler for: " + handle);
  172. processHash.put(handle.toLowerCase(), processor);
  173. }
  174. }
  175. /**
  176. * Remove a Process type.
  177. *
  178. * @param processor IRCProcessor subclass for the processor.
  179. */
  180. public void delProcessor(final IRCProcessor processor) {
  181. IRCProcessor testProcessor;
  182. doDebug("Deleting processor: " + processor.getName());
  183. for (String elementName : processHash.keySet()) {
  184. doDebug("\t Checking handler for: " + elementName);
  185. testProcessor = processHash.get(elementName);
  186. if (testProcessor.getName().equalsIgnoreCase(processor.getName())) {
  187. doDebug("\t Removed handler for: " + elementName);
  188. processHash.remove(elementName);
  189. }
  190. }
  191. }
  192. /**
  193. * Get the processor used for a specified token.
  194. *
  195. * @param sParam Type of line to process ("005", "PRIVMSG" etc)
  196. * @return IRCProcessor for the given param.
  197. * @throws ProcessorNotFoundException if no processer exists for the param
  198. */
  199. public IRCProcessor getProcessor(final String sParam) throws ProcessorNotFoundException {
  200. if (processHash.containsKey(sParam.toLowerCase())) {
  201. return processHash.get(sParam.toLowerCase());
  202. } else {
  203. throw new ProcessorNotFoundException("No processors will handle " + sParam);
  204. }
  205. }
  206. /**
  207. * Process a Line.
  208. *
  209. * @param sParam Type of line to process ("005", "PRIVMSG" etc)
  210. * @param token IRCTokenised line to process
  211. * @throws ProcessorNotFoundException exception if no processors exists to handle the line
  212. */
  213. public void process(final String sParam, final String... token) throws ProcessorNotFoundException {
  214. process(new Date(), sParam, token);
  215. }
  216. /**
  217. * Process a Line.
  218. *
  219. * @param date Date of line.
  220. * @param sParam Type of line to process ("005", "PRIVMSG" etc)
  221. * @param token IRCTokenised line to process
  222. * @throws ProcessorNotFoundException exception if no processors exists to handle the line
  223. */
  224. public void process(final Date date, final String sParam, final String... token) throws ProcessorNotFoundException {
  225. IRCProcessor messageProcessor = null;
  226. try {
  227. messageProcessor = getProcessor(sParam);
  228. if (messageProcessor instanceof TimestampedIRCProcessor) {
  229. ((TimestampedIRCProcessor)messageProcessor).process(date, sParam, token);
  230. } else {
  231. messageProcessor.process(sParam, token);
  232. }
  233. } catch (ProcessorNotFoundException p) {
  234. throw p;
  235. } catch (Exception e) {
  236. final ParserError ei = new ParserError(ParserError.ERROR_ERROR,
  237. "Exception in Processor. [" + messageProcessor + "]: "
  238. + e.getMessage(), parser.getLastLine());
  239. ei.setException(e);
  240. parser.callErrorInfo(ei);
  241. } finally {
  242. // Try to call callNumeric. We don't want this to work if sParam is a non
  243. // integer param, hense the empty catch
  244. try {
  245. callNumeric(Integer.parseInt(sParam), token);
  246. } catch (NumberFormatException e) {
  247. }
  248. }
  249. }
  250. /**
  251. * Callback to all objects implementing the onNumeric Callback.
  252. *
  253. * @param numeric What numeric is this for
  254. * @param token IRC Tokenised line
  255. */
  256. protected void callNumeric(final int numeric, final String... token) {
  257. parser.getCallbackManager().publish(new NumericEvent(parser, new Date(), numeric, token));
  258. }
  259. }