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.

ActionServerPerformMigrator.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.auto;
  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 java.util.Optional;
  35. import javax.inject.Inject;
  36. import javax.inject.Singleton;
  37. import org.slf4j.Logger;
  38. import org.slf4j.LoggerFactory;
  39. import static com.dmdirc.util.LogUtils.APP_ERROR;
  40. /**
  41. * Migrates server/network-based action performs into auto commands.
  42. */
  43. @Singleton
  44. public class ActionServerPerformMigrator implements Migrator {
  45. private static final Logger LOG = LoggerFactory.getLogger(ActionServerPerformMigrator.class);
  46. private final Path directory;
  47. private final AutoCommandManager autoCommandManager;
  48. @Inject
  49. public ActionServerPerformMigrator(
  50. @Directory(DirectoryType.ACTIONS) final Path directory,
  51. final AutoCommandManager autoCommandManager) {
  52. this.directory = directory.resolve("performs");
  53. this.autoCommandManager = autoCommandManager;
  54. }
  55. @Override
  56. public boolean needsMigration() {
  57. return Files.isDirectory(directory);
  58. }
  59. @Override
  60. public void migrate() {
  61. try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) {
  62. for (Path path : directoryStream) {
  63. if (migrate(path)) {
  64. Files.delete(path);
  65. }
  66. }
  67. Files.delete(directory);
  68. } catch (IOException ex) {
  69. LOG.warn(APP_ERROR, "Unable to migrate performs", ex);
  70. }
  71. }
  72. /**
  73. * Migrates the specified file.
  74. *
  75. * @param file The file to be migrated.
  76. *
  77. * @return True if the file was migrated successfully, false otherwise.
  78. */
  79. private boolean migrate(final Path file) {
  80. try {
  81. final ConfigFile configFile = new ConfigFile(file);
  82. configFile.read();
  83. final String response = Joiner.on('\n').join(configFile.getFlatDomain("response"));
  84. final Optional<String> server = getCondition(configFile, "SERVER_NAME");
  85. final Optional<String> network = getCondition(configFile, "SERVER_NETWORK");
  86. final Optional<String> profile = getCondition(configFile,
  87. "SERVER_PROFILE.IDENTITY_NAME");
  88. autoCommandManager.addAutoCommand(
  89. AutoCommand.create(server, network, profile, response));
  90. return true;
  91. } catch (IOException | InvalidConfigFileException | NumberFormatException ex) {
  92. return false;
  93. }
  94. }
  95. private Optional<String> getCondition(final ConfigFile configFile, final String component) {
  96. for (Map.Entry<String, Map<String, String>> section : configFile.getKeyDomains().entrySet()) {
  97. if (section.getKey().startsWith("condition")
  98. && component.equals(section.getValue().get("component"))) {
  99. return Optional.ofNullable(section.getValue().get("target"));
  100. }
  101. }
  102. return Optional.empty();
  103. }
  104. }