選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Query.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright (c) 2006-2014 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.CommandType;
  26. import com.dmdirc.commandparser.parsers.QueryCommandParser;
  27. import com.dmdirc.interfaces.CommandController;
  28. import com.dmdirc.interfaces.Connection;
  29. import com.dmdirc.interfaces.actions.ActionType;
  30. import com.dmdirc.logger.ErrorLevel;
  31. import com.dmdirc.logger.Logger;
  32. import com.dmdirc.messages.MessageSinkManager;
  33. import com.dmdirc.parser.common.CallbackManager;
  34. import com.dmdirc.parser.common.CallbackNotFoundException;
  35. import com.dmdirc.parser.common.CompositionState;
  36. import com.dmdirc.parser.interfaces.ClientInfo;
  37. import com.dmdirc.parser.interfaces.Parser;
  38. import com.dmdirc.parser.interfaces.callbacks.CompositionStateChangeListener;
  39. import com.dmdirc.parser.interfaces.callbacks.NickChangeListener;
  40. import com.dmdirc.parser.interfaces.callbacks.PrivateActionListener;
  41. import com.dmdirc.parser.interfaces.callbacks.PrivateMessageListener;
  42. import com.dmdirc.parser.interfaces.callbacks.QuitListener;
  43. import com.dmdirc.ui.core.components.WindowComponent;
  44. import com.dmdirc.ui.input.TabCompleter;
  45. import com.dmdirc.ui.input.TabCompleterFactory;
  46. import com.dmdirc.util.URLBuilder;
  47. import com.dmdirc.util.annotations.factory.Factory;
  48. import com.dmdirc.util.annotations.factory.Unbound;
  49. import java.awt.Toolkit;
  50. import java.util.ArrayList;
  51. import java.util.Arrays;
  52. import java.util.Date;
  53. import java.util.List;
  54. /**
  55. * The Query class represents the client's view of a query with another user. It handles callbacks
  56. * for query events from the parser, maintains the corresponding QueryWindow, and handles user input
  57. * for the query.
  58. */
  59. @Factory(inject = true, providers = true, singleton = true)
  60. public class Query extends MessageTarget implements PrivateActionListener,
  61. PrivateMessageListener, NickChangeListener, QuitListener,
  62. CompositionStateChangeListener {
  63. /** The Server this Query is on. */
  64. private final Server server;
  65. /** The full host of the client associated with this query. */
  66. private String host;
  67. /** The nickname of the client associated with this query. */
  68. private String nickname;
  69. /** The tab completer for the query window. */
  70. private final TabCompleter tabCompleter;
  71. /**
  72. * Creates a new instance of Query.
  73. *
  74. * @param newHost host of the remove client
  75. * @param newServer The server object that this Query belongs to
  76. * @param tabCompleterFactory The factory to use to create tab completers.
  77. * @param commandController The controller to load commands from.
  78. * @param messageSinkManager The sink manager to use to despatch messages.
  79. * @param urlBuilder The URL builder to use when finding icons.
  80. */
  81. public Query(
  82. @Unbound final Server newServer,
  83. @Unbound final String newHost,
  84. final TabCompleterFactory tabCompleterFactory,
  85. final CommandController commandController,
  86. final MessageSinkManager messageSinkManager,
  87. final URLBuilder urlBuilder) {
  88. super("query", newServer.parseHostmask(newHost)[0],
  89. newServer.parseHostmask(newHost)[0],
  90. newServer.getConfigManager(), new QueryCommandParser(newServer, commandController),
  91. messageSinkManager,
  92. urlBuilder,
  93. Arrays.asList(
  94. WindowComponent.TEXTAREA.getIdentifier(),
  95. WindowComponent.INPUTFIELD.getIdentifier()));
  96. this.server = newServer;
  97. this.host = newHost;
  98. this.nickname = server.parseHostmask(host)[0];
  99. tabCompleter = tabCompleterFactory.getTabCompleter(server.getTabCompleter(),
  100. getConfigManager(), CommandType.TYPE_QUERY, CommandType.TYPE_CHAT);
  101. if (!server.getState().isDisconnected()) {
  102. reregister();
  103. }
  104. updateTitle();
  105. }
  106. @Override
  107. public TabCompleter getTabCompleter() {
  108. return tabCompleter;
  109. }
  110. @Override
  111. public void sendLine(final String line) {
  112. sendLine(line, getNickname());
  113. }
  114. /**
  115. * Sends a line to the recipient of this query using the specified nickname or hostmask as a
  116. * target. This allows for users to send messages with a server specified (e.g.
  117. * <code>nick@server</code>) as though the query wasn't open.
  118. * <p>
  119. * The caller is responsible for ensuring that the <code>target</code> does actually correspond
  120. * to this query.
  121. *
  122. * @since 0.6.4
  123. * @param line The line to be sent
  124. * @param target The target to send the line to
  125. */
  126. public void sendLine(final String line, final String target) {
  127. if (server.getState() != ServerState.CONNECTED) {
  128. Toolkit.getDefaultToolkit().beep();
  129. return;
  130. }
  131. for (String part : splitLine(line)) {
  132. if (!part.isEmpty()) {
  133. server.getParser().sendMessage(target, part);
  134. doNotification("querySelfMessage",
  135. CoreActionType.QUERY_SELF_MESSAGE,
  136. server.getParser().getLocalClient(), part);
  137. triggerAction("querySelfMessage", CoreActionType.QUERY_SELF_MESSAGE, server.
  138. getParser().getLocalClient(), part);
  139. }
  140. }
  141. }
  142. @Override
  143. protected boolean processNotificationArg(final Object arg, final List<Object> args) {
  144. if (arg instanceof ClientInfo) {
  145. final ClientInfo clientInfo = (ClientInfo) arg;
  146. args.add(clientInfo.getNickname());
  147. args.add(clientInfo.getUsername());
  148. args.add(clientInfo.getHostname());
  149. return true;
  150. } else {
  151. return super.processNotificationArg(arg, args);
  152. }
  153. }
  154. @Override
  155. public int getMaxLineLength() {
  156. return server.getState() == ServerState.CONNECTED ? server.getParser()
  157. .getMaxLength("PRIVMSG", host) : -1;
  158. }
  159. /**
  160. * Sends a private action to the remote user.
  161. *
  162. * @param action action text to send
  163. */
  164. @Override
  165. public void sendAction(final String action) {
  166. if (server.getState() != ServerState.CONNECTED) {
  167. Toolkit.getDefaultToolkit().beep();
  168. return;
  169. }
  170. final ClientInfo client = server.getParser().getLocalClient();
  171. final int maxLineLength = server.getParser().getMaxLength("PRIVMSG", host);
  172. if (maxLineLength >= action.length() + 2) {
  173. server.getParser().sendAction(getNickname(), action);
  174. doNotification("querySelfAction", CoreActionType.QUERY_SELF_ACTION,
  175. client, action);
  176. triggerAction("querySelfAction", CoreActionType.QUERY_SELF_ACTION, client, action);
  177. } else {
  178. addLine("actionTooLong", action.length());
  179. }
  180. }
  181. @Override
  182. public void onPrivateMessage(final Parser parser, final Date date,
  183. final String message, final String host) {
  184. final String[] parts = server.parseHostmask(host);
  185. final StringBuffer buff = new StringBuffer("queryMessage");
  186. ActionManager.getActionManager().triggerEvent(
  187. CoreActionType.QUERY_MESSAGE, buff, this,
  188. parser.getClient(host), message);
  189. addLine(buff, parts[0], parts[1], parts[2], message);
  190. }
  191. @Override
  192. public void onPrivateAction(final Parser parser, final Date date,
  193. final String message, final String host) {
  194. final String[] parts = server.parseHostmask(host);
  195. final StringBuffer buff = new StringBuffer("queryAction");
  196. ActionManager.getActionManager().triggerEvent(
  197. CoreActionType.QUERY_ACTION, buff, this, parser.getClient(host),
  198. message);
  199. addLine(buff, parts[0], parts[1], parts[2], message);
  200. }
  201. /**
  202. * Updates the QueryWindow's title.
  203. */
  204. private void updateTitle() {
  205. setTitle(getNickname());
  206. }
  207. /**
  208. * Reregisters query callbacks. Called when reconnecting to the server.
  209. */
  210. public void reregister() {
  211. final CallbackManager callbackManager = server.getParser().getCallbackManager();
  212. final String nick = getNickname();
  213. try {
  214. callbackManager.addCallback(PrivateActionListener.class, this, nick);
  215. callbackManager.addCallback(PrivateMessageListener.class, this, nick);
  216. callbackManager.addCallback(CompositionStateChangeListener.class, this, nick);
  217. callbackManager.addCallback(QuitListener.class, this);
  218. callbackManager.addCallback(NickChangeListener.class, this);
  219. } catch (CallbackNotFoundException ex) {
  220. Logger.appError(ErrorLevel.HIGH, "Unable to get query events", ex);
  221. }
  222. }
  223. @Override
  224. public void onNickChanged(final Parser parser, final Date date,
  225. final ClientInfo client, final String oldNick) {
  226. if (oldNick.equals(getNickname())) {
  227. final CallbackManager callbackManager = server.getParser().getCallbackManager();
  228. callbackManager.delCallback(PrivateActionListener.class, this);
  229. callbackManager.delCallback(PrivateMessageListener.class, this);
  230. callbackManager.delCallback(CompositionStateChangeListener.class, this);
  231. try {
  232. callbackManager.addCallback(PrivateActionListener.class, this, client.getNickname());
  233. callbackManager.
  234. addCallback(PrivateMessageListener.class, this, client.getNickname());
  235. callbackManager.addCallback(CompositionStateChangeListener.class, this, client.
  236. getNickname());
  237. } catch (CallbackNotFoundException ex) {
  238. Logger.appError(ErrorLevel.HIGH, "Unable to get query events", ex);
  239. }
  240. final StringBuffer format = new StringBuffer("queryNickChanged");
  241. ActionManager.getActionManager().triggerEvent(
  242. CoreActionType.QUERY_NICKCHANGE, format, this, oldNick);
  243. server.updateQuery(this, oldNick, client.getNickname());
  244. addLine(format, oldNick, client.getUsername(),
  245. client.getHostname(), client.getNickname());
  246. host = client.getNickname() + "!" + client.getUsername() + "@" + client.getHostname();
  247. nickname = client.getNickname();
  248. updateTitle();
  249. setName(client.getNickname());
  250. }
  251. }
  252. @Override
  253. public void onQuit(final Parser parser, final Date date,
  254. final ClientInfo client, final String reason) {
  255. if (client.getNickname().equals(getNickname())) {
  256. final StringBuffer format = new StringBuffer(reason.isEmpty()
  257. ? "queryQuit" : "queryQuitReason");
  258. ActionManager.getActionManager().triggerEvent(
  259. CoreActionType.QUERY_QUIT, format, this, reason);
  260. addLine(format, client.getNickname(),
  261. client.getUsername(), client.getHostname(), reason);
  262. }
  263. }
  264. @Override
  265. public void onCompositionStateCanged(final Parser parser, final Date date,
  266. final CompositionState state, final String host) {
  267. if (state == CompositionState.TYPING) {
  268. addComponent(WindowComponent.TYPING_INDICATOR.getIdentifier());
  269. } else {
  270. removeComponent(WindowComponent.TYPING_INDICATOR.getIdentifier());
  271. }
  272. }
  273. @Override
  274. public Connection getConnection() {
  275. return server;
  276. }
  277. @Override
  278. public void close() {
  279. super.close();
  280. // Remove any callbacks or listeners
  281. if (server != null && server.getParser() != null) {
  282. server.getParser().getCallbackManager().delAllCallback(this);
  283. }
  284. // Trigger action for the window closing
  285. ActionManager.getActionManager().triggerEvent(
  286. CoreActionType.QUERY_CLOSED, null, this);
  287. // Inform any parents that the window is closing
  288. if (server != null) {
  289. server.delQuery(this);
  290. }
  291. }
  292. /**
  293. * Returns the host that this query is with.
  294. *
  295. * @return The full host that this query is with
  296. */
  297. public String getHost() {
  298. return host;
  299. }
  300. /**
  301. * Returns the current nickname of the user that this query is with.
  302. *
  303. * @return The nickname of this query's user
  304. */
  305. public String getNickname() {
  306. return nickname;
  307. }
  308. @Override
  309. public void setCompositionState(final CompositionState state) {
  310. getConnection().getParser().setCompositionState(host, state);
  311. }
  312. private boolean triggerAction(final String messageType, final ActionType actionType,
  313. final Object... args) {
  314. final StringBuffer buffer = new StringBuffer(messageType);
  315. final List<Object> actionArgs = new ArrayList<>();
  316. actionArgs.add(this);
  317. actionArgs.addAll(Arrays.asList(args));
  318. return ActionManager.getActionManager().triggerEvent(actionType, buffer, actionArgs.
  319. toArray());
  320. }
  321. }