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.

QAuthManager.java 8.1KB

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