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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Copyright (c) 2006-2010 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.logger.ErrorLevel;
  28. import com.dmdirc.logger.Logger;
  29. import com.dmdirc.parser.interfaces.ClientInfo;
  30. import com.dmdirc.parser.interfaces.Parser;
  31. import com.dmdirc.parser.common.CallbackManager;
  32. import com.dmdirc.parser.common.CallbackNotFoundException;
  33. import com.dmdirc.parser.interfaces.callbacks.NickChangeListener;
  34. import com.dmdirc.parser.interfaces.callbacks.PrivateActionListener;
  35. import com.dmdirc.parser.interfaces.callbacks.PrivateMessageListener;
  36. import com.dmdirc.parser.interfaces.callbacks.QuitListener;
  37. import com.dmdirc.ui.WindowManager;
  38. import com.dmdirc.ui.input.TabCompleter;
  39. import com.dmdirc.ui.input.TabCompletionType;
  40. import com.dmdirc.ui.interfaces.InputWindow;
  41. import com.dmdirc.ui.interfaces.QueryWindow;
  42. import java.awt.Toolkit;
  43. import java.io.Serializable;
  44. /**
  45. * The Query class represents the client's view of a query with another user.
  46. * It handles callbacks for query events from the parser, maintains the
  47. * corresponding QueryWindow, and handles user input for the query.
  48. * @author chris
  49. */
  50. public final class Query extends MessageTarget implements
  51. PrivateActionListener, PrivateMessageListener, NickChangeListener, QuitListener, Serializable {
  52. /**
  53. * A version number for this class. It should be changed whenever the class
  54. * structure is changed (or anything else that would prevent serialized
  55. * objects being unserialized with the new class).
  56. */
  57. private static final long serialVersionUID = 1;
  58. /** The Server this Query is on. */
  59. private Server server;
  60. /** The QueryWindow used for this Query. */
  61. private QueryWindow window;
  62. /** The full host and nickname of the client associated with this Query. */
  63. private String host, nickname;
  64. /** The tab completer for the query window. */
  65. private final TabCompleter tabCompleter;
  66. /**
  67. * Creates a new instance of Query.
  68. *
  69. * @param newHost host of the remove client
  70. * @param newServer The server object that this Query belongs to
  71. */
  72. public Query(final Server newServer, final String newHost) {
  73. super("query", newServer.getParser().parseHostmask(newHost)[0],
  74. newServer.getConfigManager());
  75. this.server = newServer;
  76. this.host = newHost;
  77. this.nickname = server.getParser().parseHostmask(host)[0];
  78. window = Main.getUI().getQuery(this);
  79. WindowManager.addWindow(server.getFrame(), window);
  80. ActionManager.processEvent(CoreActionType.QUERY_OPENED, null, this);
  81. if (!server.getConfigManager().getOptionBool("general", "hidequeries")) {
  82. window.open();
  83. }
  84. tabCompleter = new TabCompleter(server.getTabCompleter());
  85. tabCompleter.addEntries(TabCompletionType.COMMAND,
  86. CommandManager.getCommandNames(CommandType.TYPE_QUERY));
  87. tabCompleter.addEntries(TabCompletionType.COMMAND,
  88. CommandManager.getCommandNames(CommandType.TYPE_CHAT));
  89. window.getInputHandler().setTabCompleter(tabCompleter);
  90. reregister();
  91. updateTitle();
  92. }
  93. /**
  94. * Shows this query's window.
  95. */
  96. public void show() {
  97. window.open();
  98. }
  99. /** {@inheritDoc} */
  100. @Override
  101. public InputWindow getFrame() {
  102. return window;
  103. }
  104. /**
  105. * Returns the tab completer for this query.
  106. *
  107. * @return This query's tab completer
  108. */
  109. public TabCompleter getTabCompleter() {
  110. return tabCompleter;
  111. }
  112. /** {@inheritDoc} */
  113. @Override
  114. public void sendLine(final String line) {
  115. if (server.getState() != ServerState.CONNECTED) {
  116. Toolkit.getDefaultToolkit().beep();
  117. return;
  118. }
  119. final ClientInfo client = server.getParser().getLocalClient();
  120. for (String part : splitLine(window.getTranscoder().encode(line))) {
  121. if (!part.isEmpty()) {
  122. server.getParser().sendMessage(getNickname(), part);
  123. final StringBuffer buff = new StringBuffer("querySelfMessage");
  124. ActionManager.processEvent(CoreActionType.QUERY_SELF_MESSAGE, buff, this, part);
  125. addLine(buff, client.getNickname(), client.getUsername(),
  126. client.getHostname(), part);
  127. }
  128. }
  129. }
  130. /** {@inheritDoc} */
  131. @Override
  132. public int getMaxLineLength() {
  133. return server.getState() == ServerState.CONNECTED ? server.getParser()
  134. .getMaxLength("PRIVMSG", host) : -1;
  135. }
  136. /**
  137. * Sends a private action to the remote user.
  138. *
  139. * @param action action text to send
  140. */
  141. @Override
  142. public void sendAction(final String action) {
  143. if (server.getState() != ServerState.CONNECTED) {
  144. Toolkit.getDefaultToolkit().beep();
  145. return;
  146. }
  147. final ClientInfo client = server.getParser().getLocalClient();
  148. final int maxLineLength = server.getParser().getMaxLength("PRIVMSG", host);
  149. if (maxLineLength >= action.length() + 2) {
  150. server.getParser().sendAction(getNickname(),
  151. window.getTranscoder().encode(action));
  152. final StringBuffer buff = new StringBuffer("querySelfAction");
  153. ActionManager.processEvent(CoreActionType.QUERY_SELF_ACTION, buff, this, action);
  154. addLine(buff, client.getNickname(), client.getUsername(),
  155. client.getHostname(), window.getTranscoder().encode(action));
  156. } else {
  157. addLine("actionTooLong", action.length());
  158. }
  159. }
  160. /**
  161. * Handles a private message event from the parser.
  162. *
  163. * @param parser Parser receiving the event
  164. * @param message message received
  165. * @param remoteHost remote user host
  166. */
  167. @Override
  168. public void onPrivateMessage(final Parser parser, final String message,
  169. final String remoteHost) {
  170. final String[] parts = parser.parseHostmask(host);
  171. final StringBuffer buff = new StringBuffer("queryMessage");
  172. ActionManager.processEvent(CoreActionType.QUERY_MESSAGE, buff, this, message);
  173. addLine(buff, parts[0], parts[1], parts[2], message);
  174. }
  175. /**
  176. * Handles a private action event from the parser.
  177. *
  178. * @param parser Parser receiving the event
  179. * @param message message received
  180. * @param remoteHost remote host
  181. */
  182. @Override
  183. public void onPrivateAction(final Parser parser, final String message,
  184. final String remoteHost) {
  185. final String[] parts = parser.parseHostmask(host);
  186. final StringBuffer buff = new StringBuffer("queryAction");
  187. ActionManager.processEvent(CoreActionType.QUERY_ACTION, buff, this, message);
  188. addLine(buff, parts[0], parts[1], parts[2], message);
  189. }
  190. /**
  191. * Updates the QueryWindow's title.
  192. */
  193. private void updateTitle() {
  194. window.setTitle(getNickname());
  195. }
  196. /**
  197. * Reregisters query callbacks. Called when reconnecting to the server.
  198. */
  199. public void reregister() {
  200. final CallbackManager<?> callbackManager = server.getParser().getCallbackManager();
  201. final String nick = getNickname();
  202. try {
  203. callbackManager.addCallback(PrivateActionListener.class, this, nick);
  204. callbackManager.addCallback(PrivateMessageListener.class, this, nick);
  205. callbackManager.addCallback(QuitListener.class, this);
  206. callbackManager.addCallback(NickChangeListener.class, this);
  207. } catch (CallbackNotFoundException ex) {
  208. Logger.appError(ErrorLevel.HIGH, "Unable to get query events", ex);
  209. }
  210. }
  211. /** {@inheritDoc} */
  212. @Override
  213. public void onNickChanged(final Parser tParser, final ClientInfo cClient,
  214. final String sOldNick) {
  215. if (sOldNick.equals(getNickname())) {
  216. final CallbackManager<?> callbackManager = server.getParser().getCallbackManager();
  217. callbackManager.delCallback(PrivateActionListener.class, this);
  218. callbackManager.delCallback(PrivateMessageListener.class, this);
  219. try {
  220. callbackManager.addCallback(PrivateActionListener.class, this, cClient.getNickname());
  221. callbackManager.addCallback(PrivateMessageListener.class, this, cClient.getNickname());
  222. } catch (CallbackNotFoundException ex) {
  223. Logger.appError(ErrorLevel.HIGH, "Unable to get query events", ex);
  224. }
  225. final StringBuffer format = new StringBuffer("queryNickChanged");
  226. ActionManager.processEvent(CoreActionType.QUERY_NICKCHANGE, format, this, sOldNick);
  227. server.getTabCompleter().removeEntry(TabCompletionType.QUERY_NICK, sOldNick);
  228. server.getTabCompleter().addEntry(TabCompletionType.QUERY_NICK, cClient.getNickname());
  229. addLine(format, sOldNick, cClient.getUsername(),
  230. cClient.getHostname(), cClient.getNickname());
  231. host = cClient.getNickname() + "!" + cClient.getUsername() + "@" + cClient.getHostname();
  232. nickname = cClient.getNickname();
  233. updateTitle();
  234. setName(cClient.getNickname());
  235. }
  236. }
  237. /** {@inheritDoc} */
  238. @Override
  239. public void onQuit(final Parser tParser, final ClientInfo cClient,
  240. final String sReason) {
  241. if (cClient.getNickname().equals(getNickname())) {
  242. final StringBuffer format = new StringBuffer(sReason.isEmpty()
  243. ? "queryQuit" : "queryQuitReason");
  244. ActionManager.processEvent(CoreActionType.QUERY_QUIT, format, this, sReason);
  245. addLine(format, cClient.getNickname(),
  246. cClient.getUsername(), cClient.getHostname(), sReason);
  247. }
  248. }
  249. /**
  250. * Returns the Server associated with this query.
  251. *
  252. * @return associated Server
  253. */
  254. @Override
  255. public Server getServer() {
  256. return server;
  257. }
  258. /** {@inheritDoc} */
  259. @Override
  260. public void windowClosing() {
  261. // 1: Make the window non-visible
  262. window.setVisible(false);
  263. // 2: Remove any callbacks or listeners
  264. if (server != null && server.getParser() != null) {
  265. server.getParser().getCallbackManager().delAllCallback(this);
  266. }
  267. // 3: Trigger any actions neccessary
  268. // 4: Trigger action for the window closing
  269. ActionManager.processEvent(CoreActionType.QUERY_CLOSED, null, this);
  270. // 5: Inform any parents that the window is closing
  271. if (server != null) {
  272. server.delQuery(this);
  273. }
  274. // 6: Remove the window from the window manager
  275. WindowManager.removeWindow(window);
  276. // 7: Remove any references to the window and parents
  277. window = null;
  278. server = null;
  279. }
  280. /**
  281. * Returns the host that this query is with.
  282. *
  283. * @return The full host that this query is with
  284. */
  285. public String getHost() {
  286. return host;
  287. }
  288. /**
  289. * Returns the current nickname of the user that this query is with.
  290. *
  291. * @return The nickname of this query's user
  292. */
  293. public String getNickname() {
  294. return nickname;
  295. }
  296. /** {@inheritDoc} */
  297. @Override
  298. public void activateFrame() {
  299. if (window == null) {
  300. return;
  301. }
  302. if (!window.isVisible()) {
  303. show();
  304. }
  305. super.activateFrame();
  306. }
  307. }