Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

IRCProcessor.java 5.7KB

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