Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

JoinChannelCommand.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.commandparser.commands.server;
  23. import com.dmdirc.commandparser.BaseCommandInfo;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.CommandInfo;
  26. import com.dmdirc.commandparser.CommandType;
  27. import com.dmdirc.commandparser.commands.BaseCommand;
  28. import com.dmdirc.commandparser.commands.IntelligentCommand;
  29. import com.dmdirc.commandparser.commands.context.CommandContext;
  30. import com.dmdirc.commandparser.commands.context.ServerCommandContext;
  31. import com.dmdirc.events.ClientLineAddedEvent;
  32. import com.dmdirc.interfaces.CommandController;
  33. import com.dmdirc.interfaces.Connection;
  34. import com.dmdirc.interfaces.EventBus;
  35. import com.dmdirc.interfaces.WindowModel;
  36. import com.dmdirc.parser.common.ChannelJoinRequest;
  37. import com.dmdirc.ui.WindowManager;
  38. import com.dmdirc.ui.input.AdditionalTabTargets;
  39. import com.dmdirc.ui.messages.Styliser;
  40. import com.google.common.collect.ArrayListMultimap;
  41. import com.google.common.collect.Multimap;
  42. import net.engio.mbassy.listener.Handler;
  43. import javax.annotation.Nonnull;
  44. import javax.annotation.concurrent.GuardedBy;
  45. import javax.inject.Inject;
  46. import java.util.ArrayList;
  47. import java.util.List;
  48. import java.util.Optional;
  49. import java.util.stream.Collectors;
  50. /**
  51. * Allows the user to join channels.
  52. *
  53. * @since 0.6.3m1
  54. */
  55. public class JoinChannelCommand extends BaseCommand implements IntelligentCommand {
  56. /** A command info object for this command. */
  57. public static final CommandInfo INFO = new BaseCommandInfo("join",
  58. "join <channel [key]>[,channel [key]...] - joins the specified channel(s)",
  59. CommandType.TYPE_SERVER);
  60. /** A map of channel name mentions. */
  61. @GuardedBy("mentionsLock")
  62. private final Multimap<WindowModel, String> mentions = ArrayListMultimap.create();
  63. /** Lock to synchronise on when accessing mentions. */
  64. private final Object mentionsLock = new Object();
  65. private final WindowManager windowManager;
  66. /**
  67. * Creates a new instance of the join channel command.
  68. *
  69. * @param controller The controller to use to retrieve command information.
  70. * @param eventBus The bus to listen on for events.
  71. */
  72. @Inject
  73. public JoinChannelCommand(
  74. final CommandController controller,
  75. final WindowManager windowManager,
  76. final EventBus eventBus) {
  77. super(controller);
  78. this.windowManager = windowManager;
  79. eventBus.subscribe(this);
  80. }
  81. @Override
  82. public void execute(@Nonnull final WindowModel origin,
  83. final CommandArguments args, final CommandContext context) {
  84. final Connection connection = ((ServerCommandContext) context).getConnection();
  85. if (args.getArguments().length == 0) {
  86. showUsage(origin, args.isSilent(), "join", "join <channel [key]>[,channel [key]...]");
  87. return;
  88. }
  89. final List<ChannelJoinRequest> channels = new ArrayList<>();
  90. for (String pair : args.getArgumentsAsString().split(",")) {
  91. final int index = pair.trim().indexOf(' ');
  92. if (index == -1) {
  93. channels.add(new ChannelJoinRequest(pair));
  94. } else {
  95. channels.add(new ChannelJoinRequest(pair.substring(0, index),
  96. pair.substring(index + 1)));
  97. }
  98. }
  99. connection.getGroupChatManager()
  100. .join(!args.isSilent(), channels.toArray(new ChannelJoinRequest[channels.size()]));
  101. }
  102. @Handler
  103. public void handleClientLineAdded(final ClientLineAddedEvent event) {
  104. final String[] parts = event.getFrameContainer().getBackBuffer().getStyliser()
  105. .doLinks(event.getLine())
  106. .split(Character.toString(Styliser.CODE_CHANNEL));
  107. synchronized (mentionsLock) {
  108. for (int i = 1; i < parts.length; i += 2) {
  109. // All of the odd parts of the array are channel names
  110. mentions.put(event.getFrameContainer(), parts[i]);
  111. }
  112. }
  113. }
  114. @Override
  115. public AdditionalTabTargets getSuggestions(final int arg,
  116. final IntelligentCommandContext context) {
  117. final WindowModel source = context.getWindow();
  118. final Connection connection = source.getConnection().get();
  119. final List<String> results = checkSource(source, true, true);
  120. final AdditionalTabTargets targets = new AdditionalTabTargets().excludeAll();
  121. final String prefix;
  122. final int index;
  123. if ((index = context.getPartial().lastIndexOf(',')) > -1) {
  124. // If they are tab completing something containing a comma, we
  125. // add our results after the comma instead of returning them as-is.
  126. prefix = context.getPartial().substring(0, index + 1);
  127. } else {
  128. prefix = "";
  129. }
  130. final boolean showExisting = source.getConfigManager()
  131. .getOptionBool("commands", "join-tabexistingchannels");
  132. if (!showExisting) {
  133. // Only tab complete channels we're not already on
  134. targets.addAll(results.stream()
  135. .filter(result -> !connection.getGroupChatManager()
  136. .getChannel(result).isPresent())
  137. .map(result -> prefix + result).collect(Collectors.toList()));
  138. }
  139. for (char chPrefix : connection.getGroupChatManager().getChannelPrefixes().toCharArray()) {
  140. // Let them tab complete the prefixes as well
  141. targets.add(prefix + chPrefix);
  142. }
  143. return targets;
  144. }
  145. /**
  146. * Checks a hierarchy of frame containers for channels which have been mentioned.
  147. *
  148. * @param source The base frame container to check
  149. * @param checkParents Whether or not to check that frame's parents
  150. * @param checkChildren Whether or not to check that frame's children
  151. *
  152. * @return A list of channel names which have been mentioned in the hierarchy
  153. *
  154. * @since 0.6.4
  155. */
  156. protected List<String> checkSource(final WindowModel source,
  157. final boolean checkParents, final boolean checkChildren) {
  158. final List<String> results = new ArrayList<>();
  159. // Check the window itself
  160. synchronized (mentionsLock) {
  161. if (mentions.containsKey(source)) {
  162. results.addAll(mentions.get(source));
  163. }
  164. }
  165. // Check the parent window
  166. final Optional<WindowModel> parent = windowManager.getParent(source);
  167. if (checkParents && parent.isPresent()) {
  168. results.addAll(checkSource(parent.get(), true, false));
  169. }
  170. // Check the children window
  171. if (checkChildren) {
  172. for (WindowModel child : windowManager.getChildren(source)) {
  173. results.addAll(checkSource(child, false, true));
  174. }
  175. }
  176. return results;
  177. }
  178. }