Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

QAuthManager.java 8.1KB

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