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.0KB

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