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

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