]> jfr.im git - irc/rizon/acid.git/commitdiff
WIP: Allow barring of users from VIzon origin/vizon-ban
authorDwarf <redacted>
Mon, 4 Oct 2021 13:54:45 +0000 (15:54 +0200)
committerDwarf <redacted>
Mon, 4 Oct 2021 13:54:45 +0000 (15:54 +0200)
vizon/src/main/java/net/rizon/acid/plugins/vizon/commands/BanCommand.java [new file with mode: 0644]
vizon/src/main/java/net/rizon/acid/plugins/vizon/commands/BanlistCommand.java [new file with mode: 0644]
vizon/src/main/java/net/rizon/acid/plugins/vizon/db/VizonBan.java [new file with mode: 0644]
vizon/src/main/java/net/rizon/acid/plugins/vizon/db/VizonDatabase.java
vizon/vizon.example.yml
vizon/vizon.sql

diff --git a/vizon/src/main/java/net/rizon/acid/plugins/vizon/commands/BanCommand.java b/vizon/src/main/java/net/rizon/acid/plugins/vizon/commands/BanCommand.java
new file mode 100644 (file)
index 0000000..b8d2506
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2021, Dwarf <dwarf@rizon.net>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+package net.rizon.acid.plugins.vizon.commands;
+
+import net.rizon.acid.core.*;
+import net.rizon.acid.plugins.vizon.Vizon;
+import net.rizon.acid.plugins.vizon.db.VizonDatabase;
+
+import java.util.Arrays;
+
+/**
+ *
+ * @author Dwarf <dwarf@rizon.net>
+ */
+public class BanCommand extends Command
+{
+    public BanCommand()
+    {
+        super(1, 1);
+    }
+
+    @Override
+    public void Run(User source, AcidUser to, Channel c, String[] args)
+    {
+        String[] parts = args[0].split(" ");
+        String nick = parts[0];
+        String reason = String.join(" ", Arrays.copyOfRange(parts, 1, parts.length));
+
+        VizonDatabase database = Vizon.getVizonDatabase();
+        if (database.findBanByNick(nick) != null)
+        {
+            Acidictive.reply(source, to, c, "Nickname is already banned.");
+            return;
+        }
+
+        if (database.insertBan(nick, source.getNick(), reason))
+        {
+            Acidictive.reply(source, to, c, "Nickname banned from playing.");
+        }
+        else
+        {
+            Acidictive.reply(source, to, c, "Couldn't create a ban, contact a dev.");
+        }
+    }
+}
\ No newline at end of file
diff --git a/vizon/src/main/java/net/rizon/acid/plugins/vizon/commands/BanlistCommand.java b/vizon/src/main/java/net/rizon/acid/plugins/vizon/commands/BanlistCommand.java
new file mode 100644 (file)
index 0000000..0ed3581
--- /dev/null
@@ -0,0 +1,34 @@
+package net.rizon.acid.plugins.vizon.commands;
+
+import net.rizon.acid.core.*;
+import net.rizon.acid.plugins.vizon.Vizon;
+import net.rizon.acid.plugins.vizon.db.VizonBan;
+import net.rizon.acid.plugins.vizon.db.VizonDatabase;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class BanlistCommand extends Command
+{
+    public BanlistCommand()
+    {
+        super(0, 0);
+    }
+
+    @Override
+    public void Run(User source, AcidUser to, Channel c, String[] args)
+    {
+        VizonDatabase database = Vizon.getVizonDatabase();
+        List<VizonBan> bans = new ArrayList<>(database.getBans());
+
+
+        Acidictive.reply(source, to, c, "VIzon bans:");
+        for (int i = 0; i <= bans.size(); i++)
+        {
+            VizonBan ban = bans.get(i);
+            Acidictive.reply(source, to, c, (i+1) + ". Nick " + ban.getNick() + " :: Banned by " + ban.getOper() + " :: Reason " + ban.getReason());
+        }
+    }
+}
diff --git a/vizon/src/main/java/net/rizon/acid/plugins/vizon/db/VizonBan.java b/vizon/src/main/java/net/rizon/acid/plugins/vizon/db/VizonBan.java
new file mode 100644 (file)
index 0000000..cde7810
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2021, Dwarf <dwarf@rizon.net>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+package net.rizon.acid.plugins.vizon.db;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ *
+ * @author Dwarf <dwarf@rizon.net>
+ */
+public class VizonBan {
+    private static final Logger logger = LoggerFactory.getLogger(VizonBan.class);
+
+    private final int id;
+    private final String nick;
+    private final String oper;
+    private final String reason;
+
+    public static VizonBan fromResultSet(ResultSet rs)
+    {
+        try
+        {
+            int id = rs.getInt("id");
+            String nick = rs.getString("nick");
+            String oper = rs.getString("oper");
+            String reason = rs.getString("reason");
+            return new VizonBan(id, nick, oper, reason);
+        }
+        catch (SQLException ex)
+        {
+            logger.warn("Unable to construct VizonBan from ResultSet", ex);
+            return null;
+        }
+    }
+
+    private VizonBan(
+            int id,
+            String nick,
+            String oper,
+            String reason)
+    {
+        this.id = id;
+        this.nick = nick;
+        this.oper = oper;
+        this.reason = reason;
+    }
+
+    public int getId()
+    {
+        return id;
+    }
+
+    public String getNick()
+    {
+        return nick;
+    }
+
+    public String getOper()
+    {
+        return oper;
+    }
+
+    public String getReason()
+    {
+        return reason;
+    }
+}
index abac98f7db8f1b87e0a4482cd7096ab036041e73..f34cb379e06eea75bbc46ad78faf8bca85824410 100644 (file)
@@ -810,4 +810,100 @@ public class VizonDatabase
 
                return drawings;
        }
+
+       /**
+        * Try and find a ban matching {@code nick}.
+        * @param nick  Nickname to find a ban for.
+        * @return A {@link VizonBan} or null if the user is not banned.
+        */
+       public VizonBan findBanByNick(String nick)
+       {
+               if (nick == null)
+               {
+                       return null;
+               }
+
+               try
+               {
+                       PreparedStatement statement = vizonSql.prepare("SELECT * FROM vizon_bans WHERE nick = ?");
+                       statement.setString(1, nick);
+
+                       ResultSet rs = vizonSql.executeQuery(statement);
+
+                       if (rs.next())
+                       {
+                               return VizonBan.fromResultSet(rs);
+                       }
+
+                       return null;
+               }
+               catch (SQLException ex)
+               {
+                       logger.warn("Unable to select or create ban in Vizon Database", ex);
+                       return null;
+               }
+       }
+
+       public Collection<VizonBan> getBans()
+       {
+               try
+               {
+                       PreparedStatement statement = vizonSql.prepare("SELECT * FROM vizon_bans");
+
+                       ResultSet rs = vizonSql.executeQuery(statement);
+
+                       List<VizonBan> bans = new ArrayList<>();
+
+                       while (rs.next())
+                       {
+                               VizonBan ban = VizonBan.fromResultSet(rs);
+
+                               if (ban != null)
+                               {
+                                       bans.add(ban);
+                               }
+                       }
+
+                       return bans;
+               }
+               catch (SQLException ex)
+               {
+                       logger.warn("Unable to select bans from Vizon Database", ex);
+                       return null;
+               }
+       }
+
+       /**
+        * Ban a nickname from playing.
+        * @param nick          The nickname to ban.
+        * @param oper          The oper who banned this nickname.
+        * @param reason    The reason to ban for.
+        * @return True on success, otherwise false.
+        */
+       public boolean insertBan(String nick, String oper, String reason)
+       {
+               if (nick.isEmpty() || reason.isEmpty())
+               {
+                       return false;
+               }
+
+               try
+               {
+                       PreparedStatement statement = vizonSql.prepare("INSERT INTO vizon_bans "
+                                       + "(nick, oper, reason) "
+                                       + "VALUES (?, ?, ?)");
+                       statement.setString(1, nick);
+                       statement.setString(2, oper);
+                       statement.setString(3, reason);
+
+                       int result = vizonSql.executeUpdateBlocking(statement);
+
+                       return result > 0;
+               }
+               catch (SQLException ex)
+               {
+                       logger.warn("Unable to insert vhost ban", ex);
+                       return false;
+               }
+       }
 }
index 2d17773051363ea4cb4c1b5b77798bb85ad0717e..01c4e64f1d73b8c98bc2f5e5fec57adcd4410bc9 100644 (file)
@@ -92,6 +92,14 @@ clients:
     name: vhost
     privilege: none
     clazz: net.rizon.acid.plugins.vizon.commands.ModifyVhostCommand
+   - name: ban
+     channels: [ vhost ]
+     privilege: none
+     clazz: net.rizon.acid.plugins.vizon.commands.BanCommand
+   - name: b
+     channels: [ vhost ]
+     privilege: none
+     clazz: net.rizon.acid.plugins.vizon.commands.BanCommand
 
 vizonChannel: "#opers"
 vizonDebugChannel: "#opers"
index c9ca4c0142770899bf75bd59a25790ab2e18d1dd..1e0dba27e020bb5690505b3f7d3f4ddd4674e1f1 100644 (file)
@@ -82,6 +82,17 @@ CREATE TABLE IF NOT EXISTS `vizon`.`vizon_bets` (
     ON UPDATE CASCADE)
 ENGINE = InnoDB;
 
+-- -----------------------------------------------------
+-- Table `vizon`.`vizon_bans`
+-- -----------------------------------------------------
+CREATE TABLE IF NOT EXISTS `vizon`.`vizon_bans` (
+`id` INT AUTO_INCREMENT,
+`nick` VARCHAR(64) not null,
+`oper` VARCHAR(64) not null,
+`reason` VARCHAR(128),
+PRIMARY KEY (id),
+UNIQUE INDEX `nick_UNIQUE` (`nick` ASC));
+
 
 SET SQL_MODE=@OLD_SQL_MODE;
 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;