#!/bin/bash
# info: list mail account autoreply
# options: USER DOMAIN ACCOUNT [FORMAT]
#
# example: v-list-mail-account-autoreply admin example.com testing
#
# This function of obtaining mail account autoreply message.

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

# Argument definition
user=$1
domain=$2
account=$3
format=${4-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() {
	TO_ESCAPE='\\'
	msg=$(echo "$msg" | sed -e "s|${TO_ESCAPE}|${TO_ESCAPE}${TO_ESCAPE}|g" -e 's/"/\\"/g' -e "s/%quote%/'/g")
	echo '{'
	echo -e "\t\"$account\": {"
	echo "            \"MSG\": \"$msg\""
	echo -e "\t}\n}"
}

# SHELL list function
shell_list() {
	echo "$msg"
}

# PLAIN list function
plain_list() {
	echo "$msg"
}

# CSV list function
csv_list() {
	echo "MSG"
	echo "$msg"
}

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

check_args '3' "$#" 'USER DOMAIN ACCOUNT [FORMAT]'
is_format_valid 'user' 'domain' 'account'
is_object_valid 'user' 'USER' "$user"
is_object_valid 'mail' 'DOMAIN' "$domain"
is_object_unsuspended 'mail' 'DOMAIN' "$domain"
is_object_valid "mail/$domain" 'ACCOUNT' "$account"
is_object_unsuspended "mail/$domain" 'ACCOUNT' "$account"

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

if [ -e "$USER_DATA/mail/$account@$domain.msg" ]; then
	msg=$(cat $USER_DATA/mail/$account@$domain.msg \
		| sed ':a;N;$!ba;s/\n/\\n/g')
fi

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

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

exit
