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.0KB

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