|
@@ -0,0 +1,214 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2013 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
|
+
|
|
23
|
+package com.dmdirc.addons.conditional_execute;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.FrameContainer;
|
|
26
|
+import com.dmdirc.WritableFrameContainer;
|
|
27
|
+import com.dmdirc.commandparser.BaseCommandInfo;
|
|
28
|
+import com.dmdirc.commandparser.CommandArguments;
|
|
29
|
+import com.dmdirc.commandparser.CommandType;
|
|
30
|
+import com.dmdirc.commandparser.commands.Command;
|
|
31
|
+import com.dmdirc.commandparser.commands.context.CommandContext;
|
|
32
|
+import java.util.HashMap;
|
|
33
|
+import java.util.Map;
|
|
34
|
+
|
|
35
|
+/**
|
|
36
|
+ * The ConditionalExecute command allows the user to conditionally execute a
|
|
37
|
+ * command based on external and pre-determined conditions.
|
|
38
|
+ */
|
|
39
|
+public class ConditionalExecuteCommand extends Command {
|
|
40
|
+
|
|
41
|
+ /** Store details about current namespaces. */
|
|
42
|
+ private final Map<String, ConditionalExecuteNamespace> namespaces = new HashMap<String, ConditionalExecuteNamespace>();
|
|
43
|
+
|
|
44
|
+ /** A command info object for this command. */
|
|
45
|
+ public static final BaseCommandInfo INFO = new BaseCommandInfo("conditionalexecute", "conditionalexecute <args> - Conditionally execute a command", CommandType.TYPE_GLOBAL);
|
|
46
|
+
|
|
47
|
+ /** {@inheritDoc} */
|
|
48
|
+ @Override
|
|
49
|
+ public void execute(final FrameContainer origin, final CommandArguments args, final CommandContext context) {
|
|
50
|
+ final String cmdname = args.getWordsAsString(0, 0);
|
|
51
|
+
|
|
52
|
+ ConditionalExecuteNamespace namespace = null;
|
|
53
|
+ final String[] arguments = args.getArguments();
|
|
54
|
+ boolean manipulated = false;
|
|
55
|
+ boolean inverse = false;
|
|
56
|
+
|
|
57
|
+ for (int i = 0; i < arguments.length; i++) {
|
|
58
|
+ final String arg = arguments[i].toLowerCase();
|
|
59
|
+ final String nextArg = i + 1 < arguments.length ? arguments[i + 1] : "";
|
|
60
|
+
|
|
61
|
+ if (arg.equalsIgnoreCase("--help")) {
|
|
62
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Usage:");
|
|
63
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "");
|
|
64
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, cmdname + " <args>");
|
|
65
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, cmdname + " --namespace <name> <namespace commands>");
|
|
66
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, cmdname + " --namespace <name> [--inverse] </commandToRun <command args>>");
|
|
67
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "");
|
|
68
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Commands can only be specified if no other non-namespace args are given.");
|
|
69
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "");
|
|
70
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "When trying to run a command, the namespace will be checked to see if the command can be run.");
|
|
71
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "The checks performed are as follows:");
|
|
72
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " 1) Does the namespace exist? if not, run the command and create the namespace.");
|
|
73
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " 2) Is the namespace inhibited? - Do not run the command.");
|
|
74
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " 3) Is the namespace in forced mode? - Run the command.");
|
|
75
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " 4) If --inverse is specified, are we under the limit time? Run the command");
|
|
76
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " 5) If --inverse is not specified, are we over the limit time? Run the command");
|
|
77
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " 6) Do not run the command.");
|
|
78
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "");
|
|
79
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "General Arguments.");
|
|
80
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " --list - List all current namespaces and their status");
|
|
81
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " --help - Print this help.");
|
|
82
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " --reset - Remove all namespaces.");
|
|
83
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "");
|
|
84
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Useful things:");
|
|
85
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " --namespace <name> - Namespace to modify. If the namespace does not exist, it will be created. Namespaces are not remembered across sessions.");
|
|
86
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "");
|
|
87
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Arguments related to a namespace:");
|
|
88
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " --settime <time> - Set the limit time on this namespace. Time can be either a time in seconds, 'now' for now, or 'nowifless' to set to now only if it is currently less.");
|
|
89
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " --delay <seconds> - Increase the 'limit' time on this namespace by <seconds> seconds");
|
|
90
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " --inhibit - Prevent any attempts at running commands in this namespace from executing");
|
|
91
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " --force - Any future attempts at running commands in this namespace will always execute");
|
|
92
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " --allow - Disable '--force' or '--inhibit' and resume normal operation.");
|
|
93
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " --remove - Remove this namespace.");
|
|
94
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " --status - Show the status of this namespace.");
|
|
95
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "");
|
|
96
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Arguments when running a command:");
|
|
97
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " --inverse - Inverse the match against the 'limit' time.");
|
|
98
|
+ return;
|
|
99
|
+ } else if (arg.equalsIgnoreCase("--list")) {
|
|
100
|
+ if (namespaces.isEmpty()) {
|
|
101
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "There are currently no known namespaces.");
|
|
102
|
+ } else {
|
|
103
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Current namespaces: ");
|
|
104
|
+ for (final Map.Entry<String, ConditionalExecuteNamespace> e : namespaces.entrySet()) {
|
|
105
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, " " + e.getValue().toString());
|
|
106
|
+ }
|
|
107
|
+ }
|
|
108
|
+ return;
|
|
109
|
+ } else if (arg.equalsIgnoreCase("--reset")) {
|
|
110
|
+ namespaces.clear();
|
|
111
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "All namespaces removed.");
|
|
112
|
+ return;
|
|
113
|
+ } else if (namespace == null) {
|
|
114
|
+ if (arg.equalsIgnoreCase("--namespace")) {
|
|
115
|
+ if (nextArg.isEmpty()) {
|
|
116
|
+ sendLine(origin, args.isSilent(), FORMAT_ERROR, "Error: You must specify a namespace.");
|
|
117
|
+ return;
|
|
118
|
+ } else {
|
|
119
|
+ if (!namespaces.containsKey(nextArg.toLowerCase())) {
|
|
120
|
+ namespaces.put(nextArg.toLowerCase(), new ConditionalExecuteNamespace(nextArg.toLowerCase()));
|
|
121
|
+ }
|
|
122
|
+ namespace = namespaces.get(nextArg.toLowerCase());
|
|
123
|
+
|
|
124
|
+ // Skip the next argument.
|
|
125
|
+ i++;
|
|
126
|
+ }
|
|
127
|
+ } else {
|
|
128
|
+ sendLine(origin, args.isSilent(), FORMAT_ERROR, "Error: You must specify a namespace first.");
|
|
129
|
+ return;
|
|
130
|
+ }
|
|
131
|
+ } else if (arg.equalsIgnoreCase("--inhibit")) {
|
|
132
|
+ namespace.inhibit();
|
|
133
|
+ manipulated = true;
|
|
134
|
+ } else if (arg.equalsIgnoreCase("--force")) {
|
|
135
|
+ namespace.force();
|
|
136
|
+ manipulated = true;
|
|
137
|
+ } else if (arg.equalsIgnoreCase("--allow")) {
|
|
138
|
+ namespace.reset();
|
|
139
|
+ manipulated = true;
|
|
140
|
+ } else if (arg.equalsIgnoreCase("--settime")) {
|
|
141
|
+ if (nextArg.isEmpty()) {
|
|
142
|
+ sendLine(origin, args.isSilent(), FORMAT_ERROR, "Error: You must provide a time to use.");
|
|
143
|
+ return;
|
|
144
|
+ } else if (nextArg.equalsIgnoreCase("now")) {
|
|
145
|
+ namespace.setLimit(System.currentTimeMillis());
|
|
146
|
+ i++;
|
|
147
|
+ manipulated = true;
|
|
148
|
+ } else if (nextArg.equalsIgnoreCase("nowifless")) {
|
|
149
|
+ if (namespace.getLimitTime() < System.currentTimeMillis()) {
|
|
150
|
+ namespace.setLimit(System.currentTimeMillis());
|
|
151
|
+ }
|
|
152
|
+ i++;
|
|
153
|
+ manipulated = true;
|
|
154
|
+ } else {
|
|
155
|
+ try {
|
|
156
|
+ namespace.setLimit(Long.parseLong(nextArg) * 1000);
|
|
157
|
+ i++;
|
|
158
|
+ manipulated = true;
|
|
159
|
+ } catch (final NumberFormatException nfe) {
|
|
160
|
+ sendLine(origin, args.isSilent(), FORMAT_ERROR, "Error: Invalid time: " + nextArg);
|
|
161
|
+ return;
|
|
162
|
+ }
|
|
163
|
+ }
|
|
164
|
+ } else if (arg.equalsIgnoreCase("--delay")) {
|
|
165
|
+ if (nextArg.isEmpty()) {
|
|
166
|
+ sendLine(origin, args.isSilent(), FORMAT_ERROR, "Error: You must provide a delay to use.");
|
|
167
|
+ return;
|
|
168
|
+ } else {
|
|
169
|
+ try {
|
|
170
|
+ namespace.changeLimit(Long.parseLong(nextArg) * 1000);
|
|
171
|
+ i++;
|
|
172
|
+ manipulated = true;
|
|
173
|
+ } catch (final NumberFormatException nfe) {
|
|
174
|
+ sendLine(origin, args.isSilent(), FORMAT_ERROR, "Error: Invalid delay: " + nextArg);
|
|
175
|
+ return;
|
|
176
|
+ }
|
|
177
|
+ }
|
|
178
|
+ } else if (arg.equalsIgnoreCase("--remove")) {
|
|
179
|
+ namespaces.remove(namespace.getName());
|
|
180
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Removed namespace '" + namespace.getName() + "'");
|
|
181
|
+ return;
|
|
182
|
+ } else if (arg.equalsIgnoreCase("--status")) {
|
|
183
|
+ // Show the current status, incase some manipulations occured prior to this.
|
|
184
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, namespaces.get(namespace.getName()));
|
|
185
|
+ return;
|
|
186
|
+ } else if (arg.equalsIgnoreCase("--inverse")) {
|
|
187
|
+ inverse = true;
|
|
188
|
+ } else if (manipulated) {
|
|
189
|
+ sendLine(origin, args.isSilent(), FORMAT_ERROR, "You can't run commands and manipulate the namespace at the same time, ignored.");
|
|
190
|
+ } else {
|
|
191
|
+ // Command to run!
|
|
192
|
+ if (namespace.canRun(inverse) && origin instanceof WritableFrameContainer) {
|
|
193
|
+ ((WritableFrameContainer) origin).getCommandParser().parseCommand(origin, args.getArgumentsAsString(i++));
|
|
194
|
+ }
|
|
195
|
+ return;
|
|
196
|
+ }
|
|
197
|
+ }
|
|
198
|
+
|
|
199
|
+ // If we get here, we either manipulated something, or should show the usage text.
|
|
200
|
+ if (manipulated) {
|
|
201
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Namespace updated.");
|
|
202
|
+ sendLine(origin, args.isSilent(), FORMAT_OUTPUT, namespace.toString());
|
|
203
|
+ namespaces.put(namespace.getName(), namespace);
|
|
204
|
+ } else {
|
|
205
|
+ sendLine(origin, args.isSilent(), FORMAT_ERROR, "Usage:");
|
|
206
|
+ sendLine(origin, args.isSilent(), FORMAT_ERROR, "");
|
|
207
|
+ sendLine(origin, args.isSilent(), FORMAT_ERROR, cmdname + " <args>");
|
|
208
|
+ sendLine(origin, args.isSilent(), FORMAT_ERROR, cmdname + " --namespace <name> <namespace commands>");
|
|
209
|
+ sendLine(origin, args.isSilent(), FORMAT_ERROR, cmdname + " --namespace <name> [--inverse] </commandToRun <command args>>");
|
|
210
|
+ sendLine(origin, args.isSilent(), FORMAT_ERROR, "");
|
|
211
|
+ sendLine(origin, args.isSilent(), FORMAT_ERROR, "For more information, see " + cmdname + " --help");
|
|
212
|
+ }
|
|
213
|
+ }
|
|
214
|
+}
|