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

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