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.

IdentityManager.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.config;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.Precondition;
  25. import com.dmdirc.logger.ErrorLevel;
  26. import com.dmdirc.logger.Logger;
  27. import java.io.File;
  28. import java.io.IOException;
  29. import java.io.InputStream;
  30. import java.util.ArrayList;
  31. import java.util.Collections;
  32. import java.util.List;
  33. /**
  34. * The identity manager manages all known identities, providing easy methods
  35. * to access them.
  36. * @author chris
  37. */
  38. public final class IdentityManager {
  39. /** The identities that have been loaded into this manager. */
  40. private final static List<Identity> identities = new ArrayList<Identity>();
  41. /** The config managers that have registered with this manager. */
  42. // XXX: Should this be a weak list?
  43. private final static List<ConfigManager> managers = new ArrayList<ConfigManager>();
  44. /** The identity file used for the global config. */
  45. private static Identity config;
  46. /** The config manager used for global settings. */
  47. private static ConfigManager globalconfig;
  48. /** Creates a new instance of IdentityManager. */
  49. private IdentityManager() {
  50. }
  51. /** Loads all identity files. */
  52. public static void load() {
  53. identities.clear();
  54. managers.clear();
  55. loadDefaults();
  56. loadUser();
  57. loadConfig();
  58. if (getProfiles().size() == 0) {
  59. Identity.buildProfile("Default Profile");
  60. }
  61. }
  62. /** Loads the default (built in) identities. */
  63. private static void loadDefaults() {
  64. final ClassLoader cldr = IdentityManager.class.getClassLoader();
  65. final String base = "com/dmdirc/config/defaults/";
  66. final String[] urls = {"default/defaults", "default/formatter"};
  67. for (String url : urls) {
  68. try {
  69. final InputStream res = cldr.getResourceAsStream(base + url);
  70. if (res == null) {
  71. Logger.userError(ErrorLevel.MEDIUM,
  72. "Unable to load default identity: " + url);
  73. } else {
  74. addIdentity(new Identity(res, false));
  75. }
  76. } catch (InvalidIdentityFileException ex) {
  77. Logger.userError(ErrorLevel.MEDIUM, "Invalid identity file");
  78. } catch (IOException ex) {
  79. Logger.userError(ErrorLevel.MEDIUM, "Unable to load identity file");
  80. }
  81. }
  82. }
  83. /**
  84. * Retrieves the directory used to store identities in.
  85. *
  86. * @return The identity directory path
  87. */
  88. public static String getDirectory() {
  89. return Main.getConfigDir() + "identities" + System.getProperty("file.separator");
  90. }
  91. /** Loads user-defined identity files. */
  92. public static void loadUser() {
  93. final File dir = new File(getDirectory());
  94. if (!dir.exists()) {
  95. try {
  96. dir.mkdirs();
  97. dir.createNewFile();
  98. } catch (IOException ex) {
  99. Logger.userError(ErrorLevel.MEDIUM, "Unable to create identity dir");
  100. }
  101. }
  102. loadUser(dir);
  103. }
  104. /**
  105. * Recursively loads files from the specified directory.
  106. *
  107. * @param dir The directory to be loaded
  108. */
  109. @Precondition({
  110. "The specified File is not null",
  111. "The specified File is a directory"
  112. })
  113. private static void loadUser(final File dir) {
  114. assert(dir != null);
  115. assert(dir.isDirectory());
  116. if (dir.listFiles() == null) {
  117. Logger.userError(ErrorLevel.MEDIUM,
  118. "Unable to load user identity files from "
  119. + dir.getAbsolutePath());
  120. } else {
  121. for (File file : dir.listFiles()) {
  122. if (file.isDirectory()) {
  123. loadUser(file);
  124. } else {
  125. try {
  126. addIdentity(new Identity(file, false));
  127. } catch (InvalidIdentityFileException ex) {
  128. Logger.userError(ErrorLevel.MEDIUM,
  129. "Invalid identity file: " + file.getAbsolutePath()
  130. + " (" + ex.getMessage() + ")");
  131. } catch (IOException ex) {
  132. Logger.userError(ErrorLevel.MEDIUM,
  133. "I/O error when reading identity file: "
  134. + file.getAbsolutePath());
  135. }
  136. }
  137. }
  138. }
  139. }
  140. /** Loads the config identity. */
  141. private static void loadConfig() {
  142. try {
  143. final File file = new File(Main.getConfigDir() + "dmdirc.config");
  144. if (!file.exists()) {
  145. file.createNewFile();
  146. }
  147. config = new Identity(file, true);
  148. config.setOption("identity", "name", "Global config");
  149. addIdentity(config);
  150. } catch (InvalidIdentityFileException ex) {
  151. // This shouldn't happen as we're forcing it to global
  152. Logger.appError(ErrorLevel.HIGH, "Unable to load global config", ex);
  153. } catch (IOException ex) {
  154. Logger.userError(ErrorLevel.MEDIUM, "I/O error when loading file: "
  155. + ex.getMessage());
  156. }
  157. }
  158. /**
  159. * Retrieves the identity used for the global config.
  160. *
  161. * @return The global config identity
  162. */
  163. public static Identity getConfigIdentity() {
  164. return config;
  165. }
  166. /**
  167. * Saves all modified identity files to disk.
  168. */
  169. public static void save() {
  170. for (Identity identity : identities) {
  171. identity.save();
  172. }
  173. }
  174. /**
  175. * Adds the specific identity to this manager.
  176. * @param identity The identity to be added
  177. */
  178. @Precondition("The specified Identity is not null")
  179. public static void addIdentity(final Identity identity) {
  180. assert(identity != null);
  181. if (identities.contains(identity)) {
  182. removeIdentity(identity);
  183. }
  184. identities.add(identity);
  185. for (ConfigManager manager : managers) {
  186. manager.checkIdentity(identity);
  187. }
  188. }
  189. /**
  190. * Removes an identity from this manager.
  191. * @param identity The identity to be removed
  192. */
  193. @Precondition({
  194. "The specified Identity is not null",
  195. "The specified Identity has previously been added and not removed"
  196. })
  197. public static void removeIdentity(final Identity identity) {
  198. assert(identity != null);
  199. assert(identities.contains(identity));
  200. identities.remove(identity);
  201. }
  202. /**
  203. * Adds a config manager to this manager.
  204. * @param manager The ConfigManager to add
  205. */
  206. @Precondition("The specified ConfigManager is not null")
  207. public static void addConfigManager(final ConfigManager manager) {
  208. assert(manager != null);
  209. managers.add(manager);
  210. }
  211. /**
  212. * Retrieves a list of identities that serve as profiles.
  213. * @return A list of profiles
  214. */
  215. public static List<Identity> getProfiles() {
  216. final List<Identity> profiles = new ArrayList<Identity>();
  217. for (Identity identity : identities) {
  218. if (identity.isProfile()) {
  219. profiles.add(identity);
  220. }
  221. }
  222. return profiles;
  223. }
  224. /**
  225. * Retrieves a list of all config sources that should be applied to the
  226. * specified target.
  227. * @param ircd The server's ircd
  228. * @param network The name of the network
  229. * @param server The server's name
  230. * @param channel The channel name (in the form channel@network)
  231. * @return A list of all matching config sources
  232. */
  233. public static List<Identity> getSources(final String ircd,
  234. final String network, final String server, final String channel) {
  235. final List<Identity> sources = new ArrayList<Identity>();
  236. String comp = "";
  237. for (Identity identity : identities) {
  238. switch (identity.getTarget().getType()) {
  239. case IRCD:
  240. comp = ircd;
  241. break;
  242. case NETWORK:
  243. comp = network;
  244. break;
  245. case SERVER:
  246. comp = server;
  247. break;
  248. case CHANNEL:
  249. comp = channel;
  250. break;
  251. case PROFILE:
  252. comp = null;
  253. break;
  254. default:
  255. comp = "";
  256. break;
  257. }
  258. if (comp != null && comp.equalsIgnoreCase(identity.getTarget().getData())) {
  259. sources.add(identity);
  260. }
  261. }
  262. Collections.sort(sources);
  263. return sources;
  264. }
  265. /**
  266. * Retrieves a list of all config sources that should be applied to the
  267. * specified target.
  268. * @param ircd The server's ircd
  269. * @param network The name of the network
  270. * @param server The server's name
  271. * @return A list of all matching config sources
  272. */
  273. public static List<Identity> getSources(final String ircd,
  274. final String network, final String server) {
  275. return getSources(ircd, network, server, "<Unknown>");
  276. }
  277. /**
  278. * Retrieves the global config manager.
  279. *
  280. * @return The global config manager
  281. */
  282. public static synchronized ConfigManager getGlobalConfig() {
  283. if (globalconfig == null) {
  284. globalconfig = new ConfigManager("", "", "");
  285. }
  286. return globalconfig;
  287. }
  288. /**
  289. * Retrieves the config for the specified channel@network. The config is
  290. * created if it doesn't exist.
  291. * @param network The name of the network
  292. * @param channel The name of the channel
  293. * @return A config source for the channel
  294. */
  295. @Precondition({
  296. "The specified network is non-null and not empty",
  297. "The specified channel is non-null and not empty"
  298. })
  299. public static Identity getChannelConfig(final String network,
  300. final String channel) {
  301. assert(network != null);
  302. assert(!network.isEmpty());
  303. assert(channel != null);
  304. assert(!channel.isEmpty());
  305. final String myTarget = (channel + "@" + network).toLowerCase();
  306. for (Identity identity : identities) {
  307. if (identity.getTarget().getType() == ConfigTarget.TYPE.CHANNEL
  308. && identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
  309. return identity;
  310. }
  311. }
  312. // We need to create one
  313. final ConfigTarget target = new ConfigTarget();
  314. target.setChannel(myTarget);
  315. return Identity.buildIdentity(target);
  316. }
  317. /**
  318. * Retrieves the config for the specified network. The config is
  319. * created if it doesn't exist.
  320. * @param network The name of the network
  321. * @return A config source for the network
  322. */
  323. @Precondition("The specified network is non-null and not empty")
  324. public static Identity getNetworkConfig(final String network) {
  325. assert(network != null);
  326. assert(!network.isEmpty());
  327. final String myTarget = network.toLowerCase();
  328. for (Identity identity : identities) {
  329. if (identity.getTarget().getType() == ConfigTarget.TYPE.NETWORK
  330. && identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
  331. return identity;
  332. }
  333. }
  334. // We need to create one
  335. final ConfigTarget target = new ConfigTarget();
  336. target.setNetwork(myTarget);
  337. return Identity.buildIdentity(target);
  338. }
  339. }