package org.freecode.irc.votebot.api;

import org.freecode.irc.Privmsg;
import org.freecode.irc.Transmittable;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class CommandModule extends FVBModule {

    private static final Pattern COMMAND_PATTERN = Pattern.compile("(!([^ ]+))|(!(.+?) (.+))");
    private final Pattern NAME_PATTERN, PARAMETER_PATTERN;

    public CommandModule() {
        this.PARAMETER_PATTERN = Pattern.compile(getParameterRegex());
        this.NAME_PATTERN = Pattern.compile(getName());
    }

    @Override
    public boolean canRun(Transmittable trns) {
        if (!trns.isPrivmsg())
            return false;
        String msg = trns.asPrivmsg().getMessage();
        Matcher matcher = COMMAND_PATTERN.matcher(msg);
        if (matcher.matches()) {
            if (matcher.group(4) == null || matcher.group(4).isEmpty()) {
                return NAME_PATTERN.matcher(matcher.group(2)).matches();
            } else {
                return NAME_PATTERN.matcher(matcher.group(4)).matches() &&
                        PARAMETER_PATTERN.matcher(matcher.group(5)).matches();
            }
        }
        return false;
    }

    @Override
    public void process(Transmittable trns) {
        processMessage((Privmsg) trns);
    }

    public abstract void processMessage(Privmsg privmsg);

    protected String getParameterRegex() {
        return ".*";
    }
}

@@ -243,15 +243,8 @@
 ate 
-static 
 fina
@@ -272,54 +272,8 @@
 TERN
- = Pattern.compile(%22(!(%5B%5E %5D+))%7C(!(.+?) (.+))%22)
 ;%0A  
@@ -355,24 +355,163 @@
 dModule() %7B%0A
+        char c = getCommandCharacter();%0A        this.COMMAND_PATTERN = Pattern.compile(String.format(%22(%25s(%5B%5E %5D+))%7C(%25s(.+?) (.+))%22, c, c));%0A
         this
@@ -1502,11 +1502,81 @@
 %22;%0A    %7D
+%0A%0A    protected char getCommandCharacter() %7B%0A        return '!';%0A    %7D
 %0A%7D%0A

Allow custom command characters