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ů.

GroupChatManagerImpl.java 7.5KB

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