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.

JoinChannelCommand.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.FrameContainer;
  24. import com.dmdirc.Server;
  25. import com.dmdirc.actions.ActionManager;
  26. import com.dmdirc.actions.CoreActionType;
  27. import com.dmdirc.actions.interfaces.ActionType;
  28. import com.dmdirc.commandparser.BaseCommandInfo;
  29. import com.dmdirc.commandparser.CommandArguments;
  30. import com.dmdirc.commandparser.CommandInfo;
  31. import com.dmdirc.commandparser.CommandType;
  32. import com.dmdirc.commandparser.commands.Command;
  33. import com.dmdirc.commandparser.commands.IntelligentCommand;
  34. import com.dmdirc.commandparser.commands.context.CommandContext;
  35. import com.dmdirc.commandparser.commands.context.ServerCommandContext;
  36. import com.dmdirc.interfaces.ActionListener;
  37. import com.dmdirc.parser.common.ChannelJoinRequest;
  38. import com.dmdirc.ui.input.AdditionalTabTargets;
  39. import com.dmdirc.ui.messages.Styliser;
  40. import com.dmdirc.util.MapList;
  41. import java.util.ArrayList;
  42. import java.util.List;
  43. /**
  44. * Allows the user to join channels.
  45. *
  46. * @since 0.6.3m1
  47. */
  48. public class JoinChannelCommand extends Command implements
  49. ActionListener, IntelligentCommand {
  50. /** A command info object for this command. */
  51. public static final CommandInfo INFO = new BaseCommandInfo("join",
  52. "join <channel [key]>[,channel [key]...] - joins the specified channel(s)",
  53. CommandType.TYPE_SERVER);
  54. /** A map of channel name mentions. */
  55. private final MapList<FrameContainer, String> mentions
  56. = new MapList<FrameContainer, String>();
  57. /**
  58. * Creates a new instance of the join channel command.
  59. */
  60. public JoinChannelCommand() {
  61. super();
  62. ActionManager.getActionManager().registerListener(this,
  63. CoreActionType.CLIENT_LINE_ADDED);
  64. }
  65. /** {@inheritDoc} */
  66. @Override
  67. public void execute(final FrameContainer origin,
  68. final CommandArguments args, final CommandContext context) {
  69. final Server server = ((ServerCommandContext) context).getServer();
  70. if (args.getArguments().length == 0) {
  71. showUsage(origin, args.isSilent(), "join", "join <channel [key]>[,channel [key]...]");
  72. return;
  73. }
  74. final List<ChannelJoinRequest> channels = new ArrayList<ChannelJoinRequest>();
  75. for (String pair : args.getArgumentsAsString().split(",")) {
  76. final int index = pair.trim().indexOf(' ');
  77. if (index == -1) {
  78. channels.add(new ChannelJoinRequest(pair));
  79. } else {
  80. channels.add(new ChannelJoinRequest(pair.substring(0, index),
  81. pair.substring(index + 1)));
  82. }
  83. }
  84. server.join(!args.isSilent(), channels.toArray(new ChannelJoinRequest[0]));
  85. }
  86. /** {@inheritDoc} */
  87. @Override
  88. public void processEvent(final ActionType type, final StringBuffer format,
  89. final Object... arguments) {
  90. final FrameContainer source = (FrameContainer) arguments[0];
  91. final String message = (String) arguments[1];
  92. final String[] parts = source.getStyliser().doLinks(message)
  93. .split("" + Styliser.CODE_CHANNEL);
  94. int i = 1;
  95. for (i = 1; i < parts.length; i += 2) {
  96. // All of the odd parts of the array are channel names
  97. mentions.add(source, parts[i]);
  98. }
  99. }
  100. /** {@inheritDoc} */
  101. @Override
  102. public AdditionalTabTargets getSuggestions(final int arg,
  103. final IntelligentCommandContext context) {
  104. final FrameContainer source = context.getWindow();
  105. final Server server = source.getServer();
  106. final List<String> results = checkSource(source, true, true);
  107. final AdditionalTabTargets targets = new AdditionalTabTargets().excludeAll();
  108. final String prefix;
  109. final int index;
  110. if ((index = context.getPartial().lastIndexOf(',')) > -1) {
  111. // If they are tab completing something containing a comma, we
  112. // add our results after the comma instead of returning them as-is.
  113. prefix = context.getPartial().substring(0, index + 1);
  114. } else {
  115. prefix = "";
  116. }
  117. final boolean showExisting = source.getConfigManager()
  118. .getOptionBool("commands", "join-tabexistingchannels");
  119. if (!showExisting) {
  120. for (String result : results) {
  121. // Only tab complete channels we're not already on
  122. if (!server.hasChannel(result)) {
  123. targets.add(prefix + result);
  124. }
  125. }
  126. }
  127. for (char chPrefix : server.getChannelPrefixes().toCharArray()) {
  128. // Let them tab complete the prefixes as well
  129. targets.add(prefix + chPrefix);
  130. }
  131. return targets;
  132. }
  133. /**
  134. * Checks a hierarchy of frame containers for channels which have been
  135. * 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. * @return A list of channel names which have been mentioned in the hierarchy
  141. * @since 0.6.4
  142. */
  143. protected List<String> checkSource(final FrameContainer source,
  144. final boolean checkParents, final boolean checkChildren) {
  145. final List<String> results = new ArrayList<String>();
  146. // Check the window itself
  147. if (mentions.containsKey(source)) {
  148. results.addAll(mentions.get(source));
  149. }
  150. // Check the parent window
  151. if (checkParents && source.getParent() != null) {
  152. results.addAll(checkSource(source.getParent(), true, false));
  153. }
  154. // Check the children window
  155. if (checkChildren) {
  156. for (FrameContainer child : source.getChildren()) {
  157. results.addAll(checkSource(child, false, true));
  158. }
  159. }
  160. return results;
  161. }
  162. }