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.

ChannelWhoManager.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.channelwho;
  18. import com.dmdirc.config.prefs.PreferencesCategory;
  19. import com.dmdirc.config.prefs.PreferencesSetting;
  20. import com.dmdirc.config.prefs.PreferencesType;
  21. import com.dmdirc.events.ClientPrefsOpenedEvent;
  22. import com.dmdirc.events.GroupChatPrefsRequestedEvent;
  23. import com.dmdirc.events.ServerConnectingEvent;
  24. import com.dmdirc.events.ServerDisconnectedEvent;
  25. import com.dmdirc.interfaces.Connection;
  26. import com.dmdirc.interfaces.ConnectionManager;
  27. import com.dmdirc.interfaces.EventBus;
  28. import com.dmdirc.plugins.PluginDomain;
  29. import com.dmdirc.util.validators.NumericalValidator;
  30. import com.google.common.annotations.VisibleForTesting;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33. import javax.inject.Inject;
  34. import net.engio.mbassy.listener.Handler;
  35. /**
  36. * Provides channel who support in DMDirc.
  37. */
  38. public class ChannelWhoManager {
  39. private final String domain;
  40. private final ConnectionHandlerFactory connectionHandlerFactory;
  41. private final ConnectionManager connectionManager;
  42. private final EventBus eventBus;
  43. private final Map<Connection, ConnectionHandler> connectionHandlers;
  44. @Inject
  45. public ChannelWhoManager(
  46. @PluginDomain(ChannelWhoPlugin.class) final String domain,
  47. final ConnectionHandlerFactory connectionHandlerFactory,
  48. final ConnectionManager connectionManager,
  49. final EventBus eventBus) {
  50. this.domain = domain;
  51. this.connectionHandlerFactory = connectionHandlerFactory;
  52. this.connectionManager = connectionManager;
  53. this.eventBus = eventBus;
  54. connectionHandlers = new HashMap<>();
  55. }
  56. public void load() {
  57. eventBus.subscribe(this);
  58. connectionManager.getConnections().forEach(this::addConnectionHandler);
  59. }
  60. public void unload() {
  61. connectionManager.getConnections().forEach(this::removeConnectionHandler);
  62. eventBus.unsubscribe(this);
  63. }
  64. private void addConnectionHandler(final Connection connection) {
  65. connectionHandlers.computeIfAbsent(connection, connectionHandlerFactory::get);
  66. }
  67. private void removeConnectionHandler(final Connection connection) {
  68. final ConnectionHandler connectionHandler = connectionHandlers.remove(connection);
  69. if (connectionHandler != null) {
  70. connectionHandler.unload();
  71. }
  72. }
  73. @VisibleForTesting
  74. @Handler
  75. void handleGroupChatPrefsRequestedEvent(final GroupChatPrefsRequestedEvent event) {
  76. event.getCategory().addSetting(new PreferencesSetting(PreferencesType.BOOLEAN, domain,
  77. "sendwho", "Send Who Requests", "Should we send who requests to the channel?",
  78. event.getConfig(), event.getIdentity()));
  79. }
  80. @VisibleForTesting
  81. @Handler
  82. void handlePrefsDialog(final ClientPrefsOpenedEvent event) {
  83. final PreferencesCategory category = new PreferencesCategory("Channel Who", "Provides " +
  84. "support for sending WHO requests to channels at regular intervals");
  85. category.addSetting(new PreferencesSetting(PreferencesType.DURATION,
  86. new NumericalValidator(0, Integer.MAX_VALUE), domain, "whointerval",
  87. "Who Interval", "The interval WHO requests will be sent to channels",
  88. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  89. event.getModel().getCategory("Plugins").addSubCategory(category);
  90. }
  91. @VisibleForTesting
  92. @Handler
  93. void handleServerConnectingEvent(final ServerConnectingEvent event) {
  94. addConnectionHandler(event.getConnection());
  95. }
  96. @VisibleForTesting
  97. @Handler
  98. void handleServerDisconnectedEvent(final ServerDisconnectedEvent event) {
  99. removeConnectionHandler(event.getConnection());
  100. }
  101. }