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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.ServerNumericEvent;
  37. import com.dmdirc.interfaces.Connection;
  38. import com.dmdirc.interfaces.User;
  39. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  40. import com.dmdirc.plugins.PluginDomain;
  41. import com.dmdirc.plugins.PluginInfo;
  42. import com.dmdirc.util.validators.NotEmptyValidator;
  43. import java.util.Optional;
  44. import javax.inject.Inject;
  45. import net.engio.mbassy.listener.Handler;
  46. /**
  47. * Provides Q AUth support in DMDirc.
  48. */
  49. public class QAuthManager {
  50. private final String domain;
  51. private final PluginInfo pluginInfo;
  52. private final DMDircMBassador eventBus;
  53. private final ConfigBinder configBinder;
  54. private String username;
  55. private String password;
  56. private boolean whois;
  57. private boolean autoInvite;
  58. private boolean acceptInvites;
  59. private boolean waitingWhois;
  60. private boolean authed;
  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. authed = false;
  101. });
  102. }
  103. @Handler
  104. void handleConnect(final ServerConnectedEvent event) {
  105. if (!isValidConnection(event.getConnection())) {
  106. return;
  107. }
  108. if (whois) {
  109. sendWhois(event.getConnection());
  110. } else {
  111. auth(event.getConnection());
  112. }
  113. }
  114. @Handler
  115. void handleServerNumericEvent(final ServerNumericEvent event) {
  116. if (!waitingWhois) {
  117. return;
  118. }
  119. final String localNickname = event.getConnection().getLocalUser()
  120. .map(User::getNickname).orElse("");
  121. if(event.getNumeric() == 330 && localNickname.equalsIgnoreCase(event.getArgs()[3])) {
  122. // TODO: Check account matches? (param arg 4)
  123. authed = true;
  124. }
  125. if (event.getNumeric() == 318 && localNickname.equalsIgnoreCase(event.getArgs()[3])) {
  126. waitingWhois = false;
  127. if (!authed) {
  128. auth(event.getConnection());
  129. }
  130. }
  131. }
  132. @Handler
  133. void handleInvite(final ServerInviteReceivedEvent event) {
  134. if (!acceptInvites) {
  135. return;
  136. }
  137. if (isValidConnection(event.getConnection()) && isValidUser(event.getUser())) {
  138. acceptInvite(event.getInvite());
  139. }
  140. }
  141. @Handler
  142. void handleNotices(final ServerNoticeEvent event) {
  143. handleCommunication(Optional.of(event.getConnection()), event.getUser(), event.getMessage());
  144. }
  145. @Handler
  146. void handleMessages(final QueryMessageEvent event) {
  147. handleCommunication(event.getQuery().getConnection(), event.getUser(), event.getMessage());
  148. }
  149. private void handleCommunication(final Optional<Connection> connection, final User user,
  150. final String message) {
  151. connection.ifPresent(c -> {
  152. if (isValidConnection(c) && isValidUser(user) &&
  153. ("You are now logged in as " + username + '.').equalsIgnoreCase(message)) {
  154. if (autoInvite) {
  155. requestInvites(c);
  156. }
  157. }
  158. });
  159. }
  160. @ConfigBinding(key = "username")
  161. void handleUsername(final String value) {
  162. username = value;
  163. }
  164. @ConfigBinding(key = "password")
  165. void handlePassword(final String value) {
  166. password = value;
  167. }
  168. @ConfigBinding(key = "whois")
  169. void handleWhois(final boolean value) {
  170. whois = value;
  171. }
  172. @ConfigBinding(key = "autoinvite")
  173. void handleAutoInvite(final boolean value) {
  174. autoInvite = value;
  175. }
  176. @ConfigBinding(key = "acceptinvites")
  177. void handleAcceptInvites(final boolean value) {
  178. acceptInvites = value;
  179. }
  180. @Handler
  181. void showConfig(final ClientPrefsOpenedEvent event) {
  182. final PluginPreferencesCategory category = new PluginPreferencesCategory(pluginInfo,
  183. "Q Auth", "Q Authentication settings");
  184. category.addSetting(new PreferencesSetting(PreferencesType.TEXT, new NotEmptyValidator(),
  185. domain, "username", "Username", "Your Q username",
  186. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  187. category.addSetting(new PreferencesSetting(PreferencesType.TEXT, new NotEmptyValidator(),
  188. domain, "password", "Password", "Your Q password",
  189. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  190. category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  191. domain, "whois", "Whois before auth",
  192. "Should we send a whois before authing and only auth if required?",
  193. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  194. category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  195. domain, "autoinvite", "Auto Invite", "Should Q autoinvite you to channels?",
  196. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  197. category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  198. domain, "acceptinvites", "Auto Accept Invites",
  199. "Should the client automatically accept invites from Q?",
  200. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  201. event.getModel().getCategory("Plugins").addSubCategory(category);
  202. }
  203. }