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

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