Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

IRCProcessor.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2006-2017 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 java.time.LocalDateTime;
  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. /** The names of the tokens this processor handles. */
  38. private final String[] handledTokens;
  39. // Some functions from the main parser are useful, and having to use parser.functionName
  40. // is annoying, so we also implement them here (calling them again using parser)
  41. /**
  42. * Create a new instance of the IRCProcessor Object.
  43. * @param parser IRCParser That owns this IRCProcessor
  44. * @param handledTokens Tokens that this processor handles
  45. */
  46. protected IRCProcessor(final IRCParser parser, final String... handledTokens) {
  47. this.parser = parser;
  48. this.handledTokens = handledTokens;
  49. }
  50. /**
  51. * Callback to all objects implementing the IErrorInfo Interface.
  52. *
  53. * @param errorInfo ParserError object representing the error.
  54. */
  55. protected final void callErrorInfo(final ParserError errorInfo) {
  56. parser.callErrorInfo(errorInfo);
  57. }
  58. /**
  59. * Callback to all objects implementing the DebugInfo Callback.
  60. *
  61. * @param level Debugging Level (DEBUG_INFO, ndSocket etc)
  62. * @param data Debugging Information
  63. * @param args Formatting String Options
  64. */
  65. protected final void callDebugInfo(final int level, final String data, final Object... args) {
  66. parser.callDebugInfo(level, data, args);
  67. }
  68. /**
  69. * Callback to all objects implementing the DebugInfo Callback.
  70. *
  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. protected CallbackManager getCallbackManager() {
  105. return parser.getCallbackManager();
  106. }
  107. /**
  108. * Send a line to the server and add proper line ending.
  109. *
  110. * @param line Line to send (\r\n termination is added automatically)
  111. * @param priority Priority of this line.
  112. */
  113. protected final void sendString(final String line, final QueuePriority priority) {
  114. parser.sendString(line, priority);
  115. }
  116. /**
  117. * Process a Line.
  118. *
  119. * @param date Date of this line
  120. * @param sParam Type of line to process ("005", "PRIVMSG" etc)
  121. * @param token IRCTokenised line to process
  122. */
  123. public abstract void process(final LocalDateTime date, final String sParam, final String... token);
  124. /**
  125. * What does this IRCProcessor handle.
  126. *
  127. * @return String[] with the names of the tokens we handle.
  128. */
  129. public String[] handles() {
  130. return handledTokens;
  131. }
  132. /**
  133. * Get the name for this Processor.
  134. * @return the name of this processor
  135. */
  136. public final String getName() {
  137. final Package thisPackage = getClass().getPackage();
  138. int packageLength = 0;
  139. if (thisPackage != null) {
  140. packageLength = thisPackage.getName().length() + 1;
  141. }
  142. return getClass().getName().substring(packageLength);
  143. }
  144. /**
  145. * Get the name for this Processor.
  146. * @return the name of this processor
  147. */
  148. @Override
  149. public final String toString() {
  150. return getName();
  151. }
  152. }