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.

JPQManager.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.jpq;
  18. import com.dmdirc.config.prefs.PreferencesSetting;
  19. import com.dmdirc.config.prefs.PreferencesType;
  20. import com.dmdirc.events.ChannelSelfJoinEvent;
  21. import com.dmdirc.events.ChannelSelfPartEvent;
  22. import com.dmdirc.events.GroupChatPrefsRequestedEvent;
  23. import com.dmdirc.events.ServerConnectedEvent;
  24. import com.dmdirc.events.ServerDisconnectedEvent;
  25. import com.dmdirc.interfaces.Connection;
  26. import com.dmdirc.interfaces.ConnectionManager;
  27. import com.dmdirc.events.eventbus.EventBus;
  28. import com.dmdirc.interfaces.GroupChat;
  29. import com.dmdirc.plugins.PluginDomain;
  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 the ability to hide joins, parts and quits from a {@link GroupChat}.
  37. */
  38. public class JPQManager {
  39. private final String domain;
  40. private final ConnectionManager connectionManager;
  41. private final GroupChatHandlerFactory groupChatHandlerFactory;
  42. private final EventBus eventBus;
  43. private final Map<GroupChat, GroupChatHandler> groupChatHandlers;
  44. @Inject
  45. public JPQManager(
  46. @PluginDomain(JPQPlugin.class) final String domain,
  47. final ConnectionManager connectionManager,
  48. final GroupChatHandlerFactory groupChatHandlerFactory,
  49. final EventBus eventBus) {
  50. this.domain = domain;
  51. this.connectionManager = connectionManager;
  52. this.groupChatHandlerFactory = groupChatHandlerFactory;
  53. this.eventBus = eventBus;
  54. groupChatHandlers = new HashMap<>();
  55. }
  56. /**
  57. * Unloads this manager, removing required listeners and bindings.
  58. */
  59. public void load() {
  60. connectionManager.getConnections().forEach(this::addGroupChatHandler);
  61. eventBus.subscribe(this);
  62. }
  63. /**
  64. * Loads this manager, adding required listeners and bindings.
  65. */
  66. public void unload() {
  67. connectionManager.getConnections().forEach(this::removeGroupChatHandler);
  68. eventBus.unsubscribe(this);
  69. }
  70. @VisibleForTesting
  71. @Handler
  72. void handleConnectionAdded(final ServerConnectedEvent event) {
  73. addGroupChatHandler(event.getConnection());
  74. }
  75. @VisibleForTesting
  76. @Handler
  77. void handleConnectionRemoved(final ServerDisconnectedEvent event) {
  78. removeGroupChatHandler(event.getConnection());
  79. }
  80. @VisibleForTesting
  81. @Handler
  82. void handleGroupChatAdded(final ChannelSelfJoinEvent event) {
  83. addGroupChatHandler(event.getChannel());
  84. }
  85. @VisibleForTesting
  86. @Handler
  87. void handleGroupChatRemoved(final ChannelSelfPartEvent event) {
  88. removeGroupChatHandler(event.getChannel());
  89. }
  90. @VisibleForTesting
  91. @Handler
  92. void handleGroupChatPrefs(final GroupChatPrefsRequestedEvent event) {
  93. event.getCategory().addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  94. domain, "hidejpq", "Hide J/P/Q", "Hides joins, parts and quits in a group chat.",
  95. event.getConfig(), event.getIdentity()));
  96. }
  97. private void addGroupChatHandler(final Connection connection) {
  98. connection.getGroupChatManager().getChannels().forEach(this::addGroupChatHandler);
  99. }
  100. private void addGroupChatHandler(final GroupChat groupChat) {
  101. groupChatHandlers.computeIfAbsent(groupChat, groupChatHandlerFactory::get);
  102. }
  103. private void removeGroupChatHandler(final Connection connection) {
  104. connection.getGroupChatManager().getChannels().forEach(this::removeGroupChatHandler);
  105. }
  106. private void removeGroupChatHandler(final GroupChat groupChat) {
  107. final GroupChatHandler groupChatHandler = groupChatHandlers.remove(groupChat);
  108. if (groupChatHandler != null) {
  109. groupChatHandler.unload();
  110. }
  111. }
  112. }