您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

QAuthManager.java 7.3KB

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.qauth;
  23. import com.dmdirc.ClientModule.GlobalConfig;
  24. import com.dmdirc.DMDircMBassador;
  25. import com.dmdirc.Invite;
  26. import com.dmdirc.config.ConfigBinder;
  27. import com.dmdirc.config.ConfigBinding;
  28. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  29. import com.dmdirc.config.prefs.PreferencesSetting;
  30. import com.dmdirc.config.prefs.PreferencesType;
  31. import com.dmdirc.events.ClientPrefsOpenedEvent;
  32. import com.dmdirc.events.QueryMessageEvent;
  33. import com.dmdirc.events.ServerConnectedEvent;
  34. import com.dmdirc.events.ServerInviteReceivedEvent;
  35. import com.dmdirc.events.ServerNoticeEvent;
  36. import com.dmdirc.interfaces.Connection;
  37. import com.dmdirc.interfaces.User;
  38. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  39. import com.dmdirc.plugins.PluginDomain;
  40. import com.dmdirc.plugins.PluginInfo;
  41. import com.dmdirc.util.validators.NotEmptyValidator;
  42. import java.util.Optional;
  43. import javax.inject.Inject;
  44. import net.engio.mbassy.listener.Handler;
  45. /**
  46. * Provides Q AUth support in DMDirc.
  47. */
  48. public class QAuthManager {
  49. private final String domain;
  50. private final PluginInfo pluginInfo;
  51. private final DMDircMBassador eventBus;
  52. private final ConfigBinder configBinder;
  53. private String username;
  54. private String password;
  55. private boolean whois;
  56. private boolean autoInvite;
  57. private boolean acceptInvites;
  58. @Inject
  59. public QAuthManager(
  60. @PluginDomain(QAuthPlugin.class) final String domain,
  61. @PluginDomain(QAuthPlugin.class) final PluginInfo pluginInfo,
  62. @GlobalConfig final AggregateConfigProvider config,
  63. final DMDircMBassador eventBus) {
  64. this.domain = domain;
  65. this.pluginInfo = pluginInfo;
  66. this.eventBus = eventBus;
  67. configBinder = config.getBinder().withDefaultDomain(domain);
  68. }
  69. public void load() {
  70. configBinder.bind(this, QAuthManager.class);
  71. eventBus.subscribe(this);
  72. }
  73. public void unload() {
  74. configBinder.unbind(this);
  75. eventBus.unsubscribe(this);
  76. }
  77. private boolean isValidConnection(final Connection connection) {
  78. return "Quakenet".equalsIgnoreCase(connection.getNetwork());
  79. }
  80. private boolean isValidUser(final User user) {
  81. // TODO: Check hostname?
  82. return "Q".equalsIgnoreCase(user.getNickname());
  83. }
  84. private void acceptInvite(final Invite invite) {
  85. invite.accept();
  86. }
  87. private void requestInvites(final Connection connection) {
  88. connection.sendMessage("Q@Cserve.quakenet.org", "invite");
  89. }
  90. private void auth(final Connection connection) {
  91. connection.sendMessage("Q@Cserve.quakenet.org", "auth " + username + ' ' + password);
  92. }
  93. @Handler
  94. void handleConnect(final ServerConnectedEvent event) {
  95. if (!isValidConnection(event.getConnection())) {
  96. return;
  97. }
  98. // TODO: whois
  99. auth(event.getConnection());
  100. }
  101. @Handler
  102. void handleInvite(final ServerInviteReceivedEvent event) {
  103. if (!acceptInvites) {
  104. return;
  105. }
  106. if (isValidConnection(event.getConnection()) && isValidUser(event.getUser())) {
  107. acceptInvite(event.getInvite());
  108. }
  109. }
  110. @Handler
  111. void handleNotices(final ServerNoticeEvent event) {
  112. handleCommunication(Optional.of(event.getConnection()), event.getUser(), event.getMessage());
  113. }
  114. @Handler
  115. void handleMessages(final QueryMessageEvent event) {
  116. handleCommunication(event.getQuery().getConnection(), event.getUser(), event.getMessage());
  117. }
  118. private void handleCommunication(final Optional<Connection> connection, final User user,
  119. final String message) {
  120. connection.ifPresent(c -> {
  121. if (isValidConnection(c) && isValidUser(user) &&
  122. ("You are now logged in as " + username + '.').equalsIgnoreCase(message)) {
  123. if (autoInvite) {
  124. requestInvites(c);
  125. }
  126. }
  127. });
  128. }
  129. @ConfigBinding(key = "username")
  130. void handleUsername(final String value) {
  131. username = value;
  132. }
  133. @ConfigBinding(key = "password")
  134. void handlePassword(final String value) {
  135. password = value;
  136. }
  137. @ConfigBinding(key = "whois")
  138. void handleWhois(final boolean value) {
  139. whois = value;
  140. }
  141. @ConfigBinding(key = "autoinvite")
  142. void handleAutoInvite(final boolean value) {
  143. autoInvite = value;
  144. }
  145. @ConfigBinding(key = "acceptinvites")
  146. void handleAcceptInvites(final boolean value) {
  147. acceptInvites = value;
  148. }
  149. @Handler
  150. void showConfig(final ClientPrefsOpenedEvent event) {
  151. final PluginPreferencesCategory category = new PluginPreferencesCategory(pluginInfo,
  152. "Q Auth", "Q Authentication settings");
  153. category.addSetting(new PreferencesSetting(PreferencesType.TEXT, new NotEmptyValidator(),
  154. domain, "username", "Username", "Your Q username",
  155. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  156. category.addSetting(new PreferencesSetting(PreferencesType.TEXT, new NotEmptyValidator(),
  157. domain, "password", "Password", "Your Q password",
  158. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  159. category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  160. domain, "whois", "Whois before auth",
  161. "Should we send a whois before authing and only auth if required?",
  162. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  163. category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  164. domain, "autoinvite", "Auto Invite", "Should Q autoinvite you to channels?",
  165. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  166. category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  167. domain, "acceptinvites", "Auto Accept Invites",
  168. "Should the client automatically accept invites from Q?",
  169. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  170. event.getModel().getCategory("Plugins").addSubCategory(category);
  171. }
  172. }