Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DCCChat.java 3.5KB

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