#!/bin/bash
# info: list firewall block list
# options: [FORMAT]
#
# example: v-list-firewall-ban json
#
# This function of obtaining the list of currently blocked ips.

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

# Argument definition
format=${1-shell}

# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

# JSON list function
json_list() {
	IFS=$'\n'
	i=1
	objects=$(grep IP $HESTIA/data/firewall/banlist.conf | wc -l)
	echo "{"
	while read str; do
		parse_object_kv_list "$str"
		echo -n '    "'$IP'": {
        "IP": "'$IP'",
        "CHAIN": "'$CHAIN'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'"
    }'
		if [ "$i" -lt "$objects" ]; then
			echo ','
		else
			echo
		fi
		((i++))
	done < <(cat $HESTIA/data/firewall/banlist.conf)
	echo '}'
}

# SHELL list function
shell_list() {
	IFS=$'\n'
	echo "IP   CHAIN   TIME   DATE"
	echo "--   -----   ----   ----"
	while read str; do
		parse_object_kv_list "$str"
		echo "$IP $CHAIN $TIME $DATE"
	done < <(cat $HESTIA/data/firewall/banlist.conf)
}

# PLAIN list function
plain_list() {
	IFS=$'\n'
	while read str; do
		parse_object_kv_list "$str"
		echo -e "$IP\t$CHAIN\t$TIME\t$DATE"
	done < <(cat $HESTIA/data/firewall/banlist.conf)
}

# CSV list function
csv_list() {
	IFS=$'\n'
	echo "IP,CHAIN,TIME,DATE"
	while read str; do
		parse_object_kv_list "$str"
		echo "$IP,$CHAIN,$TIME,$DATE"
	done < <(cat $HESTIA/data/firewall/banlist.conf)
}

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

# Listing data
case $format in
	json) json_list ;;
	plain) plain_list ;;
	csv) csv_list ;;
	shell) shell_list | column -t ;;
esac

#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

exit
