Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2006-2015 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.events.CommandErrorEvent;
  24. import com.dmdirc.events.QueryActionEvent;
  25. import com.dmdirc.events.QueryClosedEvent;
  26. import com.dmdirc.events.QueryMessageEvent;
  27. import com.dmdirc.events.QueryNickChangeEvent;
  28. import com.dmdirc.events.QueryQuitEvent;
  29. import com.dmdirc.events.QuerySelfActionEvent;
  30. import com.dmdirc.events.QuerySelfMessageEvent;
  31. import com.dmdirc.interfaces.Connection;
  32. import com.dmdirc.interfaces.PrivateChat;
  33. import com.dmdirc.interfaces.User;
  34. import com.dmdirc.interfaces.WindowModel;
  35. import com.dmdirc.parser.common.CallbackManager;
  36. import com.dmdirc.parser.common.CompositionState;
  37. import com.dmdirc.parser.events.CompositionStateChangeEvent;
  38. import com.dmdirc.parser.events.NickChangeEvent;
  39. import com.dmdirc.parser.events.PrivateActionEvent;
  40. import com.dmdirc.parser.events.PrivateMessageEvent;
  41. import com.dmdirc.parser.events.QuitEvent;
  42. import com.dmdirc.parser.interfaces.ClientInfo;
  43. import com.dmdirc.parser.interfaces.Parser;
  44. import com.dmdirc.ui.core.components.WindowComponent;
  45. import com.dmdirc.ui.messages.BackBufferFactory;
  46. import net.engio.mbassy.listener.Handler;
  47. import java.awt.*;
  48. import java.util.Arrays;
  49. import java.util.Optional;
  50. /**
  51. * The Query class represents the client's view of a query with another user. It handles callbacks
  52. * for query events from the parser, maintains the corresponding QueryWindow, and handles user input
  53. * for the query.
  54. */
  55. public class Query extends FrameContainer implements PrivateChat {
  56. /** The connection this Query is on. */
  57. private final Connection connection;
  58. /** The user associated with this query. */
  59. private final User user;
  60. public Query(
  61. final Connection connection,
  62. final User user,
  63. final BackBufferFactory backBufferFactory) {
  64. super("query",
  65. user.getNickname(),
  66. user.getNickname(),
  67. connection.getWindowModel().getConfigManager(),
  68. backBufferFactory,
  69. connection.getWindowModel().getEventBus(),
  70. Arrays.asList(
  71. WindowComponent.TEXTAREA.getIdentifier(),
  72. WindowComponent.INPUTFIELD.getIdentifier()));
  73. this.connection = connection;
  74. this.user = user;
  75. updateTitle();
  76. initBackBuffer();
  77. }
  78. @Override
  79. public void sendLine(final String line) {
  80. sendLine(line, getNickname());
  81. }
  82. @Override
  83. public void sendLine(final String line, final String target) {
  84. if (connection.getState() != ServerState.CONNECTED) {
  85. Toolkit.getDefaultToolkit().beep();
  86. return;
  87. }
  88. getInputModel().get()
  89. .splitLine(line)
  90. .stream()
  91. .filter(part -> !part.isEmpty())
  92. .forEach(part -> {
  93. connection.getParser().get().sendMessage(target, part);
  94. getEventBus().publishAsync(new QuerySelfMessageEvent(this,
  95. connection.getLocalUser().get(), part));
  96. });
  97. }
  98. @Override
  99. public int getMaxLineLength() {
  100. // TODO: The parser layer should abstract this
  101. return connection.getState() == ServerState.CONNECTED ? connection.getParser().get()
  102. .getMaxLength("PRIVMSG", user.getNickname()
  103. + '!' + user.getUsername().orElse("")
  104. + '@' + user.getHostname().orElse("")) : -1;
  105. }
  106. @Override
  107. public void sendAction(final String action) {
  108. if (connection.getState() != ServerState.CONNECTED) {
  109. Toolkit.getDefaultToolkit().beep();
  110. return;
  111. }
  112. final int maxLineLength = getInputModel().get().getMaxLineLength();
  113. if (maxLineLength >= action.length() + 2) {
  114. connection.getParser().get().sendAction(getNickname(), action);
  115. getEventBus().publishAsync(
  116. new QuerySelfActionEvent(this, connection.getLocalUser().get(), action));
  117. } else {
  118. getEventBus().publishAsync(
  119. new CommandErrorEvent(this, "Warning: action too long to be sent"));
  120. }
  121. }
  122. @Handler
  123. public void onPrivateMessage(final PrivateMessageEvent event) {
  124. if (!checkQuery(event.getHost())) {
  125. return;
  126. }
  127. getEventBus().publishAsync(
  128. new QueryMessageEvent(this, connection.getUser(event.getHost()), event.getMessage()));
  129. }
  130. @Handler
  131. public void onPrivateAction(final PrivateActionEvent event) {
  132. if (!checkQuery(event.getHost())) {
  133. return;
  134. }
  135. getEventBus().publishAsync(
  136. new QueryActionEvent(this, connection.getUser(event.getHost()), event.getMessage()));
  137. }
  138. /**
  139. * Updates the QueryWindow's title.
  140. */
  141. private void updateTitle() {
  142. setTitle(getNickname());
  143. }
  144. /**
  145. * Reregisters query callbacks. Called when reconnecting to the server.
  146. */
  147. public void reregister() {
  148. final CallbackManager callbackManager = connection.getParser().get().getCallbackManager();
  149. callbackManager.subscribe(this);
  150. }
  151. @Handler
  152. public void onNickChanged(final NickChangeEvent event) {
  153. if (!checkQuery(event.getClient().getHostname())) {
  154. return;
  155. }
  156. final ClientInfo client = event.getClient();
  157. final String oldNick = event.getOldNick();
  158. if (client.getNickname().equals(getNickname())) {
  159. getEventBus().publish(new QueryNickChangeEvent(this, oldNick, client.getNickname()));
  160. updateTitle();
  161. setName(client.getNickname());
  162. }
  163. }
  164. @Handler
  165. public void onQuit(final QuitEvent event) {
  166. if (!checkQuery(event.getClient().getHostname())) {
  167. return;
  168. }
  169. if (event.getClient().getNickname().equals(getNickname())) {
  170. getEventBus().publish(new QueryQuitEvent(this, event.getReason()));
  171. }
  172. }
  173. @Handler
  174. public void onCompositionStateChanged(final CompositionStateChangeEvent event) {
  175. if (!checkQuery(event.getHost())) {
  176. return;
  177. }
  178. if (event.getState() == CompositionState.TYPING) {
  179. addComponent(WindowComponent.TYPING_INDICATOR.getIdentifier());
  180. } else {
  181. removeComponent(WindowComponent.TYPING_INDICATOR.getIdentifier());
  182. }
  183. }
  184. @Override
  185. public Optional<Connection> getConnection() {
  186. return Optional.of(connection);
  187. }
  188. @Override
  189. public void close() {
  190. super.close();
  191. // Remove any callbacks or listeners
  192. connection.getParser().map(Parser::getCallbackManager).ifPresent(cm -> cm.unsubscribe(this));
  193. // Trigger action for the window closing
  194. getEventBus().publishAsync(new QueryClosedEvent(this));
  195. // Inform any parents that the window is closing
  196. connection.delQuery(this);
  197. }
  198. @Override
  199. public String getNickname() {
  200. return user.getNickname();
  201. }
  202. @Override
  203. public void setCompositionState(final CompositionState state) {
  204. connection.getParser().get().setCompositionState(user.getNickname(), state);
  205. }
  206. @Override
  207. public WindowModel getWindowModel() {
  208. return this;
  209. }
  210. @Override
  211. public User getUser() {
  212. return user;
  213. }
  214. private boolean checkQuery(final String host) {
  215. return connection.getUser(host).getNickname().equals(user.getNickname());
  216. }
  217. }