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.

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