#!/bin/bash
# info: list mail account autoreply
# options: user domain account [format]
#
# The function of obtainin mail account autoreply message.


#----------------------------------------------------------#
#                    Variable&Function                     #
#----------------------------------------------------------#

# Argument defenition
user=$1
domain=$2
account=$3
format=${4-shell}

# Includes
source $VESTA/func/main.sh

# Json function
json_list_msg() {
    i='1'       # iterator
    echo '{'
    echo -e "\t\"$account\": {"
    echo "            \"MSG\": \"$msg\""
    echo -e "\t}\n}"
}

# Shell function
shell_list_msg() {
    if [ ! -z "$msg" ]; then
        echo -e "$msg"
    fi
}


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

check_args '2' "$#" 'user domain [format]'
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 -e ':a;N;$!ba;s/\n/\\n/g' )
fi

# Listing domains
case $format in
    json)   json_list_msg ;;
    plain)  nohead=1; shell_list_msg ;;
    shell)  shell_list_msg ;;
    *)      check_args '1' '0' '[format]'
esac


#----------------------------------------------------------#
#                       Vesta                              #
#----------------------------------------------------------#

exit
