-
-
Couldn't load subscription status.
- Fork 101
Forbid posting blacklisted files #542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Zabuzard
merged 17 commits into
Together-Java:develop
from
Mom0aut:feature/forbid-posting-exe-files
Sep 1, 2022
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
141d7f0
added AttachmentListener checks if the message attachment contains bl…
Mom0aut 1774d41
added review feedback
Mom0aut 21052b8
added review feedback
Mom0aut 87fbb7f
added review feedback
Mom0aut a4e5678
added review feedback
Mom0aut f45d56c
added review feedback
Mom0aut aabb469
added review feedback
Mom0aut e7b4419
added review feedback
Mom0aut 5143aac
added review feedback
Mom0aut 90b6065
added review feedback
Mom0aut 0f87027
Merge branch 'develop' into feature/forbid-posting-exe-files
Mom0aut b94177e
added review feedback
Mom0aut 8f2d28d
added review feedback
Mom0aut 416afb7
added review feedback
Mom0aut c72f69b
fixed typo
Mom0aut 9db679c
added review feedback
Mom0aut f3ab6f4
added review feedback
Mom0aut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
.../org/togetherjava/tjbot/commands/moderation/attachment/BlacklistedAttachmentListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package org.togetherjava.tjbot.commands.moderation.attachment; | ||
|
|
||
| import net.dv8tion.jda.api.EmbedBuilder; | ||
| import net.dv8tion.jda.api.MessageBuilder; | ||
| import net.dv8tion.jda.api.entities.Message; | ||
| import net.dv8tion.jda.api.entities.MessageEmbed; | ||
| import net.dv8tion.jda.api.events.message.MessageReceivedEvent; | ||
| import net.dv8tion.jda.api.requests.RestAction; | ||
| import org.togetherjava.tjbot.commands.MessageReceiverAdapter; | ||
| import org.togetherjava.tjbot.config.Config; | ||
| import org.togetherjava.tjbot.moderation.ModAuditLogWriter; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import java.awt.*; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * Reacts to blacklisted attachments being posted, upon which they are deleted. | ||
| */ | ||
| public final class BlacklistedAttachmentListener extends MessageReceiverAdapter { | ||
| private final ModAuditLogWriter modAuditLogWriter; | ||
| private final List<String> blacklistedFileExtensions; | ||
|
|
||
| /** | ||
| * Creates a AttachmentListener to receive all message sent in any channel. | ||
| * | ||
| * @param config to find the blacklisted media attachments | ||
| * @param modAuditLogWriter to inform the mods about the suspicious attachment | ||
| */ | ||
| public BlacklistedAttachmentListener(Config config, ModAuditLogWriter modAuditLogWriter) { | ||
| super(Pattern.compile(".*")); | ||
| this.modAuditLogWriter = modAuditLogWriter; | ||
| this.blacklistedFileExtensions = config.getBlacklistedFileExtensions(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onMessageReceived(MessageReceivedEvent event) { | ||
| if (event.getAuthor().isBot() || event.isWebhookMessage()) { | ||
| return; | ||
| } | ||
| if (doesMessageContainBlacklistedContent(event.getMessage())) { | ||
| handleBadMessage(event.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private void handleBadMessage(Message message) { | ||
| message.delete().flatMap(any -> dmUser(message)).queue(any -> warnMods(message)); | ||
| } | ||
|
|
||
| @Nonnull | ||
| private RestAction<Message> dmUser(Message message) { | ||
Mom0aut marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Message dmMessage = createDmMessage(message); | ||
| return message.getAuthor() | ||
| .openPrivateChannel() | ||
| .flatMap(privateChannel -> privateChannel.sendMessage(dmMessage)); | ||
| } | ||
|
|
||
| @Nonnull | ||
| private Message createDmMessage(Message originalMessage) { | ||
Mom0aut marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| String contentRaw = originalMessage.getContentRaw(); | ||
| String blacklistedAttachments = | ||
| String.join(", ", getBlacklistedAttachmentsFromMessage(originalMessage)); | ||
|
|
||
| String dmMessageContent = | ||
| """ | ||
| Hey there, you posted a message containing a blacklisted file attachment: %s. | ||
| We had to delete your message for security reasons. | ||
|
|
||
| Feel free to repost your message without, or with a different file instead. Sorry for any inconvenience caused by this 🙇️ | ||
| """ | ||
| .formatted(blacklistedAttachments); | ||
|
|
||
| // No embed needed if there was no message from the user | ||
| if (contentRaw.isEmpty()) { | ||
| return new MessageBuilder(dmMessageContent).build(); | ||
| } | ||
| return createBaseResponse(contentRaw, dmMessageContent); | ||
| } | ||
|
|
||
| @Nonnull | ||
| private Message createBaseResponse(String originalMessageContent, String dmMessageContent) { | ||
Mom0aut marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| MessageEmbed originalMessageEmbed = | ||
| new EmbedBuilder().setDescription(originalMessageContent) | ||
| .setColor(Color.ORANGE) | ||
| .build(); | ||
| return new MessageBuilder(dmMessageContent).setEmbeds(originalMessageEmbed).build(); | ||
| } | ||
|
|
||
| @Nonnull | ||
| private List<String> getBlacklistedAttachmentsFromMessage(Message originalMessage) { | ||
Mom0aut marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return originalMessage.getAttachments() | ||
| .stream() | ||
| .filter(attachment -> blacklistedFileExtensions | ||
| .contains(attachment.getFileExtension().toLowerCase(Locale.US))) | ||
| .map(Message.Attachment::getFileName) | ||
| .toList(); | ||
| } | ||
|
|
||
| private boolean doesMessageContainBlacklistedContent(Message message) { | ||
| return message.getAttachments() | ||
| .stream() | ||
| .anyMatch(attachment -> blacklistedFileExtensions | ||
| .contains(attachment.getFileExtension().toLowerCase(Locale.US))); | ||
| } | ||
|
|
||
| private void warnMods(Message sentUserMessage) { | ||
| String blacklistedAttachmentsFromMessage = | ||
| String.join(", ", getBlacklistedAttachmentsFromMessage(sentUserMessage)); | ||
|
|
||
| modAuditLogWriter.write( | ||
| "Message with blacklisted content detected: %s" | ||
| .formatted(blacklistedAttachmentsFromMessage), | ||
| "Sent Message: %s".formatted(sentUserMessage), sentUserMessage.getAuthor(), | ||
| sentUserMessage.getTimeCreated(), sentUserMessage.getGuild()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.