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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Copyright (c) 2006-2008 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.irc.ClientInfo;
  30. import com.dmdirc.parser.irc.IRCParser;
  31. import com.dmdirc.parser.irc.callbacks.CallbackManager;
  32. import com.dmdirc.parser.irc.callbacks.CallbackNotFoundException;
  33. import com.dmdirc.parser.irc.callbacks.interfaces.INickChanged;
  34. import com.dmdirc.parser.irc.callbacks.interfaces.IPrivateAction;
  35. import com.dmdirc.parser.irc.callbacks.interfaces.IPrivateMessage;
  36. import com.dmdirc.parser.irc.callbacks.interfaces.IQuit;
  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. IPrivateAction, IPrivateMessage, INickChanged, IQuit, 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 of the client associated with this Query. */
  63. private String host;
  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.getConfigManager());
  74. this.server = newServer;
  75. this.host = newHost;
  76. window = Main.getUI().getQuery(this);
  77. WindowManager.addWindow(server.getFrame(), window);
  78. ActionManager.processEvent(CoreActionType.QUERY_OPENED, null, this);
  79. if (!server.getConfigManager().getOptionBool("general", "hidequeries", false)) {
  80. window.open();
  81. }
  82. tabCompleter = new TabCompleter(server.getTabCompleter());
  83. tabCompleter.addEntries(TabCompletionType.COMMAND,
  84. CommandManager.getCommandNames(CommandType.TYPE_QUERY));
  85. tabCompleter.addEntries(TabCompletionType.COMMAND,
  86. CommandManager.getCommandNames(CommandType.TYPE_CHAT));
  87. window.getInputHandler().setTabCompleter(tabCompleter);
  88. reregister();
  89. updateTitle();
  90. }
  91. /**
  92. * Shows this query's window.
  93. */
  94. public void show() {
  95. window.open();
  96. }
  97. /** {@inheritDoc} */
  98. @Override
  99. public InputWindow getFrame() {
  100. return window;
  101. }
  102. /**
  103. * Returns the tab completer for this query.
  104. *
  105. * @return This query's tab completer
  106. */
  107. public TabCompleter getTabCompleter() {
  108. return tabCompleter;
  109. }
  110. /** {@inheritDoc} */
  111. @Override
  112. public void sendLine(final String line) {
  113. if (server.getState() != ServerState.CONNECTED) {
  114. Toolkit.getDefaultToolkit().beep();
  115. return;
  116. }
  117. if (line.indexOf('\n') > -1) {
  118. for (String part : line.split("\n")) {
  119. sendLine(part);
  120. }
  121. return;
  122. }
  123. final ClientInfo client = server.getParser().getMyself();
  124. if (line.length() <= getMaxLineLength()) {
  125. server.getParser().sendMessage(ClientInfo.parseHost(host), window.getTranscoder().encode(line));
  126. final StringBuffer buff = new StringBuffer("querySelfMessage");
  127. ActionManager.processEvent(CoreActionType.QUERY_SELF_MESSAGE, buff, this, line);
  128. addLine(buff, client.getNickname(), client.getIdent(),
  129. client.getHost(), window.getTranscoder().encode(line));
  130. } else {
  131. sendLine(line.substring(0, getMaxLineLength()));
  132. sendLine(line.substring(getMaxLineLength()));
  133. }
  134. }
  135. /** {@inheritDoc} */
  136. @Override
  137. public int getMaxLineLength() {
  138. return server.getParser().getMaxLength("PRIVMSG", host);
  139. }
  140. /**
  141. * Sends a private action to the remote user.
  142. *
  143. * @param action action text to send
  144. */
  145. @Override
  146. public void sendAction(final String action) {
  147. if (server.getState() != ServerState.CONNECTED) {
  148. Toolkit.getDefaultToolkit().beep();
  149. return;
  150. }
  151. final ClientInfo client = server.getParser().getMyself();
  152. final int maxLineLength = server.getParser().getMaxLength("PRIVMSG", host);
  153. if (maxLineLength >= action.length() + 2) {
  154. server.getParser().sendAction(ClientInfo.parseHost(host), window.getTranscoder().encode(action));
  155. final StringBuffer buff = new StringBuffer("querySelfAction");
  156. ActionManager.processEvent(CoreActionType.QUERY_SELF_ACTION, buff, this, action);
  157. addLine(buff, client.getNickname(), client.getIdent(),
  158. client.getHost(), window.getTranscoder().encode(action));
  159. } else {
  160. addLine("actionTooLong", action.length());
  161. }
  162. }
  163. /**
  164. * Handles a private message event from the parser.
  165. *
  166. * @param parser Parser receiving the event
  167. * @param message message received
  168. * @param remoteHost remote user host
  169. */
  170. @Override
  171. public void onPrivateMessage(final IRCParser parser, final String message,
  172. final String remoteHost) {
  173. final String[] parts = ClientInfo.parseHostFull(remoteHost);
  174. final StringBuffer buff = new StringBuffer("queryMessage");
  175. ActionManager.processEvent(CoreActionType.QUERY_MESSAGE, buff, this, message);
  176. addLine(buff, parts[0], parts[1], parts[2], message);
  177. }
  178. /**
  179. * Handles a private action event from the parser.
  180. *
  181. * @param parser Parser receiving the event
  182. * @param message message received
  183. * @param remoteHost remote host
  184. */
  185. @Override
  186. public void onPrivateAction(final IRCParser parser, final String message,
  187. final String remoteHost) {
  188. final String[] parts = ClientInfo.parseHostFull(host);
  189. final StringBuffer buff = new StringBuffer("queryAction");
  190. ActionManager.processEvent(CoreActionType.QUERY_ACTION, buff, this, message);
  191. addLine(buff, parts[0], parts[1], parts[2], message);
  192. }
  193. /**
  194. * Updates the QueryWindow's title.
  195. */
  196. private void updateTitle() {
  197. final String title = ClientInfo.parseHost(host);
  198. window.setTitle(title);
  199. }
  200. /**
  201. * Reregisters query callbacks. Called when reconnecting to the server.
  202. */
  203. public void reregister() {
  204. final CallbackManager callbackManager = server.getParser().getCallbackManager();
  205. try {
  206. callbackManager.addCallback("onPrivateAction", this, ClientInfo.parseHost(host));
  207. callbackManager.addCallback("onPrivateMessage", this, ClientInfo.parseHost(host));
  208. callbackManager.addCallback("onQuit", this);
  209. callbackManager.addCallback("onNickChanged", this);
  210. } catch (CallbackNotFoundException ex) {
  211. Logger.appError(ErrorLevel.HIGH, "Unable to get query events", ex);
  212. }
  213. }
  214. /** {@inheritDoc} */
  215. @Override
  216. public void onNickChanged(final IRCParser tParser, final ClientInfo cClient,
  217. final String sOldNick) {
  218. if (sOldNick.equals(ClientInfo.parseHost(host))) {
  219. final CallbackManager callbackManager = server.getParser().getCallbackManager();
  220. callbackManager.delCallback("onPrivateAction", this);
  221. callbackManager.delCallback("onPrivateMessage", this);
  222. try {
  223. callbackManager.addCallback("onPrivateAction", this, cClient.getNickname());
  224. callbackManager.addCallback("onPrivateMessage", this, cClient.getNickname());
  225. } catch (CallbackNotFoundException ex) {
  226. Logger.appError(ErrorLevel.HIGH, "Unable to get query events", ex);
  227. }
  228. final StringBuffer format = new StringBuffer("queryNickChanged");
  229. ActionManager.processEvent(CoreActionType.QUERY_NICKCHANGE, format, this, sOldNick);
  230. server.getTabCompleter().removeEntry(TabCompletionType.QUERY_NICK, sOldNick);
  231. server.getTabCompleter().addEntry(TabCompletionType.QUERY_NICK, cClient.getNickname());
  232. addLine(format, sOldNick, cClient.getIdent(),
  233. cClient.getHost(), cClient.getNickname());
  234. host = cClient.getNickname() + "!" + cClient.getIdent() + "@" + cClient.getHost();
  235. updateTitle();
  236. }
  237. }
  238. /** {@inheritDoc} */
  239. @Override
  240. public void onQuit(final IRCParser tParser, final ClientInfo cClient,
  241. final String sReason) {
  242. if (cClient.getNickname().equals(ClientInfo.parseHost(host))) {
  243. final StringBuffer format = new StringBuffer(sReason.isEmpty()
  244. ? "queryQuit" : "queryQuitReason");
  245. ActionManager.processEvent(CoreActionType.QUERY_QUIT, format, this, sReason);
  246. addLine(format, cClient.getNickname(),
  247. cClient.getIdent(), cClient.getHost(), sReason);
  248. }
  249. }
  250. /**
  251. * Returns the Server assocaited with this query.
  252. *
  253. * @return asscoaited Server
  254. */
  255. @Override
  256. public Server getServer() {
  257. return server;
  258. }
  259. /** {@inheritDoc} */
  260. @Override
  261. public void windowClosing() {
  262. // 1: Make the window non-visible
  263. window.setVisible(false);
  264. // 2: Remove any callbacks or listeners
  265. if (server != null && server.getParser() != null) {
  266. server.getParser().getCallbackManager().delAllCallback(this);
  267. }
  268. // 3: Trigger any actions neccessary
  269. // 4: Trigger action for the window closing
  270. ActionManager.processEvent(CoreActionType.QUERY_CLOSED, null, this);
  271. // 5: Inform any parents that the window is closing
  272. server.delQuery(this);
  273. // 6: Remove the window from the window manager
  274. WindowManager.removeWindow(window);
  275. // 7: Remove any references to the window and parents
  276. window = null;
  277. server = null;
  278. }
  279. /**
  280. * Returns this query's name.
  281. *
  282. * @return A string representation of this query (i.e., the user's name)
  283. */
  284. @Override
  285. public String toString() {
  286. return ClientInfo.parseHost(host);
  287. }
  288. /**
  289. * Returns the host that this query is with.
  290. *
  291. * @return The full host that this query is with
  292. */
  293. public String getHost() {
  294. return host;
  295. }
  296. /**
  297. * Returns the current nickname of the user that this query is with.
  298. *
  299. * @return The nickname of this query's user
  300. */
  301. public String getNickname() {
  302. return ClientInfo.parseHost(host);
  303. }
  304. /** {@inheritDoc} */
  305. @Override
  306. public void activateFrame() {
  307. if (window == null) {
  308. return;
  309. }
  310. if (!window.isVisible()) {
  311. show();
  312. }
  313. Main.getUI().getMainWindow().setActiveFrame(window);
  314. }
  315. }