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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.events.DataOutEvent;
  28. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  29. import com.dmdirc.parser.interfaces.ChannelInfo;
  30. import com.dmdirc.parser.irc.CapabilityState;
  31. import com.dmdirc.parser.irc.IRCChannelClientInfo;
  32. import com.dmdirc.parser.irc.IRCChannelInfo;
  33. import com.dmdirc.parser.irc.IRCClientInfo;
  34. import com.dmdirc.parser.irc.IRCParser;
  35. import com.dmdirc.parser.irc.ModeManager;
  36. import com.dmdirc.parser.irc.PrefixModeManager;
  37. import com.dmdirc.parser.irc.ProcessorNotFoundException;
  38. import com.dmdirc.parser.irc.events.IRCDataOutEvent;
  39. import net.engio.mbassy.listener.Handler;
  40. import net.engio.mbassy.listener.Invoke;
  41. import java.time.LocalDateTime;
  42. import java.util.Arrays;
  43. import java.util.LinkedList;
  44. import java.util.Queue;
  45. import javax.inject.Inject;
  46. import javax.inject.Named;
  47. /**
  48. * Process a channel join.
  49. */
  50. public class ProcessJoin extends IRCProcessor {
  51. /** The manager to use to access prefix modes. */
  52. private final PrefixModeManager prefixModeManager;
  53. /** Mode manager to use for user modes. */
  54. private final ModeManager userModeManager;
  55. /** Mode manager to use for channel modes. */
  56. private final ModeManager chanModeManager;
  57. /** Pending Joins. */
  58. private final Queue<String> pendingJoins = new LinkedList<>();
  59. /** Pending Join Keys. */
  60. private final Queue<String> pendingJoinKeys = new LinkedList<>();
  61. /**
  62. * Create a new instance of the IRCProcessor Object.
  63. *
  64. * @param parser IRCParser That owns this IRCProcessor
  65. * @param prefixModeManager The manager to use to access prefix modes.
  66. * @param userModeManager Mode manager to use for user modes.
  67. * @param chanModeManager Mode manager to use for channel modes.
  68. */
  69. @Inject
  70. public ProcessJoin(final IRCParser parser, final PrefixModeManager prefixModeManager,
  71. @Named("user") final ModeManager userModeManager,
  72. @Named("channel") final ModeManager chanModeManager) {
  73. super(parser, "JOIN", "329", "471", "473", "474", "475", "476", "477", "479");
  74. this.prefixModeManager = prefixModeManager;
  75. this.userModeManager = userModeManager;
  76. this.chanModeManager = chanModeManager;
  77. getCallbackManager().subscribe(this);
  78. }
  79. /**
  80. * Process a channel join.
  81. *
  82. * @param sParam Type of line to process ("JOIN")
  83. * @param token IRCTokenised line to process
  84. */
  85. @Override
  86. public void process(final String sParam, final String... token) {
  87. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: %s | %s", sParam, Arrays.toString(token));
  88. if ("329".equals(sParam)) {
  89. if (token.length < 5) {
  90. return;
  91. }
  92. final IRCChannelInfo iChannel = parser.getChannel(token[3]);
  93. if (iChannel != null) {
  94. try {
  95. iChannel.setCreateTime(Integer.parseInt(token[4]));
  96. } catch (NumberFormatException nfe) {
  97. // Oh well, not a normal ircd I guess
  98. }
  99. }
  100. } else if ("JOIN".equals(sParam)) {
  101. // :nick!ident@host JOIN (:)#Channel
  102. if (token.length < 3) {
  103. return;
  104. }
  105. final boolean extendedJoin = parser.getCapabilityState("extended-join") == CapabilityState.ENABLED;
  106. IRCClientInfo iClient = getClientInfo(token[0]);
  107. final String realName;
  108. final String accountName;
  109. final String channelName;
  110. if (extendedJoin) {
  111. // :nick!ident@host JOIN #Channel accountName :Real Name
  112. channelName = token[2];
  113. accountName = token.length > 3 ? token[3] : "*";
  114. realName = token.length > 4 ? token[token.length - 1] : "";
  115. } else {
  116. channelName = token[token.length - 1];
  117. accountName = "*";
  118. realName = "";
  119. }
  120. IRCChannelInfo iChannel = parser.getChannel(token[2]);
  121. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: client: %s", iClient);
  122. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: channel: %s", iChannel);
  123. if (iClient == null) {
  124. iClient = new IRCClientInfo(parser, userModeManager, token[0]);
  125. parser.addClient(iClient);
  126. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: new client.", iClient);
  127. }
  128. if (extendedJoin) {
  129. iClient.setAccountName("*".equals(accountName) ? null : accountName);
  130. iClient.setRealName(realName);
  131. }
  132. // Check to see if we know the host/ident for this client to facilitate dmdirc Formatter
  133. if (iClient.getHostname().isEmpty()) {
  134. iClient.setUserBits(token[0], false);
  135. }
  136. if (iChannel != null) {
  137. if (iClient == parser.getLocalClient()) {
  138. try {
  139. if (iChannel.getChannelClient(iClient) == null) {
  140. // Otherwise we have a channel known, that we are not in?
  141. parser.callErrorInfo(new ParserError(ParserError.ERROR_FATAL, "Joined known channel that we wern't already on..", parser.getLastLine()));
  142. } else {
  143. // If we are joining a channel we are already on, fake a part from
  144. // the channel internally, and rejoin.
  145. parser.getProcessingManager().process("PART", token);
  146. }
  147. } catch (ProcessorNotFoundException e) {
  148. }
  149. } else if (iChannel.getChannelClient(iClient) == null) {
  150. // This is only done if we are already the channel, and it isn't us that
  151. // joined.
  152. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Adding client to channel.");
  153. final IRCChannelClientInfo iChannelClient = iChannel.addClient(iClient);
  154. callChannelJoin(iChannel, iChannelClient);
  155. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Added client to channel.");
  156. return;
  157. } else {
  158. // Client joined channel that we already know of.
  159. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Not adding client to channel they are already on.");
  160. return;
  161. }
  162. }
  163. iChannel = new IRCChannelInfo(parser, prefixModeManager, userModeManager,
  164. chanModeManager, channelName);
  165. // Add ourself to the channel, this will be overridden by the NAMES reply
  166. iChannel.addClient(iClient);
  167. parser.addChannel(iChannel);
  168. sendString("MODE " + iChannel.getName(), QueuePriority.LOW);
  169. final String pendingJoin = pendingJoins.poll();
  170. final String pendingJoinKey = pendingJoinKeys.poll();
  171. if (pendingJoin != null && pendingJoin.equalsIgnoreCase(channelName)) {
  172. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Guessing channel Key: " + pendingJoin + " -> " + pendingJoinKey);
  173. iChannel.setInternalPassword(pendingJoinKey == null ? "" : pendingJoinKey);
  174. } else {
  175. // Out of sync, clear
  176. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: pending join keys out of sync (Got: " + pendingJoin + ", Wanted: " + channelName + ") - Clearing.");
  177. pendingJoins.clear();
  178. pendingJoinKeys.clear();
  179. }
  180. callChannelSelfJoin(iChannel);
  181. } else {
  182. // Some kind of failed to join, pop the pending join queues.
  183. final String pendingJoin = pendingJoins.poll();
  184. final String pendingJoinKey = pendingJoinKeys.poll();
  185. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Failed to join channel (" + sParam + ") - Skipping " + pendingJoin + " (" + pendingJoinKey + ")");
  186. }
  187. }
  188. @Handler(condition = "msg.action == 'JOIN'")
  189. public void handleDataOut(final IRCDataOutEvent event) {
  190. // As long as this is called before the resulting DataIn
  191. // Processors fire then this will work, otherwise we'll end
  192. // up with an out-of-sync pendingJoins list.
  193. final String[] newLine = event.getTokenisedData();
  194. if (newLine.length > 1) {
  195. final Queue<String> keys = new LinkedList<>();
  196. if (newLine.length > 2) {
  197. keys.addAll(Arrays.asList(newLine[2].split(",")));
  198. }
  199. // PendingJoins and PendingJoinKeys should always be the same length (even if no key was given).
  200. //
  201. // We don't get any errors for channels we try to join that we are already in
  202. // But the IRCD will still swallow the key attempt.
  203. //
  204. // Make sure that we always have a guessed key for every channel (even if null) and that we
  205. // don't have guesses for channels we are already in.
  206. for (final String chan : newLine[1].split(",")) {
  207. final String key = keys.poll();
  208. if (getChannel(chan) == null) {
  209. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Intercepted possible channel Key: " + chan + " -> " + key);
  210. pendingJoins.add(chan);
  211. pendingJoinKeys.add(key);
  212. } else {
  213. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Ignoring possible channel Key for existing channel: " + chan + " -> " + key);
  214. }
  215. }
  216. }
  217. }
  218. /**
  219. * Callback to all objects implementing the ChannelJoin Callback.
  220. *
  221. * @param cChannel Channel Object
  222. * @param cChannelClient ChannelClient object for new person
  223. */
  224. protected void callChannelJoin(final ChannelInfo cChannel,
  225. final ChannelClientInfo cChannelClient) {
  226. getCallbackManager().publish(
  227. new ChannelJoinEvent(parser, LocalDateTime.now(), cChannel, cChannelClient));
  228. }
  229. /**
  230. * Callback to all objects implementing the ChannelSelfJoin Callback.
  231. *
  232. * @param cChannel Channel Object
  233. */
  234. protected void callChannelSelfJoin(final ChannelInfo cChannel) {
  235. getCallbackManager().publish(new ChannelSelfJoinEvent(
  236. parser, LocalDateTime.now(), cChannel));
  237. }
  238. }