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.

ProcessJoin.java 8.4KB

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