Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

GroupChatManagerImpl.java 7.5KB

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