Java IRC bot
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 6.9KB

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