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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.md87.charliebravo;
  6. import com.dmdirc.util.ConfigFile;
  7. import com.dmdirc.util.InvalidConfigFileException;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. /**
  13. *
  14. * @author chris
  15. */
  16. public class Config implements Runnable {
  17. private static final String FS = System.getProperty("file.separator");
  18. private static final String FILE = System.getProperty("user.home") + FS + ".charliebravo";
  19. private static final List<String> SETTINGS = Arrays.asList(new String[] {
  20. "eve.apikey:String", "eve.userid:int", "eve.charid:int",
  21. "admin.level:int",
  22. "internal.lastseen:int", "internal.lastuser:String"
  23. });
  24. protected final ConfigFile configfile;
  25. public Config() {
  26. final File file = new File(FILE);
  27. configfile = new ConfigFile(file);
  28. configfile.setAutomake(true);
  29. try {
  30. if (file.exists()) {
  31. configfile.read();
  32. }
  33. } catch (IOException ex) {
  34. // Ignore
  35. } catch (InvalidConfigFileException ex) {
  36. // Ignore
  37. }
  38. Runtime.getRuntime().addShutdownHook(new Thread(this));
  39. }
  40. public boolean isLegalSetting(final String key) {
  41. return getType(key) != null;
  42. }
  43. public String getType(final String key) {
  44. for (String setting : SETTINGS) {
  45. if (setting.startsWith(key + ":")) {
  46. return setting.substring(setting.indexOf(':') + 1);
  47. }
  48. }
  49. return null;
  50. }
  51. public boolean hasOption(final String user, final String key) {
  52. return configfile.isKeyDomain(user) && configfile.getKeyDomain(user).containsKey(key);
  53. }
  54. public String getOption(final String user, final String key) {
  55. return configfile.getKeyDomain(user).get(key);
  56. }
  57. public String setOption(final String user, final String key, final Object obj) {
  58. final String value = String.valueOf(obj);
  59. if (getType(key).equals("int") && !value.matches("^[0-9]+$")) {
  60. throw new IllegalArgumentException("That setting must be an integer");
  61. }
  62. return configfile.getKeyDomain(user).put(key, value);
  63. }
  64. public void run() {
  65. try {
  66. configfile.write();
  67. } catch (IOException ex) {
  68. // Uh oh
  69. }
  70. }
  71. public ConfigFile getConfigfile() {
  72. return configfile;
  73. }
  74. }