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.

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