選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Channel.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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.events.ChannelSelfActionEvent;
  25. import com.dmdirc.events.ChannelSelfJoinEvent;
  26. import com.dmdirc.events.ChannelSelfMessageEvent;
  27. import com.dmdirc.events.CommandErrorEvent;
  28. import com.dmdirc.events.NickListClientAddedEvent;
  29. import com.dmdirc.events.NickListClientRemovedEvent;
  30. import com.dmdirc.events.NickListClientsChangedEvent;
  31. import com.dmdirc.events.NickListUpdatedEvent;
  32. import com.dmdirc.interfaces.Connection;
  33. import com.dmdirc.interfaces.GroupChat;
  34. import com.dmdirc.interfaces.GroupChatUser;
  35. import com.dmdirc.interfaces.User;
  36. import com.dmdirc.interfaces.WindowModel;
  37. import com.dmdirc.interfaces.config.ConfigProviderMigrator;
  38. import com.dmdirc.parser.common.ChannelListModeItem;
  39. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  40. import com.dmdirc.parser.interfaces.ChannelInfo;
  41. import com.dmdirc.parser.interfaces.Parser;
  42. import com.dmdirc.ui.core.components.WindowComponent;
  43. import com.dmdirc.ui.input.TabCompletionType;
  44. import com.dmdirc.ui.messages.BackBufferFactory;
  45. import com.dmdirc.ui.messages.StyledMessageUtils;
  46. import com.google.common.collect.EvictingQueue;
  47. import java.util.ArrayList;
  48. import java.util.Arrays;
  49. import java.util.Collection;
  50. import java.util.Collections;
  51. import java.util.List;
  52. import java.util.Optional;
  53. import java.util.Queue;
  54. import java.util.stream.Collectors;
  55. import javax.annotation.Nullable;
  56. /**
  57. * The Channel class represents the client's view of the channel. It handles callbacks for channel
  58. * events from the parser, maintains the corresponding ChannelWindow, and handles user input for the
  59. * channel.
  60. */
  61. public class Channel extends FrameContainer implements GroupChat {
  62. /** The parser's pChannel class. */
  63. private ChannelInfo channelInfo;
  64. /** The connection this channel is on. */
  65. private final Connection connection;
  66. /** A list of previous topics we've seen. */
  67. private final Queue<Topic> topics;
  68. /** Our event handler. */
  69. private final ChannelEventHandler eventHandler;
  70. /** The migrator to use to migrate our config provider. */
  71. private final ConfigProviderMigrator configMigrator;
  72. /** Manager used to retrieve {@link GroupChatUser}s */
  73. private final GroupChatUserManager groupChatUserManager;
  74. /** Whether we're currently in this channel or not. */
  75. private boolean isOnChannel;
  76. /** Reason for leaving the channel. */
  77. private PartReason partReason;
  78. /**
  79. * Creates a new instance of Channel.
  80. *
  81. * @param connection The connection object that this channel belongs to
  82. * @param newChannelInfo The parser's channel object that corresponds to this channel
  83. * @param configMigrator The config migrator which provides the config for this channel.
  84. */
  85. public Channel(
  86. final Connection connection,
  87. final ChannelInfo newChannelInfo,
  88. final ConfigProviderMigrator configMigrator,
  89. final BackBufferFactory backBufferFactory,
  90. final GroupChatUserManager groupChatUserManager) {
  91. super("channel-inactive",
  92. newChannelInfo.getName(),
  93. new StyledMessageUtils().stripControlCodes(newChannelInfo.getName()), // TODO: Inject this
  94. configMigrator.getConfigProvider(),
  95. backBufferFactory,
  96. connection.getWindowModel().getEventBus(),
  97. Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
  98. WindowComponent.INPUTFIELD.getIdentifier(),
  99. WindowComponent.TOPICBAR.getIdentifier(),
  100. WindowComponent.USERLIST.getIdentifier()));
  101. this.configMigrator = configMigrator;
  102. this.channelInfo = newChannelInfo;
  103. this.connection = connection;
  104. this.groupChatUserManager = groupChatUserManager;
  105. topics = EvictingQueue.create(
  106. getConfigManager().getOptionInt("channel", "topichistorysize"));
  107. eventHandler = new ChannelEventHandler(this, getEventBus(), groupChatUserManager);
  108. initBackBuffer();
  109. registerCallbacks();
  110. updateTitle();
  111. }
  112. public ChannelInfo getChannelInfo() {
  113. return channelInfo;
  114. }
  115. @Override
  116. public boolean isOnChannel() {
  117. return isOnChannel;
  118. }
  119. @Override
  120. public PartReason getPartReason() {
  121. return partReason;
  122. }
  123. /**
  124. * Registers callbacks with the parser for this channel.
  125. */
  126. private void registerCallbacks() {
  127. eventHandler.registerCallbacks();
  128. configMigrator.migrate(connection.getProtocol(), connection.getIrcd(), connection.getNetwork(),
  129. connection.getAddress(), channelInfo.getName());
  130. }
  131. @Override
  132. public void sendLine(final String line) {
  133. if (connection.getState() != ServerState.CONNECTED
  134. || connection.getParser().get().getChannel(channelInfo.getName()) == null) {
  135. // We're not in the channel/connected to the server
  136. return;
  137. }
  138. final GroupChatUser me = getUser(connection.getLocalUser().get()).get();
  139. getInputModel().get()
  140. .splitLine(line)
  141. .stream()
  142. .filter(part -> !part.isEmpty())
  143. .forEach(part -> {
  144. getEventBus().publishAsync(new ChannelSelfMessageEvent(this, me, part));
  145. channelInfo.sendMessage(part);
  146. });
  147. }
  148. @Override
  149. public int getMaxLineLength() {
  150. return connection.getState() == ServerState.CONNECTED
  151. ? connection.getParser().get().getMaxLength("PRIVMSG", getChannelInfo().getName())
  152. : -1;
  153. }
  154. @Override
  155. public void sendAction(final String action) {
  156. if (connection.getState() != ServerState.CONNECTED
  157. || connection.getParser().get().getChannel(channelInfo.getName()) == null) {
  158. // We're not on the server/channel
  159. return;
  160. }
  161. if (connection.getParser().get().getMaxLength("PRIVMSG", getChannelInfo().getName())
  162. <= action.length()) {
  163. getEventBus().publishAsync(new CommandErrorEvent(this,
  164. "Warning: action too long to be sent"));
  165. } else {
  166. final GroupChatUser me = getUser(connection.getLocalUser().get()).get();
  167. getEventBus().publishAsync(new ChannelSelfActionEvent(this, me, action));
  168. channelInfo.sendAction(action);
  169. }
  170. }
  171. /**
  172. * Sets this object's ChannelInfo reference to the one supplied. This only needs to be done if
  173. * the channel window (and hence this channel object) has stayed open while the user has been
  174. * out of the channel.
  175. *
  176. * @param newChannelInfo The new ChannelInfo object
  177. */
  178. public void setChannelInfo(final ChannelInfo newChannelInfo) {
  179. channelInfo = newChannelInfo;
  180. registerCallbacks();
  181. }
  182. /**
  183. * Called when we join this channel. Just needs to output a message.
  184. */
  185. public void selfJoin() {
  186. isOnChannel = true;
  187. final User me = connection.getLocalUser().get();
  188. getEventBus().publishAsync(new ChannelSelfJoinEvent(this, me));
  189. setIcon("channel");
  190. connection.getInviteManager().removeInvites(channelInfo.getName());
  191. }
  192. /**
  193. * Updates the title of the channel window, and of the main window if appropriate.
  194. */
  195. private void updateTitle() {
  196. final StyledMessageUtils styleUtils = new StyledMessageUtils();
  197. String temp = styleUtils.stripControlCodes(channelInfo.getName());
  198. if (!channelInfo.getTopic().isEmpty()) {
  199. temp += " - " + styleUtils.stripControlCodes(channelInfo.getTopic());
  200. }
  201. setTitle(temp);
  202. }
  203. @Override
  204. public void join() {
  205. connection.getParser().get().joinChannel(channelInfo.getName());
  206. }
  207. @Override
  208. public void part(final String reason) {
  209. channelInfo.part(reason);
  210. resetWindow(PartReason.LOCAL_PART);
  211. }
  212. @Override
  213. public void retrieveListModes() {
  214. channelInfo.requestListModes();
  215. }
  216. /**
  217. * Resets the window state after the client has left a channel.
  218. */
  219. public void resetWindow(final PartReason reason) {
  220. if (isOnChannel) {
  221. partReason = reason;
  222. isOnChannel = false;
  223. }
  224. setIcon("channel-inactive");
  225. // Needs to be published synchronously so that nicklists are cleared before the parser
  226. // is disconnected (which happens synchronously after this method returns).
  227. getEventBus().publish(
  228. new NickListClientsChangedEvent(this, Collections.emptyList()));
  229. }
  230. @Override
  231. public void close() {
  232. super.close();
  233. // Remove any callbacks or listeners
  234. eventHandler.unregisterCallbacks();
  235. connection.getParser().map(Parser::getCallbackManager)
  236. .ifPresent(cm -> cm.unsubscribe(eventHandler));
  237. // Trigger any actions neccessary
  238. if (isOnChannel && connection.getState() != ServerState.CLOSING) {
  239. part(getConfigManager().getOption("general", "partmessage"));
  240. }
  241. // Trigger action for the window closing
  242. getEventBus().publish(new ChannelClosedEvent(this));
  243. }
  244. /**
  245. * Adds a ChannelClient to this Channel.
  246. *
  247. * @param client The client to be added
  248. */
  249. public void addClient(final GroupChatUser client) {
  250. getEventBus().publishAsync(new NickListClientAddedEvent(this, client));
  251. getInputModel().get().getTabCompleter().addEntry(
  252. TabCompletionType.CHANNEL_NICK, client.getNickname());
  253. }
  254. /**
  255. * Removes the specified ChannelClient from this channel.
  256. *
  257. * @param client The client to be removed
  258. */
  259. public void removeClient(final GroupChatUser client) {
  260. getEventBus().publishAsync(new NickListClientRemovedEvent(this, client));
  261. getInputModel().get().getTabCompleter().removeEntry(
  262. TabCompletionType.CHANNEL_NICK, client.getNickname());
  263. if (client.getUser().equals(connection.getLocalUser().orElse(null))) {
  264. resetWindow(PartReason.REMOTE_PART);
  265. }
  266. }
  267. /**
  268. * Replaces the list of known clients on this channel with the specified one.
  269. *
  270. * @param clients The list of clients to use
  271. */
  272. public void setClients(final Collection<GroupChatUser> clients) {
  273. getEventBus().publishAsync(new NickListClientsChangedEvent(this, clients));
  274. getInputModel().get().getTabCompleter().clear(TabCompletionType.CHANNEL_NICK);
  275. getInputModel().get().getTabCompleter().addEntries(
  276. TabCompletionType.CHANNEL_NICK,
  277. clients.stream().map(GroupChatUser::getNickname).collect(Collectors.toList()));
  278. }
  279. /**
  280. * Renames a client that is in this channel.
  281. *
  282. * @param oldName The old nickname of the client
  283. * @param newName The new nickname of the client
  284. */
  285. public void renameClient(final String oldName, final String newName) {
  286. getInputModel().get().getTabCompleter().removeEntry(
  287. TabCompletionType.CHANNEL_NICK, oldName);
  288. getInputModel().get().getTabCompleter().addEntry(
  289. TabCompletionType.CHANNEL_NICK, newName);
  290. refreshClients();
  291. }
  292. @Override
  293. public void refreshClients() {
  294. if (!isOnChannel) {
  295. return;
  296. }
  297. getEventBus().publishAsync(new NickListUpdatedEvent(this));
  298. }
  299. // ---------------------------------------------------- TOPIC HANDLING -----
  300. /**
  301. * Adds the specified topic to this channel's topic list.
  302. *
  303. * @param topic The topic to be added.
  304. */
  305. public void addTopic(final Topic topic) {
  306. synchronized (topics) {
  307. topics.add(topic);
  308. }
  309. updateTitle();
  310. }
  311. @Override
  312. public List<Topic> getTopics() {
  313. synchronized (topics) {
  314. return new ArrayList<>(topics);
  315. }
  316. }
  317. @Override
  318. public Optional<Topic> getCurrentTopic() {
  319. synchronized (topics) {
  320. if (topics.isEmpty()) {
  321. return Optional.empty();
  322. } else {
  323. return Optional.of(getTopics().get(topics.size() - 1));
  324. }
  325. }
  326. }
  327. // ------------------------------------------ PARSER METHOD DELEGATION -----
  328. @Override
  329. public void setTopic(final String topic) {
  330. channelInfo.setTopic(topic);
  331. }
  332. @Override
  333. public int getMaxTopicLength() {
  334. return connection.getParser().get().getMaxTopicLength();
  335. }
  336. @Override
  337. public Optional<Connection> getConnection() {
  338. return Optional.of(connection);
  339. }
  340. @Override
  341. public Optional<GroupChatUser> getUser(final User user) {
  342. final ChannelClientInfo ci = channelInfo.getChannelClient(((Client) user).getClientInfo());
  343. if (ci == null) {
  344. return Optional.empty();
  345. }
  346. return Optional.of(groupChatUserManager.getUserFromClient(ci, user, this));
  347. }
  348. @Override
  349. public Collection<GroupChatUser> getUsers() {
  350. return channelInfo.getChannelClients().stream()
  351. .map(client -> groupChatUserManager.getUserFromClient(client, this))
  352. .collect(Collectors.toList());
  353. }
  354. @Override
  355. public WindowModel getWindowModel() {
  356. return this;
  357. }
  358. @Override
  359. public void kick(final GroupChatUser user, final Optional<String> reason) {
  360. ((ChannelClient) user).getClientInfo().kick(
  361. reason.orElse(getConfigManager().getOption("general", "kickmessage")));
  362. }
  363. @Override
  364. public Collection<ChannelListModeItem> getListModeItems(final char mode) {
  365. return channelInfo.getListMode(mode);
  366. }
  367. @Override
  368. public void setMode(final char mode, @Nullable final String value) {
  369. channelInfo.alterMode(true, mode, value);
  370. }
  371. @Override
  372. public void removeMode(final char mode, final String value) {
  373. channelInfo.alterMode(false, mode, value);
  374. }
  375. @Override
  376. public void flushModes() {
  377. channelInfo.flushModes();
  378. }
  379. @Override
  380. public String getModes() {
  381. return channelInfo.getModes();
  382. }
  383. @Override
  384. public String getModeValue(final char mode) {
  385. return channelInfo.getMode(mode);
  386. }
  387. @Override
  388. public void requestUsersInfo() {
  389. channelInfo.sendWho();
  390. }
  391. }