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

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