diff --git a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java index e614c5ee..f2f9eed7 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 00000000..766fca46 --- /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 cea408cb..cff8c1b9 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; + } }