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.

Query.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (c) 2006-2011 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.commandparser.parsers.QueryCommandParser;
  28. import com.dmdirc.logger.ErrorLevel;
  29. import com.dmdirc.logger.Logger;
  30. import com.dmdirc.parser.common.CallbackManager;
  31. import com.dmdirc.parser.common.CallbackNotFoundException;
  32. import com.dmdirc.parser.interfaces.ClientInfo;
  33. import com.dmdirc.parser.interfaces.Parser;
  34. import com.dmdirc.parser.interfaces.callbacks.NickChangeListener;
  35. import com.dmdirc.parser.interfaces.callbacks.PrivateActionListener;
  36. import com.dmdirc.parser.interfaces.callbacks.PrivateMessageListener;
  37. import com.dmdirc.parser.interfaces.callbacks.QuitListener;
  38. import com.dmdirc.ui.WindowManager;
  39. import com.dmdirc.ui.core.components.WindowComponent;
  40. import com.dmdirc.ui.input.TabCompleter;
  41. import com.dmdirc.ui.input.TabCompletionType;
  42. import com.dmdirc.ui.interfaces.InputWindow;
  43. import java.awt.Toolkit;
  44. import java.util.Arrays;
  45. import java.util.Date;
  46. import java.util.List;
  47. /**
  48. * The Query class represents the client's view of a query with another user.
  49. * It handles callbacks for query events from the parser, maintains the
  50. * corresponding QueryWindow, and handles user input for the query.
  51. */
  52. public class Query extends MessageTarget implements PrivateActionListener,
  53. PrivateMessageListener, NickChangeListener, QuitListener {
  54. /** The Server this Query is on. */
  55. private Server server;
  56. /** The full host and nickname of the client associated with this Query. */
  57. private String host, nickname;
  58. /** The tab completer for the query window. */
  59. private final TabCompleter tabCompleter;
  60. /**
  61. * Creates a new instance of Query.
  62. *
  63. * @param newHost host of the remove client
  64. * @param newServer The server object that this Query belongs to
  65. */
  66. public Query(final Server newServer, final String newHost) {
  67. super("query", newServer.parseHostmask(newHost)[0],
  68. newServer.parseHostmask(newHost)[0],
  69. InputWindow.class, newServer.getConfigManager(),
  70. new QueryCommandParser(newServer),
  71. Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
  72. WindowComponent.INPUTFIELD.getIdentifier()));
  73. this.server = newServer;
  74. this.host = newHost;
  75. this.nickname = server.parseHostmask(host)[0];
  76. WindowManager.addWindow(server, this,
  77. !getConfigManager().getOptionBool("general", "hidequeries"));
  78. ActionManager.getActionManager().triggerEvent(
  79. CoreActionType.QUERY_OPENED, null, this);
  80. tabCompleter = new TabCompleter(server.getTabCompleter());
  81. tabCompleter.addEntries(TabCompletionType.COMMAND,
  82. CommandManager.getCommandNames(CommandType.TYPE_QUERY));
  83. tabCompleter.addEntries(TabCompletionType.COMMAND,
  84. CommandManager.getCommandNames(CommandType.TYPE_CHAT));
  85. if (!server.getState().isDisconnected()) {
  86. reregister();
  87. }
  88. updateTitle();
  89. }
  90. /** {@inheritDoc} */
  91. @Override
  92. public TabCompleter getTabCompleter() {
  93. return tabCompleter;
  94. }
  95. /** {@inheritDoc} */
  96. @Override
  97. public void sendLine(final String line) {
  98. sendLine(line, getNickname());
  99. }
  100. /**
  101. * Sends a line to the recipient of this query using the specified
  102. * nickname or hostmask as a target. This allows for users to send
  103. * messages with a server specified (e.g. <code>nick@server</code>)
  104. * as though the query wasn't open.
  105. * <p>
  106. * The caller is responsible for ensuring that the <code>target</code>
  107. * does actually correspond to this query.
  108. *
  109. * @since 0.6.4
  110. * @param line The line to be sent
  111. * @param target The target to send the line to
  112. */
  113. public void sendLine(final String line, final String target) {
  114. if (server.getState() != ServerState.CONNECTED) {
  115. Toolkit.getDefaultToolkit().beep();
  116. return;
  117. }
  118. for (String part : splitLine(line)) {
  119. if (!part.isEmpty()) {
  120. server.getParser().sendMessage(target, part);
  121. doNotification("querySelfMessage",
  122. CoreActionType.QUERY_SELF_MESSAGE,
  123. server.getParser().getLocalClient(), line);
  124. }
  125. }
  126. }
  127. /** {@inheritDoc} */
  128. @Override
  129. protected boolean processNotificationArg(final Object arg, final List<Object> args) {
  130. if (arg instanceof ClientInfo) {
  131. final ClientInfo clientInfo = (ClientInfo) arg;
  132. args.add(clientInfo.getNickname());
  133. args.add(clientInfo.getUsername());
  134. args.add(clientInfo.getHostname());
  135. return true;
  136. } else {
  137. return super.processNotificationArg(arg, args);
  138. }
  139. }
  140. /** {@inheritDoc} */
  141. @Override
  142. public int getMaxLineLength() {
  143. return server.getState() == ServerState.CONNECTED ? server.getParser()
  144. .getMaxLength("PRIVMSG", host) : -1;
  145. }
  146. /**
  147. * Sends a private action to the remote user.
  148. *
  149. * @param action action text to send
  150. */
  151. @Override
  152. public void sendAction(final String action) {
  153. if (server.getState() != ServerState.CONNECTED) {
  154. Toolkit.getDefaultToolkit().beep();
  155. return;
  156. }
  157. final ClientInfo client = server.getParser().getLocalClient();
  158. final int maxLineLength = server.getParser().getMaxLength("PRIVMSG", host);
  159. if (maxLineLength >= action.length() + 2) {
  160. server.getParser().sendAction(getNickname(), action);
  161. doNotification("querySelfAction", CoreActionType.QUERY_SELF_ACTION,
  162. client, action);
  163. } else {
  164. addLine("actionTooLong", action.length());
  165. }
  166. }
  167. /** {@inheritDoc} */
  168. @Override
  169. public void onPrivateMessage(final Parser parser, final Date date,
  170. final String message, final String host) {
  171. final String[] parts = server.parseHostmask(host);
  172. final StringBuffer buff = new StringBuffer("queryMessage");
  173. ActionManager.getActionManager().triggerEvent(
  174. CoreActionType.QUERY_MESSAGE, buff, this,
  175. parser.getClient(host), message);
  176. addLine(buff, parts[0], parts[1], parts[2], message);
  177. }
  178. /** {@inheritDoc} */
  179. @Override
  180. public void onPrivateAction(final Parser parser, final Date date,
  181. final String message, final String host) {
  182. final String[] parts = server.parseHostmask(host);
  183. final StringBuffer buff = new StringBuffer("queryAction");
  184. ActionManager.getActionManager().triggerEvent(
  185. CoreActionType.QUERY_ACTION, buff, this, parser.getClient(host),
  186. message);
  187. addLine(buff, parts[0], parts[1], parts[2], message);
  188. }
  189. /**
  190. * Updates the QueryWindow's title.
  191. */
  192. private void updateTitle() {
  193. setTitle(getNickname());
  194. }
  195. /**
  196. * Reregisters query callbacks. Called when reconnecting to the server.
  197. */
  198. public void reregister() {
  199. final CallbackManager callbackManager = server.getParser().getCallbackManager();
  200. final String nick = getNickname();
  201. try {
  202. callbackManager.addCallback(PrivateActionListener.class, this, nick);
  203. callbackManager.addCallback(PrivateMessageListener.class, this, nick);
  204. callbackManager.addCallback(QuitListener.class, this);
  205. callbackManager.addCallback(NickChangeListener.class, this);
  206. } catch (CallbackNotFoundException ex) {
  207. Logger.appError(ErrorLevel.HIGH, "Unable to get query events", ex);
  208. }
  209. }
  210. /** {@inheritDoc} */
  211. @Override
  212. public void onNickChanged(final Parser parser, final Date date,
  213. final ClientInfo client, final String oldNick) {
  214. if (oldNick.equals(getNickname())) {
  215. final CallbackManager callbackManager = server.getParser().getCallbackManager();
  216. callbackManager.delCallback(PrivateActionListener.class, this);
  217. callbackManager.delCallback(PrivateMessageListener.class, this);
  218. try {
  219. callbackManager.addCallback(PrivateActionListener.class, this, client.getNickname());
  220. callbackManager.addCallback(PrivateMessageListener.class, this, client.getNickname());
  221. } catch (CallbackNotFoundException ex) {
  222. Logger.appError(ErrorLevel.HIGH, "Unable to get query events", ex);
  223. }
  224. final StringBuffer format = new StringBuffer("queryNickChanged");
  225. ActionManager.getActionManager().triggerEvent(
  226. CoreActionType.QUERY_NICKCHANGE, format, this, oldNick);
  227. server.updateQuery(this, oldNick, client.getNickname());
  228. addLine(format, oldNick, client.getUsername(),
  229. client.getHostname(), client.getNickname());
  230. host = client.getNickname() + "!" + client.getUsername() + "@" + client.getHostname();
  231. nickname = client.getNickname();
  232. updateTitle();
  233. setName(client.getNickname());
  234. }
  235. }
  236. /** {@inheritDoc} */
  237. @Override
  238. public void onQuit(final Parser parser, final Date date,
  239. final ClientInfo client, final String reason) {
  240. if (client.getNickname().equals(getNickname())) {
  241. final StringBuffer format = new StringBuffer(reason.isEmpty()
  242. ? "queryQuit" : "queryQuitReason");
  243. ActionManager.getActionManager().triggerEvent(
  244. CoreActionType.QUERY_QUIT, format, this, reason);
  245. addLine(format, client.getNickname(),
  246. client.getUsername(), client.getHostname(), reason);
  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. // 2: Remove any callbacks or listeners
  262. if (server != null && server.getParser() != null) {
  263. server.getParser().getCallbackManager().delAllCallback(this);
  264. }
  265. // 3: Trigger any actions neccessary
  266. // 4: Trigger action for the window closing
  267. ActionManager.getActionManager().triggerEvent(
  268. CoreActionType.QUERY_CLOSED, null, this);
  269. // 5: Inform any parents that the window is closing
  270. if (server != null) {
  271. server.delQuery(this);
  272. }
  273. }
  274. /** {@inheritDoc} */
  275. @Override
  276. public void windowClosed() {
  277. // 7: Remove any references to the window and parents
  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. }