Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ActionAliasMigrator.java 5.7KB

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