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

Channel.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.actions.ActionManager;
  24. import com.dmdirc.actions.CoreActionType;
  25. import com.dmdirc.commandparser.CommandManager;
  26. import com.dmdirc.commandparser.CommandType;
  27. import com.dmdirc.config.ConfigManager;
  28. import com.dmdirc.interfaces.ConfigChangeListener;
  29. import com.dmdirc.parser.interfaces.ChannelInfo;
  30. import com.dmdirc.parser.interfaces.ClientInfo;
  31. import com.dmdirc.parser.irc.ChannelClientInfo;
  32. import com.dmdirc.ui.WindowManager;
  33. import com.dmdirc.ui.input.TabCompleter;
  34. import com.dmdirc.ui.input.TabCompletionType;
  35. import com.dmdirc.ui.interfaces.ChannelWindow;
  36. import com.dmdirc.ui.interfaces.InputWindow;
  37. import com.dmdirc.ui.messages.ColourManager;
  38. import com.dmdirc.ui.messages.Styliser;
  39. import com.dmdirc.util.RollingList;
  40. import java.awt.Color;
  41. import java.io.Serializable;
  42. import java.util.ArrayList;
  43. import java.util.Arrays;
  44. import java.util.List;
  45. import java.util.Map;
  46. /**
  47. * The Channel class represents the client's view of the channel. It handles
  48. * callbacks for channel events from the parser, maintains the corresponding
  49. * ChannelWindow, and handles user input for the channel.
  50. *
  51. * @author chris
  52. */
  53. public class Channel extends MessageTarget implements ConfigChangeListener,
  54. Serializable {
  55. /**
  56. * A version number for this class. It should be changed whenever the class
  57. * structure is changed (or anything else that would prevent serialized
  58. * objects being unserialized with the new class).
  59. */
  60. private static final long serialVersionUID = 1;
  61. /** The parser's pChannel class. */
  62. private transient ChannelInfo channelInfo;
  63. /** The server this channel is on. */
  64. private Server server;
  65. /** The ChannelWindow used for this channel. */
  66. private ChannelWindow window;
  67. /** The tabcompleter used for this channel. */
  68. private final TabCompleter tabCompleter;
  69. /** A list of previous topics we've seen. */
  70. private final RollingList<Topic> topics;
  71. /** Our event handler. */
  72. private final ChannelEventHandler eventHandler;
  73. /** Whether we're in this channel or not. */
  74. private boolean onChannel;
  75. /** Whether we should send WHO requests for this channel. */
  76. private volatile boolean sendWho;
  77. /** Whether we should show mode prefixes in text. */
  78. private volatile boolean showModePrefix;
  79. /** Whether we should show colours in nicks. */
  80. private volatile boolean showColours;
  81. /**
  82. * Creates a new instance of Channel.
  83. *
  84. * @param newServer The server object that this channel belongs to
  85. * @param newChannelInfo The parser's channel object that corresponds to
  86. * this channel
  87. */
  88. public Channel(final Server newServer, final ChannelInfo newChannelInfo) {
  89. super("channel", newChannelInfo.getName(),
  90. new ConfigManager(newServer.getIrcd(), newServer.getNetwork(),
  91. newServer.getName(), newChannelInfo.getName()));
  92. channelInfo = newChannelInfo;
  93. server = newServer;
  94. getConfigManager().addChangeListener("channel", this);
  95. getConfigManager().addChangeListener("ui", "shownickcoloursintext", this);
  96. topics = new RollingList<Topic>(getConfigManager().getOptionInt("channel",
  97. "topichistorysize"));
  98. sendWho = getConfigManager().getOptionBool("channel", "sendwho");
  99. showModePrefix = getConfigManager().getOptionBool("channel", "showmodeprefix");
  100. showColours = getConfigManager().getOptionBool("ui", "shownickcoloursintext");
  101. tabCompleter = new TabCompleter(server.getTabCompleter());
  102. tabCompleter.addEntries(TabCompletionType.COMMAND,
  103. CommandManager.getCommandNames(CommandType.TYPE_CHANNEL));
  104. tabCompleter.addEntries(TabCompletionType.COMMAND,
  105. CommandManager.getCommandNames(CommandType.TYPE_CHAT));
  106. window = Main.getUI().getChannel(Channel.this);
  107. WindowManager.addWindow(server.getFrame(), window);
  108. window.getInputHandler().setTabCompleter(tabCompleter);
  109. eventHandler = new ChannelEventHandler(this);
  110. registerCallbacks();
  111. ActionManager.processEvent(CoreActionType.CHANNEL_OPENED, null, this);
  112. updateTitle();
  113. selfJoin();
  114. }
  115. /**
  116. * Registers callbacks with the parser for this channel.
  117. */
  118. private void registerCallbacks() {
  119. eventHandler.registerCallbacks();
  120. getConfigManager().migrate(server.getIrcd(), server.getNetwork(),
  121. server.getName(), channelInfo.getName());
  122. }
  123. /**
  124. * Shows this channel's window.
  125. */
  126. public void show() {
  127. window.open();
  128. }
  129. /** {@inheritDoc} */
  130. @Override
  131. public void sendLine(final String line) {
  132. if (server.getState() != ServerState.CONNECTED
  133. || server.getParser().getChannel(channelInfo.getName()) == null) {
  134. // We're not in the channel/connected to the server
  135. return;
  136. }
  137. final ClientInfo me = server.getParser().getLocalClient();
  138. final String[] details = getDetails(channelInfo.getUser(me), showColours);
  139. for (String part : splitLine(window.getTranscoder().encode(line))) {
  140. if (!part.isEmpty()) {
  141. final StringBuffer buff = new StringBuffer("channelSelfMessage");
  142. ActionManager.processEvent(CoreActionType.CHANNEL_SELF_MESSAGE, buff,
  143. this, channelInfo.getUser(me), part);
  144. addLine(buff, details[0], details[1], details[2], details[3],
  145. part, channelInfo);
  146. channelInfo.sendMessage(part);
  147. }
  148. }
  149. }
  150. /** {@inheritDoc} */
  151. @Override
  152. public int getMaxLineLength() {
  153. return server.getState() == ServerState.CONNECTED
  154. ? server.getParser().getMaxLength("PRIVMSG", getChannelInfo().getName())
  155. : -1;
  156. }
  157. /** {@inheritDoc} */
  158. @Override
  159. public void sendAction(final String action) {
  160. if (server.getState() != ServerState.CONNECTED
  161. || server.getParser().getChannel(channelInfo.getName()) == null) {
  162. // We're not on the server/channel
  163. return;
  164. }
  165. final ClientInfo me = server.getParser().getLocalClient();
  166. final String[] details = getDetails(channelInfo.getUser(me), showColours);
  167. if (server.getParser().getMaxLength("PRIVMSG", getChannelInfo().getName())
  168. <= action.length()) {
  169. addLine("actionTooLong", action.length());
  170. } else {
  171. final StringBuffer buff = new StringBuffer("channelSelfAction");
  172. ActionManager.processEvent(CoreActionType.CHANNEL_SELF_ACTION, buff,
  173. this, channelInfo.getUser(me), action);
  174. addLine(buff, details[0], details[1], details[2], details[3],
  175. window.getTranscoder().encode(action), channelInfo);
  176. channelInfo.sendAction(window.getTranscoder().encode(action));
  177. }
  178. }
  179. /**
  180. * Returns the server object that this channel belongs to.
  181. *
  182. * @return The server object
  183. */
  184. @Override
  185. public Server getServer() {
  186. return server;
  187. }
  188. /**
  189. * Returns the parser's ChannelInfo object that this object is associated
  190. * with.
  191. *
  192. * @return The ChannelInfo object associated with this object
  193. */
  194. public ChannelInfo getChannelInfo() {
  195. return channelInfo;
  196. }
  197. /**
  198. * Sets this object's ChannelInfo reference to the one supplied. This only
  199. * needs to be done if the channel window (and hence this channel object)
  200. * has stayed open while the user has been out of the channel.
  201. *
  202. * @param newChannelInfo The new ChannelInfo object
  203. */
  204. public void setChannelInfo(final ChannelInfo newChannelInfo) {
  205. channelInfo = newChannelInfo;
  206. registerCallbacks();
  207. }
  208. /**
  209. * Returns the internal window belonging to this object.
  210. *
  211. * @return This object's internal window
  212. */
  213. @Override
  214. public InputWindow getFrame() {
  215. return window;
  216. }
  217. /**
  218. * Returns the tab completer for this channel.
  219. *
  220. * @return This channel's tab completer
  221. */
  222. public TabCompleter getTabCompleter() {
  223. return tabCompleter;
  224. }
  225. /**
  226. * Called when we join this channel. Just needs to output a message.
  227. */
  228. public void selfJoin() {
  229. onChannel = true;
  230. final ClientInfo me = server.getParser().getLocalClient();
  231. addLine("channelSelfJoin", "", me.getNickname(), me.getIdent(),
  232. me.getHost(), channelInfo.getName());
  233. setIcon("channel");
  234. server.removeInvites(channelInfo.getName());
  235. }
  236. /**
  237. * Updates the title of the channel window, and of the main window if
  238. * appropriate.
  239. */
  240. private void updateTitle() {
  241. String temp = Styliser.stipControlCodes(channelInfo.getName());
  242. if (!channelInfo.getTopic().isEmpty()) {
  243. temp = temp + " - " + Styliser.stipControlCodes(channelInfo.getTopic());
  244. }
  245. window.setTitle(temp);
  246. }
  247. /**
  248. * Joins the specified channel. This only makes sense if used after a call
  249. * to part().
  250. */
  251. public void join() {
  252. server.getParser().joinChannel(channelInfo.getName());
  253. activateFrame();
  254. setIcon("channel");
  255. }
  256. /**
  257. * Parts this channel with the specified message. Parting does NOT close the
  258. * channel window.
  259. *
  260. * @param reason The reason for parting the channel
  261. */
  262. public void part(final String reason) {
  263. if (server != null && server.getParser() != null) {
  264. server.getParser().partChannel(channelInfo.getName(), reason);
  265. }
  266. resetWindow();
  267. }
  268. /**
  269. * Resets the window state after the client has left a channel.
  270. */
  271. public void resetWindow() {
  272. onChannel = false;
  273. setIcon("channel-inactive");
  274. window.updateNames(new ArrayList<ChannelClientInfo>());
  275. }
  276. /** {@inheritDoc} */
  277. @Override
  278. public void windowClosing() {
  279. // 1: Make the window non-visible
  280. window.setVisible(false);
  281. // 2: Remove any callbacks or listeners
  282. eventHandler.unregisterCallbacks();
  283. if (server.getParser() != null) {
  284. server.getParser().getCallbackManager().delAllCallback(eventHandler);
  285. }
  286. // 3: Trigger any actions neccessary
  287. if (onChannel) {
  288. part(getConfigManager().getOption("general", "partmessage"));
  289. }
  290. // 4: Trigger action for the window closing
  291. ActionManager.processEvent(CoreActionType.CHANNEL_CLOSED, null, this);
  292. // 5: Inform any parents that the window is closing
  293. server.delChannel(channelInfo.getName());
  294. // 6: Remove the window from the window manager
  295. WindowManager.removeWindow(window);
  296. // 7: Remove any references to the window and parents
  297. window = null; // NOPMD
  298. server = null; // NOPMD
  299. }
  300. /**
  301. * Called every {general.whotime} seconds to check if the channel needs
  302. * to send a who request.
  303. */
  304. public void checkWho() {
  305. if (onChannel && sendWho) {
  306. server.getParser().sendLine("WHO :" + channelInfo.getName());
  307. }
  308. }
  309. /**
  310. * Adds a ChannelClient to this Channel.
  311. *
  312. * @param client The client to be added
  313. */
  314. public void addClient(final ChannelClientInfo client) {
  315. window.addName(client);
  316. tabCompleter.addEntry(TabCompletionType.CHANNEL_NICK, client.getNickname());
  317. }
  318. /**
  319. * Removes the specified ChannelClient from this channel.
  320. *
  321. * @param client The client to be removed
  322. */
  323. public void removeClient(final ChannelClientInfo client) {
  324. window.removeName(client);
  325. tabCompleter.removeEntry(TabCompletionType.CHANNEL_NICK, client.getNickname());
  326. if (client.getClient().equals(server.getParser().getLocalClient())) {
  327. resetWindow();
  328. }
  329. }
  330. /**
  331. * Replaces the list of known clients on this channel with the specified
  332. * one.
  333. *
  334. * @param clients The list of clients to use
  335. */
  336. public void setClients(final List<ChannelClientInfo> clients) {
  337. window.updateNames(clients);
  338. tabCompleter.clear(TabCompletionType.CHANNEL_NICK);
  339. for (ChannelClientInfo client : clients) {
  340. tabCompleter.addEntry(TabCompletionType.CHANNEL_NICK, client.getNickname());
  341. }
  342. }
  343. /**
  344. * Renames a client that is in this channel.
  345. *
  346. * @param oldName The old nickname of the client
  347. * @param newName The new nickname of the client
  348. */
  349. public void renameClient(final String oldName, final String newName) {
  350. tabCompleter.removeEntry(TabCompletionType.CHANNEL_NICK, oldName);
  351. tabCompleter.addEntry(TabCompletionType.CHANNEL_NICK, newName);
  352. refreshClients();
  353. }
  354. /**
  355. * Refreshes the list of clients stored by this channel. Should be called
  356. * when (visible) user modes or nicknames change.
  357. */
  358. public void refreshClients() {
  359. window.updateNames();
  360. }
  361. /**
  362. * Returns a string containing the most important mode for the specified
  363. * client.
  364. *
  365. * @param channelClient The channel client to check.
  366. * @return A string containing the most important mode, or an empty string
  367. * if there are no (known) modes.
  368. */
  369. private String getModes(final ChannelClientInfo channelClient) {
  370. if (channelClient == null || !showModePrefix) {
  371. return "";
  372. } else {
  373. return channelClient.getImportantModePrefix();
  374. }
  375. }
  376. /**
  377. * Adds the specified topic to this channel's topic list.
  378. *
  379. * @param topic The topic to be added.
  380. */
  381. public void addTopic(final Topic topic) {
  382. topics.add(topic);
  383. updateTitle();
  384. }
  385. /**
  386. * Retrieve the topics that have been seen on this channel.
  387. *
  388. * @return A list of topics that have been seen on this channel, including
  389. * the current one.
  390. */
  391. public List<Topic> getTopics() {
  392. return topics.getList();
  393. }
  394. /** {@inheritDoc} */
  395. @Override
  396. public void configChanged(final String domain, final String key) {
  397. if ("sendwho".equals(key)) {
  398. sendWho = getConfigManager().getOptionBool("channel", "sendwho");
  399. } else if ("showmodeprefix".equals(key)) {
  400. showModePrefix = getConfigManager().getOptionBool("channel", "showmodeprefix");
  401. } else if ("shownickcoloursintext".equals(key)) {
  402. showColours = getConfigManager().getOptionBool("ui", "shownickcoloursintext");
  403. }
  404. }
  405. /**
  406. * Returns a string[] containing the nickname/ident/host of a channel
  407. * client.
  408. *
  409. * @param client The channel client to check
  410. * @param showColours Whether or not to show colours
  411. * @return A string[] containing displayable components
  412. */
  413. private String[] getDetails(final ChannelClientInfo client,
  414. final boolean showColours) {
  415. if (client == null) {
  416. // WTF?
  417. throw new UnsupportedOperationException("getDetails called with"
  418. + " null ChannelClientInfo");
  419. }
  420. final String[] res = new String[4];
  421. res[0] = getModes(client);
  422. res[1] = Styliser.CODE_NICKNAME + client.getNickname() + Styliser.CODE_NICKNAME;
  423. res[2] = client.getClient().getIdent();
  424. res[3] = client.getClient().getHost();
  425. if (showColours) {
  426. final Map map = client.getMap();
  427. String prefix = null;
  428. Color colour;
  429. if (map.containsKey(ChannelClientProperty.TEXT_FOREGROUND)) {
  430. colour = (Color) map.get(ChannelClientProperty.TEXT_FOREGROUND);
  431. prefix = Styliser.CODE_HEXCOLOUR + ColourManager.getHex(colour);
  432. if (map.containsKey(ChannelClientProperty.TEXT_BACKGROUND)) {
  433. colour = (Color) map.get(ChannelClientProperty.TEXT_BACKGROUND);
  434. prefix = "," + ColourManager.getHex(colour);
  435. }
  436. }
  437. if (prefix != null) {
  438. res[1] = prefix + res[1] + Styliser.CODE_HEXCOLOUR;
  439. }
  440. }
  441. return res;
  442. }
  443. /** {@inheritDoc} */
  444. @Override
  445. protected boolean processNotificationArg(final Object arg, final List<Object> args) {
  446. if (arg instanceof ClientInfo) {
  447. // Format ClientInfos
  448. final ClientInfo clientInfo = (ClientInfo) arg;
  449. args.add(clientInfo.getNickname());
  450. args.add(clientInfo.getIdent());
  451. args.add(clientInfo.getHost());
  452. return true;
  453. } else if (arg instanceof ChannelClientInfo) {
  454. // Format ChannelClientInfos
  455. final ChannelClientInfo clientInfo = (ChannelClientInfo) arg;
  456. args.addAll(Arrays.asList(getDetails(clientInfo, showColours)));
  457. return true;
  458. } else if (arg instanceof Topic) {
  459. // Format topics
  460. args.add("");
  461. args.addAll(Arrays.asList(ClientInfo.parseHostFull(((Topic) arg).getClient())));
  462. args.add(((Topic) arg).getTopic());
  463. args.add(((Topic) arg).getTime() * 1000);
  464. return true;
  465. } else {
  466. // Everything else - default formatting
  467. return super.processNotificationArg(arg, args);
  468. }
  469. }
  470. /** {@inheritDoc} */
  471. @Override
  472. protected void modifyNotificationArgs(final List<Object> actionArgs,
  473. final List<Object> messageArgs) {
  474. messageArgs.add(channelInfo.getName());
  475. }
  476. // ------------------------------------------ PARSER METHOD DELEGATION -----
  477. /**
  478. * Attempts to set the topic of this channel.
  479. *
  480. * @param topic The new topic to be used. An empty string will clear the
  481. * current topic
  482. */
  483. public void setTopic(final String topic) {
  484. server.getParser().sendLine("TOPIC " + channelInfo.getName() + " :" + topic);
  485. }
  486. }