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