您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ConnectionHandler.java 5.1KB

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.ConnectionManager;
  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 Connection connection;
  48. private final String domain;
  49. private final ScheduledExecutorService executorService;
  50. private final ConnectionManager connectionManager;
  51. private final ConfigBinder configBinder;
  52. private ScheduledFuture<?> future;
  53. public ConnectionHandler(
  54. final AggregateConfigProvider config,
  55. final ScheduledExecutorService executorService,
  56. final ConnectionManager connectionManager, final String domain,
  57. final Connection connection) {
  58. this.connection = connection;
  59. this.domain = domain;
  60. this.executorService = executorService;
  61. this.connectionManager = connectionManager;
  62. configBinder = config.getBinder().withDefaultDomain(domain);
  63. users = HashMultimap.create();
  64. }
  65. public void load() {
  66. configBinder.bind(this, ConnectionHandler.class);
  67. connection.getWindowModel().getEventBus().subscribe(this);
  68. }
  69. public void unload() {
  70. configBinder.unbind(this);
  71. executorService.shutdown();
  72. connection.getWindowModel().getEventBus().unsubscribe(this);
  73. if (future != null) {
  74. future.cancel(false);
  75. }
  76. }
  77. @VisibleForTesting
  78. void checkWho() {
  79. connection.getGroupChatManager().getChannels().forEach(channel -> {
  80. if (channel.getWindowModel().getConfigManager().getOptionBool(domain, "sendwho")) {
  81. channel.requestUsersInfo();
  82. }
  83. });
  84. }
  85. @VisibleForTesting
  86. @ConfigBinding(key="whointerval")
  87. void handleWhoInterval(final int value) {
  88. if (future != null) {
  89. future.cancel(false);
  90. }
  91. future = executorService.scheduleAtFixedRate(this::checkWho, value, value,
  92. TimeUnit.MILLISECONDS);
  93. }
  94. @VisibleForTesting
  95. @Handler
  96. void handleAwayEvent(final ChannelUserAwayEvent event) {
  97. if (event.getChannel().getConnection().equals(Optional.of(connection))
  98. && !event.getReason().isPresent()) {
  99. event.setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
  100. final boolean notseen = !users.containsKey(event.getUser().getNickname());
  101. users.put(event.getUser().getNickname(), event.getUser());
  102. if (notseen) {
  103. event.getChannel().getConnection()
  104. .ifPresent(c -> c.requestUserInfo(event.getUser().getUser()));
  105. }
  106. }
  107. }
  108. @VisibleForTesting
  109. @Handler
  110. void handleServerNumericEvent(final ServerNumericEvent event) {
  111. if (event.getConnection().equals(connection) && event.getNumeric() == 301) {
  112. final String nickname = event.getArgs()[3];
  113. final String reason = event.getArgs()[4];
  114. users.removeAll(nickname).forEach(u -> u.getGroupChat().getEventBus()
  115. .publishAsync(new ChannelUserAwayEvent(u.getGroupChat(), u,
  116. Optional.ofNullable(reason))));
  117. }
  118. }
  119. }