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.

ProcessJoin.java 8.6KB

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