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.

SetCommand.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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.Channel;
  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.ChannelCommandContext;
  32. import com.dmdirc.commandparser.commands.context.CommandContext;
  33. import com.dmdirc.commandparser.commands.flags.CommandFlag;
  34. import com.dmdirc.commandparser.commands.flags.CommandFlagHandler;
  35. import com.dmdirc.commandparser.commands.flags.CommandFlagResult;
  36. import com.dmdirc.interfaces.CommandController;
  37. import com.dmdirc.interfaces.Connection;
  38. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  39. import com.dmdirc.interfaces.config.ConfigProvider;
  40. import com.dmdirc.interfaces.config.IdentityController;
  41. import com.dmdirc.interfaces.config.IdentityFactory;
  42. import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
  43. import com.dmdirc.ui.input.AdditionalTabTargets;
  44. import java.util.List;
  45. import java.util.Optional;
  46. import javax.annotation.Nonnull;
  47. import javax.annotation.Nullable;
  48. import javax.inject.Inject;
  49. /**
  50. * The set command allows the user to inspect and change global config settings.
  51. */
  52. public class SetCommand extends Command implements IntelligentCommand {
  53. /** A command info object for this command. */
  54. public static final CommandInfo INFO = new BaseCommandInfo("set",
  55. "set [--server|--channel] [domain [option [newvalue]]] - inspect or change configuration settings"
  56. + "\nset [--server|--channel] --append <domain> <option> <data> - appends data to the specified option"
  57. + "\nset [--server|--channel] --unset <domain> <option> - unsets the specified option",
  58. CommandType.TYPE_GLOBAL);
  59. /** The flag to indicate the set command should apply to a server's settings. */
  60. private final CommandFlag serverFlag = new CommandFlag("server");
  61. /** The flag to indicate the set command should apply to a channel's settings. */
  62. private final CommandFlag channelFlag = new CommandFlag("channel");
  63. /** The flag to indicate that the specified setting should be unset. */
  64. private final CommandFlag unsetFlag = new CommandFlag("unset", true, 0, 2);
  65. /** The flag to indicate that the specified setting should be appended to. */
  66. private final CommandFlag appendFlag = new CommandFlag("append", true, 0, 2);
  67. /** The command flag handler for this command. */
  68. private final CommandFlagHandler handler;
  69. /** The controller to use to set settings. */
  70. private final IdentityController identityController;
  71. /** The factory to use to create new identities. */
  72. private final IdentityFactory identityFactory;
  73. /**
  74. * Creates a new instance of Set.
  75. *
  76. * @param controller The controller to use for command information.
  77. * @param identityController The controller to use to set settings.
  78. * @param identityFactory The factory to use to create new identities.
  79. */
  80. @Inject
  81. public SetCommand(
  82. final CommandController controller,
  83. final IdentityController identityController,
  84. final IdentityFactory identityFactory) {
  85. super(controller);
  86. this.identityController = identityController;
  87. this.identityFactory = identityFactory;
  88. unsetFlag.addDisabled(appendFlag);
  89. appendFlag.addDisabled(unsetFlag);
  90. channelFlag.addDisabled(serverFlag);
  91. serverFlag.addDisabled(channelFlag);
  92. handler = new CommandFlagHandler(serverFlag, channelFlag, unsetFlag, appendFlag);
  93. }
  94. @Override
  95. public void execute(@Nonnull final FrameContainer origin,
  96. final CommandArguments args, final CommandContext context) {
  97. @Nullable final CommandFlagResult res = handler.process(origin, args);
  98. if (res == null) {
  99. return;
  100. }
  101. ConfigProvider identity = identityController.getUserSettings();
  102. AggregateConfigProvider manager = identityController.getGlobalConfiguration();
  103. final Optional<Connection> connection = origin.getOptionalConnection();
  104. if (res.hasFlag(serverFlag)) {
  105. if (!connection.isPresent()) {
  106. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  107. "Cannot use --server in this context");
  108. return;
  109. }
  110. identity = connection.get().getServerIdentity();
  111. manager = connection.get().getWindowModel().getConfigManager();
  112. }
  113. if (res.hasFlag(channelFlag)) {
  114. if (!(context instanceof ChannelCommandContext)) {
  115. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  116. "Cannot use --channel in this context");
  117. return;
  118. }
  119. final Channel channel = ((ChannelCommandContext) context).getChannel();
  120. identity = identityFactory.createChannelConfig(
  121. connection.get().getNetwork(), channel.getName());
  122. manager = channel.getConfigManager();
  123. }
  124. if (res.hasFlag(unsetFlag)) {
  125. final String[] arguments = res.getArguments(unsetFlag);
  126. doUnsetOption(origin, args.isSilent(), identity, arguments[0], arguments[1]);
  127. return;
  128. }
  129. if (res.hasFlag(appendFlag)) {
  130. final String[] arguments = res.getArguments(appendFlag);
  131. doAppendOption(origin, args.isSilent(), identity, manager,
  132. arguments[0], arguments[1], res.getArgumentsAsString());
  133. return;
  134. }
  135. final String[] arguments = res.getArguments();
  136. switch (arguments.length) {
  137. case 0:
  138. doDomainList(origin, args.isSilent(), manager);
  139. break;
  140. case 1:
  141. doOptionsList(origin, args.isSilent(), manager, arguments[0]);
  142. break;
  143. case 2:
  144. doShowOption(origin, args.isSilent(), manager, arguments[0],
  145. arguments[1]);
  146. break;
  147. default:
  148. doSetOption(origin, args.isSilent(), identity, arguments[0],
  149. arguments[1], res.getArgumentsAsString(2));
  150. break;
  151. }
  152. }
  153. /**
  154. * Shows the user a list of valid domains.
  155. *
  156. * @param origin The window the command was issued from
  157. * @param isSilent Whether or not the command is being silenced or not
  158. * @param manager The config manager to use to retrieve data
  159. */
  160. private void doDomainList(final FrameContainer origin, final boolean isSilent,
  161. final AggregateConfigProvider manager) {
  162. final StringBuilder output = new StringBuilder(67);
  163. output.append("Valid domains (use ");
  164. output.append(getController().getCommandChar());
  165. output.append("set <domain> to see options within a domain): ");
  166. for (String domain : manager.getDomains()) {
  167. output.append(domain);
  168. output.append(", ");
  169. }
  170. sendLine(origin, isSilent, FORMAT_OUTPUT, output.substring(0, output.length() - 2));
  171. }
  172. /**
  173. * Shows the user a list of valid options within a domain.
  174. *
  175. * @param origin The window the command was issued from
  176. * @param isSilent Whether or not the command is being silenced or not
  177. * @param manager The config manager to use to retrieve data
  178. * @param domain The domain to be inspected
  179. */
  180. private void doOptionsList(final FrameContainer origin,
  181. final boolean isSilent, final ReadOnlyConfigProvider manager, final String domain) {
  182. final StringBuilder output = new StringBuilder(24);
  183. output.append("Options in domain '");
  184. output.append(domain);
  185. output.append("': ");
  186. boolean found = false;
  187. for (String option : manager.getOptions(domain).keySet()) {
  188. output.append(option);
  189. output.append(", ");
  190. found = true;
  191. }
  192. if (found) {
  193. sendLine(origin, isSilent, FORMAT_OUTPUT, output.substring(0, output.length() - 2));
  194. } else {
  195. sendLine(origin, isSilent, FORMAT_ERROR,
  196. "There are no options in the domain '" + domain + "'.");
  197. }
  198. }
  199. /**
  200. * Shows the user the current value of one option.
  201. *
  202. * @param origin The window the command was issued from
  203. * @param isSilent Whether or not the command is being silenced or not
  204. * @param manager The config manager to use to retrieve data
  205. * @param domain The domain of the option
  206. * @param option The name of the option
  207. */
  208. private void doShowOption(final FrameContainer origin,
  209. final boolean isSilent, final ReadOnlyConfigProvider manager,
  210. final String domain, final String option) {
  211. if (manager.hasOptionString(domain, option)) {
  212. sendLine(origin, isSilent, FORMAT_OUTPUT, "The current value of "
  213. + domain + '.' + option + " is: " + manager.getOption(domain, option));
  214. } else {
  215. sendLine(origin, isSilent, FORMAT_ERROR, "Option not found: " + domain + '.' + option);
  216. }
  217. }
  218. /**
  219. * Sets the value of the specified option.
  220. *
  221. * @param origin The window the command was issued from
  222. * @param isSilent Whether or not the command is being silenced or not
  223. * @param identity The identity to use to set data
  224. * @param domain The domain of the option
  225. * @param option The name of the option
  226. * @param newvalue The value the option should be set to
  227. */
  228. private void doSetOption(final FrameContainer origin,
  229. final boolean isSilent, final ConfigProvider identity,
  230. final String domain, final String option, final String newvalue) {
  231. identity.setOption(domain, option, newvalue);
  232. sendLine(origin, isSilent, FORMAT_OUTPUT, domain + '.' + option
  233. + " has been set to: " + newvalue);
  234. }
  235. /**
  236. * Appends data to the specified option.
  237. *
  238. * @param origin The window the command was issued from
  239. * @param isSilent Whether or not the command is being silenced or not
  240. * @param identity The identity to use to set data
  241. * @param manager The config manager to use to retrieve data
  242. * @param domain The domain of the option
  243. * @param option The name of the option
  244. * @param data The data to be appended
  245. */
  246. private void doAppendOption(final FrameContainer origin,
  247. final boolean isSilent, final ConfigProvider identity,
  248. final ReadOnlyConfigProvider manager,
  249. final String domain, final String option, final String data) {
  250. doSetOption(origin, isSilent, identity, domain, option,
  251. (manager.hasOptionString(domain, option)
  252. ? manager.getOption(domain, option) : "") + data);
  253. }
  254. /**
  255. * Unsets the specified option.
  256. *
  257. * @param origin The window the command was issued from
  258. * @param isSilent Whether or not the command is being silenced or not
  259. * @param identity The identity to use to set data
  260. * @param domain The domain of the option
  261. * @param option The name of the option
  262. */
  263. private void doUnsetOption(final FrameContainer origin,
  264. final boolean isSilent, final ConfigProvider identity, final String domain,
  265. final String option) {
  266. identity.unsetOption(domain, option);
  267. sendLine(origin, isSilent, FORMAT_OUTPUT, domain + '.' + option + " has been unset.");
  268. }
  269. @Override
  270. public AdditionalTabTargets getSuggestions(final int arg,
  271. final IntelligentCommandContext context) {
  272. final List<String> previousArgs = context.getPreviousArgs();
  273. final AdditionalTabTargets res = new AdditionalTabTargets();
  274. if (arg == 0) {
  275. res.addAll(context.getWindow().getConfigManager()
  276. .getDomains());
  277. res.add("--unset");
  278. res.add("--append");
  279. res.add("--server");
  280. res.add("--channel");
  281. res.excludeAll();
  282. } else if (arg == 1 && !previousArgs.isEmpty()) {
  283. if ("--unset".equalsIgnoreCase(previousArgs.get(0))
  284. || "--append".equalsIgnoreCase(previousArgs.get(0))
  285. || "--server".equalsIgnoreCase(previousArgs.get(0))
  286. || "--channel".equalsIgnoreCase(previousArgs.get(0))) {
  287. res.addAll(context.getWindow().getConfigManager()
  288. .getDomains());
  289. } else {
  290. res.addAll(context.getWindow().getConfigManager()
  291. .getOptions(previousArgs.get(0)).keySet());
  292. }
  293. res.excludeAll();
  294. } else if (arg == 2 && ("--unset".equalsIgnoreCase(previousArgs.get(0))
  295. || "--append".equalsIgnoreCase(previousArgs.get(0))
  296. || "--server".equalsIgnoreCase(previousArgs.get(0))
  297. || "--channel".equalsIgnoreCase(previousArgs.get(0)))) {
  298. res.addAll(context.getWindow().getConfigManager()
  299. .getOptions(previousArgs.get(1)).keySet());
  300. res.excludeAll();
  301. }
  302. return res;
  303. }
  304. }