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

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