| 1 | package net.minecraft.network.chat;
|
| 2 |
|
| 3 | import com.google.common.primitives.Ints;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.security.SignatureException;
|
| 7 | import java.time.Duration;
|
| 8 | import java.time.Instant;
|
| 9 | import java.util.Objects;
|
| 10 | import java.util.Optional;
|
| 11 | import java.util.UUID;
|
| 12 | import java.util.stream.Collectors;
|
| 13 | import net.minecraft.util.SignatureUpdater;
|
| 14 | import net.minecraft.util.SignatureValidator;
|
| 15 | import net.minecraft.util.Util;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | public record PlayerChatMessage(
|
| 19 | SignedMessageLink link, @Nullable MessageSignature signature, SignedMessageBody signedBody, @Nullable Component unsignedContent, FilterMask filterMask
|
| 20 | ) {
|
| 21 | public static final MapCodec<PlayerChatMessage> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 22 | i -> i.group(
|
| 23 | SignedMessageLink.CODEC.fieldOf("link").forGetter(PlayerChatMessage::link),
|
| 24 | MessageSignature.CODEC.optionalFieldOf("signature").forGetter(playerChatMessage -> Optional.ofNullable(playerChatMessage.signature)),
|
| 25 | SignedMessageBody.MAP_CODEC.forGetter(PlayerChatMessage::signedBody),
|
| 26 | ComponentSerialization.CODEC.optionalFieldOf("unsigned_content").forGetter(playerChatMessage -> Optional.ofNullable(playerChatMessage.unsignedContent)),
|
| 27 | FilterMask.CODEC.optionalFieldOf("filter_mask", FilterMask.PASS_THROUGH).forGetter(PlayerChatMessage::filterMask)
|
| 28 | )
|
| 29 | .apply(
|
| 30 | i,
|
| 31 | (link, signature, signedBody, unsignedContent, filterMask) -> new PlayerChatMessage(
|
| 32 | link, signature.orElse(null), signedBody, unsignedContent.orElse(null), filterMask
|
| 33 | )
|
| 34 | )
|
| 35 | );
|
| 36 | private static final UUID SYSTEM_SENDER = Util.NIL_UUID;
|
| 37 | public static final Duration MESSAGE_EXPIRES_AFTER_SERVER = Duration.ofMinutes(5L);
|
| 38 | public static final Duration MESSAGE_EXPIRES_AFTER_CLIENT = MESSAGE_EXPIRES_AFTER_SERVER.plus(Duration.ofMinutes(2L));
|
| 39 |
|
| 40 | public static PlayerChatMessage system(final String content) {
|
| 41 | return unsigned(SYSTEM_SENDER, content);
|
| 42 | }
|
| 43 |
|
| 44 | public static PlayerChatMessage unsigned(final UUID sender, final String content) {
|
| 45 | SignedMessageBody body = SignedMessageBody.unsigned(content);
|
| 46 | SignedMessageLink link = SignedMessageLink.unsigned(sender);
|
| 47 | return new PlayerChatMessage(link, null, body, null, FilterMask.PASS_THROUGH);
|
| 48 | }
|
| 49 |
|
| 50 | public PlayerChatMessage withUnsignedContent(final Component content) {
|
| 51 | Component unsignedContent = !content.equals(Component.literal(this.signedContent())) ? content : null;
|
| 52 | return new PlayerChatMessage(this.link, this.signature, this.signedBody, unsignedContent, this.filterMask);
|
| 53 | }
|
| 54 |
|
| 55 | public PlayerChatMessage removeUnsignedContent() {
|
| 56 | return this.unsignedContent != null ? new PlayerChatMessage(this.link, this.signature, this.signedBody, null, this.filterMask) : this;
|
| 57 | }
|
| 58 |
|
| 59 | public PlayerChatMessage filter(final FilterMask filterMask) {
|
| 60 | return this.filterMask.equals(filterMask) ? this : new PlayerChatMessage(this.link, this.signature, this.signedBody, this.unsignedContent, filterMask);
|
| 61 | }
|
| 62 |
|
| 63 | public PlayerChatMessage filter(final boolean filtered) {
|
| 64 | return this.filter(filtered ? this.filterMask : FilterMask.PASS_THROUGH);
|
| 65 | }
|
| 66 |
|
| 67 | public PlayerChatMessage removeSignature() {
|
| 68 | SignedMessageBody body = SignedMessageBody.unsigned(this.signedContent());
|
| 69 | SignedMessageLink link = SignedMessageLink.unsigned(this.sender());
|
| 70 | return new PlayerChatMessage(link, null, body, this.unsignedContent, this.filterMask);
|
| 71 | }
|
| 72 |
|
| 73 | public static void updateSignature(final SignatureUpdater.Output output, final SignedMessageLink link, final SignedMessageBody body) throws SignatureException {
|
| 74 | output.update(Ints.toByteArray(1));
|
| 75 | link.updateSignature(output);
|
| 76 | body.updateSignature(output);
|
| 77 | }
|
| 78 |
|
| 79 | public boolean verify(final SignatureValidator signatureValidator) {
|
| 80 | return this.signature != null && this.signature.verify(signatureValidator, output -> updateSignature(output, this.link, this.signedBody));
|
| 81 | }
|
| 82 |
|
| 83 | public String signedContent() {
|
| 84 | return this.signedBody.content();
|
| 85 | }
|
| 86 |
|
| 87 | public Component decoratedContent() {
|
| 88 | return Objects.requireNonNullElseGet(this.unsignedContent, () -> Component.literal(this.signedContent()));
|
| 89 | }
|
| 90 |
|
| 91 | public Instant timeStamp() {
|
| 92 | return this.signedBody.timeStamp();
|
| 93 | }
|
| 94 |
|
| 95 | public long salt() {
|
| 96 | return this.signedBody.salt();
|
| 97 | }
|
| 98 |
|
| 99 | public boolean hasExpiredServer(final Instant now) {
|
| 100 | return now.isAfter(this.timeStamp().plus(MESSAGE_EXPIRES_AFTER_SERVER));
|
| 101 | }
|
| 102 |
|
| 103 | public boolean hasExpiredClient(final Instant now) {
|
| 104 | return now.isAfter(this.timeStamp().plus(MESSAGE_EXPIRES_AFTER_CLIENT));
|
| 105 | }
|
| 106 |
|
| 107 | public UUID sender() {
|
| 108 | return this.link.sender();
|
| 109 | }
|
| 110 |
|
| 111 | public boolean isSystem() {
|
| 112 | return this.sender().equals(SYSTEM_SENDER);
|
| 113 | }
|
| 114 |
|
| 115 | public boolean hasSignature() {
|
| 116 | return this.signature != null;
|
| 117 | }
|
| 118 |
|
| 119 | public boolean hasSignatureFrom(final UUID profileId) {
|
| 120 | return this.hasSignature() && this.link.sender().equals(profileId);
|
| 121 | }
|
| 122 |
|
| 123 | public boolean isFullyFiltered() {
|
| 124 | return this.filterMask.isFullyFiltered();
|
| 125 | }
|
| 126 |
|
| 127 | public static String describeSigned(final PlayerChatMessage message) {
|
| 128 | return "'"
|
| 129 | + message.signedBody.content()
|
| 130 | + "' @ "
|
| 131 | + message.signedBody.timeStamp()
|
| 132 | + "\n - From: "
|
| 133 | + message.link.sender()
|
| 134 | + "/"
|
| 135 | + message.link.sessionId()
|
| 136 | + ", message #"
|
| 137 | + message.link.index()
|
| 138 | + "\n - Salt: "
|
| 139 | + message.signedBody.salt()
|
| 140 | + "\n - Signature: "
|
| 141 | + MessageSignature.describe(message.signature)
|
| 142 | + "\n - Last Seen: [\n"
|
| 143 | + message.signedBody.lastSeen().entries().stream().map(signature -> " " + MessageSignature.describe(signature) + "\n").collect(Collectors.joining())
|
| 144 | + " ]\n";
|
| 145 | }
|
| 146 | }
|
| 147 |
|