Java IRC bot
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

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