#!/bin/sh
#
# Copyright 2026 Rafał Wabik (IceG) - From eko.one.pl forum
# Licensed to the GNU General Public License v3.0.
#

. /usr/share/libubox/jshn.sh

send_email() {
    local to="$1"
    local subject="$2"
    local body="$3"
    local error_msg=""
    
    local from=$(uci -q get sms_tool_js.@sms_tool_js[0].forward_sms_mail_sender)
    local user=$(uci -q get sms_tool_js.@sms_tool_js[0].forward_sms_mail_user)
    local password=$(uci -q get sms_tool_js.@sms_tool_js[0].forward_sms_mail_password)
    local smtp=$(uci -q get sms_tool_js.@sms_tool_js[0].forward_sms_mail_smtp)
    local port=$(uci -q get sms_tool_js.@sms_tool_js[0].forward_sms_mail_smtp_port)
    local security=$(uci -q get sms_tool_js.@sms_tool_js[0].forward_sms_mail_security)

    if [ -z "$to" ]; then
        echo "Recipient email address is not configured"
        return 1
    fi
    
    if [ -z "$from" ]; then
        echo "Sender email address is not configured"
        return 1
    fi
    
    if [ -z "$user" ]; then
        echo "Email user (login) is not configured"
        return 1
    fi
    
    if [ -z "$password" ]; then
        echo "Email password is not configured"
        return 1
    fi
    
    if [ -z "$smtp" ]; then
        echo "SMTP server is not configured"
        return 1
    fi
    
    if [ -z "$port" ]; then
        echo "SMTP port is not configured"
        return 1
    fi
    
    local security_opts="-starttls -auth-login"
    case "$security" in
        ssl) security_opts="-ssl -auth" ;;
        tls) security_opts="-starttls -auth-login" ;;
        *) security_opts="-starttls -auth-login" ;;
    esac
    
    result=$(mailsend $security_opts \
        -smtp "$smtp" -port "$port" \
        -ct 5 -read-timeout 5 \
        -cs utf-8 \
        -user "$user" -pass "$password" \
        -f "$from" -t "$to" \
        -sub "$subject" \
        -M "$body" 2>&1)
    
    local ret=$?
    
    if [ $ret -ne 0 ]; then
        echo "$result" | grep -i "error\|failed\|unable\|denied\|authentication\|connection" || echo "$result"
        return $ret
    fi
    
    echo "success"
    return 0
}

case "$1" in
    list)
        echo '{ "forward": { "subject": "str", "message": "str" } }'
    ;;
    call)
        case "$2" in
            forward)
                read input
                json_load "$input"
                json_get_var subject subject
                json_get_var message message

                recipient=$(uci -q get sms_tool_js.@sms_tool_js[0].forward_sms_mail_recipient)
                
                error_output=$(send_email "$recipient" "$subject" "$message")
                result_code=$?
                
                if [ $result_code -eq 0 ]; then
                    json_init
                    json_add_boolean success 1
                    json_dump
                else
                    json_init
                    json_add_boolean success 0
                    json_add_string error "$error_output"
                    json_dump
                fi
            ;;
        esac
    ;;
esac
