您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ProcessJoin.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2006-2014 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.ParserError;
  24. import com.dmdirc.parser.common.QueuePriority;
  25. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  26. import com.dmdirc.parser.interfaces.ChannelInfo;
  27. import com.dmdirc.parser.interfaces.callbacks.ChannelJoinListener;
  28. import com.dmdirc.parser.interfaces.callbacks.ChannelSelfJoinListener;
  29. import com.dmdirc.parser.irc.CapabilityState;
  30. import com.dmdirc.parser.irc.IRCChannelClientInfo;
  31. import com.dmdirc.parser.irc.IRCChannelInfo;
  32. import com.dmdirc.parser.irc.IRCClientInfo;
  33. import com.dmdirc.parser.irc.IRCParser;
  34. import com.dmdirc.parser.irc.ProcessingManager;
  35. import com.dmdirc.parser.irc.ProcessorNotFoundException;
  36. import java.util.Arrays;
  37. /**
  38. * Process a channel join.
  39. */
  40. public class ProcessJoin extends IRCProcessor {
  41. /**
  42. * Create a new instance of the IRCProcessor Object.
  43. *
  44. * @param parser IRCParser That owns this IRCProcessor
  45. * @param manager ProcessingManager that is in charge of this IRCProcessor
  46. */
  47. public ProcessJoin(final IRCParser parser, final ProcessingManager manager) {
  48. super(parser, manager);
  49. }
  50. /**
  51. * Process a channel join.
  52. *
  53. * @param sParam Type of line to process ("JOIN")
  54. * @param token IRCTokenised line to process
  55. */
  56. @Override
  57. public void process(final String sParam, final String[] token) {
  58. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: %s | %s", sParam, Arrays.toString(token));
  59. if ("329".equals(sParam)) {
  60. if (token.length < 5) {
  61. return;
  62. }
  63. final IRCChannelInfo iChannel = parser.getChannel(token[3]);
  64. if (iChannel != null) {
  65. try {
  66. iChannel.setCreateTime(Integer.parseInt(token[4]));
  67. } catch (NumberFormatException nfe) {
  68. // Oh well, not a normal ircd I guess
  69. }
  70. }
  71. } else {
  72. // :nick!ident@host JOIN (:)#Channel
  73. if (token.length < 3) {
  74. return;
  75. }
  76. IRCClientInfo iClient;
  77. IRCChannelInfo iChannel;
  78. final IRCChannelClientInfo iChannelClient;
  79. final String channelName;
  80. final boolean extendedJoin = parser.getCapabilityState("extended-join") == CapabilityState.ENABLED;
  81. final String accountName;
  82. final String realName;
  83. iClient = getClientInfo(token[0]);
  84. if (extendedJoin) {
  85. // :nick!ident@host JOIN #Channel accountName :Real Name
  86. channelName = token[2];
  87. accountName = token.length > 3 ? token[3] : "*";
  88. realName = token.length > 4 ? token[token.length - 1] : "";
  89. } else {
  90. channelName = token[token.length - 1];
  91. accountName = "*";
  92. realName = "";
  93. }
  94. iChannel = parser.getChannel(token[2]);
  95. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: client: %s", iClient);
  96. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: channel: %s", iChannel);
  97. if (iClient == null) {
  98. iClient = new IRCClientInfo(parser, token[0]);
  99. parser.addClient(iClient);
  100. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: new client.", iClient);
  101. }
  102. if (extendedJoin) {
  103. iClient.setAccountName("*".equals(accountName) ? null : accountName);
  104. iClient.setRealName(realName);
  105. }
  106. // Check to see if we know the host/ident for this client to facilitate dmdirc Formatter
  107. if (iClient.getHostname().isEmpty()) {
  108. iClient.setUserBits(token[0], false);
  109. }
  110. if (iChannel != null) {
  111. if (iClient == parser.getLocalClient()) {
  112. try {
  113. if (iChannel.getChannelClient(iClient) == null) {
  114. // Otherwise we have a channel known, that we are not in?
  115. parser.callErrorInfo(new ParserError(ParserError.ERROR_FATAL, "Joined known channel that we wern't already on..", parser.getLastLine()));
  116. } else {
  117. // If we are joining a channel we are already on, fake a part from
  118. // the channel internally, and rejoin.
  119. parser.getProcessingManager().process("PART", token);
  120. }
  121. } catch (ProcessorNotFoundException e) {
  122. }
  123. } else if (iChannel.getChannelClient(iClient) == null) {
  124. // This is only done if we are already the channel, and it isn't us that
  125. // joined.
  126. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Adding client to channel.");
  127. iChannelClient = iChannel.addClient(iClient);
  128. callChannelJoin(iChannel, iChannelClient);
  129. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Added client to channel.");
  130. return;
  131. } else {
  132. // Client joined channel that we already know of.
  133. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Not adding client to channel they are already on.");
  134. return;
  135. }
  136. }
  137. iChannel = new IRCChannelInfo(parser, channelName);
  138. // Add ourself to the channel, this will be overridden by the NAMES reply
  139. iChannel.addClient(iClient);
  140. parser.addChannel(iChannel);
  141. sendString("MODE " + iChannel.getName(), QueuePriority.LOW);
  142. callChannelSelfJoin(iChannel);
  143. }
  144. }
  145. /**
  146. * Callback to all objects implementing the ChannelJoin Callback.
  147. *
  148. * @see com.dmdirc.parser.interfaces.callbacks.ChannelJoinListener
  149. * @param cChannel Channel Object
  150. * @param cChannelClient ChannelClient object for new person
  151. * @return true if a method was called, false otherwise
  152. */
  153. protected boolean callChannelJoin(final ChannelInfo cChannel, final ChannelClientInfo cChannelClient) {
  154. return getCallbackManager().getCallbackType(ChannelJoinListener.class).call(cChannel, cChannelClient);
  155. }
  156. /**
  157. * Callback to all objects implementing the ChannelSelfJoin Callback.
  158. *
  159. * @see com.dmdirc.parser.interfaces.callbacks.ChannelSelfJoinListener
  160. * @param cChannel Channel Object
  161. * @return true if a method was called, false otherwise
  162. */
  163. protected boolean callChannelSelfJoin(final ChannelInfo cChannel) {
  164. return getCallbackManager().getCallbackType(ChannelSelfJoinListener.class).call(cChannel);
  165. }
  166. /**
  167. * What does this IRCProcessor handle.
  168. *
  169. * @return String[] with the names of the tokens we handle.
  170. */
  171. @Override
  172. public String[] handles() {
  173. return new String[]{"JOIN", "329"};
  174. }
  175. }