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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.identd;
  18. import com.dmdirc.config.GlobalConfig;
  19. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  20. import com.dmdirc.config.prefs.PreferencesCategory;
  21. import com.dmdirc.config.prefs.PreferencesDialogModel;
  22. import com.dmdirc.config.prefs.PreferencesSetting;
  23. import com.dmdirc.config.prefs.PreferencesType;
  24. import com.dmdirc.events.ClientPrefsOpenedEvent;
  25. import com.dmdirc.events.ServerConnectErrorEvent;
  26. import com.dmdirc.events.ServerConnectedEvent;
  27. import com.dmdirc.events.ServerConnectingEvent;
  28. import com.dmdirc.interfaces.Connection;
  29. import com.dmdirc.events.eventbus.EventBus;
  30. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  31. import com.dmdirc.plugins.PluginDomain;
  32. import com.dmdirc.plugins.PluginInfo;
  33. import com.dmdirc.util.validators.PortValidator;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. import javax.inject.Inject;
  37. import net.engio.mbassy.listener.Handler;
  38. public class IdentdManager {
  39. /** List of all the connections that need ident replies. */
  40. private final List<Connection> connections;
  41. /** Global config. */
  42. private final AggregateConfigProvider config;
  43. /** This plugin's settings domain. */
  44. private final String domain;
  45. /** Ident server. */
  46. private final IdentdServer server;
  47. /** Event bus to subscribe to events on. */
  48. private final EventBus eventBus;
  49. private final PluginInfo pluginInfo;
  50. @Inject
  51. public IdentdManager(@GlobalConfig final AggregateConfigProvider config,
  52. @PluginDomain(IdentdPlugin.class) final String domain,
  53. @PluginDomain(IdentdPlugin.class) final PluginInfo pluginInfo,
  54. final IdentdServer server, final EventBus eventBus) {
  55. this.pluginInfo = pluginInfo;
  56. connections = new ArrayList<>();
  57. this.config = config;
  58. this.domain = domain;
  59. this.server = server;
  60. this.eventBus = eventBus;
  61. }
  62. /**
  63. * Called when the plugin is loaded.
  64. */
  65. public void onLoad() {
  66. eventBus.subscribe(this);
  67. if (config.getOptionBool(domain, "advanced.alwaysOn")) {
  68. server.startServer();
  69. }
  70. }
  71. /**
  72. * Called when this plugin is unloaded.
  73. */
  74. public void onUnload() {
  75. eventBus.unsubscribe(this);
  76. server.stopServer();
  77. connections.clear();
  78. }
  79. @Handler
  80. public void handleServerConnecting(final ServerConnectingEvent event) {
  81. synchronized (connections) {
  82. if (connections.isEmpty()) {
  83. server.startServer();
  84. }
  85. connections.add(event.getConnection());
  86. }
  87. }
  88. @Handler
  89. public void handleServerConnected(final ServerConnectedEvent event) {
  90. handleServerRemoved(event.getConnection());
  91. }
  92. @Handler
  93. public void handleServerConnectError(final ServerConnectErrorEvent event) {
  94. handleServerRemoved(event.getConnection());
  95. }
  96. private void handleServerRemoved(final Connection connection) {
  97. synchronized (connections) {
  98. connections.remove(connection);
  99. if (connections.isEmpty() && !config.getOptionBool(domain, "advanced.alwaysOn")) {
  100. server.stopServer();
  101. }
  102. }
  103. }
  104. @Handler
  105. public void showConfig(final ClientPrefsOpenedEvent event) {
  106. final PreferencesDialogModel manager = event.getModel();
  107. final PreferencesCategory general = new PluginPreferencesCategory(
  108. pluginInfo, "Identd",
  109. "General Identd Plugin config ('Lower' options take priority "
  110. + "over those above them)");
  111. final PreferencesCategory advanced = new PluginPreferencesCategory(
  112. pluginInfo, "Advanced",
  113. "Advanced Identd Plugin config - Only edit these if you need "
  114. + "to/know what you are doing. Editing these could prevent "
  115. + "access to some servers. ('Lower' options take priority over "
  116. + "those above them)");
  117. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  118. domain, "general.useUsername", "Use connection "
  119. + "username rather than system username", "If this is enabled,"
  120. + " the username for the connection will be used rather than '"
  121. + System.getProperty("user.name") + '\'',
  122. manager.getConfigManager(), manager.getIdentity()));
  123. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  124. domain, "general.useNickname", "Use connection "
  125. + "nickname rather than system username", "If this is enabled, "
  126. + "the nickname 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.useCustomName", "Use custom name" + " all the time",
  131. "If this is enabled, the name specified below" + " will be used all the time",
  132. manager.getConfigManager(),
  133. manager.getIdentity()));
  134. general.addSetting(new PreferencesSetting(PreferencesType.TEXT,
  135. domain, "general.customName", "Custom Name to use",
  136. "The custom name to use when 'Use Custom Name' is enabled",
  137. manager.getConfigManager(), manager.getIdentity()));
  138. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  139. domain, "advanced.alwaysOn", "Always have ident " + "port open",
  140. "By default the identd only runs when there are "
  141. + "active connection attempts. This overrides that.",
  142. manager.getConfigManager(), manager.getIdentity()));
  143. advanced.addSetting(new PreferencesSetting(PreferencesType.INTEGER,
  144. new PortValidator(), domain, "advanced.port",
  145. "What port should the identd listen on", "Default port is 113,"
  146. + " this is probably useless if changed unless you port forward"
  147. + " ident to a different port", manager.getConfigManager(),
  148. manager.getIdentity()));
  149. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  150. domain, "advanced.useCustomSystem", "Use custom OS",
  151. "By default the plugin uses 'UNIX' or 'WIN32' as the system "
  152. + "type, this can be overridden by enabling this.",
  153. manager.getConfigManager(), manager.getIdentity()));
  154. advanced.addSetting(new PreferencesSetting(PreferencesType.TEXT,
  155. domain, "advanced.customSystem", "Custom OS to use",
  156. "The custom system to use when 'Use Custom System' is enabled",
  157. manager.getConfigManager(), manager.getIdentity()));
  158. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  159. domain, "advanced.isHiddenUser", "Respond to ident"
  160. + " requests with HIDDEN-USER error", "By default the plugin will"
  161. + " give a USERID response, this can force an 'ERROR :"
  162. + " HIDDEN-USER' response instead.", manager.getConfigManager(),
  163. manager.getIdentity()));
  164. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  165. domain, "advanced.isNoUser", "Respond to ident"
  166. + " requests with NO-USER error", "By default the plugin will"
  167. + " give a USERID response, this can force an 'ERROR : NO-USER'"
  168. + " response instead. (Overrides HIDDEN-USER)",
  169. manager.getConfigManager(), manager.getIdentity()));
  170. manager.getCategory("Plugins").addSubCategory(general);
  171. general.addSubCategory(advanced);
  172. }
  173. }