From 8a43110100b455f54c074aa11ad60460c1b17682 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 12 Dec 2021 20:40:25 +0100 Subject: [PATCH 1/5] Add Discord Bot Upload Signed-off-by: Chaoscaot --- .../bungeecore/bot/SteamwarDiscordBot.java | 1 + .../bot/listeners/PrivateMessageListener.java | 68 +++++++++++++++++++ .../bungeecore/sql/SchematicNode.java | 43 ++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java diff --git a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java index e614c5e..f2f9eed 100644 --- a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java +++ b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java @@ -104,6 +104,7 @@ public class SteamwarDiscordBot { new DiscordTicketListener(); new DiscordAuthListener(); new DiscordEventListener(); + new PrivateMessageListener(); announcementListener = new AnnouncementListener(); ingameChatListener = new IngameChatListener(); serverTeamChatListener = new ServerTeamChatListener(); diff --git a/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java b/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java new file mode 100644 index 0000000..766fca4 --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java @@ -0,0 +1,68 @@ +/* + * This file is a part of the SteamWar software. + *

+ * Copyright (C) 2021 SteamWar.de-Serverteam + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + *

+ * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + *

+ * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bungeecore.bot.listeners; + +import de.steamwar.bungeecore.sql.SchematicNode; +import de.steamwar.bungeecore.sql.SteamwarUser; +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent; +import org.jetbrains.annotations.NotNull; + +public class PrivateMessageListener extends BasicDiscordListener { + + @Override + public void onPrivateMessageReceived(@NotNull PrivateMessageReceivedEvent event) { + if(!event.getMessage().getAttachments().isEmpty()) { + SteamwarUser user = SteamwarUser.get(event.getAuthor().getIdLong()); + if (user == null) { + event.getMessage().reply("Du must dein Minecraft Account mit dem Bot verbinden, gehe dazu auf dem SteamWar Discord in den `regeln-infos` Channel und Klicke auf `Minecraft Verknüpfen`").complete(); + return; + } + + for (Message.Attachment attachment : event.getMessage().getAttachments()) { + if(attachment.getFileExtension() == null || + (!attachment.getFileExtension().equalsIgnoreCase("schem") && + !attachment.getFileExtension().equalsIgnoreCase("schematic"))) { + continue; + } + + boolean newFormat = attachment.getFileExtension().equalsIgnoreCase("schem"); + int dot = attachment.getFileName().lastIndexOf("."); + String name = attachment.getFileName().substring(0, dot); + if(SchematicNode.invalidSchemName(new String[] {name})) { + event.getMessage().reply("`" + name + "` hat nicht zugelassene Zeichen im namen").queue(); + continue; + } + + SchematicNode node = SchematicNode.getSchematicNode(user.getId(), name, 0); + if(node == null) { + node = SchematicNode.createSchematic(user.getId(), name, null); + } + + try { + node.saveFromStream(attachment.retrieveInputStream().get(), newFormat); + event.getMessage().reply("`" + name + "` wurde erfolgreich hochgeladen").queue(); + } catch (Exception e) { + event.getMessage().reply("`" + name + "` konnte nicht geholt werden, bitte versuche es später nochmal.").queue(); + throw new SecurityException("Could not Download", e); + } + } + } + } +} diff --git a/src/de/steamwar/bungeecore/sql/SchematicNode.java b/src/de/steamwar/bungeecore/sql/SchematicNode.java index cea408c..cff8c1b 100644 --- a/src/de/steamwar/bungeecore/sql/SchematicNode.java +++ b/src/de/steamwar/bungeecore/sql/SchematicNode.java @@ -19,6 +19,8 @@ package de.steamwar.bungeecore.sql; +import net.md_5.bungee.api.connection.ProxiedPlayer; + import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -467,4 +469,45 @@ public class SchematicNode { SchematicNode node = (SchematicNode) obj; return node.getId() == id; } + + public void saveFromBytes(byte[] bytes, boolean newFormat) { + if(isDir) + throw new SecurityException("Node is Directory"); + updateDatabase(new ByteArrayInputStream(bytes), newFormat); + } + + public void saveFromStream(InputStream stream, boolean newFormat) { + if(isDir) + throw new SecurityException("Node is Directory"); + updateDatabase(stream, newFormat); + } + + private void updateDatabase(InputStream blob, boolean newFormat) { + updateDatabase.update(blob, newFormat, id); + schemFormat = newFormat; + } + + private static final List FORBIDDEN_NAMES = Collections.unmodifiableList(Arrays.asList("public")); + + public static boolean invalidSchemName(String[] layers) { + for (String layer : layers) { + if (layer.isEmpty()) { + return true; + } + if (layer.contains("/") || + layer.contains("\\") || + layer.contains("<") || + layer.contains(">") || + layer.contains("^") || + layer.contains("°") || + layer.contains("'") || + layer.contains("\"")) { + return true; + } + if(FORBIDDEN_NAMES.contains(layer.toLowerCase())) { + return true; + } + } + return false; + } } -- 2.39.2 From 98d06544ef6a9c27d9c6803339ab4de89d84afa0 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 25 Dec 2021 22:44:29 +0100 Subject: [PATCH 2/5] Doing PR Stuff Signed-off-by: Chaoscaot --- .../bungeecore/bot/listeners/PrivateMessageListener.java | 5 +++-- src/de/steamwar/bungeecore/sql/SchematicNode.java | 6 ------ 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java b/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java index 766fca4..ac5aa36 100644 --- a/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java +++ b/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java @@ -18,6 +18,7 @@ package de.steamwar.bungeecore.bot.listeners; +import de.steamwar.bungeecore.BungeeCore; import de.steamwar.bungeecore.sql.SchematicNode; import de.steamwar.bungeecore.sql.SteamwarUser; import net.dv8tion.jda.api.entities.Message; @@ -59,8 +60,8 @@ public class PrivateMessageListener extends BasicDiscordListener { node.saveFromStream(attachment.retrieveInputStream().get(), newFormat); event.getMessage().reply("`" + name + "` wurde erfolgreich hochgeladen").queue(); } catch (Exception e) { - event.getMessage().reply("`" + name + "` konnte nicht geholt werden, bitte versuche es später nochmal.").queue(); - throw new SecurityException("Could not Download", e); + event.getMessage().reply("`" + name + "` konnte nicht geholt werden, bitte versuche es später nochmal oder wende dich an einen Developer").queue(); + BungeeCore.log("Could not Upload Schem \"" + name + "\" from User \"" + user.getUserName() + "\"" + e); } } } diff --git a/src/de/steamwar/bungeecore/sql/SchematicNode.java b/src/de/steamwar/bungeecore/sql/SchematicNode.java index cff8c1b..d3b7a66 100644 --- a/src/de/steamwar/bungeecore/sql/SchematicNode.java +++ b/src/de/steamwar/bungeecore/sql/SchematicNode.java @@ -470,12 +470,6 @@ public class SchematicNode { return node.getId() == id; } - public void saveFromBytes(byte[] bytes, boolean newFormat) { - if(isDir) - throw new SecurityException("Node is Directory"); - updateDatabase(new ByteArrayInputStream(bytes), newFormat); - } - public void saveFromStream(InputStream stream, boolean newFormat) { if(isDir) throw new SecurityException("Node is Directory"); -- 2.39.2 From f547667937ca29af27999370808795603b5af997 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Thu, 30 Dec 2021 11:53:54 +0100 Subject: [PATCH 3/5] Doing PR Stuff Signed-off-by: Chaoscaot --- .../bungeecore/bot/listeners/PrivateMessageListener.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java b/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java index ac5aa36..e543292 100644 --- a/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java +++ b/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java @@ -40,6 +40,7 @@ public class PrivateMessageListener extends BasicDiscordListener { if(attachment.getFileExtension() == null || (!attachment.getFileExtension().equalsIgnoreCase("schem") && !attachment.getFileExtension().equalsIgnoreCase("schematic"))) { + event.getMessage().reply("`" + attachment.getFileName() + "` wird ignoriert, da die Datei keine Schematic ist").queue(); continue; } @@ -47,7 +48,7 @@ public class PrivateMessageListener extends BasicDiscordListener { int dot = attachment.getFileName().lastIndexOf("."); String name = attachment.getFileName().substring(0, dot); if(SchematicNode.invalidSchemName(new String[] {name})) { - event.getMessage().reply("`" + name + "` hat nicht zugelassene Zeichen im namen").queue(); + event.getMessage().reply("`" + name + "` hat nicht zugelassene Zeichen im Namen").queue(); continue; } @@ -60,7 +61,7 @@ public class PrivateMessageListener extends BasicDiscordListener { node.saveFromStream(attachment.retrieveInputStream().get(), newFormat); event.getMessage().reply("`" + name + "` wurde erfolgreich hochgeladen").queue(); } catch (Exception e) { - event.getMessage().reply("`" + name + "` konnte nicht geholt werden, bitte versuche es später nochmal oder wende dich an einen Developer").queue(); + event.getMessage().reply("`" + name + "` konnte nicht hochgeladen werden, bitte versuche es später nochmal oder wende dich an einen Developer").queue(); BungeeCore.log("Could not Upload Schem \"" + name + "\" from User \"" + user.getUserName() + "\"" + e); } } -- 2.39.2 From 646b23af084730547f635c8fb7c105b416bc4027 Mon Sep 17 00:00:00 2001 From: Chaos Date: Sun, 6 Mar 2022 17:49:21 +0100 Subject: [PATCH 4/5] fix(bot_upload): Use Punishment --- .../bungeecore/bot/listeners/PrivateMessageListener.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java b/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java index e543292..f90b9d1 100644 --- a/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java +++ b/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java @@ -19,6 +19,7 @@ package de.steamwar.bungeecore.bot.listeners; import de.steamwar.bungeecore.BungeeCore; +import de.steamwar.bungeecore.sql.Punishment; import de.steamwar.bungeecore.sql.SchematicNode; import de.steamwar.bungeecore.sql.SteamwarUser; import net.dv8tion.jda.api.entities.Message; @@ -36,6 +37,11 @@ public class PrivateMessageListener extends BasicDiscordListener { return; } + if(user.isPunished(Punishment.PunishmentType.NoSchemReceiving)) { + event.getMessage().reply("Du darfst keine Schematics hochladen").complete(); + return; + } + for (Message.Attachment attachment : event.getMessage().getAttachments()) { if(attachment.getFileExtension() == null || (!attachment.getFileExtension().equalsIgnoreCase("schem") && -- 2.39.2 From 680a969435a7dc637c7cac46f2bd26e0519234f2 Mon Sep 17 00:00:00 2001 From: Chaos Date: Sun, 6 Mar 2022 17:51:34 +0100 Subject: [PATCH 5/5] build(bot_upload): Rebuild --- .../bungeecore/bot/listeners/PrivateMessageListener.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java b/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java index f90b9d1..8f55731 100644 --- a/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java +++ b/src/de/steamwar/bungeecore/bot/listeners/PrivateMessageListener.java @@ -24,12 +24,11 @@ import de.steamwar.bungeecore.sql.SchematicNode; import de.steamwar.bungeecore.sql.SteamwarUser; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent; -import org.jetbrains.annotations.NotNull; public class PrivateMessageListener extends BasicDiscordListener { @Override - public void onPrivateMessageReceived(@NotNull PrivateMessageReceivedEvent event) { + public void onPrivateMessageReceived(PrivateMessageReceivedEvent event) { if(!event.getMessage().getAttachments().isEmpty()) { SteamwarUser user = SteamwarUser.get(event.getAuthor().getIdLong()); if (user == null) { -- 2.39.2