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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.events.UserInfoResponseEvent;
  37. import com.dmdirc.interfaces.Connection;
  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 DMDircMBassador 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 DMDircMBassador 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. // TODO: Check account matches?
  121. if (!event.getInfo(UserInfoEvent.UserInfoType.ACCOUNT_NAME).isPresent()) {
  122. auth(event.getConnection());
  123. }
  124. waitingWhois = false;
  125. }
  126. });
  127. }
  128. @Handler
  129. void handleInvite(final ServerInviteReceivedEvent event) {
  130. if (!acceptInvites) {
  131. return;
  132. }
  133. if (isValidConnection(event.getConnection()) && isValidUser(event.getUser())) {
  134. acceptInvite(event.getInvite());
  135. }
  136. }
  137. @Handler
  138. void handleNotices(final ServerNoticeEvent event) {
  139. handleCommunication(Optional.of(event.getConnection()), event.getUser(), event.getMessage());
  140. }
  141. @Handler
  142. void handleMessages(final QueryMessageEvent event) {
  143. handleCommunication(event.getQuery().getConnection(), event.getUser(), event.getMessage());
  144. }
  145. private void handleCommunication(final Optional<Connection> connection, final User user,
  146. final String message) {
  147. connection.ifPresent(c -> {
  148. if (isValidConnection(c) && isValidUser(user) &&
  149. ("You are now logged in as " + username + '.').equalsIgnoreCase(message)) {
  150. if (autoInvite) {
  151. requestInvites(c);
  152. }
  153. }
  154. });
  155. }
  156. @ConfigBinding(key = "username")
  157. void handleUsername(final String value) {
  158. username = value;
  159. }
  160. @ConfigBinding(key = "password")
  161. void handlePassword(final String value) {
  162. password = value;
  163. }
  164. @ConfigBinding(key = "whois")
  165. void handleWhois(final boolean value) {
  166. whois = value;
  167. }
  168. @ConfigBinding(key = "autoinvite")
  169. void handleAutoInvite(final boolean value) {
  170. autoInvite = value;
  171. }
  172. @ConfigBinding(key = "acceptinvites")
  173. void handleAcceptInvites(final boolean value) {
  174. acceptInvites = value;
  175. }
  176. @Handler
  177. void showConfig(final ClientPrefsOpenedEvent event) {
  178. final PluginPreferencesCategory category = new PluginPreferencesCategory(pluginInfo,
  179. "Q Auth", "Q Authentication settings");
  180. category.addSetting(new PreferencesSetting(PreferencesType.TEXT, new NotEmptyValidator(),
  181. domain, "username", "Username", "Your Q username",
  182. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  183. category.addSetting(new PreferencesSetting(PreferencesType.TEXT, new NotEmptyValidator(),
  184. domain, "password", "Password", "Your Q password",
  185. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  186. category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  187. domain, "whois", "Whois before auth",
  188. "Should we send a whois before authing and only auth if required?",
  189. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  190. category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  191. domain, "autoinvite", "Auto Invite", "Should Q autoinvite you to channels?",
  192. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  193. category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  194. domain, "acceptinvites", "Auto Accept Invites",
  195. "Should the client automatically accept invites from Q?",
  196. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  197. event.getModel().getCategory("Plugins").addSubCategory(category);
  198. }
  199. }