Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

JoinChannelCommand.java 7.3KB

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