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.

IdentdPlugin.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2006-2008 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.addons.identd;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.Server;
  25. import com.dmdirc.actions.ActionManager;
  26. import com.dmdirc.actions.interfaces.ActionType;
  27. import com.dmdirc.actions.CoreActionType;
  28. import com.dmdirc.config.IdentityManager;
  29. import com.dmdirc.config.Identity;
  30. import com.dmdirc.interfaces.ActionListener;
  31. import com.dmdirc.plugins.Plugin;
  32. import com.dmdirc.ui.interfaces.PreferencesInterface;
  33. import com.dmdirc.ui.interfaces.PreferencesPanel;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. import java.util.Properties;
  37. /**
  38. * The Identd plugin answers ident requests from IRC servers.
  39. *
  40. * @author Shane
  41. */
  42. public class IdentdPlugin extends Plugin implements ActionListener, PreferencesInterface {
  43. /** What domain do we store all settings in the global config under. */
  44. private static final String MY_DOMAIN = "plugin-Identd";
  45. /** Array list to store all the servers in that need ident replies */
  46. private final List<Server> servers = new ArrayList<Server>();
  47. /** The IdentdServer that we use*/
  48. private IdentdServer myServer;
  49. /**
  50. * Creates a new instance of IdentdPlugin.
  51. */
  52. public IdentdPlugin() { }
  53. /**
  54. * Called when the plugin is loaded.
  55. */
  56. @Override
  57. public void onLoad() {
  58. // Add action hooks
  59. ActionManager.addListener(this, CoreActionType.SERVER_CONNECTED, CoreActionType.SERVER_CONNECTING, CoreActionType.SERVER_CONNECTERROR);
  60. // Set defaults
  61. Properties defaults = new Properties();
  62. defaults.setProperty(getDomain() + ".general.useUsername", "false");
  63. defaults.setProperty(getDomain() + ".general.useNickname", "false");
  64. defaults.setProperty(getDomain() + ".general.useCustomName", "false");
  65. defaults.setProperty(getDomain() + ".general.customName", "DMDirc-user");
  66. defaults.setProperty(getDomain() + ".advanced.alwaysOn", "false");
  67. defaults.setProperty(getDomain() + ".advanced.port", "113");
  68. defaults.setProperty(getDomain() + ".advanced.useCustomSystem", "false");
  69. defaults.setProperty(getDomain() + ".advanced.customSystem", "OTHER");
  70. defaults.setProperty(getDomain() + ".advanced.isHiddenUser", "false");
  71. defaults.setProperty(getDomain() + ".advanced.isNoUser", "false");
  72. defaults.setProperty("identity.name", "Identd Plugin Defaults");
  73. IdentityManager.addIdentity(new Identity(defaults));
  74. myServer = new IdentdServer(this);
  75. if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "advanced.alwaysOn")) {
  76. myServer.startServer();
  77. }
  78. }
  79. /**
  80. * Called when this plugin is unloaded.
  81. */
  82. @Override
  83. public void onUnload() {
  84. myServer.stopServer();
  85. servers.clear();
  86. ActionManager.removeListener(this);
  87. }
  88. /**
  89. * Process an event of the specified type.
  90. *
  91. * @param type The type of the event to process
  92. * @param format Format of messages that are about to be sent. (May be null)
  93. * @param arguments The arguments for the event
  94. */
  95. @Override
  96. public void processEvent(final ActionType type, final StringBuffer format, final Object... arguments) {
  97. if (type == CoreActionType.SERVER_CONNECTING) {
  98. synchronized (servers) {
  99. if (servers.size() == 0) {
  100. myServer.startServer();
  101. }
  102. servers.add((Server) arguments[0]);
  103. }
  104. } else if (type == CoreActionType.SERVER_CONNECTED || type == CoreActionType.SERVER_CONNECTERROR) {
  105. synchronized (servers) {
  106. servers.remove(arguments[0]);
  107. if (servers.size() == 0) {
  108. if (!IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "advanced.alwaysOn")) {
  109. myServer.stopServer();
  110. }
  111. }
  112. }
  113. }
  114. }
  115. /**
  116. * Called to see if the plugin has configuration options (via dialog).
  117. *
  118. * @return true if the plugin has configuration options via a dialog.
  119. */
  120. @Override
  121. public boolean isConfigurable() { return true; }
  122. /**
  123. * Called to show the Configuration dialog of the plugin if appropriate.
  124. */
  125. @Override
  126. public void showConfig() {
  127. final PreferencesPanel preferencesPanel = Main.getUI().getPreferencesPanel(this, "Identd Plugin - Config");
  128. preferencesPanel.addCategory("General", "General Identd Plugin config ('Lower' options take priority over those above them)");
  129. preferencesPanel.addCategory("Advanced", "Advanced Identd Plugin config - Only edit these if you need to/know what you are doing. Editing these could prevent access to some servers. ('Lower' options take priority over those above them)");
  130. preferencesPanel.addCheckboxOption("General", "general.useUsername", "Use connection username rather than system username: ", "If this is enabled, the username for the connection will be used rather than '"+System.getProperty("user.name")+"'", IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.useUsername"));
  131. preferencesPanel.addCheckboxOption("General", "general.useNickname", "Use connection nickname rather than system username: ", "If this is enabled, the nickname for the connection will be used rather than '"+System.getProperty("user.name")+"'", IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.useNickname"));
  132. preferencesPanel.addCheckboxOption("General", "general.useCustomName", "Use custom name all the time: ", "If this is enabled, the name specified below will be used all the time", IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.useCustomName"));
  133. preferencesPanel.addTextfieldOption("General", "general.customName", "Custom Name to use: ", "The custom name to use when 'Use Custom Name' is enabled", IdentityManager.getGlobalConfig().getOption(getDomain(), "general.customName"));
  134. preferencesPanel.addCheckboxOption("Advanced", "advanced.alwaysOn", "Always have ident port open: ", "By default the identd only runs when there is active connection attempts. This overrides that.", IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "advanced.alwaysOn"));
  135. preferencesPanel.addSpinnerOption("Advanced", "advanced.port", "What port should the identd listen on: ", "Default port is 113, this is probably useless if changed unless you port forward ident to a different port", IdentityManager.getGlobalConfig().getOptionInt(getDomain(), "advanced.port", 113));
  136. preferencesPanel.addCheckboxOption("Advanced", "advanced.useCustomSystem", "Use custom OS: ", "By default the plugin uses 'UNIX' or 'WIN32' as the system type, this can be overriden by enabling this.", IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "advanced.useCustomSystem"));
  137. preferencesPanel.addTextfieldOption("Advanced", "advanced.customSystem", "Custom OS to use: ", "The custom system to use when 'Use Custom System' is enabled", IdentityManager.getGlobalConfig().getOption(getDomain(), "advanced.customSystem"));
  138. preferencesPanel.addCheckboxOption("Advanced", "advanced.isHiddenUser", "Respond to ident requests with HIDDEN-USER error: ", "By default the plugin will give a USERID response, this can force an 'ERROR : HIDDEN-USER' response instead.", IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "advanced.isHiddenUser"));
  139. preferencesPanel.addCheckboxOption("Advanced", "advanced.isNoUser", "Respond to ident requests with NO-USER error: ", "By default the plugin will give a USERID response, this can force an 'ERROR : NO-USER' response instead. (Overrides HIDDEN-USER)", IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "advanced.isNoUser"));
  140. preferencesPanel.display();
  141. }
  142. /**
  143. * Get the name of the domain we store all settings in the global config under.
  144. *
  145. * @return the plugins domain
  146. */
  147. protected static String getDomain() { return MY_DOMAIN; }
  148. /**
  149. * Copy the new vaule of an option to the global config.
  150. *
  151. * @param properties Source of option value, or null if setting default values
  152. * @param name name of option
  153. */
  154. protected void updateOption(final Properties properties, final String name) {
  155. String value = null;
  156. // Get the value from the properties file if one is given, else use the
  157. // value from the global config.
  158. if (properties != null) {
  159. value = properties.getProperty(name);
  160. } else {
  161. value = IdentityManager.getGlobalConfig().getOption(getDomain(), name);
  162. }
  163. // Check if the Value exists
  164. if (value != null) {
  165. // It does, so update the global config with the new value
  166. IdentityManager.getConfigIdentity().setOption(getDomain(), name, value);
  167. }
  168. }
  169. /**
  170. * Called when the preferences dialog is closed.
  171. *
  172. * @param properties user preferences
  173. */
  174. @Override
  175. public void configClosed(final Properties properties) {
  176. // Update Config options
  177. final int oldPort = IdentityManager.getGlobalConfig().getOptionInt(getDomain(), "advanced.port", 113);
  178. updateOption(properties, "general.useUsername");
  179. updateOption(properties, "general.useNickname");
  180. updateOption(properties, "general.useCustomName");
  181. updateOption(properties, "general.customName");
  182. updateOption(properties, "advanced.alwaysOn");
  183. updateOption(properties, "advanced.port");
  184. updateOption(properties, "advanced.useCustomSystem");
  185. updateOption(properties, "advanced.customSystem");
  186. updateOption(properties, "advanced.isHiddenUser");
  187. updateOption(properties, "advanced.isNoUser");
  188. final int newPort = IdentityManager.getGlobalConfig().getOptionInt(getDomain(), "advanced.port", 113);
  189. if (myServer.isRunning() && oldPort != newPort) {
  190. myServer.stopServer();
  191. myServer.startServer();
  192. }
  193. }
  194. /**
  195. * Called when the preferences dialog is cancelled.
  196. */
  197. @Override
  198. public void configCancelled() { }
  199. }