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 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.interfaces.callbacks.NumericListener;
  25. import java.util.Hashtable;
  26. /**
  27. * IRC Parser Processing Manager.
  28. * Manages adding/removing/calling processing stuff.
  29. *
  30. * @author Shane Mc Cormack
  31. */
  32. public class ProcessingManager {
  33. /** Reference to the parser object that owns this ProcessingManager */
  34. IRCParser myParser;
  35. /** Hashtable used to store the different types of IRCProcessor known. */
  36. private final Hashtable<String,IRCProcessor> processHash = new Hashtable<String,IRCProcessor>();
  37. /**
  38. * Debugging Data to the console.
  39. */
  40. private void doDebug(final String line, final Object... args) {
  41. myParser.callDebugInfo(IRCParser.DEBUG_PROCESSOR, line, args);
  42. }
  43. /**
  44. * Constructor to create a ProcessingManager.
  45. *
  46. * @param parser IRCParser that owns this Processing Manager
  47. */
  48. public ProcessingManager(IRCParser parser) {
  49. myParser = parser;
  50. //------------------------------------------------
  51. // Add processors
  52. //------------------------------------------------
  53. // NOTICE AUTH
  54. addProcessor(new ProcessNoticeAuth(myParser, this));
  55. // 001
  56. addProcessor(new Process001(myParser, this));
  57. // 004
  58. // 005
  59. addProcessor(new Process004005(myParser, this));
  60. // 464
  61. addProcessor(new Process464(myParser, this));
  62. // 301
  63. // 305
  64. // 306
  65. addProcessor(new ProcessAway(myParser, this));
  66. // 352
  67. addProcessor(new ProcessWho(myParser, this));
  68. // INVITE
  69. addProcessor(new ProcessInvite(myParser, this));
  70. // JOIN
  71. addProcessor(new ProcessJoin(myParser, this));
  72. // KICK
  73. addProcessor(new ProcessKick(myParser, this));
  74. // PRIVMSG
  75. // NOTICE
  76. addProcessor(new ProcessMessage(myParser, this));
  77. // MODE
  78. // 324
  79. addProcessor(new ProcessMode(myParser, this));
  80. // 372
  81. // 375
  82. // 376
  83. // 422
  84. addProcessor(new ProcessMOTD(myParser, this));
  85. // 353
  86. // 366
  87. addProcessor(new ProcessNames(myParser, this));
  88. // 433
  89. addProcessor(new ProcessNickInUse(myParser, this));
  90. // NICK
  91. addProcessor(new ProcessNick(myParser, this));
  92. // PART
  93. addProcessor(new ProcessPart(myParser, this));
  94. // QUIT
  95. addProcessor(new ProcessQuit(myParser, this));
  96. // TOPIC
  97. // 332
  98. // 333
  99. addProcessor(new ProcessTopic(myParser, this));
  100. // 344
  101. // 345
  102. // 346
  103. // 347
  104. // 348
  105. // 349
  106. // 367
  107. // 368
  108. addProcessor(new ProcessListModes(myParser, this));
  109. // WALLOPS
  110. addProcessor(new ProcessWallops(myParser, this));
  111. }
  112. /**
  113. * Add new Process type.
  114. *
  115. * @param processor IRCProcessor subclass for the processor.
  116. */
  117. public void addProcessor(final IRCProcessor processor) {
  118. // handles() returns a String array of all the tokens
  119. // that this processor will parse.
  120. addProcessor(processor.handles(), processor);
  121. }
  122. /**
  123. * Add a processor to tokens not-specified in the handles() reply.
  124. *
  125. * @param processor IRCProcessor subclass for the processor.
  126. * @param handles String Array of tokens to add this processor as a hadler for
  127. */
  128. public void addProcessor(final String[] handles, final IRCProcessor processor) {
  129. doDebug("Adding processor: "+processor.getName());
  130. for (int i = 0; i < handles.length; ++i) {
  131. if (processHash.containsKey(handles[i].toLowerCase())) {
  132. // New Processors take priority over old ones
  133. processHash.remove(handles[i].toLowerCase());
  134. }
  135. doDebug("\t Added handler for: "+handles[i]);
  136. processHash.put(handles[i].toLowerCase(), processor);
  137. }
  138. }
  139. /**
  140. * Remove a Process type.
  141. *
  142. * @param processor IRCProcessor subclass for the processor.
  143. */
  144. public void delProcessor(final IRCProcessor processor) {
  145. IRCProcessor testProcessor;
  146. doDebug("Deleting processor: "+processor.getName());
  147. for (String elementName : processHash.keySet()) {
  148. doDebug("\t Checking handler for: "+elementName);
  149. testProcessor = processHash.get(elementName);
  150. if (testProcessor.getName().equalsIgnoreCase(processor.getName())) {
  151. doDebug("\t Removed handler for: "+elementName);
  152. processHash.remove(elementName);
  153. }
  154. }
  155. }
  156. /**
  157. * Get the processor used for a specified token.
  158. *
  159. * @param sParam Type of line to process ("005", "PRIVMSG" etc)
  160. * @return IRCProcessor for the given param.
  161. * @throws ProcessorNotFoundException if no processer exists for the param
  162. */
  163. public IRCProcessor getProcessor(final String sParam) throws ProcessorNotFoundException {
  164. if (processHash.containsKey(sParam.toLowerCase())) {
  165. return processHash.get(sParam.toLowerCase());
  166. } else {
  167. throw new ProcessorNotFoundException("No processors will handle "+sParam);
  168. }
  169. }
  170. /**
  171. * Process a Line.
  172. *
  173. * @param sParam Type of line to process ("005", "PRIVMSG" etc)
  174. * @param token IRCTokenised line to process
  175. * @throws ProcessorNotFoundException exception if no processors exists to handle the line
  176. */
  177. public void process(final String sParam, final String[] token) throws ProcessorNotFoundException {
  178. IRCProcessor messageProcessor = null;
  179. try {
  180. messageProcessor = getProcessor(sParam);
  181. messageProcessor.process(sParam, token);
  182. } catch (ProcessorNotFoundException p) {
  183. throw p;
  184. } catch (Exception e) {
  185. final ParserError ei = new ParserError(ParserError.ERROR_ERROR,"Exception in Processor. ["+messageProcessor+"]: "+e.getMessage(), myParser.getLastLine());
  186. ei.setException(e);
  187. myParser.callErrorInfo(ei);
  188. } finally {
  189. // Try to call callNumeric. We don't want this to work if sParam is a non
  190. // integer param, hense the empty catch
  191. try {
  192. callNumeric(Integer.parseInt(sParam), token);
  193. } catch (NumberFormatException e) { }
  194. }
  195. }
  196. /**
  197. * Callback to all objects implementing the onNumeric Callback.
  198. *
  199. * @see INumeric
  200. * @param numeric What numeric is this for
  201. * @param token IRC Tokenised line
  202. * @return true if a method was called, false otherwise
  203. */
  204. protected boolean callNumeric(final int numeric, final String[] token) {
  205. return myParser.getCallbackManager().getCallbackType(NumericListener.class).call(numeric, token);
  206. }
  207. }