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

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