]> jfr.im git - irc/rizon/acid.git/commitdiff
Allow manual override of a user's colored vhost
authorOrillion <redacted>
Thu, 24 Aug 2017 19:16:40 +0000 (21:16 +0200)
committerOrillion <redacted>
Sun, 27 Aug 2017 21:28:47 +0000 (23:28 +0200)
vizon/src/main/java/net/rizon/acid/plugins/vizon/VhostManager.java
vizon/src/main/java/net/rizon/acid/plugins/vizon/commands/ModifyVhostCommand.java [new file with mode: 0644]
vizon/src/main/java/net/rizon/acid/plugins/vizon/commands/RequestCommand.java
vizon/vizon.example.yml

index e1451a5dc09dc1de8ef7c40080cf0b6a8186dbe5..749d33e82e25af50426521ff0552ea7cdb9723aa 100644 (file)
@@ -43,6 +43,7 @@ public class VhostManager
        private static final Logger logger = LoggerFactory.getLogger(VhostManager.class);
        private static final String DELETE_FORMAT = "DEL %s";
 
+       public static final int MAX_HOST_BYTES = 63;
        public static final Pattern NORMAL_PATTERN = Pattern.compile("^[\u0003\u000Fa-zA-Z0-9-.]*[a-zA-Z]+[\u0003\u000Fa-zA-Z0-9-.]*$");
        public static final Pattern BOLD_PATTERN = Pattern.compile("^[\u0003\u0002\u000Fa-zA-Z0-9-.]*[a-zA-Z]+[\u0003\u0002\u000Fa-zA-Z0-9-.]*$");
 
diff --git a/vizon/src/main/java/net/rizon/acid/plugins/vizon/commands/ModifyVhostCommand.java b/vizon/src/main/java/net/rizon/acid/plugins/vizon/commands/ModifyVhostCommand.java
new file mode 100644 (file)
index 0000000..0bd0466
--- /dev/null
@@ -0,0 +1,171 @@
+/*
+ * Copyright (c) 2017, Orillion <orillion@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 java.util.regex.Matcher;
+import net.rizon.acid.core.AcidUser;
+import net.rizon.acid.core.Acidictive;
+import net.rizon.acid.core.Channel;
+import net.rizon.acid.core.Command;
+import net.rizon.acid.core.User;
+import net.rizon.acid.plugins.vizon.VhostManager;
+import net.rizon.acid.plugins.vizon.Vizon;
+import net.rizon.acid.plugins.vizon.db.VizonDatabase;
+import net.rizon.acid.plugins.vizon.db.VizonUser;
+
+/**
+ *
+ * @author Orillion <orillion@rizon.net>
+ */
+public class ModifyVhostCommand extends Command
+{
+
+       private static final String UPDATE_MESSAGE_FORMAT = "%s updated the vhost of %s to %s";
+
+       public ModifyVhostCommand()
+       {
+               super(3, 3);
+       }
+
+       @Override
+       public void Run(User source, AcidUser to, Channel c, String[] args)
+       {
+               String command = args[0];
+               String nick = args[1];
+               String vhost = args[2];
+
+               if ("SET".equalsIgnoreCase(command))
+               {
+                       this.set(source, to, c, nick, vhost);
+               }
+       }
+
+       private void set(User source, AcidUser to, Channel c, String target, String vhost)
+       {
+               VizonDatabase database = Vizon.getVizonDatabase();
+
+               VizonUser vizonUser = database.findUser(target);
+
+               if (vizonUser == null)
+               {
+                       Acidictive.reply(source, to, c, "Nickname not in database");
+                       return;
+               }
+
+               if (vizonUser.getObtained() == null)
+               {
+                       Acidictive.reply(source, to, c, "User has no VIP status");
+                       return;
+               }
+
+               if (vizonUser.getVhost() == null)
+               {
+                       // @NOTE(Orillion): Do not set when user does not have one yet,
+                       // this can conflict with HostServ, and I don't want to do extra
+                       // work here
+                       Acidictive.reply(source, to, c, "User does not have a vhost yet");
+                       return;
+               }
+
+               Matcher matcher;
+
+               if (vizonUser.isBold())
+               {
+                       matcher = VhostManager.BOLD_PATTERN.matcher(vhost);
+               }
+               else
+               {
+                       matcher = VhostManager.NORMAL_PATTERN.matcher(vhost);
+               }
+
+               if (!matcher.matches())
+               {
+                       // Vhost contains illegal characters
+                       Acidictive.reply(source, to, null, "Requested vhost contains illegal characters, or is empty");
+                       return;
+               }
+
+               if (vhost.getBytes().length > VhostManager.MAX_HOST_BYTES)
+               {
+                       // We count bytes instead of chars, since counting chars is wrong.
+                       Acidictive.reply(source, to, null, "Requested vhost is too long");
+                       return;
+               }
+
+               if (vhost.charAt(vhost.length() - 1) != '\u000F')
+               {
+                       // Add mandatory reset all character
+                       vhost = vhost + '\u000F';
+               }
+
+               vizonUser.setVhost(vhost);
+
+               database.updateUser(vizonUser);
+
+               User user = User.findUser(target);
+
+               if (user != null)
+               {
+                       Vizon.getVhostManager().applyVhostIfApplicable(source);
+               }
+
+               Acidictive.reply(source, to, c, String.format(
+                               "Updated the vhost of %s to %s",
+                               vizonUser.getNick(),
+                               vizonUser.getVhost()));
+
+               Channel channel = Channel.findChannel(Acidictive.conf.getChannelNamed("vhost"));
+
+               // Report manual update to vhost channel.
+               Acidictive.reply(
+                               null,
+                               Vizon.getVizonBot(),
+                               channel,
+                               String.format(
+                                               UPDATE_MESSAGE_FORMAT,
+                                               source.getNick(),
+                                               vizonUser.getNick(),
+                                               vizonUser.getVhost()));
+       }
+
+       @Override
+       public void onHelp(User u, AcidUser to, Channel c)
+       {
+               Acidictive.reply(u, to, c, "\002VHOST SET \u001Fnick\u001F \u001Fvhost\u001F\002 Change user's vhost");
+       }
+
+       @Override
+       public boolean onHelpCommand(User u, AcidUser to, Channel c)
+       {
+               Acidictive.reply(u, to, c, "Syntax: \002VHOST SET \u001Fnick\u001F \u001Fvhost\u001F\002 Change user's vhost");
+               Acidictive.reply(u, to, c, " ");
+               Acidictive.reply(u, to, c, "Examples:");
+               Acidictive.reply(u, to, c, "    \002VHOST SET magic \00313rainbows.are.magic\003\002");
+               Acidictive.reply(u, to, c, "        Sets magic's colored vhost to \00313rainsbow.are.magic\003");
+
+               return true;
+       }
+}
index d13da0734357a23a5d354521a46cedd040b9f757..f40303c739a3b549b61488baab2322fcd67f5d5c 100644 (file)
@@ -44,7 +44,6 @@ import net.rizon.acid.plugins.vizon.db.VizonUser;
  */
 public class RequestCommand extends Command
 {
-       private static final int MAX_HOST_BYTES = 63;
 
        public RequestCommand()
        {
@@ -97,7 +96,7 @@ public class RequestCommand extends Command
                        return;
                }
 
-               if (vhost.getBytes().length > MAX_HOST_BYTES)
+               if (vhost.getBytes().length > VhostManager.MAX_HOST_BYTES)
                {
                        // We count bytes instead of chars, since counting chars is wrong.
                        Acidictive.reply(source, to, null, "Requested vhost is too long");
index 6f1802afa578b2daf58dc261e0b2d075746f08c4..0387e6994b8381dd2138c6c6015ff00ffee77ed6 100644 (file)
@@ -7,6 +7,10 @@ clients:
   name: VIzon
   modes: iUop
   channels: [ vizonChannel, vizonCommandChannel ]
+  # Command privilege levels:
+  # none     - All opers can use
+  # anyone   - Any user can use
+  # flag     - Only opers with correct flag can use
   commands:
    -
     name: help
@@ -69,6 +73,10 @@ clients:
     channels: [vizonCommandChannel]
     privilege: none
     clazz: net.rizon.acid.plugins.vizon.commands.OrderResultsCommand
+   -
+    name: vhost
+    privilege: none
+    clazz: net.rizon.acid.plugins.vizon.commands.ModifyVhostCommand
 
 vizonChannel: "#opers"
 vizonBot: "VizonBot"