Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

IdentdManager.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.config.prefs.PluginPreferencesCategory;
  25. import com.dmdirc.config.prefs.PreferencesCategory;
  26. import com.dmdirc.config.prefs.PreferencesDialogModel;
  27. import com.dmdirc.config.prefs.PreferencesSetting;
  28. import com.dmdirc.config.prefs.PreferencesType;
  29. import com.dmdirc.events.ClientPrefsOpenedEvent;
  30. import com.dmdirc.events.ServerConnectErrorEvent;
  31. import com.dmdirc.events.ServerConnectedEvent;
  32. import com.dmdirc.events.ServerConnectingEvent;
  33. import com.dmdirc.interfaces.Connection;
  34. import com.dmdirc.interfaces.EventBus;
  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 EventBus 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 EventBus 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. eventBus.subscribe(this);
  72. if (config.getOptionBool(domain, "advanced.alwaysOn")) {
  73. server.startServer();
  74. }
  75. }
  76. /**
  77. * Called when this plugin is unloaded.
  78. */
  79. public void onUnload() {
  80. eventBus.unsubscribe(this);
  81. server.stopServer();
  82. connections.clear();
  83. }
  84. @Handler
  85. public void handleServerConnecting(final ServerConnectingEvent event) {
  86. synchronized (connections) {
  87. if (connections.isEmpty()) {
  88. server.startServer();
  89. }
  90. connections.add(event.getConnection());
  91. }
  92. }
  93. @Handler
  94. public void handleServerConnected(final ServerConnectedEvent event) {
  95. handleServerRemoved(event.getConnection());
  96. }
  97. @Handler
  98. public void handleServerConnectError(final ServerConnectErrorEvent event) {
  99. handleServerRemoved(event.getConnection());
  100. }
  101. private void handleServerRemoved(final Connection connection) {
  102. synchronized (connections) {
  103. connections.remove(connection);
  104. if (connections.isEmpty() && !config.getOptionBool(domain, "advanced.alwaysOn")) {
  105. server.stopServer();
  106. }
  107. }
  108. }
  109. @Handler
  110. public void showConfig(final ClientPrefsOpenedEvent event) {
  111. final PreferencesDialogModel manager = event.getModel();
  112. final PreferencesCategory general = new PluginPreferencesCategory(
  113. pluginInfo, "Identd",
  114. "General Identd Plugin config ('Lower' options take priority "
  115. + "over those above them)");
  116. final PreferencesCategory advanced = new PluginPreferencesCategory(
  117. pluginInfo, "Advanced",
  118. "Advanced Identd Plugin config - Only edit these if you need "
  119. + "to/know what you are doing. Editing these could prevent "
  120. + "access to some servers. ('Lower' options take priority over "
  121. + "those above them)");
  122. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  123. domain, "general.useUsername", "Use connection "
  124. + "username rather than system username", "If this is enabled,"
  125. + " the username for the connection will be used rather than '"
  126. + System.getProperty("user.name") + '\'',
  127. manager.getConfigManager(), manager.getIdentity()));
  128. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  129. domain, "general.useNickname", "Use connection "
  130. + "nickname rather than system username", "If this is enabled, "
  131. + "the nickname for the connection will be used rather than '"
  132. + System.getProperty("user.name") + '\'',
  133. manager.getConfigManager(), manager.getIdentity()));
  134. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  135. domain, "general.useCustomName", "Use custom name" + " all the time",
  136. "If this is enabled, the name specified below" + " will be used all the time",
  137. manager.getConfigManager(),
  138. manager.getIdentity()));
  139. general.addSetting(new PreferencesSetting(PreferencesType.TEXT,
  140. domain, "general.customName", "Custom Name to use",
  141. "The custom name to use when 'Use Custom Name' is enabled",
  142. manager.getConfigManager(), manager.getIdentity()));
  143. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  144. domain, "advanced.alwaysOn", "Always have ident " + "port open",
  145. "By default the identd only runs when there are "
  146. + "active connection attempts. This overrides that.",
  147. manager.getConfigManager(), manager.getIdentity()));
  148. advanced.addSetting(new PreferencesSetting(PreferencesType.INTEGER,
  149. new PortValidator(), domain, "advanced.port",
  150. "What port should the identd listen on", "Default port is 113,"
  151. + " this is probably useless if changed unless you port forward"
  152. + " ident to a different port", manager.getConfigManager(),
  153. manager.getIdentity()));
  154. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  155. domain, "advanced.useCustomSystem", "Use custom OS",
  156. "By default the plugin uses 'UNIX' or 'WIN32' as the system "
  157. + "type, this can be overridden by enabling this.",
  158. manager.getConfigManager(), manager.getIdentity()));
  159. advanced.addSetting(new PreferencesSetting(PreferencesType.TEXT,
  160. domain, "advanced.customSystem", "Custom OS to use",
  161. "The custom system to use when 'Use Custom System' is enabled",
  162. manager.getConfigManager(), manager.getIdentity()));
  163. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  164. domain, "advanced.isHiddenUser", "Respond to ident"
  165. + " requests with HIDDEN-USER error", "By default the plugin will"
  166. + " give a USERID response, this can force an 'ERROR :"
  167. + " HIDDEN-USER' response instead.", manager.getConfigManager(),
  168. manager.getIdentity()));
  169. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  170. domain, "advanced.isNoUser", "Respond to ident"
  171. + " requests with NO-USER error", "By default the plugin will"
  172. + " give a USERID response, this can force an 'ERROR : NO-USER'"
  173. + " response instead. (Overrides HIDDEN-USER)",
  174. manager.getConfigManager(), manager.getIdentity()));
  175. manager.getCategory("Plugins").addSubCategory(general);
  176. general.addSubCategory(advanced);
  177. }
  178. }