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.

ConnectionHandler.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.ConfigBinder;
  19. import com.dmdirc.config.ConfigBinding;
  20. import com.dmdirc.events.ChannelUserAwayEvent;
  21. import com.dmdirc.events.DisplayProperty;
  22. import com.dmdirc.events.ServerNumericEvent;
  23. import com.dmdirc.interfaces.Connection;
  24. import com.dmdirc.interfaces.EventBus;
  25. import com.dmdirc.interfaces.GroupChat;
  26. import com.dmdirc.interfaces.GroupChatUser;
  27. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  28. import com.google.common.annotations.VisibleForTesting;
  29. import com.google.common.collect.HashMultimap;
  30. import com.google.common.collect.Multimap;
  31. import java.util.Optional;
  32. import java.util.concurrent.ScheduledExecutorService;
  33. import java.util.concurrent.ScheduledFuture;
  34. import java.util.concurrent.TimeUnit;
  35. import net.engio.mbassy.listener.Handler;
  36. /**
  37. * Responsible for managing timers and settings required to who any {@link GroupChat}s on a
  38. * {@link Connection} as specified by the user.
  39. */
  40. public class ConnectionHandler {
  41. private final Multimap<String, GroupChatUser> users;
  42. private final EventBus eventBus;
  43. private final Connection connection;
  44. private final String domain;
  45. private final ScheduledExecutorService executorService;
  46. private final ConfigBinder configBinder;
  47. private ScheduledFuture<?> future;
  48. public ConnectionHandler(
  49. final AggregateConfigProvider config,
  50. final ScheduledExecutorService executorService,
  51. final EventBus eventBus,
  52. final String domain,
  53. final Connection connection) {
  54. this.eventBus = eventBus;
  55. this.connection = connection;
  56. this.domain = domain;
  57. this.executorService = executorService;
  58. configBinder = config.getBinder().withDefaultDomain(domain);
  59. users = HashMultimap.create();
  60. }
  61. public void load() {
  62. configBinder.bind(this, ConnectionHandler.class);
  63. connection.getWindowModel().getEventBus().subscribe(this);
  64. }
  65. public void unload() {
  66. configBinder.unbind(this);
  67. executorService.shutdown();
  68. connection.getWindowModel().getEventBus().unsubscribe(this);
  69. if (future != null) {
  70. future.cancel(false);
  71. }
  72. }
  73. @VisibleForTesting
  74. void checkWho() {
  75. connection.getGroupChatManager().getChannels().forEach(channel -> {
  76. if (channel.getWindowModel().getConfigManager().getOptionBool(domain, "sendwho")) {
  77. channel.requestUsersInfo();
  78. }
  79. });
  80. }
  81. @VisibleForTesting
  82. @ConfigBinding(key="whointerval")
  83. void handleWhoInterval(final int value) {
  84. if (future != null) {
  85. future.cancel(false);
  86. }
  87. future = executorService.scheduleAtFixedRate(this::checkWho, value, value,
  88. TimeUnit.MILLISECONDS);
  89. }
  90. @VisibleForTesting
  91. @Handler
  92. void handleAwayEvent(final ChannelUserAwayEvent event) {
  93. if (event.getChannel().getConnection().equals(Optional.of(connection))
  94. && !event.getReason().isPresent()) {
  95. event.setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
  96. final boolean notseen = !users.containsKey(event.getUser().getNickname());
  97. users.put(event.getUser().getNickname(), event.getUser());
  98. if (notseen) {
  99. event.getChannel().getConnection()
  100. .ifPresent(c -> c.requestUserInfo(event.getUser().getUser()));
  101. }
  102. }
  103. }
  104. @VisibleForTesting
  105. @Handler
  106. void handleServerNumericEvent(final ServerNumericEvent event) {
  107. if (event.getConnection().equals(connection) && event.getNumeric() == 301) {
  108. final String nickname = event.getArgs()[3];
  109. final String reason = event.getArgs()[4];
  110. users.removeAll(nickname).forEach(u -> eventBus.publishAsync(
  111. new ChannelUserAwayEvent(u.getGroupChat(), u, Optional.ofNullable(reason))));
  112. }
  113. }
  114. }