Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Client.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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;
  18. import com.dmdirc.interfaces.Connection;
  19. import com.dmdirc.interfaces.GroupChat;
  20. import com.dmdirc.interfaces.User;
  21. import com.dmdirc.parser.common.AwayState;
  22. import com.dmdirc.parser.interfaces.ClientInfo;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.Collections;
  26. import java.util.Optional;
  27. /**
  28. * Implementation of a {@link User}.
  29. */
  30. public class Client implements User {
  31. private final Connection connection;
  32. private final ClientInfo clientInfo;
  33. public Client(final Connection connection, final ClientInfo clientInfo) {
  34. this.connection = connection;
  35. this.clientInfo = clientInfo;
  36. }
  37. @Override
  38. public String getNickname() {
  39. return clientInfo.getNickname();
  40. }
  41. @Override
  42. public Optional<String> getUsername() {
  43. if (clientInfo.getUsername().isEmpty()) {
  44. return Optional.empty();
  45. } else {
  46. return Optional.of(clientInfo.getUsername());
  47. }
  48. }
  49. @Override
  50. public Optional<String> getHostname() {
  51. if (clientInfo.getHostname().isEmpty()) {
  52. return Optional.empty();
  53. } else {
  54. return Optional.of(clientInfo.getHostname());
  55. }
  56. }
  57. @Override
  58. public Optional<String> getRealname() {
  59. if (clientInfo.getRealname().isEmpty()) {
  60. return Optional.empty();
  61. } else {
  62. return Optional.of(clientInfo.getRealname());
  63. }
  64. }
  65. @Override
  66. public Collection<GroupChat> getGroupChats() {
  67. final Collection<GroupChat> channels = new ArrayList<>();
  68. clientInfo.getChannelClients().forEach(c -> connection.getGroupChatManager()
  69. .getChannel(c.getChannel().getName()).ifPresent(channels::add));
  70. return Collections.unmodifiableCollection(channels);
  71. }
  72. @Override
  73. public Optional<String> getAwayMessage() {
  74. if (clientInfo.getAwayReason().isEmpty()) {
  75. return Optional.empty();
  76. } else {
  77. return Optional.of(clientInfo.getAwayReason());
  78. }
  79. }
  80. @Override
  81. public AwayState getAwayState() {
  82. return clientInfo.getAwayState();
  83. }
  84. @Override
  85. public Connection getConnection() {
  86. return connection;
  87. }
  88. public ClientInfo getClientInfo() {
  89. return clientInfo;
  90. }
  91. }