|
@@ -0,0 +1,145 @@
|
|
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
|
+
|
|
23
|
+package com.dmdirc.commandparser.aliases;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
|
|
26
|
+import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
|
|
27
|
+import com.dmdirc.interfaces.Migrator;
|
|
28
|
+import com.dmdirc.util.io.ConfigFile;
|
|
29
|
+import com.dmdirc.util.io.InvalidConfigFileException;
|
|
30
|
+
|
|
31
|
+import com.google.common.base.Joiner;
|
|
32
|
+
|
|
33
|
+import java.io.File;
|
|
34
|
+import java.io.IOException;
|
|
35
|
+import java.util.Map;
|
|
36
|
+
|
|
37
|
+import javax.inject.Inject;
|
|
38
|
+import javax.inject.Singleton;
|
|
39
|
+
|
|
40
|
+/**
|
|
41
|
+ * Migrates "alias" actions into proper aliases.
|
|
42
|
+ */
|
|
43
|
+@Singleton
|
|
44
|
+public class ActionAliasMigrator implements Migrator {
|
|
45
|
+
|
|
46
|
+ private final File directory;
|
|
47
|
+ private final AliasFactory aliasFactory;
|
|
48
|
+ private final AliasManager aliasManager;
|
|
49
|
+
|
|
50
|
+ /**
|
|
51
|
+ * Creates a new alias migrator.
|
|
52
|
+ *
|
|
53
|
+ * @param directory The base directory to read alias actions from.
|
|
54
|
+ * @param aliasFactory The factory to use to create new aliases.
|
|
55
|
+ * @param aliasManager The manager to add aliases to.
|
|
56
|
+ */
|
|
57
|
+ @Inject
|
|
58
|
+ public ActionAliasMigrator(
|
|
59
|
+ @Directory(DirectoryType.ACTIONS) final String directory,
|
|
60
|
+ final AliasFactory aliasFactory,
|
|
61
|
+ final AliasManager aliasManager) {
|
|
62
|
+ this.directory = new File(directory, "aliases");
|
|
63
|
+ this.aliasFactory = aliasFactory;
|
|
64
|
+ this.aliasManager = aliasManager;
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ @Override
|
|
68
|
+ public boolean needsMigration() {
|
|
69
|
+ return directory.exists();
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ @Override
|
|
73
|
+ public void migrate() {
|
|
74
|
+ for (File child : directory.listFiles()) {
|
|
75
|
+ if (migrate(child)) {
|
|
76
|
+ child.delete();
|
|
77
|
+ }
|
|
78
|
+ }
|
|
79
|
+ directory.delete();
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ /**
|
|
83
|
+ * Migrates the specified file.
|
|
84
|
+ *
|
|
85
|
+ * @param file The file to be migrated.
|
|
86
|
+ *
|
|
87
|
+ * @return True if the file was migrated successfully, false otherwise.
|
|
88
|
+ */
|
|
89
|
+ private boolean migrate(final File file) {
|
|
90
|
+ try {
|
|
91
|
+ final ConfigFile configFile = new ConfigFile(file);
|
|
92
|
+ configFile.read();
|
|
93
|
+
|
|
94
|
+ final String response = Joiner.on('\n').join(configFile.getFlatDomain("response"));
|
|
95
|
+ final String name = getTrigger(configFile);
|
|
96
|
+ final int minArguments = getArguments(configFile);
|
|
97
|
+
|
|
98
|
+ aliasManager.addAlias(aliasFactory.createAlias(name, minArguments, response));
|
|
99
|
+ return true;
|
|
100
|
+ } catch (IOException | InvalidConfigFileException | NumberFormatException ex) {
|
|
101
|
+ return false;
|
|
102
|
+ }
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ /**
|
|
106
|
+ * Finds and returns the trigger for an alias.
|
|
107
|
+ *
|
|
108
|
+ * @param configFile The config file to read triggers from.
|
|
109
|
+ *
|
|
110
|
+ * @return The trigger for the alias.
|
|
111
|
+ *
|
|
112
|
+ * @throws InvalidConfigFileException If the config file is missing a trigger
|
|
113
|
+ */
|
|
114
|
+ private String getTrigger(final ConfigFile configFile) throws InvalidConfigFileException {
|
|
115
|
+ for (Map<String, String> section : configFile.getKeyDomains().values()) {
|
|
116
|
+ if (section.containsKey("comparison")
|
|
117
|
+ && section.containsKey("target")
|
|
118
|
+ && "STRING_EQUALS".equals(section.get("comparison"))) {
|
|
119
|
+ return section.get("target");
|
|
120
|
+ }
|
|
121
|
+ }
|
|
122
|
+ throw new InvalidConfigFileException("No trigger found");
|
|
123
|
+ }
|
|
124
|
+
|
|
125
|
+ /**
|
|
126
|
+ * Finds and returns the minimum number of arguments an alias requires.
|
|
127
|
+ *
|
|
128
|
+ * @param configFile The config file to read minimum arguments from.
|
|
129
|
+ *
|
|
130
|
+ * @return The minimum number of arguments if present, or <code>0</code> otherwise.
|
|
131
|
+ *
|
|
132
|
+ * @throws NumberFormatException If the config file contains an invalid number of args.
|
|
133
|
+ */
|
|
134
|
+ private int getArguments(final ConfigFile configFile) throws NumberFormatException {
|
|
135
|
+ for (Map<String, String> section : configFile.getKeyDomains().values()) {
|
|
136
|
+ if (section.containsKey("comparison")
|
|
137
|
+ && section.containsKey("target")
|
|
138
|
+ && "INT_GREATER".equals(section.get("comparison"))) {
|
|
139
|
+ return 1 + Integer.valueOf(section.get("target"));
|
|
140
|
+ }
|
|
141
|
+ }
|
|
142
|
+ return 0;
|
|
143
|
+ }
|
|
144
|
+
|
|
145
|
+}
|