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.

GroupChatManagerImpl.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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;
  23. import com.dmdirc.events.ChannelClosedEvent;
  24. import com.dmdirc.interfaces.Connection;
  25. import com.dmdirc.interfaces.GroupChat;
  26. import com.dmdirc.interfaces.GroupChatManager;
  27. import com.dmdirc.interfaces.config.ConfigProviderMigrator;
  28. import com.dmdirc.interfaces.config.IdentityFactory;
  29. import com.dmdirc.parser.common.ChannelJoinRequest;
  30. import com.dmdirc.parser.interfaces.ChannelInfo;
  31. import com.dmdirc.parser.interfaces.Parser;
  32. import com.dmdirc.ui.input.TabCompletionType;
  33. import java.util.ArrayList;
  34. import java.util.Collection;
  35. import java.util.Date;
  36. import java.util.HashSet;
  37. import java.util.List;
  38. import java.util.Optional;
  39. import java.util.concurrent.ScheduledExecutorService;
  40. import java.util.concurrent.ScheduledFuture;
  41. import java.util.concurrent.TimeUnit;
  42. import java.util.stream.Collectors;
  43. import net.engio.mbassy.listener.Handler;
  44. /**
  45. * Manages group chats for a {@link Connection}.
  46. */
  47. public class GroupChatManagerImpl implements GroupChatManager {
  48. private final Connection connection;
  49. /** Factory to use for creating channels. */
  50. private final ChannelFactory channelFactory;
  51. /** Factory to use to create new identities. */
  52. private final IdentityFactory identityFactory;
  53. /** Open channels that currently exist on the server. */
  54. private final ChannelMap channels = new ChannelMap();
  55. /** A set of channels we want to join without focusing. */
  56. private final Collection<String> backgroundChannels = new HashSet<>();
  57. private final ScheduledExecutorService executorService;
  58. /** The future used when a who timer is scheduled. */
  59. private ScheduledFuture<?> whoTimerFuture;
  60. public GroupChatManagerImpl(final Connection connection,
  61. final IdentityFactory identityFactory,
  62. final ChannelFactory channelFactory,
  63. final ScheduledExecutorService executorService) {
  64. this.connection = connection;
  65. this.identityFactory = identityFactory;
  66. this.channelFactory = channelFactory;
  67. this.executorService = executorService;
  68. }
  69. @Override
  70. public Optional<GroupChat> getChannel(final String channel) {
  71. return channels.get(channel).map(c -> (GroupChat) c);
  72. }
  73. @Override
  74. public String getChannelPrefixes() {
  75. return connection.getParser().map(Parser::getChannelPrefixes).orElse("#%");
  76. }
  77. @Override
  78. public Collection<GroupChat> getChannels() {
  79. return channels.getAll().parallelStream()
  80. .map(c -> (GroupChat) c)
  81. .collect(Collectors.toList());
  82. }
  83. @Override
  84. public boolean isValidChannelName(final String channelName) {
  85. return getChannel(channelName).isPresent()
  86. || connection.getParser().map(p -> p.isValidChannelName(channelName)).orElse(false);
  87. }
  88. @Override
  89. public void join(final ChannelJoinRequest... requests) {
  90. join(true, requests);
  91. }
  92. @Override
  93. public void join(final boolean focus, final ChannelJoinRequest... requests) {
  94. final Optional<Parser> parser = connection.getParser();
  95. parser.ifPresent(p -> {
  96. final Collection<ChannelJoinRequest> pending = new ArrayList<>();
  97. for (ChannelJoinRequest request : requests) {
  98. connection.getInviteManager().removeInvites(request.getName());
  99. final String name;
  100. if (p.isValidChannelName(request.getName())) {
  101. name = request.getName();
  102. } else {
  103. name = p.getChannelPrefixes().substring(0, 1) + request.getName();
  104. }
  105. if (!getChannel(name).map(GroupChat::isOnChannel).orElse(false)) {
  106. if (!focus) {
  107. backgroundChannels.add(name);
  108. }
  109. pending.add(request);
  110. }
  111. }
  112. p.joinChannels(pending.toArray(new ChannelJoinRequest[pending.size()]));
  113. });
  114. }
  115. public void addChannel(final ChannelInfo chan) {
  116. addChannel(chan,
  117. !backgroundChannels.contains(chan.getName())
  118. || connection.getWindowModel().getConfigManager()
  119. .getOptionBool("general", "hidechannels"));
  120. }
  121. public void addChannel(final ChannelInfo chan, final boolean focus) {
  122. if (connection.getState() == ServerState.CLOSING) {
  123. // Can't join channels while the server is closing
  124. return;
  125. }
  126. backgroundChannels.remove(chan.getName());
  127. final Optional<Channel> channel = channels.get(chan.getName());
  128. if (channel.isPresent()) {
  129. channel.get().setChannelInfo(chan);
  130. channel.get().selfJoin();
  131. } else {
  132. final ConfigProviderMigrator channelConfig = identityFactory.createMigratableConfig(
  133. connection.getProtocol(), connection.getIrcd(), connection.getNetwork(),
  134. connection.getAddress(), chan.getName());
  135. final Channel newChan = channelFactory.getChannel(
  136. connection, chan, channelConfig);
  137. connection.getWindowModel().getTabCompleter().addEntry(TabCompletionType.CHANNEL,
  138. chan.getName());
  139. newChan.getWindowModel().getEventBus().subscribe(this);
  140. channels.add(newChan);
  141. }
  142. }
  143. public void handleDisconnect() {
  144. channels.resetAll();
  145. backgroundChannels.clear();
  146. if (connection.getWindowModel().getConfigManager()
  147. .getOptionBool("general", "closechannelsonquit")) {
  148. channels.closeAll();
  149. }
  150. }
  151. public void closeAll() {
  152. channels.closeAll();
  153. }
  154. public void addLineToAll(final String messageType, final Date date, final Object... args) {
  155. channels.addLineToAll(messageType, date, args);
  156. }
  157. public void handleSocketClosed() {
  158. if (whoTimerFuture != null) {
  159. whoTimerFuture.cancel(false);
  160. }
  161. channels.resetAll();
  162. if (connection.getWindowModel().getConfigManager()
  163. .getOptionBool("general", "closechannelsondisconnect")) {
  164. channels.closeAll();
  165. }
  166. }
  167. public void handleConnected() {
  168. channels.setStringConverter(connection.getParser().get().getStringConverter());
  169. final List<ChannelJoinRequest> requests = new ArrayList<>();
  170. if (connection.getWindowModel().getConfigManager()
  171. .getOptionBool("general", "rejoinchannels")) {
  172. requests.addAll(channels.asJoinRequests());
  173. }
  174. join(requests.toArray(new ChannelJoinRequest[requests.size()]));
  175. final int whoTime = connection.getWindowModel().getConfigManager()
  176. .getOptionInt("general", "whotime");
  177. whoTimerFuture = executorService.scheduleAtFixedRate(
  178. channels.getWhoRunnable(), whoTime, whoTime, TimeUnit.MILLISECONDS);
  179. }
  180. @Handler
  181. void handleChannelClosing(final ChannelClosedEvent event) {
  182. final GroupChat channel = event.getChannel();
  183. connection.getWindowModel().getTabCompleter()
  184. .removeEntry(TabCompletionType.CHANNEL, channel.getName());
  185. channels.remove(channel.getName());
  186. channel.getEventBus().unsubscribe(this);
  187. }
  188. }