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.

Set.java 13KB

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