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

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