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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (c) 2006-2015 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.commandparser.BaseCommandInfo;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.CommandInfo;
  26. import com.dmdirc.commandparser.CommandType;
  27. import com.dmdirc.commandparser.commands.BaseCommand;
  28. import com.dmdirc.commandparser.commands.IntelligentCommand;
  29. import com.dmdirc.commandparser.commands.context.ChannelCommandContext;
  30. import com.dmdirc.commandparser.commands.context.CommandContext;
  31. import com.dmdirc.commandparser.commands.flags.CommandFlag;
  32. import com.dmdirc.commandparser.commands.flags.CommandFlagHandler;
  33. import com.dmdirc.commandparser.commands.flags.CommandFlagResult;
  34. import com.dmdirc.interfaces.CommandController;
  35. import com.dmdirc.interfaces.Connection;
  36. import com.dmdirc.interfaces.GroupChat;
  37. import com.dmdirc.interfaces.WindowModel;
  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 javax.annotation.Nonnull;
  45. import javax.annotation.Nullable;
  46. import javax.inject.Inject;
  47. import java.util.List;
  48. import java.util.Optional;
  49. /**
  50. * The set command allows the user to inspect and change global config settings.
  51. */
  52. public class SetCommand extends BaseCommand 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 WindowModel 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.getConnection();
  104. if (res.hasFlag(serverFlag)) {
  105. if (!connection.isPresent()) {
  106. showError(origin, args.isSilent(), "Cannot use --server in this context");
  107. return;
  108. }
  109. identity = connection.get().getServerIdentity();
  110. manager = connection.get().getWindowModel().getConfigManager();
  111. }
  112. if (res.hasFlag(channelFlag)) {
  113. if (!(context instanceof ChannelCommandContext)) {
  114. showError(origin, args.isSilent(),"Cannot use --channel in this context");
  115. return;
  116. }
  117. final GroupChat groupChat = ((ChannelCommandContext) context).getGroupChat();
  118. identity = identityFactory.createChannelConfig(connection.get().getNetwork(),
  119. groupChat.getName());
  120. manager = groupChat.getWindowModel().getConfigManager();
  121. }
  122. if (res.hasFlag(unsetFlag)) {
  123. final String[] arguments = res.getArguments(unsetFlag);
  124. doUnsetOption(origin, args.isSilent(), identity, arguments[0], arguments[1]);
  125. return;
  126. }
  127. if (res.hasFlag(appendFlag)) {
  128. final String[] arguments = res.getArguments(appendFlag);
  129. doAppendOption(origin, args.isSilent(), identity, manager,
  130. arguments[0], arguments[1], res.getArgumentsAsString());
  131. return;
  132. }
  133. final String[] arguments = res.getArguments();
  134. switch (arguments.length) {
  135. case 0:
  136. doDomainList(origin, args.isSilent(), manager);
  137. break;
  138. case 1:
  139. doOptionsList(origin, args.isSilent(), manager, arguments[0]);
  140. break;
  141. case 2:
  142. doShowOption(origin, args.isSilent(), manager, arguments[0],
  143. arguments[1]);
  144. break;
  145. default:
  146. doSetOption(origin, args.isSilent(), identity, arguments[0],
  147. arguments[1], res.getArgumentsAsString(2));
  148. break;
  149. }
  150. }
  151. /**
  152. * Shows the user a list of valid domains.
  153. *
  154. * @param origin The window the command was issued from
  155. * @param isSilent Whether or not the command is being silenced or not
  156. * @param manager The config manager to use to retrieve data
  157. */
  158. private void doDomainList(final WindowModel origin, final boolean isSilent,
  159. final AggregateConfigProvider manager) {
  160. final StringBuilder output = new StringBuilder(67);
  161. output.append("Valid domains (use ");
  162. output.append(getController().getCommandChar());
  163. output.append("set <domain> to see options within a domain): ");
  164. for (String domain : manager.getDomains()) {
  165. output.append(domain);
  166. output.append(", ");
  167. }
  168. showOutput(origin, isSilent, output.substring(0, output.length() - 2));
  169. }
  170. /**
  171. * Shows the user a list of valid options within a domain.
  172. *
  173. * @param origin The window the command was issued from
  174. * @param isSilent Whether or not the command is being silenced or not
  175. * @param manager The config manager to use to retrieve data
  176. * @param domain The domain to be inspected
  177. */
  178. private void doOptionsList(final WindowModel origin,
  179. final boolean isSilent, final ReadOnlyConfigProvider manager, final String domain) {
  180. final StringBuilder output = new StringBuilder(24);
  181. output.append("Options in domain '");
  182. output.append(domain);
  183. output.append("': ");
  184. boolean found = false;
  185. for (String option : manager.getOptions(domain).keySet()) {
  186. output.append(option);
  187. output.append(", ");
  188. found = true;
  189. }
  190. if (found) {
  191. showOutput(origin, isSilent, output.substring(0, output.length() - 2));
  192. } else {
  193. showError(origin, isSilent, "There are no options in the domain '" + domain + "'.");
  194. }
  195. }
  196. /**
  197. * Shows the user the current value of one option.
  198. *
  199. * @param origin The window the command was issued from
  200. * @param isSilent Whether or not the command is being silenced or not
  201. * @param manager The config manager to use to retrieve data
  202. * @param domain The domain of the option
  203. * @param option The name of the option
  204. */
  205. private void doShowOption(final WindowModel origin,
  206. final boolean isSilent, final ReadOnlyConfigProvider manager,
  207. final String domain, final String option) {
  208. if (manager.hasOptionString(domain, option)) {
  209. showOutput(origin, isSilent, "The current value of "
  210. + domain + '.' + option + " is: " + manager.getOption(domain, option));
  211. } else {
  212. showError(origin, isSilent, "Option not found: " + domain + '.' + option);
  213. }
  214. }
  215. /**
  216. * Sets the value of the specified option.
  217. *
  218. * @param origin The window the command was issued from
  219. * @param isSilent Whether or not the command is being silenced or not
  220. * @param identity The identity to use to set data
  221. * @param domain The domain of the option
  222. * @param option The name of the option
  223. * @param newvalue The value the option should be set to
  224. */
  225. private void doSetOption(final WindowModel origin,
  226. final boolean isSilent, final ConfigProvider identity,
  227. final String domain, final String option, final String newvalue) {
  228. identity.setOption(domain, option, newvalue);
  229. showOutput(origin, isSilent, domain + '.' + option + " has been set to: " + newvalue);
  230. }
  231. /**
  232. * Appends data to the specified option.
  233. *
  234. * @param origin The window the command was issued from
  235. * @param isSilent Whether or not the command is being silenced or not
  236. * @param identity The identity to use to set data
  237. * @param manager The config manager to use to retrieve data
  238. * @param domain The domain of the option
  239. * @param option The name of the option
  240. * @param data The data to be appended
  241. */
  242. private void doAppendOption(final WindowModel origin,
  243. final boolean isSilent, final ConfigProvider identity,
  244. final ReadOnlyConfigProvider manager,
  245. final String domain, final String option, final String data) {
  246. doSetOption(origin, isSilent, identity, domain, option,
  247. (manager.hasOptionString(domain, option)
  248. ? manager.getOption(domain, option) : "") + data);
  249. }
  250. /**
  251. * Unsets the specified option.
  252. *
  253. * @param origin The window the command was issued from
  254. * @param isSilent Whether or not the command is being silenced or not
  255. * @param identity The identity to use to set data
  256. * @param domain The domain of the option
  257. * @param option The name of the option
  258. */
  259. private void doUnsetOption(final WindowModel origin,
  260. final boolean isSilent, final ConfigProvider identity, final String domain,
  261. final String option) {
  262. identity.unsetOption(domain, option);
  263. showOutput(origin, isSilent, domain + '.' + option + " has been unset.");
  264. }
  265. @Override
  266. public AdditionalTabTargets getSuggestions(final int arg,
  267. final IntelligentCommandContext context) {
  268. final List<String> previousArgs = context.getPreviousArgs();
  269. final AdditionalTabTargets res = new AdditionalTabTargets();
  270. if (arg == 0) {
  271. res.addAll(context.getWindow().getConfigManager()
  272. .getDomains());
  273. res.add("--unset");
  274. res.add("--append");
  275. res.add("--server");
  276. res.add("--channel");
  277. res.excludeAll();
  278. } else if (arg == 1 && !previousArgs.isEmpty()) {
  279. if ("--unset".equalsIgnoreCase(previousArgs.get(0))
  280. || "--append".equalsIgnoreCase(previousArgs.get(0))
  281. || "--server".equalsIgnoreCase(previousArgs.get(0))
  282. || "--channel".equalsIgnoreCase(previousArgs.get(0))) {
  283. res.addAll(context.getWindow().getConfigManager()
  284. .getDomains());
  285. } else {
  286. res.addAll(context.getWindow().getConfigManager()
  287. .getOptions(previousArgs.get(0)).keySet());
  288. }
  289. res.excludeAll();
  290. } else if (arg == 2 && ("--unset".equalsIgnoreCase(previousArgs.get(0))
  291. || "--append".equalsIgnoreCase(previousArgs.get(0))
  292. || "--server".equalsIgnoreCase(previousArgs.get(0))
  293. || "--channel".equalsIgnoreCase(previousArgs.get(0)))) {
  294. res.addAll(context.getWindow().getConfigManager()
  295. .getOptions(previousArgs.get(1)).keySet());
  296. res.excludeAll();
  297. }
  298. return res;
  299. }
  300. }