#!/bin/bash
# info: list mail domain
# options: USER DOMAIN [FORMAT]
#
# example: v-list-mail-domain user01 mydomain.com
#
# This function of obtaining the list of domain parameters.

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

# Argument definition
user=$1
domain=$2
format=${3-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() {
	parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/mail.conf)
	echo '{'
	echo '    "'$DOMAIN'": {
        "ANTIVIRUS": "'$ANTIVIRUS'",
        "ANTISPAM": "'$ANTISPAM'",
        "DKIM": "'$DKIM'",
        "CATCHALL": "'$CATCHALL'",
        "ACCOUNTS": "'$ACCOUNTS'",
        "RATE_LIMIT": "'$RATE_LIMIT'",
        "REJECT": "'$REJECT'",
        "U_DISK": "'$U_DISK'",
        "SSL": "'$SSL'",
        "LETSENCRYPT": "'$LETSENCRYPT'",
        "SUSPENDED": "'$SUSPENDED'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'",
        "WEBMAIL_ALIAS": "'$WEBMAIL_ALIAS.$domain'",
        "WEBMAIL":"'$WEBMAIL'",
        "U_SMTP_RELAY":"'$U_SMTP_RELAY'",
        "U_SMTP_RELAY_HOST":"'$U_SMTP_RELAY_HOST'",
        "U_SMTP_RELAY_PORT":"'$U_SMTP_RELAY_PORT'",
        "U_SMTP_RELAY_USERNAME":"'$U_SMTP_RELAY_USERNAME'"
        }'
	echo '}'
}

# SHELL list function
shell_list() {
	echo "DOMAIN:             $DOMAIN"
	echo "ANTIVIRUS:          $ANTIVIRUS"
	echo "ANTISPAM:           $ANTISPAM"
	echo "DKIM:               $DKIM"
	echo "CATCHALL:           $CATCHALL"
	echo "ACCOUNTS:           $ACCOUNTS"
	echo "DISK:               $U_DISK"
	echo "SSL:                $SSL"
	echo "RATE_LIMIT:         $RATE_LIMIT"
	echo "REJECT:             $REJECT"
	echo "LETSENCRYPT:        $LETSENCRYPT"
	echo "SUSPENDED:          $SUSPENDED"
	echo "TIME:               $TIME"
	echo "DATE:               $DATE"
	echo "WEBMAIL_ALIAS:      $WEBMAIL_ALIAS.$domain"
	echo "WEBMAIL:            $WEBMAIL"
	echo "U_SMTP_RELAY:          $U_SMTP_RELAY"
	echo "U_SMTP_RELAY_HOST      $U_SMTP_RELAY_HOST"
	echo "U_SMTP_RELAY_PORT      $U_SMTP_RELAY_PORT"
	echo "U_SMTP_RELAY_USERNAME  $U_SMTP_RELAY_USERNAME"
}

# PLAIN list function
plain_list() {
	echo -ne "$DOMAIN\t$ANTIVIRUS\t$ANTISPAM\t$DKIM\t$CATCHALL\t"
	echo -e "$ACCOUNTS\t$U_DISK\t$SSL\t$LETSENCRYPT\t$SUSPENDED\t$TIME\t$DATE\t$WEBMAIL_ALIAS.$domain\t$WEBMAIL\t"
	echo -e "$U_SMTP_RELAY\t$U_SMTP_RELAY_HOST\t$U_SMTP_RELAY_PORT\t$U_SMTP_RELAY_USERNAME"
}

# CSV list function
csv_list() {
	echo -n "DOMAIN,ANTIVIRUS,ANTISPAM,DKIM,CATCHALL,ACCOUNTS,U_DISK,"
	echo "SSL,LETSENCRYPT,SUSPENDED,TIME,DATE,WEBMAIL_ALIAS,WEBMAIL,"
	echo "U_SMTP_RELAY,U_SMTP_RELAY_HOST,U_SMTP_RELAY_PORT,U_SMTP_RELAY_USERNAME"
	echo -n "$DOMAIN,$ANTIVIRUS,$ANTISPAM,$DKIM,$CATCHALL,$ACCOUNTS,$U_DISK,"
	echo "$SSL,$LETSENCRYPT,$SUSPENDED,$TIME,$DATE,$WEBMAIL_ALIAS.$domain,$WEBMAIL,"
	echo "$U_SMTP_RELAY,$U_SMTP_RELAY_HOST,$U_SMTP_RELAY_PORT,$U_SMTP_RELAY_USERNAME"
}

#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

check_args '2' "$#" 'USER DOMAIN [FORMAT]'
is_format_valid 'user' 'domain'
is_object_valid 'user' 'USER' "$user"
is_object_valid 'mail' 'DOMAIN' "$domain"

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

# Parsing mail domain
parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/mail.conf)

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

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

exit
