Java IRC bot
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.

Config.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  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.md87.charliebravo;
  23. import com.dmdirc.util.ConfigFile;
  24. import com.dmdirc.util.InvalidConfigFileException;
  25. import java.io.File;
  26. import java.io.IOException;
  27. import java.util.Arrays;
  28. import java.util.List;
  29. /**
  30. *
  31. * @author chris
  32. */
  33. public class Config implements Runnable {
  34. private static final String FS = System.getProperty("file.separator");
  35. private static final String FILE = System.getProperty("user.home") + FS + ".charliebravo";
  36. private static final List<String> SETTINGS = Arrays.asList(new String[] {
  37. "eve.apikey:String", "eve.userid:int", "eve.charid:int",
  38. "twitter.username:String", "twitter.password:String",
  39. "admin.level:int",
  40. "internal.lastseen:int", "internal.lastuser:String"
  41. });
  42. protected final ConfigFile configfile;
  43. public Config() {
  44. final File file = new File(FILE);
  45. configfile = new ConfigFile(file);
  46. configfile.setAutomake(true);
  47. try {
  48. if (file.exists()) {
  49. configfile.read();
  50. }
  51. } catch (IOException ex) {
  52. // Ignore
  53. } catch (InvalidConfigFileException ex) {
  54. // Ignore
  55. }
  56. Runtime.getRuntime().addShutdownHook(new Thread(this));
  57. }
  58. public boolean isLegalSetting(final String key) {
  59. return getType(key) != null;
  60. }
  61. public String getType(final String key) {
  62. for (String setting : SETTINGS) {
  63. if (setting.startsWith(key + ":")) {
  64. return setting.substring(setting.indexOf(':') + 1);
  65. }
  66. }
  67. return null;
  68. }
  69. public boolean hasOption(final String user, final String key) {
  70. return configfile.isKeyDomain(user) && configfile.getKeyDomain(user).containsKey(key);
  71. }
  72. public String getOption(final String user, final String key) {
  73. return configfile.getKeyDomain(user).get(key);
  74. }
  75. public String setOption(final String user, final String key, final Object obj) {
  76. final String value = String.valueOf(obj);
  77. if (getType(key).equals("int") && !value.matches("^[0-9]+$")) {
  78. throw new IllegalArgumentException("That setting must be an integer");
  79. }
  80. return configfile.getKeyDomain(user).put(key, value);
  81. }
  82. public void run() {
  83. try {
  84. configfile.write();
  85. } catch (IOException ex) {
  86. // Uh oh
  87. }
  88. }
  89. public ConfigFile getConfigfile() {
  90. return configfile;
  91. }
  92. }