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

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