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

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