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.7KB

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