Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Channel.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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.Styliser;
  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. Styliser.stipControlCodes(newChannelInfo.getName()),
  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. String temp = Styliser.stipControlCodes(channelInfo.getName());
  197. if (!channelInfo.getTopic().isEmpty()) {
  198. temp += " - " + Styliser.stipControlCodes(channelInfo.getTopic());
  199. }
  200. setTitle(temp);
  201. }
  202. @Override
  203. public void join() {
  204. connection.getParser().get().joinChannel(channelInfo.getName());
  205. }
  206. @Override
  207. public void part(final String reason) {
  208. channelInfo.part(reason);
  209. resetWindow(PartReason.LOCAL_PART);
  210. }
  211. @Override
  212. public void retrieveListModes() {
  213. channelInfo.requestListModes();
  214. }
  215. /**
  216. * Resets the window state after the client has left a channel.
  217. */
  218. public void resetWindow(final PartReason reason) {
  219. if (isOnChannel) {
  220. partReason = reason;
  221. isOnChannel = false;
  222. }
  223. setIcon("channel-inactive");
  224. // Needs to be published synchronously so that nicklists are cleared before the parser
  225. // is disconnected (which happens synchronously after this method returns).
  226. getEventBus().publish(
  227. new NickListClientsChangedEvent(this, Collections.emptyList()));
  228. }
  229. @Override
  230. public void close() {
  231. super.close();
  232. // Remove any callbacks or listeners
  233. eventHandler.unregisterCallbacks();
  234. connection.getParser().map(Parser::getCallbackManager)
  235. .ifPresent(cm -> cm.unsubscribe(eventHandler));
  236. // Trigger any actions neccessary
  237. if (isOnChannel && connection.getState() != ServerState.CLOSING) {
  238. part(getConfigManager().getOption("general", "partmessage"));
  239. }
  240. // Trigger action for the window closing
  241. getEventBus().publish(new ChannelClosedEvent(this));
  242. }
  243. /**
  244. * Adds a ChannelClient to this Channel.
  245. *
  246. * @param client The client to be added
  247. */
  248. public void addClient(final GroupChatUser client) {
  249. getEventBus().publishAsync(new NickListClientAddedEvent(this, client));
  250. getInputModel().get().getTabCompleter().addEntry(
  251. TabCompletionType.CHANNEL_NICK, client.getNickname());
  252. }
  253. /**
  254. * Removes the specified ChannelClient from this channel.
  255. *
  256. * @param client The client to be removed
  257. */
  258. public void removeClient(final GroupChatUser client) {
  259. getEventBus().publishAsync(new NickListClientRemovedEvent(this, client));
  260. getInputModel().get().getTabCompleter().removeEntry(
  261. TabCompletionType.CHANNEL_NICK, client.getNickname());
  262. if (client.getUser().equals(connection.getLocalUser().orElse(null))) {
  263. resetWindow(PartReason.REMOTE_PART);
  264. }
  265. }
  266. /**
  267. * Replaces the list of known clients on this channel with the specified one.
  268. *
  269. * @param clients The list of clients to use
  270. */
  271. public void setClients(final Collection<GroupChatUser> clients) {
  272. getEventBus().publishAsync(new NickListClientsChangedEvent(this, clients));
  273. getInputModel().get().getTabCompleter().clear(TabCompletionType.CHANNEL_NICK);
  274. getInputModel().get().getTabCompleter().addEntries(
  275. TabCompletionType.CHANNEL_NICK,
  276. clients.stream().map(GroupChatUser::getNickname).collect(Collectors.toList()));
  277. }
  278. /**
  279. * Renames a client that is in this channel.
  280. *
  281. * @param oldName The old nickname of the client
  282. * @param newName The new nickname of the client
  283. */
  284. public void renameClient(final String oldName, final String newName) {
  285. getInputModel().get().getTabCompleter().removeEntry(
  286. TabCompletionType.CHANNEL_NICK, oldName);
  287. getInputModel().get().getTabCompleter().addEntry(
  288. TabCompletionType.CHANNEL_NICK, newName);
  289. refreshClients();
  290. }
  291. @Override
  292. public void refreshClients() {
  293. if (!isOnChannel) {
  294. return;
  295. }
  296. getEventBus().publishAsync(new NickListUpdatedEvent(this));
  297. }
  298. // ---------------------------------------------------- TOPIC HANDLING -----
  299. /**
  300. * Adds the specified topic to this channel's topic list.
  301. *
  302. * @param topic The topic to be added.
  303. */
  304. public void addTopic(final Topic topic) {
  305. synchronized (topics) {
  306. topics.add(topic);
  307. }
  308. updateTitle();
  309. }
  310. @Override
  311. public List<Topic> getTopics() {
  312. synchronized (topics) {
  313. return new ArrayList<>(topics);
  314. }
  315. }
  316. @Override
  317. public Optional<Topic> getCurrentTopic() {
  318. synchronized (topics) {
  319. if (topics.isEmpty()) {
  320. return Optional.empty();
  321. } else {
  322. return Optional.of(getTopics().get(topics.size() - 1));
  323. }
  324. }
  325. }
  326. // ------------------------------------------ PARSER METHOD DELEGATION -----
  327. @Override
  328. public void setTopic(final String topic) {
  329. channelInfo.setTopic(topic);
  330. }
  331. @Override
  332. public int getMaxTopicLength() {
  333. return connection.getParser().get().getMaxTopicLength();
  334. }
  335. @Override
  336. public Optional<Connection> getConnection() {
  337. return Optional.of(connection);
  338. }
  339. @Override
  340. public Optional<GroupChatUser> getUser(final User user) {
  341. final ChannelClientInfo ci = channelInfo.getChannelClient(((Client) user).getClientInfo());
  342. if (ci == null) {
  343. return Optional.empty();
  344. }
  345. return Optional.of(groupChatUserManager.getUserFromClient(ci, user, this));
  346. }
  347. @Override
  348. public Collection<GroupChatUser> getUsers() {
  349. return channelInfo.getChannelClients().stream()
  350. .map(client -> groupChatUserManager.getUserFromClient(client, this))
  351. .collect(Collectors.toList());
  352. }
  353. @Override
  354. public WindowModel getWindowModel() {
  355. return this;
  356. }
  357. @Override
  358. public void kick(final GroupChatUser user, final Optional<String> reason) {
  359. ((ChannelClient) user).getClientInfo().kick(
  360. reason.orElse(getConfigManager().getOption("general", "kickmessage")));
  361. }
  362. @Override
  363. public Collection<ChannelListModeItem> getListModeItems(final char mode) {
  364. return channelInfo.getListMode(mode);
  365. }
  366. @Override
  367. public void setMode(final char mode, @Nullable final String value) {
  368. channelInfo.alterMode(true, mode, value);
  369. }
  370. @Override
  371. public void removeMode(final char mode, final String value) {
  372. channelInfo.alterMode(false, mode, value);
  373. }
  374. @Override
  375. public void flushModes() {
  376. channelInfo.flushModes();
  377. }
  378. @Override
  379. public String getModes() {
  380. return channelInfo.getModes();
  381. }
  382. @Override
  383. public String getModeValue(final char mode) {
  384. return channelInfo.getMode(mode);
  385. }
  386. @Override
  387. public void requestUsersInfo() {
  388. channelInfo.sendWho();
  389. }
  390. }