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.

DCCChat.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.addons.dcc.io;
  18. import com.dmdirc.addons.dcc.DCCChatHandler;
  19. import java.io.BufferedReader;
  20. import java.io.IOException;
  21. import java.io.InputStreamReader;
  22. import java.io.PrintWriter;
  23. /**
  24. * This class handles a DCC Chat
  25. */
  26. public class DCCChat extends DCC {
  27. /** The handler for this DCCChat. */
  28. private DCCChatHandler handler = null;
  29. /** Used to send data out the socket. */
  30. private PrintWriter out;
  31. /** Used to read data from the socket. */
  32. private BufferedReader in;
  33. /** Are we active? */
  34. private boolean active = false;
  35. /**
  36. * Change the handler for this DCC Chat.
  37. *
  38. * @param handler A class implementing DCCChatHandler
  39. */
  40. public void setHandler(final DCCChatHandler handler) {
  41. this.handler = handler;
  42. }
  43. @Override
  44. protected void socketOpened() {
  45. active = true;
  46. try {
  47. out = new PrintWriter(socket.getOutputStream(), true);
  48. in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  49. if (handler != null) {
  50. handler.socketOpened(this);
  51. }
  52. } catch (IOException ioe) {
  53. socketClosed();
  54. }
  55. }
  56. @Override
  57. protected void socketClosed() {
  58. out = null;
  59. in = null;
  60. if (handler != null) {
  61. handler.socketClosed(this);
  62. }
  63. active = false;
  64. }
  65. @Override
  66. protected boolean handleSocket() {
  67. if (out == null || in == null) {
  68. return false;
  69. }
  70. final String inLine;
  71. try {
  72. inLine = in.readLine();
  73. if (inLine == null) {
  74. return false;
  75. } else {
  76. if (handler != null) {
  77. handler.handleChatMessage(this, inLine);
  78. }
  79. return true;
  80. }
  81. } catch (IOException e) {
  82. return false;
  83. }
  84. }
  85. @Override
  86. public boolean isWriteable() {
  87. return out != null;
  88. }
  89. /**
  90. * Send a line out the socket.
  91. *
  92. * @param line The line to be sent
  93. */
  94. public void sendLine(final String line) {
  95. if (out != null) {
  96. out.printf("%s\r\n", line);
  97. }
  98. }
  99. /**
  100. * Are we an active DCC Chat?
  101. *
  102. * @return true iif active
  103. */
  104. public boolean isActive() {
  105. return active;
  106. }
  107. }