|
| 1 | +package net.discordjug.javabot.listener.filter; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.URI; |
| 5 | +import java.net.http.HttpClient; |
| 6 | +import java.net.http.HttpRequest; |
| 7 | +import java.net.http.HttpResponse; |
| 8 | +import java.net.http.HttpResponse.BodyHandlers; |
| 9 | +import java.security.MessageDigest; |
| 10 | +import java.security.NoSuchAlgorithmException; |
| 11 | +import java.time.Duration; |
| 12 | +import java.util.Base64; |
| 13 | +import java.util.List; |
| 14 | +import java.util.regex.Pattern; |
| 15 | +import java.util.stream.Collectors; |
| 16 | + |
| 17 | +import com.google.gson.Gson; |
| 18 | +import com.google.gson.GsonBuilder; |
| 19 | +import lombok.RequiredArgsConstructor; |
| 20 | +import net.discordjug.javabot.data.config.BotConfig; |
| 21 | +import net.discordjug.javabot.data.config.PatternTypeAdapter; |
| 22 | +import net.discordjug.javabot.data.config.guild.MessageRule; |
| 23 | +import net.discordjug.javabot.data.config.guild.MessageRule.MessageAction; |
| 24 | +import net.discordjug.javabot.data.config.guild.ModerationConfig; |
| 25 | +import net.discordjug.javabot.data.h2db.message_cache.MessageCache; |
| 26 | +import net.discordjug.javabot.data.h2db.message_cache.model.CachedMessage; |
| 27 | +import net.discordjug.javabot.util.ExceptionLogger; |
| 28 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 29 | +import net.dv8tion.jda.api.entities.Message.Attachment; |
| 30 | +import net.dv8tion.jda.api.entities.Message; |
| 31 | +import net.dv8tion.jda.api.entities.MessageEmbed; |
| 32 | +import org.springframework.stereotype.Component; |
| 33 | + |
| 34 | +/** |
| 35 | + * This {@link MessageFilter} acts on messages according to {@link MessageRule}s. |
| 36 | + * If a message rule matches, the corresponding action is executed. |
| 37 | + */ |
| 38 | +@Component |
| 39 | +@RequiredArgsConstructor |
| 40 | +public class MessageRuleFilter implements MessageFilter { |
| 41 | + |
| 42 | + private final BotConfig botConfig; |
| 43 | + private final MessageCache messageCache; |
| 44 | + |
| 45 | + @Override |
| 46 | + public MessageModificationStatus processMessage(MessageContent content) { |
| 47 | + |
| 48 | + ModerationConfig moderationConfig = botConfig.get(content.event().getGuild()).getModerationConfig(); |
| 49 | + List<MessageRule> messageRules = moderationConfig.getMessageRules(); |
| 50 | + |
| 51 | + MessageRule ruleToExecute = null; |
| 52 | + for (MessageRule rule : messageRules) { |
| 53 | + if (matches(content, rule)) { |
| 54 | + if (ruleToExecute == null || rule.getAction() == MessageAction.BLOCK) { |
| 55 | + ruleToExecute = rule; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + MessageModificationStatus status = MessageModificationStatus.NOT_MODIFIED; |
| 60 | + if (ruleToExecute != null) { |
| 61 | + if (ruleToExecute.getAction() == MessageAction.BLOCK) { |
| 62 | + content.event().getMessage().delete() |
| 63 | + .flatMap(_ -> content.event().getChannel().sendMessage(content.event().getAuthor().getAsMention() + " Your message has been deleted for moderative reasons. If you believe this happened by mistake, please contact the server staff.")) |
| 64 | + .delay(Duration.ofSeconds(60)) |
| 65 | + .flatMap(Message::delete) |
| 66 | + .queue(); |
| 67 | + status = MessageModificationStatus.STOP_PROCESSING; |
| 68 | + } |
| 69 | + log(content, ruleToExecute, moderationConfig); |
| 70 | + } |
| 71 | + |
| 72 | + return status; |
| 73 | + } |
| 74 | + |
| 75 | + private void log(MessageContent content, MessageRule ruleToExecute, ModerationConfig moderationConfig) { |
| 76 | + Gson gson = new GsonBuilder() |
| 77 | + .serializeNulls() |
| 78 | + .setPrettyPrinting() |
| 79 | + .registerTypeAdapter(Pattern.class, new PatternTypeAdapter()) |
| 80 | + .create(); |
| 81 | + EmbedBuilder embed = messageCache.buildMessageCacheEmbed( |
| 82 | + content.event().getMessage().getChannel(), |
| 83 | + content.event().getMessage().getAuthor(), |
| 84 | + CachedMessage.of(content.event().getMessage()), "Message content") |
| 85 | + .setTitle("Message rule triggered") |
| 86 | + .addField("Rule description", "```\n" + gson.toJson(ruleToExecute) + "\n```", false); |
| 87 | + if (!content.attachments().isEmpty()) { |
| 88 | + embed.addField("Attachment hashes", computeAttachmentDescription(content.attachments()), false); |
| 89 | + } |
| 90 | + content.event().getChannel().sendMessageEmbeds(embed.build()).queue(); |
| 91 | + } |
| 92 | + |
| 93 | + private boolean matches(MessageContent content, MessageRule rule) { |
| 94 | + if (rule.getMessageRegex() != null && !rule.getMessageRegex().matcher(content.messageText()).matches()) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + if (content.attachments().size() > rule.getMaxAttachments()) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + if (content.attachments().size() < rule.getMinAttachments()) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + boolean matchesSHA = rule.getAttachmentSHAs().isEmpty(); |
| 104 | + for (Attachment attachment : content.attachments()) { |
| 105 | + if (rule.getAttachmentNameRegex() != null && !rule.getAttachmentNameRegex().matcher(attachment.getFileName()).matches()) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + if (!matchesSHA) { |
| 109 | + if (rule.getAttachmentSHAs().contains(computeSHA(attachment))) { |
| 110 | + matchesSHA = true; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + return matchesSHA; |
| 115 | + } |
| 116 | + |
| 117 | + private String computeAttachmentDescription(List<Message.Attachment> attachments) { |
| 118 | + return attachments.stream() |
| 119 | + .map(attachment -> "- " + attachment.getUrl() + ": `" + computeSHA(attachment) + "`") |
| 120 | + .collect(Collectors.joining("\n")); |
| 121 | + } |
| 122 | + |
| 123 | + private String computeSHA(Attachment attachment) { |
| 124 | + try { |
| 125 | + HttpResponse<byte[]> res = HttpClient.newHttpClient().send(HttpRequest.newBuilder(URI.create(attachment.getProxyUrl())).build(), BodyHandlers.ofByteArray()); |
| 126 | + MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
| 127 | + byte[] hash = digest.digest(res.body()); |
| 128 | + return Base64.getEncoder().encodeToString(hash); |
| 129 | + } catch (IOException | InterruptedException | NoSuchAlgorithmException e) { |
| 130 | + ExceptionLogger.capture(e); |
| 131 | + return ""; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments