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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.global;
  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.config.profiles.ProfileManager;
  26. import com.dmdirc.events.CommandErrorEvent;
  27. import com.dmdirc.interfaces.CommandController;
  28. import com.dmdirc.interfaces.Connection;
  29. import com.dmdirc.interfaces.ConnectionFactory;
  30. import com.dmdirc.interfaces.WindowModel;
  31. import com.dmdirc.plugins.ServiceManager;
  32. import com.dmdirc.ui.input.AdditionalTabTargets;
  33. import com.dmdirc.util.InvalidURIException;
  34. import com.dmdirc.util.URIParser;
  35. import javax.annotation.Nonnull;
  36. import javax.inject.Inject;
  37. import java.net.URI;
  38. import java.util.stream.Collectors;
  39. /**
  40. * The new server command allows users to open a new server window.
  41. */
  42. public class NewServer extends BaseCommand implements IntelligentCommand {
  43. /** A command info object for this command. */
  44. public static final CommandInfo INFO = new BaseCommandInfo("newserver",
  45. "newserver <host[:[+]port]> [password] - connect to a new server",
  46. CommandType.TYPE_GLOBAL);
  47. /** The factory to use to construct servers. */
  48. private final ConnectionFactory connectionFactory;
  49. /** Service manager to query for available services. */
  50. private final ServiceManager serviceManager;
  51. /** Manager to use to find profiles. */
  52. private final ProfileManager profileManager;
  53. /** The parser to use for user input. */
  54. private final URIParser uriParser;
  55. /**
  56. * Creates a new newserver command which will use the specified factory to construct servers.
  57. */
  58. @Inject
  59. public NewServer(
  60. final CommandController controller,
  61. final ConnectionFactory connectionFactory,
  62. final ServiceManager serviceManager,
  63. final ProfileManager profileManager,
  64. final URIParser uriParser) {
  65. super(controller);
  66. this.connectionFactory = connectionFactory;
  67. this.serviceManager = serviceManager;
  68. this.profileManager = profileManager;
  69. this.uriParser = uriParser;
  70. }
  71. @Override
  72. public void execute(@Nonnull final WindowModel origin,
  73. final CommandArguments args, final CommandContext context) {
  74. if (args.getArguments().length == 0) {
  75. showUsage(origin, args.isSilent(), "newserver", "<host[:[+]port]> [password]");
  76. return;
  77. }
  78. try {
  79. final URI address = uriParser.parseFromText(args.getArgumentsAsString());
  80. final Connection server = connectionFactory.createServer(address,
  81. profileManager.getDefault());
  82. server.connect();
  83. } catch (InvalidURIException ex) {
  84. origin.getEventBus().publishAsync(new CommandErrorEvent(origin,
  85. "Invalid URI: " + ex.getMessage() +
  86. (ex.getCause() == null ? "" : ": " + ex.getCause().getMessage())));
  87. }
  88. }
  89. @Override
  90. public AdditionalTabTargets getSuggestions(final int arg,
  91. final IntelligentCommandContext context) {
  92. final AdditionalTabTargets res = new AdditionalTabTargets();
  93. if (arg == 0) {
  94. res.addAll(serviceManager.getServicesByType("parser").stream()
  95. .map(parserType -> parserType.getName() + "://").collect(Collectors.toList()));
  96. }
  97. res.excludeAll();
  98. return res;
  99. }
  100. }