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

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