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.4KB

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