Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Query.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * Copyright (c) 2006-2011 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.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. this(newServer, newHost, false);
  68. }
  69. /**
  70. * Creates a new instance of Query.
  71. *
  72. * @param newHost host of the remove client
  73. * @param newServer The server object that this Query belongs to
  74. * @param focus Should we focus the query on open?
  75. */
  76. public Query(final Server newServer, final String newHost,
  77. final boolean focus) {
  78. super("query", newServer.parseHostmask(newHost)[0],
  79. newServer.parseHostmask(newHost)[0],
  80. InputWindow.class, newServer.getConfigManager(),
  81. new QueryCommandParser(newServer),
  82. Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
  83. WindowComponent.INPUTFIELD.getIdentifier()));
  84. this.server = newServer;
  85. this.host = newHost;
  86. this.nickname = server.parseHostmask(host)[0];
  87. WindowManager.getWindowManager().addWindow(server, this, focus);
  88. ActionManager.getActionManager().triggerEvent(
  89. CoreActionType.QUERY_OPENED, null, this);
  90. tabCompleter = new TabCompleter(server.getTabCompleter());
  91. tabCompleter.addEntries(TabCompletionType.COMMAND,
  92. CommandManager.getCommandManager().getCommandNames(CommandType.TYPE_QUERY));
  93. tabCompleter.addEntries(TabCompletionType.COMMAND,
  94. CommandManager.getCommandManager().getCommandNames(CommandType.TYPE_CHAT));
  95. if (!server.getState().isDisconnected()) {
  96. reregister();
  97. }
  98. updateTitle();
  99. }
  100. /** {@inheritDoc} */
  101. @Override
  102. public TabCompleter getTabCompleter() {
  103. return tabCompleter;
  104. }
  105. /** {@inheritDoc} */
  106. @Override
  107. public void sendLine(final String line) {
  108. sendLine(line, getNickname());
  109. }
  110. /**
  111. * Sends a line to the recipient of this query using the specified
  112. * nickname or hostmask as a target. This allows for users to send
  113. * messages with a server specified (e.g. <code>nick@server</code>)
  114. * as though the query wasn't open.
  115. * <p>
  116. * The caller is responsible for ensuring that the <code>target</code>
  117. * does actually correspond to this query.
  118. *
  119. * @since 0.6.4
  120. * @param line The line to be sent
  121. * @param target The target to send the line to
  122. */
  123. public void sendLine(final String line, final String target) {
  124. if (server.getState() != ServerState.CONNECTED) {
  125. Toolkit.getDefaultToolkit().beep();
  126. return;
  127. }
  128. for (String part : splitLine(line)) {
  129. if (!part.isEmpty()) {
  130. server.getParser().sendMessage(target, part);
  131. doNotification("querySelfMessage",
  132. CoreActionType.QUERY_SELF_MESSAGE,
  133. server.getParser().getLocalClient(), line);
  134. }
  135. }
  136. }
  137. /** {@inheritDoc} */
  138. @Override
  139. protected boolean processNotificationArg(final Object arg, final List<Object> args) {
  140. if (arg instanceof ClientInfo) {
  141. final ClientInfo clientInfo = (ClientInfo) arg;
  142. args.add(clientInfo.getNickname());
  143. args.add(clientInfo.getUsername());
  144. args.add(clientInfo.getHostname());
  145. return true;
  146. } else {
  147. return super.processNotificationArg(arg, args);
  148. }
  149. }
  150. /** {@inheritDoc} */
  151. @Override
  152. public int getMaxLineLength() {
  153. return server.getState() == ServerState.CONNECTED ? server.getParser()
  154. .getMaxLength("PRIVMSG", host) : -1;
  155. }
  156. /**
  157. * Sends a private action to the remote user.
  158. *
  159. * @param action action text to send
  160. */
  161. @Override
  162. public void sendAction(final String action) {
  163. if (server.getState() != ServerState.CONNECTED) {
  164. Toolkit.getDefaultToolkit().beep();
  165. return;
  166. }
  167. final ClientInfo client = server.getParser().getLocalClient();
  168. final int maxLineLength = server.getParser().getMaxLength("PRIVMSG", host);
  169. if (maxLineLength >= action.length() + 2) {
  170. server.getParser().sendAction(getNickname(), action);
  171. doNotification("querySelfAction", CoreActionType.QUERY_SELF_ACTION,
  172. client, action);
  173. } else {
  174. addLine("actionTooLong", action.length());
  175. }
  176. }
  177. /** {@inheritDoc} */
  178. @Override
  179. public void onPrivateMessage(final Parser parser, final Date date,
  180. final String message, final String host) {
  181. final String[] parts = server.parseHostmask(host);
  182. final StringBuffer buff = new StringBuffer("queryMessage");
  183. ActionManager.getActionManager().triggerEvent(
  184. CoreActionType.QUERY_MESSAGE, buff, this,
  185. parser.getClient(host), message);
  186. addLine(buff, parts[0], parts[1], parts[2], message);
  187. }
  188. /** {@inheritDoc} */
  189. @Override
  190. public void onPrivateAction(final Parser parser, final Date date,
  191. final String message, final String host) {
  192. final String[] parts = server.parseHostmask(host);
  193. final StringBuffer buff = new StringBuffer("queryAction");
  194. ActionManager.getActionManager().triggerEvent(
  195. CoreActionType.QUERY_ACTION, buff, this, parser.getClient(host),
  196. message);
  197. addLine(buff, parts[0], parts[1], parts[2], message);
  198. }
  199. /**
  200. * Updates the QueryWindow's title.
  201. */
  202. private void updateTitle() {
  203. setTitle(getNickname());
  204. }
  205. /**
  206. * Reregisters query callbacks. Called when reconnecting to the server.
  207. */
  208. public void reregister() {
  209. final CallbackManager callbackManager = server.getParser().getCallbackManager();
  210. final String nick = getNickname();
  211. try {
  212. callbackManager.addCallback(PrivateActionListener.class, this, nick);
  213. callbackManager.addCallback(PrivateMessageListener.class, this, nick);
  214. callbackManager.addCallback(QuitListener.class, this);
  215. callbackManager.addCallback(NickChangeListener.class, this);
  216. } catch (CallbackNotFoundException ex) {
  217. Logger.appError(ErrorLevel.HIGH, "Unable to get query events", ex);
  218. }
  219. }
  220. /** {@inheritDoc} */
  221. @Override
  222. public void onNickChanged(final Parser parser, final Date date,
  223. final ClientInfo client, final String oldNick) {
  224. if (oldNick.equals(getNickname())) {
  225. final CallbackManager callbackManager = server.getParser().getCallbackManager();
  226. callbackManager.delCallback(PrivateActionListener.class, this);
  227. callbackManager.delCallback(PrivateMessageListener.class, this);
  228. try {
  229. callbackManager.addCallback(PrivateActionListener.class, this, client.getNickname());
  230. callbackManager.addCallback(PrivateMessageListener.class, this, client.getNickname());
  231. } catch (CallbackNotFoundException ex) {
  232. Logger.appError(ErrorLevel.HIGH, "Unable to get query events", ex);
  233. }
  234. final StringBuffer format = new StringBuffer("queryNickChanged");
  235. ActionManager.getActionManager().triggerEvent(
  236. CoreActionType.QUERY_NICKCHANGE, format, this, oldNick);
  237. server.updateQuery(this, oldNick, client.getNickname());
  238. addLine(format, oldNick, client.getUsername(),
  239. client.getHostname(), client.getNickname());
  240. host = client.getNickname() + "!" + client.getUsername() + "@" + client.getHostname();
  241. nickname = client.getNickname();
  242. updateTitle();
  243. setName(client.getNickname());
  244. }
  245. }
  246. /** {@inheritDoc} */
  247. @Override
  248. public void onQuit(final Parser parser, final Date date,
  249. final ClientInfo client, final String reason) {
  250. if (client.getNickname().equals(getNickname())) {
  251. final StringBuffer format = new StringBuffer(reason.isEmpty()
  252. ? "queryQuit" : "queryQuitReason");
  253. ActionManager.getActionManager().triggerEvent(
  254. CoreActionType.QUERY_QUIT, format, this, reason);
  255. addLine(format, client.getNickname(),
  256. client.getUsername(), client.getHostname(), reason);
  257. }
  258. }
  259. /**
  260. * Returns the Server associated with this query.
  261. *
  262. * @return associated Server
  263. */
  264. @Override
  265. public Server getServer() {
  266. return server;
  267. }
  268. /** {@inheritDoc} */
  269. @Override
  270. public void windowClosing() {
  271. // 2: Remove any callbacks or listeners
  272. if (server != null && server.getParser() != null) {
  273. server.getParser().getCallbackManager().delAllCallback(this);
  274. }
  275. // 3: Trigger any actions neccessary
  276. // 4: Trigger action for the window closing
  277. ActionManager.getActionManager().triggerEvent(
  278. CoreActionType.QUERY_CLOSED, null, this);
  279. // 5: Inform any parents that the window is closing
  280. if (server != null) {
  281. server.delQuery(this);
  282. }
  283. }
  284. /** {@inheritDoc} */
  285. @Override
  286. public void windowClosed() {
  287. // 7: Remove any references to the window and parents
  288. server = null;
  289. }
  290. /**
  291. * Returns the host that this query is with.
  292. *
  293. * @return The full host that this query is with
  294. */
  295. public String getHost() {
  296. return host;
  297. }
  298. /**
  299. * Returns the current nickname of the user that this query is with.
  300. *
  301. * @return The nickname of this query's user
  302. */
  303. public String getNickname() {
  304. return nickname;
  305. }
  306. }