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.

Channel.java 18KB

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