您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

NewServer.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.global;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.Server;
  25. import com.dmdirc.commandparser.CommandArguments;
  26. import com.dmdirc.commandparser.CommandInfo;
  27. import com.dmdirc.commandparser.CommandType;
  28. import com.dmdirc.commandparser.commands.Command;
  29. import com.dmdirc.commandparser.commands.IntelligentCommand;
  30. import com.dmdirc.commandparser.commands.context.CommandContext;
  31. import com.dmdirc.config.IdentityManager;
  32. import com.dmdirc.logger.ErrorLevel;
  33. import com.dmdirc.logger.Logger;
  34. import com.dmdirc.plugins.PluginManager;
  35. import com.dmdirc.plugins.Service;
  36. import com.dmdirc.ui.input.AdditionalTabTargets;
  37. import java.net.URI;
  38. import java.net.URISyntaxException;
  39. import java.util.regex.Matcher;
  40. import java.util.regex.Pattern;
  41. /**
  42. * The new server command allows users to open a new server window.
  43. */
  44. public class NewServer extends Command implements IntelligentCommand,
  45. CommandInfo {
  46. /** {@inheritDoc} */
  47. @Override
  48. public void execute(final FrameContainer origin,
  49. final CommandArguments args, final CommandContext context) {
  50. URI address = null;
  51. if (args.getArguments().length == 0) {
  52. showUsage(origin, args.isSilent(), "newserver",
  53. "<host[:[+]port]> [password]");
  54. return;
  55. } else if (args.getArguments().length == 1
  56. && args.getArgumentsAsString().contains("://")) {
  57. try {
  58. address = getURI(args.getArgumentsAsString());
  59. } catch (URISyntaxException ex) {
  60. origin.addLine(FORMAT_ERROR, "URI specified was invalid.");
  61. return;
  62. }
  63. }
  64. if (address == null) {
  65. address = parseInput(origin, args.isSilent(), args);
  66. }
  67. if (address == null) {
  68. return;
  69. }
  70. final Server server = new Server(address, IdentityManager
  71. .getCustomIdentities("profile").get(0));
  72. server.connect();
  73. }
  74. /**
  75. * Get a URI from the given string.
  76. * This method allows for +port in the uri (eg irc://server.host:+6668/)
  77. *
  78. * @param address Address to parse
  79. * @return URI from address.
  80. * @throws URISyntaxException If the string is not parseable.
  81. */
  82. public static URI getURI(final String address) throws URISyntaxException {
  83. final URI uri = new URI(address);
  84. final int port = uri.getPort();
  85. // Either no port specified, or a +port was used, lets try to find one.
  86. // Otherwise just return the URI.
  87. if (port == -1) {
  88. final Matcher m = Pattern.compile(".*://[^/:]+:\\+([0-9]+).*").matcher(address);
  89. // If no port is found, then again just return the uri above, the
  90. // Parser can use its default.
  91. if (m.matches()) {
  92. m.find();
  93. int newPort = -1;
  94. try {
  95. newPort = Integer.parseInt(m.group(2));
  96. } catch (final NumberFormatException nfe) { }
  97. // Add 's' to scheme if not already there.
  98. String scheme = uri.getScheme();
  99. if (scheme.charAt(scheme.length() - 1) != 's') {
  100. scheme += "s";
  101. }
  102. return new URI(scheme, uri.getUserInfo(), uri.getHost(), newPort, uri.getPath(), uri.getQuery(), uri.getFragment());
  103. }
  104. }
  105. // If a port already existed, or we were unable to find a port, just
  106. // use the default one we had to start with.
  107. return uri;
  108. }
  109. /**
  110. * Parses an input string and attempts to create a URI from it.
  111. *
  112. * @param origin origin input window
  113. * @param isSilent is this a silent command
  114. * @param args command arguments
  115. *
  116. * @return URI is input was valid
  117. */
  118. public static URI parseInput(final FrameContainer origin, final boolean isSilent,
  119. final CommandArguments args) {
  120. boolean ssl = false;
  121. String host = "";
  122. String pass = null;
  123. int port = 6667;
  124. int offset = 0;
  125. // Check for SSL
  126. if (args.getArguments()[offset].equalsIgnoreCase("--ssl")) {
  127. Logger.userError(ErrorLevel.LOW,
  128. "Using /newserver --ssl is deprecated, and may be removed in the future."
  129. + " Use /newserver <host>:+<port> instead.");
  130. ssl = true;
  131. offset++;
  132. }
  133. // Check for port
  134. if (args.getArguments()[offset].indexOf(':') > -1) {
  135. final String[] parts = args.getArguments()[offset].split(":");
  136. if (parts.length < 2) {
  137. if (origin != null) {
  138. origin.addLine(FORMAT_ERROR, "Invalid port specified");
  139. } else {
  140. Logger.userError(ErrorLevel.LOW, "Invalid port specified "
  141. + "in newserver command");
  142. }
  143. return null;
  144. }
  145. host = parts[0];
  146. if (parts[1].length() > 0 && parts[1].charAt(0) == '+') {
  147. ssl = true;
  148. parts[1] = parts[1].substring(1);
  149. }
  150. try {
  151. port = Integer.parseInt(parts[1]);
  152. } catch (NumberFormatException ex) {
  153. if (origin != null) {
  154. origin.addLine(FORMAT_ERROR, "Invalid port specified");
  155. } else {
  156. Logger.userError(ErrorLevel.LOW, "Invalid port specified "
  157. + "in newserver command");
  158. }
  159. return null;
  160. }
  161. if (port <= 0 || port > 65535) {
  162. if (origin == null) {
  163. Logger.userError(ErrorLevel.LOW, "Port must be between 1 "
  164. + "and 65535 in newserver command");
  165. } else if (!args.isSilent()) {
  166. origin.addLine(FORMAT_ERROR, "Port must be between 1 and 65535");
  167. }
  168. return null;
  169. }
  170. } else {
  171. host = args.getArguments()[offset];
  172. }
  173. // Check for password
  174. if (args.getArguments().length > ++offset) {
  175. pass = args.getArgumentsAsString(offset);
  176. }
  177. try {
  178. return new URI("irc" + (ssl ? "s" : ""), pass, host, port, null, null, null);
  179. } catch (URISyntaxException ex) {
  180. if (origin == null) {
  181. Logger.userError(ErrorLevel.LOW, "Invalid address provided to "
  182. + "newserver command. Host: " + host + ", Port: " + port, ex);
  183. } else if (!args.isSilent()) {
  184. origin.addLine(FORMAT_ERROR, "Invalid address specified.");
  185. }
  186. return null;
  187. }
  188. }
  189. /** {@inheritDoc} */
  190. @Override
  191. public String getName() {
  192. return "newserver";
  193. }
  194. /** {@inheritDoc} */
  195. @Override
  196. public boolean showInHelp() {
  197. return true;
  198. }
  199. /** {@inheritDoc} */
  200. @Override
  201. public CommandType getType() {
  202. return CommandType.TYPE_GLOBAL;
  203. }
  204. /** {@inheritDoc} */
  205. @Override
  206. public String getHelp() {
  207. return "newserver <host[:[+]port]> [password] - connect to a new server";
  208. }
  209. /** {@inheritDoc} */
  210. @Override
  211. public AdditionalTabTargets getSuggestions(final int arg,
  212. final IntelligentCommandContext context) {
  213. final AdditionalTabTargets res = new AdditionalTabTargets();
  214. if (arg == 0) {
  215. for (Service parserType : PluginManager.getPluginManager().getServicesByType("parser")) {
  216. res.add(parserType.getName()+"://");
  217. }
  218. }
  219. res.excludeAll();
  220. return res;
  221. }
  222. }