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.

NewServer.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2006-2014 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.global;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.Server;
  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.interfaces.CommandController;
  33. import com.dmdirc.interfaces.ServerFactory;
  34. import com.dmdirc.interfaces.config.IdentityController;
  35. import com.dmdirc.logger.ErrorLevel;
  36. import com.dmdirc.logger.Logger;
  37. import com.dmdirc.plugins.PluginManager;
  38. import com.dmdirc.plugins.Service;
  39. import com.dmdirc.ui.input.AdditionalTabTargets;
  40. import com.dmdirc.util.InvalidURIException;
  41. import com.dmdirc.util.URIParser;
  42. import java.net.URI;
  43. import java.net.URISyntaxException;
  44. import java.util.regex.Matcher;
  45. import java.util.regex.Pattern;
  46. import javax.inject.Inject;
  47. /**
  48. * The new server command allows users to open a new server window.
  49. */
  50. public class NewServer extends Command implements IntelligentCommand {
  51. /** A command info object for this command. */
  52. public static final CommandInfo INFO = new BaseCommandInfo("newserver",
  53. "newserver <host[:[+]port]> [password] - connect to a new server",
  54. CommandType.TYPE_GLOBAL);
  55. /** The factory to use to construct servers. */
  56. private final ServerFactory serverFactory;
  57. /** Plugin manager to query for available services. */
  58. private final PluginManager pluginManager;
  59. /** Identity controller to use to find profiles. */
  60. private final IdentityController identityController;
  61. /** The parser to use for user input. */
  62. private final URIParser uriParser;
  63. /**
  64. * Creates a new newserver command which will use the specified factory to construct servers.
  65. *
  66. * @param controller The controller to use for command information.
  67. * @param serverFactory The factory to use to construct servers.
  68. * @param pluginManager The plugin manager to use to query available services.
  69. * @param identityController Identity controller to use to find profiles.
  70. * @param uriParser The parser to use for user input.
  71. */
  72. @Inject
  73. public NewServer(
  74. final CommandController controller,
  75. final ServerFactory serverFactory,
  76. final PluginManager pluginManager,
  77. final IdentityController identityController,
  78. final URIParser uriParser) {
  79. super(controller);
  80. this.serverFactory = serverFactory;
  81. this.pluginManager = pluginManager;
  82. this.identityController = identityController;
  83. this.uriParser = uriParser;
  84. }
  85. /** {@inheritDoc} */
  86. @Override
  87. public void execute(final FrameContainer origin,
  88. final CommandArguments args, final CommandContext context) {
  89. if (args.getArguments().length == 0) {
  90. showUsage(origin, args.isSilent(), "newserver", "<host[:[+]port]> [password]");
  91. return;
  92. }
  93. try {
  94. final URI address = uriParser.parseFromText(args.getArgumentsAsString());
  95. final Server server = serverFactory.createServer(address,
  96. identityController.getProvidersByType("profile").get(0));
  97. server.connect();
  98. } catch (InvalidURIException ex) {
  99. if (origin == null) {
  100. Logger.userError(ErrorLevel.LOW, "Invalid URI given to /newserver", ex);
  101. } else {
  102. origin.addLine(FORMAT_ERROR, "Invalid URI: " + ex.getMessage()
  103. + (ex.getCause() == null ? "" : ": " + ex.getCause().getMessage()));
  104. }
  105. }
  106. }
  107. /**
  108. * Get a URI from the given string. This method allows for +port in the uri (eg
  109. * irc://server.host:+6668/)
  110. *
  111. * @param address Address to parse
  112. *
  113. * @return URI from address.
  114. *
  115. * @throws URISyntaxException If the string is not parseable.
  116. * @deprecated Use a {@link URIParser}.
  117. */
  118. @Deprecated
  119. public static URI getURI(final String address) throws URISyntaxException {
  120. final URI uri = new URI(address);
  121. final int port = uri.getPort();
  122. // Either no port specified, or a +port was used, lets try to find one.
  123. // Otherwise just return the URI.
  124. if (port == -1) {
  125. final Matcher m = Pattern.compile(".*://[^/:]+:\\+([0-9]+).*").matcher(address);
  126. // If no port is found, then again just return the uri above, the
  127. // Parser can use its default.
  128. if (m.matches()) {
  129. m.find();
  130. int newPort = -1;
  131. try {
  132. newPort = Integer.parseInt(m.group(2));
  133. } catch (final NumberFormatException nfe) {
  134. }
  135. // Add 's' to scheme if not already there.
  136. String scheme = uri.getScheme();
  137. if (scheme.charAt(scheme.length() - 1) != 's') {
  138. scheme += "s";
  139. }
  140. return new URI(scheme, uri.getUserInfo(), uri.getHost(), newPort, uri.getPath(),
  141. uri.getQuery(), uri.getFragment());
  142. }
  143. }
  144. // If a port already existed, or we were unable to find a port, just
  145. // use the default one we had to start with.
  146. return uri;
  147. }
  148. /** {@inheritDoc} */
  149. @Override
  150. public AdditionalTabTargets getSuggestions(final int arg,
  151. final IntelligentCommandContext context) {
  152. final AdditionalTabTargets res = new AdditionalTabTargets();
  153. if (arg == 0) {
  154. for (Service parserType : pluginManager.getServicesByType("parser")) {
  155. res.add(parserType.getName() + "://");
  156. }
  157. }
  158. res.excludeAll();
  159. return res;
  160. }
  161. }