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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (c) 2006-2017 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 date The LocalDateTime that this event occurred at.
  79. * @param sParam Type of line to process ("JOIN")
  80. * @param token IRCTokenised line to process
  81. */
  82. @Override
  83. public void process(final LocalDateTime date, final String sParam, final String... token) {
  84. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: %s | %s", sParam, Arrays.toString(token));
  85. if ("329".equals(sParam)) {
  86. if (token.length < 5) {
  87. return;
  88. }
  89. final IRCChannelInfo iChannel = parser.getChannel(token[3]);
  90. if (iChannel != null) {
  91. try {
  92. iChannel.setCreateTime(Integer.parseInt(token[4]));
  93. } catch (NumberFormatException nfe) {
  94. // Oh well, not a normal ircd I guess
  95. }
  96. }
  97. } else if ("JOIN".equals(sParam)) {
  98. // :nick!ident@host JOIN (:)#Channel
  99. if (token.length < 3) {
  100. return;
  101. }
  102. final boolean extendedJoin = parser.getCapabilityState("extended-join") == CapabilityState.ENABLED;
  103. IRCClientInfo iClient = getClientInfo(token[0]);
  104. final String realName;
  105. final String accountName;
  106. final String channelName;
  107. if (extendedJoin) {
  108. // :nick!ident@host JOIN #Channel accountName :Real Name
  109. channelName = token[2];
  110. accountName = token.length > 3 ? token[3] : "*";
  111. realName = token.length > 4 ? token[token.length - 1] : "";
  112. } else {
  113. channelName = token[token.length - 1];
  114. accountName = "*";
  115. realName = "";
  116. }
  117. IRCChannelInfo iChannel = parser.getChannel(token[2]);
  118. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: client: %s", iClient);
  119. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: channel: %s", iChannel);
  120. if (iClient == null) {
  121. iClient = new IRCClientInfo(parser, userModeManager, token[0]);
  122. parser.addClient(iClient);
  123. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: new client.", iClient);
  124. }
  125. if (extendedJoin) {
  126. iClient.setAccountName("*".equals(accountName) ? null : accountName);
  127. iClient.setRealName(realName);
  128. }
  129. // Check to see if we know the host/ident for this client to facilitate dmdirc Formatter
  130. if (iClient.getHostname().isEmpty()) {
  131. iClient.setUserBits(token[0], false);
  132. }
  133. if (iChannel != null) {
  134. if (iClient == parser.getLocalClient()) {
  135. try {
  136. if (iChannel.getChannelClient(iClient) == null) {
  137. // Otherwise we have a channel known, that we are not in?
  138. parser.callErrorInfo(new ParserError(ParserError.ERROR_FATAL, "Joined known channel that we wern't already on..", parser.getLastLine()));
  139. } else {
  140. // If we are joining a channel we are already on, fake a part from
  141. // the channel internally, and rejoin.
  142. parser.getProcessingManager().process(date, "PART", token);
  143. }
  144. } catch (ProcessorNotFoundException e) {
  145. }
  146. } else if (iChannel.getChannelClient(iClient) == null) {
  147. // This is only done if we are already the channel, and it isn't us that
  148. // joined.
  149. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Adding client to channel.");
  150. final IRCChannelClientInfo iChannelClient = iChannel.addClient(iClient);
  151. callChannelJoin(date, iChannel, iChannelClient);
  152. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Added client to channel.");
  153. return;
  154. } else {
  155. // Client joined channel that we already know of.
  156. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Not adding client to channel they are already on.");
  157. return;
  158. }
  159. }
  160. iChannel = new IRCChannelInfo(parser, prefixModeManager, userModeManager,
  161. chanModeManager, channelName);
  162. // Add ourself to the channel, this will be overridden by the NAMES reply
  163. iChannel.addClient(iClient);
  164. parser.addChannel(iChannel);
  165. sendString("MODE " + iChannel.getName(), QueuePriority.LOW);
  166. final PendingJoin pendingJoin = pendingJoins.poll();
  167. if (pendingJoin != null && parser.getStringConverter().equalsIgnoreCase(pendingJoin.getChannel(), channelName)) {
  168. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Guessing channel Key: %s -> %s", pendingJoin.getChannel(), pendingJoin.getKey());
  169. iChannel.setInternalPassword(pendingJoin.getKey());
  170. } else {
  171. // Out of sync, clear
  172. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: pending join keys out of sync (Got: %s, Wanted: %s) - Clearing.", (pendingJoin == null ? pendingJoin : pendingJoin.getChannel()), channelName);
  173. pendingJoins.clear();
  174. }
  175. callChannelSelfJoin(date, iChannel);
  176. } else {
  177. // Some kind of failed to join, pop the pending join queues.
  178. final PendingJoin pendingJoin = pendingJoins.poll();
  179. if (pendingJoin != null && parser.getStringConverter().equalsIgnoreCase(pendingJoin.getChannel(), sParam)) {
  180. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Failed to join channel (%s) - Skipping %s (%s)", sParam, pendingJoin.getChannel(), pendingJoin.getKey());
  181. } else {
  182. // Out of sync, clear
  183. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Failed to join channel (%s) - pending join keys out of sync (Got: %s, Wanted: %s) - Clearing.", sParam, (pendingJoin == null ? pendingJoin : pendingJoin.getChannel()), sParam);
  184. pendingJoins.clear();
  185. }
  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. // We don't get any errors for channels we try to join that we are already in
  200. // But the IRCD will still swallow the key attempt.
  201. //
  202. // Make sure that we always have a guessed key for every channel (even if null) and that we
  203. // don't have guesses for channels we are already in.
  204. for (final String chan : newLine[1].split(",")) {
  205. final String key = keys.poll();
  206. if (chan.equals("0")) {
  207. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Ignoring possible channel Key for part-all channel: %s -> %s", chan, key);
  208. } else if (getChannel(chan) == null) {
  209. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Intercepted possible channel Key: %s -> %s", chan, key);
  210. pendingJoins.add(new PendingJoin(chan, key));
  211. } else {
  212. callDebugInfo(IRCParser.DEBUG_INFO, "processJoin: Ignoring possible channel Key for existing channel: %s -> %s", chan, key);
  213. }
  214. }
  215. }
  216. }
  217. /**
  218. * Callback to all objects implementing the ChannelJoin Callback.
  219. *
  220. * @param date The LocalDateTime that this event occurred at.
  221. * @param cChannel Channel Object
  222. * @param cChannelClient ChannelClient object for new person
  223. */
  224. protected void callChannelJoin(final LocalDateTime date, final ChannelInfo cChannel,
  225. final ChannelClientInfo cChannelClient) {
  226. getCallbackManager().publish(
  227. new ChannelJoinEvent(parser, date, cChannel, cChannelClient));
  228. }
  229. /**
  230. * Callback to all objects implementing the ChannelSelfJoin Callback.
  231. *
  232. * @param date The LocalDateTime that this event occurred at.
  233. * @param cChannel Channel Object
  234. */
  235. protected void callChannelSelfJoin(final LocalDateTime date, final ChannelInfo cChannel) {
  236. getCallbackManager().publish(new ChannelSelfJoinEvent(
  237. parser, date, cChannel));
  238. }
  239. /** Class to link channels to pending keys. */
  240. private static class PendingJoin {
  241. /** Channel name. */
  242. private final String channel;
  243. /** Guessed Key. */
  244. private final String key;
  245. /**
  246. * Create a new PendingJoin
  247. *
  248. * @param channel Channel
  249. * @param key Guessed Key (if there is one)
  250. */
  251. public PendingJoin(final String channel, final String key) {
  252. this.channel = channel;
  253. this.key = (key == null ? "" : key);
  254. }
  255. /**
  256. * Get the channel name.
  257. *
  258. * @return Channel name,
  259. */
  260. public String getChannel() {
  261. return channel;
  262. }
  263. /**
  264. * Get the key
  265. *
  266. * @return Key
  267. */
  268. public String getKey() {
  269. return key;
  270. }
  271. }
  272. }