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

SetCommand.java 14KB

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