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 7.5KB

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