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.

IdentdManager.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  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.ClientModule.GlobalConfig;
  24. import com.dmdirc.DMDircMBassador;
  25. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  26. import com.dmdirc.config.prefs.PreferencesCategory;
  27. import com.dmdirc.config.prefs.PreferencesDialogModel;
  28. import com.dmdirc.config.prefs.PreferencesSetting;
  29. import com.dmdirc.config.prefs.PreferencesType;
  30. import com.dmdirc.events.ClientPrefsOpenedEvent;
  31. import com.dmdirc.events.ServerConnectErrorEvent;
  32. import com.dmdirc.events.ServerConnectedEvent;
  33. import com.dmdirc.events.ServerConnectingEvent;
  34. import com.dmdirc.interfaces.Connection;
  35. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  36. import com.dmdirc.plugins.PluginDomain;
  37. import com.dmdirc.plugins.PluginInfo;
  38. import com.dmdirc.util.validators.PortValidator;
  39. import java.util.ArrayList;
  40. import java.util.List;
  41. import javax.inject.Inject;
  42. import net.engio.mbassy.listener.Handler;
  43. public class IdentdManager {
  44. /** List of all the connections that need ident replies. */
  45. private final List<Connection> connections;
  46. /** Global config. */
  47. private final AggregateConfigProvider config;
  48. /** This plugin's settings domain. */
  49. private final String domain;
  50. /** Ident server. */
  51. private final IdentdServer server;
  52. /** Event bus to subscribe to events on. */
  53. private final DMDircMBassador eventBus;
  54. private final PluginInfo pluginInfo;
  55. @Inject
  56. public IdentdManager(@GlobalConfig final AggregateConfigProvider config,
  57. @PluginDomain(IdentdPlugin.class) final String domain,
  58. @PluginDomain(IdentdPlugin.class) final PluginInfo pluginInfo,
  59. final IdentdServer server, final DMDircMBassador eventBus) {
  60. this.pluginInfo = pluginInfo;
  61. connections = new ArrayList<>();
  62. this.config = config;
  63. this.domain = domain;
  64. this.server = server;
  65. this.eventBus = eventBus;
  66. }
  67. /**
  68. * Called when the plugin is loaded.
  69. */
  70. public void onLoad() {
  71. // Add action hooks
  72. eventBus.subscribe(this);
  73. if (config.getOptionBool(domain, "advanced.alwaysOn")) {
  74. server.startServer();
  75. }
  76. }
  77. /**
  78. * Called when this plugin is unloaded.
  79. */
  80. public void onUnload() {
  81. eventBus.unsubscribe(this);
  82. server.stopServer();
  83. connections.clear();
  84. }
  85. @Handler
  86. public void handleServerConnecting(final ServerConnectingEvent event) {
  87. synchronized (connections) {
  88. if (connections.isEmpty()) {
  89. server.startServer();
  90. }
  91. connections.add(event.getConnection());
  92. }
  93. }
  94. @Handler
  95. public void handleServerConnected(final ServerConnectedEvent event) {
  96. handleServerRemoved(event.getConnection());
  97. }
  98. @Handler
  99. public void handleServerConnectError(final ServerConnectErrorEvent event) {
  100. handleServerRemoved(event.getConnection());
  101. }
  102. private void handleServerRemoved(final Connection connection) {
  103. synchronized (connections) {
  104. connections.remove(connection);
  105. if (connections.isEmpty() && !config.getOptionBool(domain, "advanced.alwaysOn")) {
  106. server.stopServer();
  107. }
  108. }
  109. }
  110. @Handler
  111. public void showConfig(final ClientPrefsOpenedEvent event) {
  112. final PreferencesDialogModel manager = event.getModel();
  113. final PreferencesCategory general = new PluginPreferencesCategory(
  114. pluginInfo, "Identd",
  115. "General Identd Plugin config ('Lower' options take priority "
  116. + "over those above them)");
  117. final PreferencesCategory advanced = new PluginPreferencesCategory(
  118. pluginInfo, "Advanced",
  119. "Advanced Identd Plugin config - Only edit these if you need "
  120. + "to/know what you are doing. Editing these could prevent "
  121. + "access to some servers. ('Lower' options take priority over "
  122. + "those above them)");
  123. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  124. domain, "general.useUsername", "Use connection "
  125. + "username rather than system username", "If this is enabled,"
  126. + " the username for the connection will be used rather than '"
  127. + System.getProperty("user.name") + '\'',
  128. manager.getConfigManager(), manager.getIdentity()));
  129. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  130. domain, "general.useNickname", "Use connection "
  131. + "nickname rather than system username", "If this is enabled, "
  132. + "the nickname for the connection will be used rather than '"
  133. + System.getProperty("user.name") + '\'',
  134. manager.getConfigManager(), manager.getIdentity()));
  135. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  136. domain, "general.useCustomName", "Use custom name" + " all the time",
  137. "If this is enabled, the name specified below" + " will be used all the time",
  138. manager.getConfigManager(),
  139. manager.getIdentity()));
  140. general.addSetting(new PreferencesSetting(PreferencesType.TEXT,
  141. domain, "general.customName", "Custom Name to use",
  142. "The custom name to use when 'Use Custom Name' is enabled",
  143. manager.getConfigManager(), manager.getIdentity()));
  144. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  145. domain, "advanced.alwaysOn", "Always have ident " + "port open",
  146. "By default the identd only runs when there are "
  147. + "active connection attempts. This overrides that.",
  148. manager.getConfigManager(), manager.getIdentity()));
  149. advanced.addSetting(new PreferencesSetting(PreferencesType.INTEGER,
  150. new PortValidator(), domain, "advanced.port",
  151. "What port should the identd listen on", "Default port is 113,"
  152. + " this is probably useless if changed unless you port forward"
  153. + " ident to a different port", manager.getConfigManager(),
  154. manager.getIdentity()));
  155. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  156. domain, "advanced.useCustomSystem", "Use custom OS",
  157. "By default the plugin uses 'UNIX' or 'WIN32' as the system "
  158. + "type, this can be overridden by enabling this.",
  159. manager.getConfigManager(), manager.getIdentity()));
  160. advanced.addSetting(new PreferencesSetting(PreferencesType.TEXT,
  161. domain, "advanced.customSystem", "Custom OS to use",
  162. "The custom system to use when 'Use Custom System' is enabled",
  163. manager.getConfigManager(), manager.getIdentity()));
  164. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  165. domain, "advanced.isHiddenUser", "Respond to ident"
  166. + " requests with HIDDEN-USER error", "By default the plugin will"
  167. + " give a USERID response, this can force an 'ERROR :"
  168. + " HIDDEN-USER' response instead.", manager.getConfigManager(),
  169. manager.getIdentity()));
  170. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  171. domain, "advanced.isNoUser", "Respond to ident"
  172. + " requests with NO-USER error", "By default the plugin will"
  173. + " give a USERID response, this can force an 'ERROR : NO-USER'"
  174. + " response instead. (Overrides HIDDEN-USER)",
  175. manager.getConfigManager(), manager.getIdentity()));
  176. manager.getCategory("Plugins").addSubCategory(general);
  177. general.addSubCategory(advanced);
  178. }
  179. }