Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

IRCProcessor.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2006-2010 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 com.dmdirc.parser.common.ParserError;
  24. import com.dmdirc.parser.common.CallbackManager;
  25. import com.dmdirc.parser.common.QueuePriority;
  26. /**
  27. * IRCProcessor.
  28. * Superclass for all IRCProcessor types.
  29. *
  30. * @author Shane Mc Cormack
  31. */
  32. abstract class IRCProcessor {
  33. /** Reference to the IRCParser that owns this IRCProcessor. */
  34. protected IRCParser myParser;
  35. /** Reference to the Processing in charge of this IRCProcessor. */
  36. protected ProcessingManager myManager;
  37. // Some functions from the main parser are useful, and having to use myParser.functionName
  38. // is annoying, so we also implement them here (calling them again using myParser)
  39. /**
  40. * Create a new instance of the IRCProcessor Object.
  41. *
  42. * @param parser IRCParser That owns this IRCProcessor
  43. * @param manager ProcessingManager that is in charge of this IRCProcessor
  44. */
  45. protected IRCProcessor(final IRCParser parser, final ProcessingManager manager) {
  46. this.myParser = parser;
  47. this.myManager = manager;
  48. }
  49. /**
  50. * Callback to all objects implementing the IErrorInfo Interface.
  51. *
  52. * @see com.dmdirc.parser.irc.callbacks.interfaces.IErrorInfo
  53. * @param errorInfo ParserError object representing the error.
  54. * @return true if a method was called, false otherwise
  55. */
  56. protected final boolean callErrorInfo(final ParserError errorInfo) {
  57. return myParser.callErrorInfo(errorInfo);
  58. }
  59. /**
  60. * Callback to all objects implementing the DebugInfo Callback.
  61. *
  62. * @see com.dmdirc.parser.irc.callbacks.interfaces.IDebugInfo
  63. * @param level Debugging Level (DEBUG_INFO, ndSocket etc)
  64. * @param data Debugging Information
  65. * @param args Formatting String Options
  66. * @return true if a method was called, false otherwise
  67. */
  68. protected final boolean callDebugInfo(final int level, final String data, final Object... args) {
  69. return myParser.callDebugInfo(level, String.format(data, args));
  70. }
  71. /**
  72. * Callback to all objects implementing the DebugInfo Callback.
  73. *
  74. * @see com.dmdirc.parser.irc.callbacks.interfaces.IDebugInfo
  75. * @param level Debugging Level (DEBUG_INFO, ndSocket etc)
  76. * @param data Debugging Information
  77. * @return true if a method was called, false otherwise
  78. */
  79. protected final boolean callDebugInfo(final int level, final String data) {
  80. return myParser.callDebugInfo(level, data);
  81. }
  82. /**
  83. * Check if a channel name is valid .
  84. *
  85. * @param sChannelName Channel name to test
  86. * @return true if name is valid on the current connection, false otherwise. (Always false before noMOTD/MOTDEnd)
  87. */
  88. protected final boolean isValidChannelName(final String sChannelName) {
  89. return myParser.isValidChannelName(sChannelName);
  90. }
  91. /**
  92. * Get the ClientInfo object for a person.
  93. *
  94. * @param sWho Who can be any valid identifier for a client as long as it contains a nickname (?:)nick(?!ident)(?@host)
  95. * @return ClientInfo Object for the client, or null
  96. */
  97. protected final IRCClientInfo getClientInfo(final String sWho) {
  98. return myParser.isKnownClient(sWho) ? myParser.getClient(sWho) : null;
  99. }
  100. /**
  101. * Get the ChannelInfo object for a channel.
  102. *
  103. * @param name This is the name of the channel.
  104. * @return ChannelInfo Object for the channel, or null
  105. */
  106. protected final IRCChannelInfo getChannel(final String name) {
  107. return myParser.getChannel(name);
  108. }
  109. /**
  110. * Get a reference to the CallbackManager.
  111. *
  112. * @return Reference to the CallbackManager
  113. */
  114. protected final CallbackManager<?> getCallbackManager() {
  115. return myParser.getCallbackManager();
  116. }
  117. /**
  118. * Send a line to the server and add proper line ending.
  119. *
  120. * @param line Line to send (\r\n termination is added automatically)
  121. */
  122. protected final void sendString(final String line) {
  123. myParser.sendString(line);
  124. }
  125. /**
  126. * Send a line to the server and add proper line ending.
  127. *
  128. * @param line Line to send (\r\n termination is added automatically)
  129. * @param priority Priority of this line.
  130. */
  131. protected final void sendString(final String line, final QueuePriority priority) {
  132. myParser.sendString(line, priority);
  133. }
  134. /**
  135. * Process a Line.
  136. *
  137. * @param sParam Type of line to process ("005", "PRIVMSG" etc)
  138. * @param token IRCTokenised line to process
  139. */
  140. public abstract void process(final String sParam, final String[] token);
  141. /**
  142. * What does this IRCProcessor handle.
  143. *
  144. * @return String[] with the names of the tokens we handle.
  145. */
  146. public abstract String[] handles();
  147. /**
  148. * Get the name for this Processor.
  149. * @return the name of this processor
  150. */
  151. public final String getName() {
  152. final Package thisPackage = this.getClass().getPackage();
  153. int packageLength = 0;
  154. if (thisPackage != null) {
  155. packageLength = thisPackage.getName().length() + 1;
  156. }
  157. return this.getClass().getName().substring(packageLength);
  158. }
  159. /**
  160. * Get the name for this Processor in lowercase.
  161. * @return lower case name of this processor
  162. */
  163. public final String getLowerName() {
  164. return this.getName().toLowerCase();
  165. }
  166. /**
  167. * Get the name for this Processor.
  168. * @return the name of this processor
  169. */
  170. @Override
  171. public final String toString() { return this.getName(); }
  172. }