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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.commandparser.aliases;
  18. import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
  19. import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
  20. import com.dmdirc.interfaces.Migrator;
  21. import com.dmdirc.util.io.ConfigFile;
  22. import com.dmdirc.util.io.InvalidConfigFileException;
  23. import com.google.common.base.Joiner;
  24. import java.io.IOException;
  25. import java.nio.file.DirectoryStream;
  26. import java.nio.file.Files;
  27. import java.nio.file.Path;
  28. import java.util.Map;
  29. import javax.inject.Inject;
  30. import javax.inject.Singleton;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33. import static com.dmdirc.util.LogUtils.APP_ERROR;
  34. /**
  35. * Migrates "alias" actions into proper aliases.
  36. */
  37. @Singleton
  38. public class ActionAliasMigrator implements Migrator {
  39. private static final Logger LOG = LoggerFactory.getLogger(ActionAliasMigrator.class);
  40. private final Path directory;
  41. private final AliasFactory aliasFactory;
  42. private final AliasManager aliasManager;
  43. /**
  44. * Creates a new alias migrator.
  45. *
  46. * @param directory The base directory to read alias actions from.
  47. * @param aliasFactory The factory to use to create new aliases.
  48. * @param aliasManager The manager to add aliases to.
  49. */
  50. @Inject
  51. public ActionAliasMigrator(
  52. @Directory(DirectoryType.ACTIONS) final Path directory,
  53. final AliasFactory aliasFactory,
  54. final AliasManager aliasManager) {
  55. this.directory = directory.resolve("aliases");
  56. this.aliasFactory = aliasFactory;
  57. this.aliasManager = aliasManager;
  58. }
  59. @Override
  60. public boolean needsMigration() {
  61. return Files.isDirectory(directory);
  62. }
  63. @Override
  64. public void migrate() {
  65. try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) {
  66. for (Path path : directoryStream) {
  67. if (migrate(path)) {
  68. Files.delete(path);
  69. }
  70. }
  71. Files.delete(directory);
  72. } catch (IOException ex) {
  73. LOG.warn(APP_ERROR, "Unable to migrate aliases", ex);
  74. }
  75. }
  76. /**
  77. * Migrates the specified file.
  78. *
  79. * @param file The file to be migrated.
  80. *
  81. * @return True if the file was migrated successfully, false otherwise.
  82. */
  83. private boolean migrate(final Path file) {
  84. try {
  85. final ConfigFile configFile = new ConfigFile(file);
  86. configFile.read();
  87. final String response = Joiner.on('\n').join(configFile.getFlatDomain("response"));
  88. final String name = getTrigger(configFile);
  89. final int minArguments = getArguments(configFile);
  90. aliasManager.addAlias(aliasFactory.createAlias(name, minArguments, response));
  91. return true;
  92. } catch (IOException | InvalidConfigFileException | NumberFormatException ex) {
  93. return false;
  94. }
  95. }
  96. /**
  97. * Finds and returns the trigger for an alias.
  98. *
  99. * @param configFile The config file to read triggers from.
  100. *
  101. * @return The trigger for the alias.
  102. *
  103. * @throws InvalidConfigFileException If the config file is missing a trigger
  104. */
  105. private String getTrigger(final ConfigFile configFile) throws InvalidConfigFileException {
  106. for (Map<String, String> section : configFile.getKeyDomains().values()) {
  107. if (section.containsKey("comparison")
  108. && section.containsKey("target")
  109. && "STRING_EQUALS".equals(section.get("comparison"))) {
  110. return section.get("target");
  111. }
  112. }
  113. throw new InvalidConfigFileException("No trigger found");
  114. }
  115. /**
  116. * Finds and returns the minimum number of arguments an alias requires.
  117. *
  118. * @param configFile The config file to read minimum arguments from.
  119. *
  120. * @return The minimum number of arguments if present, or <code>0</code> otherwise.
  121. *
  122. * @throws NumberFormatException If the config file contains an invalid number of args.
  123. */
  124. private int getArguments(final ConfigFile configFile) throws NumberFormatException {
  125. for (Map<String, String> section : configFile.getKeyDomains().values()) {
  126. if (section.containsKey("comparison")
  127. && section.containsKey("target")) {
  128. if ("INT_GREATER".equals(section.get("comparison"))) {
  129. return 1 + Integer.parseInt(section.get("target"));
  130. }
  131. if ("INT_EQUALS".equals(section.get("comparison"))) {
  132. return Integer.parseInt(section.get("target"));
  133. }
  134. }
  135. }
  136. return 0;
  137. }
  138. }