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.

ActionAliasMigrator.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2006-2015 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.aliases;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
  25. import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
  26. import com.dmdirc.events.AppErrorEvent;
  27. import com.dmdirc.interfaces.Migrator;
  28. import com.dmdirc.logger.ErrorLevel;
  29. import com.dmdirc.util.io.ConfigFile;
  30. import com.dmdirc.util.io.InvalidConfigFileException;
  31. import com.google.common.base.Joiner;
  32. import java.io.IOException;
  33. import java.nio.file.DirectoryStream;
  34. import java.nio.file.Files;
  35. import java.nio.file.Path;
  36. import java.util.Map;
  37. import javax.inject.Inject;
  38. import javax.inject.Singleton;
  39. /**
  40. * Migrates "alias" actions into proper aliases.
  41. */
  42. @Singleton
  43. public class ActionAliasMigrator implements Migrator {
  44. private final Path directory;
  45. private final AliasFactory aliasFactory;
  46. private final AliasManager aliasManager;
  47. private final DMDircMBassador eventBus;
  48. /**
  49. * Creates a new alias migrator.
  50. *
  51. * @param directory The base directory to read alias actions from.
  52. * @param aliasFactory The factory to use to create new aliases.
  53. * @param aliasManager The manager to add aliases to.
  54. * @param eventBus Bus to report errors on.
  55. */
  56. @Inject
  57. public ActionAliasMigrator(
  58. @Directory(DirectoryType.ACTIONS) final Path directory,
  59. final AliasFactory aliasFactory,
  60. final AliasManager aliasManager,
  61. final DMDircMBassador eventBus) {
  62. this.directory = directory.resolve("aliases");
  63. this.aliasFactory = aliasFactory;
  64. this.aliasManager = aliasManager;
  65. this.eventBus = eventBus;
  66. }
  67. @Override
  68. public boolean needsMigration() {
  69. return Files.isDirectory(directory);
  70. }
  71. @Override
  72. public void migrate() {
  73. try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) {
  74. for (Path path : directoryStream) {
  75. if (migrate(path)) {
  76. Files.delete(path);
  77. }
  78. }
  79. Files.delete(directory);
  80. } catch (IOException ex) {
  81. eventBus.publish(new AppErrorEvent(ErrorLevel.MEDIUM, ex,
  82. "Unable to migrate aliases", ex.getMessage()));
  83. }
  84. }
  85. /**
  86. * Migrates the specified file.
  87. *
  88. * @param file The file to be migrated.
  89. *
  90. * @return True if the file was migrated successfully, false otherwise.
  91. */
  92. private boolean migrate(final Path file) {
  93. try {
  94. final ConfigFile configFile = new ConfigFile(file);
  95. configFile.read();
  96. final String response = Joiner.on('\n').join(configFile.getFlatDomain("response"));
  97. final String name = getTrigger(configFile);
  98. final int minArguments = getArguments(configFile);
  99. aliasManager.addAlias(aliasFactory.createAlias(name, minArguments, response));
  100. return true;
  101. } catch (IOException | InvalidConfigFileException | NumberFormatException ex) {
  102. return false;
  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. * Finds and returns the minimum number of arguments an alias requires.
  126. *
  127. * @param configFile The config file to read minimum arguments from.
  128. *
  129. * @return The minimum number of arguments if present, or <code>0</code> otherwise.
  130. *
  131. * @throws NumberFormatException If the config file contains an invalid number of args.
  132. */
  133. private int getArguments(final ConfigFile configFile) throws NumberFormatException {
  134. for (Map<String, String> section : configFile.getKeyDomains().values()) {
  135. if (section.containsKey("comparison")
  136. && section.containsKey("target")) {
  137. if ("INT_GREATER".equals(section.get("comparison"))) {
  138. return 1 + Integer.parseInt(section.get("target"));
  139. }
  140. if ("INT_EQUALS".equals(section.get("comparison"))) {
  141. return Integer.parseInt(section.get("target"));
  142. }
  143. }
  144. }
  145. return 0;
  146. }
  147. }