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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc;
  18. import com.dmdirc.events.CommandErrorEvent;
  19. import com.dmdirc.events.QueryActionEvent;
  20. import com.dmdirc.events.QueryClosedEvent;
  21. import com.dmdirc.events.QueryMessageEvent;
  22. import com.dmdirc.events.QueryNickChangeEvent;
  23. import com.dmdirc.events.QueryQuitEvent;
  24. import com.dmdirc.events.QuerySelfActionEvent;
  25. import com.dmdirc.events.QuerySelfMessageEvent;
  26. import com.dmdirc.interfaces.Connection;
  27. import com.dmdirc.interfaces.PrivateChat;
  28. import com.dmdirc.interfaces.User;
  29. import com.dmdirc.interfaces.WindowModel;
  30. import com.dmdirc.parser.common.CallbackManager;
  31. import com.dmdirc.parser.common.CompositionState;
  32. import com.dmdirc.parser.events.CompositionStateChangeEvent;
  33. import com.dmdirc.parser.events.NickChangeEvent;
  34. import com.dmdirc.parser.events.PrivateActionEvent;
  35. import com.dmdirc.parser.events.PrivateMessageEvent;
  36. import com.dmdirc.parser.events.QuitEvent;
  37. import com.dmdirc.parser.interfaces.ClientInfo;
  38. import com.dmdirc.parser.interfaces.Parser;
  39. import com.dmdirc.ui.core.components.WindowComponent;
  40. import com.dmdirc.ui.messages.BackBufferFactory;
  41. import java.awt.Toolkit;
  42. import java.util.Arrays;
  43. import java.util.Optional;
  44. import net.engio.mbassy.listener.Handler;
  45. /**
  46. * The Query class represents the client's view of a query with another user. It handles callbacks
  47. * for query events from the parser, maintains the corresponding QueryWindow, and handles user input
  48. * for the query.
  49. */
  50. public class Query extends FrameContainer implements PrivateChat {
  51. /** The connection this Query is on. */
  52. private final Connection connection;
  53. /** The user associated with this query. */
  54. private final User user;
  55. public Query(
  56. final Connection connection,
  57. final User user,
  58. final BackBufferFactory backBufferFactory) {
  59. super("query",
  60. user.getNickname(),
  61. user.getNickname(),
  62. connection.getWindowModel().getConfigManager(),
  63. backBufferFactory,
  64. connection.getWindowModel().getEventBus(),
  65. Arrays.asList(
  66. WindowComponent.TEXTAREA.getIdentifier(),
  67. WindowComponent.INPUTFIELD.getIdentifier()));
  68. this.connection = connection;
  69. this.user = user;
  70. updateTitle();
  71. initBackBuffer();
  72. }
  73. @Override
  74. public void sendLine(final String line) {
  75. sendLine(line, getNickname());
  76. }
  77. @Override
  78. public void sendLine(final String line, final String target) {
  79. if (connection.getState() != ServerState.CONNECTED) {
  80. Toolkit.getDefaultToolkit().beep();
  81. return;
  82. }
  83. getInputModel().get()
  84. .splitLine(line)
  85. .stream()
  86. .filter(part -> !part.isEmpty())
  87. .forEach(part -> {
  88. connection.getParser().get().sendMessage(target, part);
  89. getEventBus().publishAsync(new QuerySelfMessageEvent(this,
  90. connection.getLocalUser().get(), part));
  91. });
  92. }
  93. @Override
  94. public int getMaxLineLength() {
  95. // TODO: The parser layer should abstract this
  96. return connection.getState() == ServerState.CONNECTED ? connection.getParser().get()
  97. .getMaxLength("PRIVMSG", user.getNickname()
  98. + '!' + user.getUsername().orElse("")
  99. + '@' + user.getHostname().orElse("")) : -1;
  100. }
  101. @Override
  102. public void sendAction(final String action) {
  103. if (connection.getState() != ServerState.CONNECTED) {
  104. Toolkit.getDefaultToolkit().beep();
  105. return;
  106. }
  107. final int maxLineLength = getInputModel().get().getMaxLineLength();
  108. if (maxLineLength >= action.length() + 2) {
  109. connection.getParser().get().sendAction(getNickname(), action);
  110. getEventBus().publishAsync(
  111. new QuerySelfActionEvent(this, connection.getLocalUser().get(), action));
  112. } else {
  113. getEventBus().publishAsync(
  114. new CommandErrorEvent(this, "Warning: action too long to be sent"));
  115. }
  116. }
  117. @Handler
  118. public void onPrivateMessage(final PrivateMessageEvent event) {
  119. if (!checkQuery(event.getHost())) {
  120. return;
  121. }
  122. getEventBus().publishAsync(
  123. new QueryMessageEvent(this, connection.getUser(event.getHost()), event.getMessage()));
  124. }
  125. @Handler
  126. public void onPrivateAction(final PrivateActionEvent event) {
  127. if (!checkQuery(event.getHost())) {
  128. return;
  129. }
  130. getEventBus().publishAsync(
  131. new QueryActionEvent(this, connection.getUser(event.getHost()), event.getMessage()));
  132. }
  133. /**
  134. * Updates the QueryWindow's title.
  135. */
  136. private void updateTitle() {
  137. setTitle(getNickname());
  138. }
  139. /**
  140. * Reregisters query callbacks. Called when reconnecting to the server.
  141. */
  142. public void reregister() {
  143. final CallbackManager callbackManager = connection.getParser().get().getCallbackManager();
  144. callbackManager.subscribe(this);
  145. }
  146. @Handler
  147. public void onNickChanged(final NickChangeEvent event) {
  148. if (!checkQuery(event.getClient().getHostname())) {
  149. return;
  150. }
  151. final ClientInfo client = event.getClient();
  152. final String oldNick = event.getOldNick();
  153. if (client.getNickname().equals(getNickname())) {
  154. getEventBus().publish(new QueryNickChangeEvent(this, oldNick, client.getNickname()));
  155. updateTitle();
  156. setName(client.getNickname());
  157. }
  158. }
  159. @Handler
  160. public void onQuit(final QuitEvent event) {
  161. if (!checkQuery(event.getClient().getHostname())) {
  162. return;
  163. }
  164. if (event.getClient().getNickname().equals(getNickname())) {
  165. getEventBus().publish(new QueryQuitEvent(this, event.getReason()));
  166. }
  167. }
  168. @Handler
  169. public void onCompositionStateChanged(final CompositionStateChangeEvent event) {
  170. if (!checkQuery(event.getHost())) {
  171. return;
  172. }
  173. if (event.getState() == CompositionState.TYPING) {
  174. addComponent(WindowComponent.TYPING_INDICATOR.getIdentifier());
  175. } else {
  176. removeComponent(WindowComponent.TYPING_INDICATOR.getIdentifier());
  177. }
  178. }
  179. @Override
  180. public Optional<Connection> getConnection() {
  181. return Optional.of(connection);
  182. }
  183. @Override
  184. public void close() {
  185. super.close();
  186. // Remove any callbacks or listeners
  187. connection.getParser().map(Parser::getCallbackManager).ifPresent(cm -> cm.unsubscribe(this));
  188. // Trigger action for the window closing
  189. getEventBus().publishAsync(new QueryClosedEvent(this));
  190. // Inform any parents that the window is closing
  191. connection.delQuery(this);
  192. }
  193. @Override
  194. public String getNickname() {
  195. return user.getNickname();
  196. }
  197. @Override
  198. public void setCompositionState(final CompositionState state) {
  199. connection.getParser().get().setCompositionState(user.getNickname(), state);
  200. }
  201. @Override
  202. public WindowModel getWindowModel() {
  203. return this;
  204. }
  205. @Override
  206. public User getUser() {
  207. return user;
  208. }
  209. private boolean checkQuery(final String host) {
  210. return connection.getUser(host).getNickname().equals(user.getNickname());
  211. }
  212. }