Client.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package server;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.ObjectOutputStream;
  8. import java.net.Socket;
  9. import java.time.LocalDateTime;
  10. import javax.swing.Timer;
  11. import common.Message;
  12. public class Client implements Runnable, ActionListener {
  13. Thread readuser;
  14. BufferedReader in;
  15. ObjectOutputStream objOut;
  16. public String username;
  17. public Socket sock;
  18. String[] permissions;
  19. int messLastPeriod = 0;
  20. Timer timer = new Timer(3000, this);
  21. public Channel primaryChannel = Server.channels.get(0);
  22. public Client(Socket s) {
  23. sock = s;
  24. try {
  25. in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  26. objOut = new ObjectOutputStream(sock.getOutputStream());
  27. username = in.readLine();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. if (!validateUser()) {
  32. disconnect(false);
  33. throw new IllegalArgumentException();
  34. }
  35. permissions = new String[] {"noob.*"};
  36. send(new Message("Welcome to the server! Enjoy your stay!"));
  37. readuser = new Thread(this, username);
  38. readuser.start();
  39. timer.start();
  40. }
  41. public Client() {}
  42. private boolean validateUser() {
  43. //No spaces
  44. if (username.contains(" ")) {
  45. send("No spaces in usernames please!");
  46. return false;
  47. }
  48. //Not same username as anyone else
  49. if (Server.clients.getClientByName(username).isPresent()) {
  50. send("Username already taken!");
  51. return false;
  52. }
  53. //No connect if banned
  54. for (BanNote note: Server.banNotes)
  55. if (note.ip.equals(sock.getInetAddress().toString())) {
  56. if (note.expiry == null) {
  57. send(note.toString());
  58. return false;
  59. } else if (note.expiry.isBefore(LocalDateTime.now())) {
  60. Server.banNotes.remove(note);
  61. return true;
  62. } else {
  63. send(note.toString());
  64. return false;
  65. }
  66. }
  67. return true;
  68. }
  69. public void disconnect(boolean output) {
  70. if (!isConnected()) //Already disconnected
  71. return;
  72. if (timer.isRunning())
  73. timer.stop();
  74. if (readuser != null)
  75. readuser.interrupt();
  76. try {
  77. sock.close();
  78. } catch (IOException e) {
  79. e.printStackTrace();
  80. }
  81. Server.cleanUp();
  82. if (output)
  83. Server.wideBroadcast(new Message(username + " has disconnected."));
  84. }
  85. public void disconnect() {
  86. disconnect(true);
  87. }
  88. public boolean isConnected() {
  89. return !sock.isClosed();
  90. }
  91. boolean hasPermission(String commandPermission) {
  92. for (int i = 0; i < permissions.length; i++) {
  93. if (commandPermission.startsWith(permissions[i].replace(".*", ".")) || commandPermission.equalsIgnoreCase(permissions[i]) || permissions[i].equalsIgnoreCase("*"))
  94. return true;
  95. }
  96. return false;
  97. }
  98. @Override
  99. public void run() {
  100. String lastMess;
  101. try {
  102. while (!readuser.isInterrupted() && ((lastMess = in.readLine()) != null)) {
  103. if (lastMess.startsWith("/")) //Command handling
  104. {
  105. String[] commandarray = lastMess.substring(1).split(" ");
  106. Server.commReg.executeCommand(commandarray, this);
  107. }
  108. else //Normal message handling
  109. {
  110. messLastPeriod++;
  111. if (messLastPeriod > 5) {
  112. send("No spamming!");
  113. disconnect(false);
  114. } else
  115. primaryChannel.broadcast(new Message(this.username, lastMess));
  116. }
  117. }
  118. disconnect();
  119. } catch (IOException e) {
  120. disconnect();
  121. }
  122. }
  123. public void send(Object message) {
  124. try {
  125. objOut.writeObject(message);
  126. objOut.flush();
  127. } catch (IOException e) {
  128. if (isConnected())
  129. disconnect();
  130. }
  131. }
  132. @Override
  133. public String toString() {
  134. return username;
  135. }
  136. @Override
  137. public void actionPerformed(ActionEvent e) {
  138. messLastPeriod = 0;
  139. }
  140. }