Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

JoinChannelCommand.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.commandparser.commands.server;
  18. import com.dmdirc.commandparser.BaseCommandInfo;
  19. import com.dmdirc.commandparser.CommandArguments;
  20. import com.dmdirc.commandparser.CommandInfo;
  21. import com.dmdirc.commandparser.CommandType;
  22. import com.dmdirc.commandparser.commands.BaseCommand;
  23. import com.dmdirc.commandparser.commands.IntelligentCommand;
  24. import com.dmdirc.commandparser.commands.context.CommandContext;
  25. import com.dmdirc.commandparser.commands.context.ServerCommandContext;
  26. import com.dmdirc.events.ClientLineAddedEvent;
  27. import com.dmdirc.interfaces.CommandController;
  28. import com.dmdirc.interfaces.Connection;
  29. import com.dmdirc.events.eventbus.EventBus;
  30. import com.dmdirc.interfaces.WindowModel;
  31. import com.dmdirc.parser.common.ChannelJoinRequest;
  32. import com.dmdirc.ui.WindowManager;
  33. import com.dmdirc.ui.input.AdditionalTabTargets;
  34. import com.dmdirc.ui.messages.Styliser;
  35. import com.google.common.collect.ArrayListMultimap;
  36. import com.google.common.collect.Multimap;
  37. import net.engio.mbassy.listener.Handler;
  38. import javax.annotation.Nonnull;
  39. import javax.annotation.concurrent.GuardedBy;
  40. import javax.inject.Inject;
  41. import java.util.ArrayList;
  42. import java.util.List;
  43. import java.util.Optional;
  44. import java.util.stream.Collectors;
  45. /**
  46. * Allows the user to join channels.
  47. *
  48. * @since 0.6.3m1
  49. */
  50. public class JoinChannelCommand extends BaseCommand implements IntelligentCommand {
  51. /** A command info object for this command. */
  52. public static final CommandInfo INFO = new BaseCommandInfo("join",
  53. "join <channel [key]>[,channel [key]...] - joins the specified channel(s)",
  54. CommandType.TYPE_SERVER);
  55. /** A map of channel name mentions. */
  56. @GuardedBy("mentionsLock")
  57. private final Multimap<WindowModel, String> mentions = ArrayListMultimap.create();
  58. /** Lock to synchronise on when accessing mentions. */
  59. private final Object mentionsLock = new Object();
  60. private final WindowManager windowManager;
  61. /**
  62. * Creates a new instance of the join channel command.
  63. *
  64. * @param controller The controller to use to retrieve command information.
  65. * @param eventBus The bus to listen on for events.
  66. */
  67. @Inject
  68. public JoinChannelCommand(
  69. final CommandController controller,
  70. final WindowManager windowManager,
  71. final EventBus eventBus) {
  72. super(controller);
  73. this.windowManager = windowManager;
  74. eventBus.subscribe(this);
  75. }
  76. @Override
  77. public void execute(@Nonnull final WindowModel origin,
  78. final CommandArguments args, final CommandContext context) {
  79. final Connection connection = ((ServerCommandContext) context).getConnection();
  80. if (args.getArguments().length == 0) {
  81. showUsage(origin, args.isSilent(), "join", "join <channel [key]>[,channel [key]...]");
  82. return;
  83. }
  84. final List<ChannelJoinRequest> channels = new ArrayList<>();
  85. for (String pair : args.getArgumentsAsString().split(",")) {
  86. final int index = pair.trim().indexOf(' ');
  87. if (index == -1) {
  88. channels.add(new ChannelJoinRequest(pair));
  89. } else {
  90. channels.add(new ChannelJoinRequest(pair.substring(0, index),
  91. pair.substring(index + 1)));
  92. }
  93. }
  94. connection.getGroupChatManager()
  95. .join(!args.isSilent(), channels.toArray(new ChannelJoinRequest[channels.size()]));
  96. }
  97. @Handler
  98. public void handleClientLineAdded(final ClientLineAddedEvent event) {
  99. final String[] parts = event.getFrameContainer().getBackBuffer().getStyliser()
  100. .doLinks(event.getLine())
  101. .split(Character.toString(Styliser.CODE_CHANNEL));
  102. synchronized (mentionsLock) {
  103. for (int i = 1; i < parts.length; i += 2) {
  104. // All of the odd parts of the array are channel names
  105. mentions.put(event.getFrameContainer(), parts[i]);
  106. }
  107. }
  108. }
  109. @Override
  110. public AdditionalTabTargets getSuggestions(final int arg,
  111. final IntelligentCommandContext context) {
  112. final WindowModel source = context.getWindow();
  113. final Connection connection = source.getConnection().get();
  114. final List<String> results = checkSource(source, true, true);
  115. final AdditionalTabTargets targets = new AdditionalTabTargets().excludeAll();
  116. final String prefix;
  117. final int index;
  118. if ((index = context.getPartial().lastIndexOf(',')) > -1) {
  119. // If they are tab completing something containing a comma, we
  120. // add our results after the comma instead of returning them as-is.
  121. prefix = context.getPartial().substring(0, index + 1);
  122. } else {
  123. prefix = "";
  124. }
  125. final boolean showExisting = source.getConfigManager()
  126. .getOptionBool("commands", "join-tabexistingchannels");
  127. if (!showExisting) {
  128. // Only tab complete channels we're not already on
  129. targets.addAll(results.stream()
  130. .filter(result -> !connection.getGroupChatManager()
  131. .getChannel(result).isPresent())
  132. .map(result -> prefix + result).collect(Collectors.toList()));
  133. }
  134. for (char chPrefix : connection.getGroupChatManager().getChannelPrefixes().toCharArray()) {
  135. // Let them tab complete the prefixes as well
  136. targets.add(prefix + chPrefix);
  137. }
  138. return targets;
  139. }
  140. /**
  141. * Checks a hierarchy of frame containers for channels which have been mentioned.
  142. *
  143. * @param source The base frame container to check
  144. * @param checkParents Whether or not to check that frame's parents
  145. * @param checkChildren Whether or not to check that frame's children
  146. *
  147. * @return A list of channel names which have been mentioned in the hierarchy
  148. *
  149. * @since 0.6.4
  150. */
  151. protected List<String> checkSource(final WindowModel source,
  152. final boolean checkParents, final boolean checkChildren) {
  153. final List<String> results = new ArrayList<>();
  154. // Check the window itself
  155. synchronized (mentionsLock) {
  156. if (mentions.containsKey(source)) {
  157. results.addAll(mentions.get(source));
  158. }
  159. }
  160. // Check the parent window
  161. final Optional<WindowModel> parent = windowManager.getParent(source);
  162. if (checkParents && parent.isPresent()) {
  163. results.addAll(checkSource(parent.get(), true, false));
  164. }
  165. // Check the children window
  166. if (checkChildren) {
  167. for (WindowModel child : windowManager.getChildren(source)) {
  168. results.addAll(checkSource(child, false, true));
  169. }
  170. }
  171. return results;
  172. }
  173. }