/*
 * Skybot, a multipurpose discord bot
 *      Copyright (C) 2017  Duncan "duncte123" Sterken
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

package ml.duncte123.skybot.commands.essentials.eval.filter;

import org.kohsuke.groovy.sandbox.GroovyValueFilter;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

public class EvalFilter extends GroovyValueFilter {

    private static final Set<Class> ALLOWED_TYPES = new HashSet<>();

    public EvalFilter() {
        ALLOWED_TYPES.addAll(Arrays.stream(ALLOWED_TYPES_LIST).collect(Collectors.toList()));
    }

    @Override
    public final Object filter(Object o) {
        if (o==null || ALLOWED_TYPES.contains(o.getClass()) )
            return o;
        /*if(o instanceof Script || o instanceof Closure)
            return o;*/
        throw new SecurityException("Class not allowed: " + o);
    }

    private static final Class[] ALLOWED_TYPES_LIST = {
            String.class,
            Integer.class,
            Boolean.class,
            Double.class,
            Float.class,
            Short.class,
            Long.class,
            BigDecimal.class,
            Character.class,
            Arrays.class,
            List.class,
            ArrayList.class
    };

}

@@ -929,16 +929,48 @@
 util.*;%0A
+import java.util.regex.Pattern;%0A
 import j
@@ -1121,16 +1121,51 @@
 t%3C%3E();%0A%0A
+    /**%0A     * Constructor%0A     */%0A
     publ
@@ -1279,24 +1279,158 @@
 )));%0A    %7D%0A%0A
+    /**%0A     * This filters the script%0A     * @param o the script to filter%0A     * @return the script if it passes the filter%0A     */%0A
     @Overrid
@@ -1652,70 +1652,877 @@
     
-throw new SecurityException(%22Class not allowed: %22 + o);%0A    %7D%0A
+if(filterArrays(String.valueOf(o)))%0A            throw new UnsupportedOperationException(%22Arrays are not allowed%22);%0A        throw new SecurityException(%22Class not allowed: %22 + o);%0A    %7D%0A%0A    /**%0A     * This checks if the script contains an array%0A     * @param toFilter the script to filter%0A     * @return true if the script contains an array%0A     */%0A    private boolean filterArrays(String toFilter) %7B%0A        return Pattern.compile(%22String%5C%5Cs*%5B%5C%5Cs*(%5B0-9%5D*)%5C%5Cs*%5D%7C%7C%22 +%0A                %22int%5C%5Cs*%5B%5C%5Cs*(%5B0-9%5D*)%5C%5Cs*%5D%7C%7C%22 +%0A                %22Integer%5C%5Cs*%5B%5C%5Cs*(%5B0-9%5D*)%5C%5Cs*%5D%7C%7C%22 +%0A                %22byte%5C%5Cs*%5B%5C%5Cs*(%5B0-9%5D*)%5C%5Cs*%5D%7C%7C%22 +%0A                %22Byte%5C%5Cs*%5B%5C%5Cs*(%5B0-9%5D*)%5C%5Cs*%5D%7C%7C%22 +%0A                %22boolean%5C%5Cs*%5B%5C%5Cs*(%5B0-9%5D*)%5C%5Cs*%5D%7C%7C%22 +%0A                %22char%5C%5Cs*%5B%5C%5Cs*(%5B0-9%5D*)%5C%5Cs*%5D%22).matcher(toFilter).matches();%0A    %7D%0A%0A    /**%0A     * This contains a list of all the allowed classes%0A     */
 %0A   

Add array filter for eval