Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ProcessJoin.java 13KB

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