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.

YamlAutoCommandStore.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.auto;
  18. import com.dmdirc.util.io.yaml.BaseYamlStore;
  19. import java.nio.file.Path;
  20. import java.util.HashMap;
  21. import java.util.HashSet;
  22. import java.util.Map;
  23. import java.util.Optional;
  24. import java.util.Set;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import static com.dmdirc.util.io.yaml.YamlReaderUtils.asMap;
  28. import static com.dmdirc.util.io.yaml.YamlReaderUtils.optionalString;
  29. import static com.dmdirc.util.io.yaml.YamlReaderUtils.requiredString;
  30. /**
  31. * Store that reads and writes auto-commands from a YAML file on disk.
  32. * <p>
  33. * Auto-commands are written as a list of maps:
  34. * <pre><code>
  35. * ---
  36. * - server: irc.foo.bar
  37. * network: quakenet
  38. * profile: bob
  39. * command: msg Q@Cserve.quakenet.org ...
  40. * </code></pre>
  41. */
  42. public class YamlAutoCommandStore extends BaseYamlStore<AutoCommand> implements AutoCommandStore {
  43. private static final Logger LOG = LoggerFactory.getLogger(YamlAutoCommandStore.class);
  44. /** The path to the file to read and write auto commands in. */
  45. private final Path path;
  46. /**
  47. * Creates a new YAML auto command store.
  48. *
  49. * @param path The path to the YAML file to read and write auto commands in.
  50. */
  51. public YamlAutoCommandStore(final Path path) {
  52. this.path = path;
  53. }
  54. @Override
  55. public Set<AutoCommand> readAutoCommands() {
  56. return new HashSet<>(read(path));
  57. }
  58. @Override
  59. public void writeAutoCommands(final Set<AutoCommand> commands) {
  60. write(path, commands);
  61. }
  62. @Override
  63. protected Optional<AutoCommand> convertFromYaml(final Object object) {
  64. try {
  65. final Map<Object, Object> map = asMap(object);
  66. final Optional<String> server =
  67. Optional.ofNullable(optionalString(map, "server"));
  68. final Optional<String> network =
  69. Optional.ofNullable(optionalString(map, "network"));
  70. final Optional<String> profile =
  71. Optional.ofNullable(optionalString(map, "profile"));
  72. final String command = requiredString(map, "command");
  73. return Optional.of(AutoCommand.create(server, network, profile, command));
  74. } catch (IllegalArgumentException ex) {
  75. LOG.info("Unable to read auto command", ex);
  76. return Optional.empty();
  77. }
  78. }
  79. @Override
  80. protected Object convertToYaml(final AutoCommand object) {
  81. final Map<Object, Object> map = new HashMap<>();
  82. object.getServer().ifPresent(v -> map.put("server", v));
  83. object.getNetwork().ifPresent(v -> map.put("network", v));
  84. object.getProfile().ifPresent(v -> map.put("profile", v));
  85. map.put("command", object.getResponse());
  86. return map;
  87. }
  88. }