File Source: Set.java
/*
P/P * Method: com.dmdirc.commandparser.commands.global.Set__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 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
23 package com.dmdirc.commandparser.commands.global;
24
25 import com.dmdirc.commandparser.CommandArguments;
26 import com.dmdirc.commandparser.CommandManager;
27 import com.dmdirc.commandparser.commands.GlobalCommand;
28 import com.dmdirc.commandparser.commands.IntelligentCommand;
29 import com.dmdirc.config.ConfigManager;
30 import com.dmdirc.config.Identity;
31 import com.dmdirc.config.IdentityManager;
32 import com.dmdirc.ui.input.AdditionalTabTargets;
33 import com.dmdirc.ui.interfaces.InputWindow;
34
35 import java.util.List;
36
37 /**
38 * The set command allows the user to inspect and change global config settings.
39 *
40 * @author chris
41 */
42 public final class Set extends GlobalCommand implements IntelligentCommand {
43
44 /**
45 * Creates a new instance of Set.
46 */
47 public Set() {
/*
P/P * Method: void com.dmdirc.commandparser.commands.global.Set()
*
* Preconditions:
* init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
*/
48 super();
49
50 CommandManager.registerCommand(this);
51 }
52
53 /** {@inheritDoc} */
54 @Override
55 public void execute(final InputWindow origin, final boolean isSilent,
56 final CommandArguments args) {
/*
P/P * Method: void execute(InputWindow, bool, CommandArguments)
*
* Preconditions:
* args != null
* init'ed(args.words)
* init'ed(com/dmdirc/config/IdentityManager.globalconfig)
* (soft) args.line != null
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
* (soft) init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
* (soft) com/dmdirc/config/IdentityManager.config != null
* (soft) com/dmdirc/config/IdentityManager.config.file != null
* (soft) com/dmdirc/config/IdentityManager.config.listeners != null
* (soft) com/dmdirc/config/IdentityManager.config.myTarget != null
* ...
*
* Presumptions:
* i < getArguments(...).length@75
* i < getArguments(...).length@78
* i <= getArguments(...).length@78 - 2
* i < getArguments(...).length@82
* i <= getArguments(...).length@83 - 2
* ...
*
* Postconditions:
* args.words != null
* init'ed(com/dmdirc/config/IdentityManager.config.globalConfig)
* possibly_updated(com/dmdirc/config/IdentityManager.config.needSave)
* com/dmdirc/config/IdentityManager.globalconfig == One-of{old com/dmdirc/config/IdentityManager.globalconfig, &new ConfigManager(getGlobalConfig#1)}
* com/dmdirc/config/IdentityManager.globalconfig != null
* init'ed(java.lang.String:split(...)._tainted)
* java.lang.String:split(...)._tainted == 0
* init'ed(java.lang.String:split(...).length)
* java.lang.StringBuilder:toString(...)._tainted == 0
* new ArrayList(getSources#1) num objects <= 1
* ...
*
* Test Vectors:
* origin: Addr_Set{null}, Inverse{null}
* com.dmdirc.WritableFrameContainer:getServer(...)@62: Addr_Set{null}, Inverse{null}
* getArguments(...).length@62: {0}, {1..+Inf}
* getArguments(...).length@70: {0}, {2}
* java.lang.String:equalsIgnoreCase(...)@62: {0}, {1}
* java.lang.String:equalsIgnoreCase(...)@82: {0}, {1}
* java.lang.String:equalsIgnoreCase(...)@85: {0}, {1}
*/
57 int i = 0;
58
59 Identity identity = IdentityManager.getConfigIdentity();
60 ConfigManager manager = IdentityManager.getGlobalConfig();
61
62 if (args.getArguments().length > 0
63 && "--server".equalsIgnoreCase(args.getArguments()[0]) && origin != null
64 && origin.getContainer().getServer() != null) {
65 i = 1;
66 identity = origin.getContainer().getServer().getServerIdentity();
67 manager = origin.getContainer().getServer().getConfigManager();
68 }
69
70 switch (args.getArguments().length - i) {
71 case 0:
72 doDomainList(origin, isSilent, manager);
73 break;
74 case 1:
75 doOptionsList(origin, isSilent, manager, args.getArguments()[i]);
76 break;
77 case 2:
78 doShowOption(origin, isSilent, manager, args.getArguments()[i],
79 args.getArguments()[1 + i]);
80 break;
81 default:
82 if (args.getArguments()[i].equalsIgnoreCase("--unset")) {
83 doUnsetOption(origin, isSilent, identity, args.getArguments()[1 + i],
84 args.getArguments()[2 + i]);
85 } else if (args.getArguments()[i].equalsIgnoreCase("--append")
86 && args.getArguments().length > 3 + i) {
87 doAppendOption(origin, isSilent, identity, manager,
88 args.getArguments()[1 + i], args.getArguments()[2 + i],
89 args.getArgumentsAsString(3 + i));
90 } else {
91 doSetOption(origin, isSilent, identity, args.getArguments()[i],
92 args.getArguments()[1 + i], args.getArgumentsAsString(2 + i));
93 }
94 }
95 }
96
97 /**
98 * Shows the user a list of valid domains.
99 *
100 * @param origin The window the command was issued from
101 * @param isSilent Whether or not the command is being silenced or not
102 * @param manager The config manager to use to retrieve data
103 */
104 private void doDomainList(final InputWindow origin, final boolean isSilent,
105 final ConfigManager manager) {
/*
P/P * Method: void doDomainList(InputWindow, bool, ConfigManager)
*
* Preconditions:
* init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
* manager != null
* manager.sources != null
*
* Presumptions:
* java.lang.StringBuffer:length(...)@117 >= -231+2
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@112: {0}, {1}
*/
106 final StringBuffer output = new StringBuffer(67);
107
108 output.append("Valid domains (use ");
109 output.append(CommandManager.getCommandChar());
110 output.append("set <domain> to see options within a domain): ");
111
112 for (String domain : manager.getDomains()) {
113 output.append(domain);
114 output.append(", ");
115 }
116
117 sendLine(origin, isSilent, FORMAT_OUTPUT, output.substring(0, output.length() - 2));
118 }
119
120 /**
121 * Shows the user a list of valid options within a domain.
122 *
123 * @param origin The window the command was issued from
124 * @param isSilent Whether or not the command is being silenced or not
125 * @param manager The config manager to use to retrieve data
126 * @param domain The domain to be inspected
127 */
128 private void doOptionsList(final InputWindow origin,
129 final boolean isSilent, final ConfigManager manager, final String domain) {
/*
P/P * Method: void doOptionsList(InputWindow, bool, ConfigManager, String)
*
* Preconditions:
* manager != null
* manager.sources != null
*
* Presumptions:
* java.lang.StringBuffer:length(...)@145 >= -231+2
* java.util.Map:keySet(...)@138 != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@138: {0}, {1}
*/
130 final StringBuffer output = new StringBuffer(24);
131
132 output.append("Options in domain '");
133 output.append(domain);
134 output.append("': ");
135
136 boolean found = false;
137
138 for (String option : manager.getOptions(domain).keySet()) {
139 output.append(option);
140 output.append(", ");
141 found = true;
142 }
143
144 if (found) {
145 sendLine(origin, isSilent, FORMAT_OUTPUT, output.substring(0, output.length() - 2));
146 } else {
147 sendLine(origin, isSilent, FORMAT_ERROR,
148 "There are no options in the domain '" + domain + "'.");
149 }
150 }
151
152 /**
153 * Shows the user the current value of one option.
154 *
155 * @param origin The window the command was issued from
156 * @param isSilent Whether or not the command is being silenced or not
157 * @param manager The config manager to use to retrieve data
158 * @param domain The domain of the option
159 * @param option The name of the option
160 */
161 private void doShowOption(final InputWindow origin,
162 final boolean isSilent, final ConfigManager manager,
163 final String domain, final String option) {
/*
P/P * Method: void doShowOption(InputWindow, bool, ConfigManager, String, String)
*
* Preconditions:
* manager != null
* (soft) manager.sources != null
*/
164 if (manager.hasOptionString(domain, option)) {
165 sendLine(origin, isSilent, FORMAT_OUTPUT, "The current value of "
166 + domain + "." + option + " is: " + manager.getOption(domain, option));
167 } else {
168 sendLine(origin, isSilent, FORMAT_ERROR, "Option not found: " + domain + "." + option);
169 }
170 }
171
172 /**
173 * Sets the value of the specified option.
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 identity The identity to use to set data
178 * @param domain The domain of the option
179 * @param option The name of the option
180 * @param newvalue The value the option should be set to
181 */
182 private void doSetOption(final InputWindow origin,
183 final boolean isSilent, final Identity identity,
184 final String domain, final String option, final String newvalue) {
/*
P/P * Method: void doSetOption(InputWindow, bool, Identity, String, String, String)
*
* Preconditions:
* identity != null
* identity.file != null
* identity.myTarget != null
* init'ed(identity.myTarget.type)
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
* (soft) identity.listeners != null
* (soft) init'ed(identity.globalConfig)
*
* Postconditions:
* identity.globalConfig == One-of{old identity.globalConfig, &new ConfigManager(setOption#2)}
* init'ed(identity.globalConfig)
* possibly_updated(identity.needSave)
* java.lang.StringBuilder:toString(...)._tainted == 0
* new ArrayList(getSources#1) num objects <= 1
* new ConfigManager(setOption#2) num objects == new ArrayList(getSources#1) num objects
* new MapList(ConfigManager#1) num objects == new ArrayList(getSources#1) num objects
* init'ed(new ConfigManager(setOption#2).channel)
* init'ed(new ConfigManager(setOption#2).ircd)
* init'ed(new ConfigManager(setOption#2).listeners)
* ...
*/
185 identity.setOption(domain, option, newvalue);
186
187 sendLine(origin, isSilent, FORMAT_OUTPUT, domain + "." + option +
188 " has been set to: " + newvalue);
189 }
190
191 /**
192 * Appends data to the specified 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 identity The identity to use to set data
197 * @param manager The config manager to use to retrieve data
198 * @param domain The domain of the option
199 * @param option The name of the option
200 * @param data The data to be appended
201 */
202 private void doAppendOption(final InputWindow origin,
203 final boolean isSilent, final Identity identity, final ConfigManager manager,
204 final String domain,final String option, final String data) {
/*
P/P * Method: void doAppendOption(InputWindow, bool, Identity, ConfigManager, String, String, String)
*
* Preconditions:
* identity != null
* identity.file != null
* identity.myTarget != null
* init'ed(identity.myTarget.type)
* manager != null
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
* (soft) identity.listeners != null
* (soft) manager.sources != null
* (soft) init'ed(identity.globalConfig)
*
* Postconditions:
* identity.globalConfig == One-of{old identity.globalConfig, &new ConfigManager(setOption#2)}
* init'ed(identity.globalConfig)
* possibly_updated(identity.needSave)
* java.lang.StringBuilder:toString(...)._tainted == 0
* new ArrayList(getSources#1) num objects <= 1
* new ConfigManager(setOption#2) num objects == new ArrayList(getSources#1) num objects
* new MapList(ConfigManager#1) num objects == new ArrayList(getSources#1) num objects
* init'ed(new ConfigManager(setOption#2).channel)
* init'ed(new ConfigManager(setOption#2).ircd)
* init'ed(new ConfigManager(setOption#2).listeners)
* ...
*/
205 doSetOption(origin, isSilent, identity, domain, option,
206 (manager.hasOptionString(domain, option) ?
207 manager.getOption(domain, option) : "") + data);
208 }
209
210 /**
211 * Unsets 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 */
219 private void doUnsetOption(final InputWindow origin,
220 final boolean isSilent, final Identity identity, final String domain,
221 final String option) {
/*
P/P * Method: void doUnsetOption(InputWindow, bool, Identity, String, String)
*
* Preconditions:
* identity != null
* identity.file != null
*
* Postconditions:
* identity.needSave == 1
*/
222 identity.unsetOption(domain, option);
223
224 sendLine(origin, isSilent, FORMAT_OUTPUT, domain + "." + option + " has been unset.");
225 }
226
227 /** {@inheritDoc} */
228 @Override
229 public String getName() {
/*
P/P * Method: String getName()
*
* Postconditions:
* return_value == &"set"
*/
230 return "set";
231 }
232
233 /** {@inheritDoc} */
234 @Override
235 public boolean showInHelp() {
/*
P/P * Method: bool showInHelp()
*
* Postconditions:
* return_value == 1
*/
236 return true;
237 }
238
239 /** {@inheritDoc} */
240 @Override
241 public String getHelp() {
/*
P/P * Method: String getHelp()
*
* Postconditions:
* return_value == &"set [--server] [domain [option [newvalue]]] - inspect or change config ... set [--server] --unset <domain> <option> - unsets the specified option"
*/
242 return "set [--server] [domain [option [newvalue]]] - inspect or change configuration settings"
243 + "\nset [--server] --append <domain> <option> <data> - appends data to the specified option"
244 + "\nset [--server] --unset <domain> <option> - unsets the specified option";
245 }
246
247 /** {@inheritDoc} */
248 @Override
249 public AdditionalTabTargets getSuggestions(final int arg, final List<String> previousArgs) {
/*
P/P * Method: AdditionalTabTargets getSuggestions(int, List)
*
* Preconditions:
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
* (soft) init'ed(com/dmdirc/config/IdentityManager.globalconfig)
* (soft) previousArgs != null
*
* Presumptions:
* getGlobalConfig(...).sources != null
* java.util.List:get(...)@259 != null
* java.util.List:get(...)@267 != null
*
* Postconditions:
* com/dmdirc/config/IdentityManager.globalconfig == One-of{old com/dmdirc/config/IdentityManager.globalconfig, &new ConfigManager(getGlobalConfig#1)}
* init'ed(com/dmdirc/config/IdentityManager.globalconfig)
* java.lang.StringBuilder:toString(...)._tainted == 0
* return_value == &new AdditionalTabTargets(getSuggestions#1)
* new AdditionalTabTargets(getSuggestions#1) num objects == 1
* new ArrayList(getSources#1) num objects <= 1
* new ConfigManager(getGlobalConfig#1) num objects <= 1
* init'ed(new ConfigManager(getGlobalConfig#1).channel)
* init'ed(new ConfigManager(getGlobalConfig#1).ircd)
* init'ed(new ConfigManager(getGlobalConfig#1).listeners)
* ...
*
* Test Vectors:
* arg: {2}, {0}, {1}
* java.lang.String:equalsIgnoreCase(...)@259: {1}, {0}
* java.lang.String:equalsIgnoreCase(...)@259: {0}, {1}
* java.lang.String:equalsIgnoreCase(...)@267: {1}, {0}
* java.lang.String:equalsIgnoreCase(...)@267: {0}, {1}
* java.util.List:size(...)@258: {-231..0}, {1..232-1}
*/
250 final AdditionalTabTargets res = new AdditionalTabTargets();
251
252 if (arg == 0) {
253 res.addAll(IdentityManager.getGlobalConfig().getDomains());
254 res.add("--unset");
255 res.add("--append");
256 res.add("--server");
257 res.excludeAll();
258 } else if (arg == 1 && previousArgs.size() >= 1) {
259 if (previousArgs.get(0).equalsIgnoreCase("--unset")
260 || previousArgs.get(0).equalsIgnoreCase("--append")
261 || previousArgs.get(0).equalsIgnoreCase("--server")) {
262 res.addAll(IdentityManager.getGlobalConfig().getDomains());
263 } else {
264 res.addAll(IdentityManager.getGlobalConfig().getOptions(previousArgs.get(0)).keySet());
265 }
266 res.excludeAll();
267 } else if (arg == 2 && (previousArgs.get(0).equalsIgnoreCase("--unset")
268 || previousArgs.get(0).equalsIgnoreCase("--append")
269 || previousArgs.get(0).equalsIgnoreCase("--server"))) {
270 res.addAll(IdentityManager.getGlobalConfig().getOptions(previousArgs.get(1)).keySet());
271 res.excludeAll();
272 }
273
274 return res;
275 }
276
277 }
SofCheck Inspector Build Version : 2.17854
| Set.java |
2009-Jun-25 01:54:24 |
| Set.class |
2009-Sep-02 17:04:16 |