Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ConnectionHandler.java 5.0KB

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