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.

IRCProcessor.java 6.2KB

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