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.

WhoisOnQueryManager.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.whoisonquery;
  18. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  19. import com.dmdirc.config.prefs.PreferencesCategory;
  20. import com.dmdirc.config.prefs.PreferencesSetting;
  21. import com.dmdirc.config.prefs.PreferencesType;
  22. import com.dmdirc.events.ClientPrefsOpenedEvent;
  23. import com.dmdirc.events.ConnectionPrefsRequestedEvent;
  24. import com.dmdirc.events.QueryOpenedEvent;
  25. import com.dmdirc.events.eventbus.EventBus;
  26. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  27. import com.dmdirc.interfaces.config.ConfigProvider;
  28. import com.dmdirc.plugins.PluginDomain;
  29. import com.dmdirc.plugins.PluginInfo;
  30. import com.google.common.annotations.VisibleForTesting;
  31. import javax.inject.Inject;
  32. import net.engio.mbassy.listener.Handler;
  33. /**
  34. * Sends a whois when a new query is opened.
  35. */
  36. public class WhoisOnQueryManager {
  37. private final String domain;
  38. private final PluginInfo pluginInfo;
  39. private final EventBus eventBus;
  40. @Inject
  41. public WhoisOnQueryManager(@PluginDomain(WhoisOnQueryPlugin.class) final String domain,
  42. @PluginDomain(WhoisOnQueryPlugin.class) final PluginInfo pluginInfo,
  43. final EventBus eventBus) {
  44. this.domain = domain;
  45. this.pluginInfo = pluginInfo;
  46. this.eventBus = eventBus;
  47. }
  48. public void load() {
  49. eventBus.subscribe(this);
  50. }
  51. public void unload() {
  52. eventBus.unsubscribe(this);
  53. }
  54. @VisibleForTesting
  55. @Handler
  56. void handleQueryEvent(final QueryOpenedEvent event) {
  57. event.getQuery().getConnection().ifPresent(c -> {
  58. final boolean enable = c.getWindowModel().getConfigManager()
  59. .getOptionBool(domain, "whoisonquery");
  60. if (enable) {
  61. c.requestUserInfo(event.getQuery().getUser());
  62. }
  63. });
  64. }
  65. @VisibleForTesting
  66. @Handler
  67. void handlePrefsEvent(final ClientPrefsOpenedEvent event) {
  68. final PreferencesCategory category = new PluginPreferencesCategory(pluginInfo,
  69. "Whois on Query", "Whois on query");
  70. category.addSetting(getSetting(event.getModel().getConfigManager(),
  71. event.getModel().getIdentity()));
  72. event.getModel().getCategory("Plugins").addSubCategory(category);
  73. }
  74. @VisibleForTesting
  75. @Handler
  76. void handleConnectionPrefsEvent(final ConnectionPrefsRequestedEvent event) {
  77. event.getCategory().addSetting(getSetting(event.getConfig(), event.getIdentity()));
  78. }
  79. private PreferencesSetting getSetting(final AggregateConfigProvider config,
  80. final ConfigProvider configProvider) {
  81. return new PreferencesSetting(PreferencesType.BOOLEAN, domain, "whoisonquery",
  82. "Send whois on query", "Request user information when a query is opened?",
  83. config, configProvider);
  84. }
  85. }