From a8b80e01b8872a568526d368d1fb73053e738105 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@freedombone.net>
Date: Wed, 3 Oct 2018 20:54:10 +0100
Subject: [PATCH] smtp proxy settings in control panel

---
 src/freedombone-controlpanel | 84 ++++++++++++++++--------------------
 1 file changed, 37 insertions(+), 47 deletions(-)

diff --git a/src/freedombone-controlpanel b/src/freedombone-controlpanel
index 13c32e1ca..73540f1fc 100755
--- a/src/freedombone-controlpanel
+++ b/src/freedombone-controlpanel
@@ -64,6 +64,8 @@ do
     source "$f"
 done
 
+source "/usr/share/${PROJECT_NAME}/base/${PROJECT_NAME}-base-email"
+
 APP_FILES="/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*"
 for f in $APP_FILES
 do
@@ -87,14 +89,6 @@ MUMBLE_ONION_PORT=8095
 
 SSH_PORT=2222
 
-# outgoing SMTP proxy
-SMTP_PROXY_ENABLE=$'no'
-SMTP_PROXY_PROTOCOL='smtps'
-SMTP_PROXY_SERVER='mail.myispdomain'
-SMTP_PROXY_PORT=465
-SMTP_PROXY_USERNAME=''
-SMTP_PROXY_PASSWORD=''
-
 WIFI_INTERFACE=wlan0
 WIFI_SSID=
 WIFI_TYPE='wpa2-psk'
@@ -112,12 +106,6 @@ read_config_param WIFI_TYPE
 read_config_param WIFI_SSID
 read_config_param WIFI_PASSPHRASE
 read_config_param SSH_PORT
-read_config_param SMTP_PROXY_ENABLE
-read_config_param SMTP_PROXY_PROTOCOL
-read_config_param SMTP_PROXY_SERVER
-read_config_param SMTP_PROXY_PORT
-read_config_param SMTP_PROXY_USERNAME
-read_config_param SMTP_PROXY_PASSWORD
 read_config_param USB_DRIVE
 read_config_param MY_USERNAME
 read_config_param ONION_ONLY
@@ -1543,20 +1531,35 @@ function email_extra_domains {
 
 function email_smtp_proxy {
     MUTTRC_FILE=/home/$ADMIN_USER/.muttrc
-    if [ ! -f $MUTTRC_FILE ]; then
-        return
+
+    isp_smtp_domain=
+    isp_smtp_port=465
+    isp_smtp_username=
+
+    if [ -f /etc/exim4/passwd.client ]; then
+        line_str=$(tail -n 1 /etc/exim4/passwd.client)
+        if [[ "$line_str" != '#'* ]]; then
+            isp_smtp_domain=$(tail -n 1 /etc/exim4/passwd.client | awk -F ':' '{print $1}')
+            isp_smtp_username=$(tail -n 1 /etc/exim4/passwd.client | awk -F ':' '{print $2}')
+        fi
+    fi
+
+    # get the remote SMTP port number
+    if [ -f /etc/exim4/update-exim4.conf.conf ]; then
+        smarthost_str=$(grep 'dc_smarthost=' /etc/exim4/update-exim4.conf.conf)
+        if [[ "$smarthost_str" == *'::'* ]]; then
+            isp_smtp_port=$(echo "$smarthost_str" | awk -F '::' '{print $2}')
+        fi
     fi
 
     data=$(mktemp 2>/dev/null)
     dialog --backtitle $"Freedombone Control Panel" \
-           --title $"SMTP Proxy for $ADMIN_USER" \
-           --form $"You may need to proxy outgoing email via your ISP's mail server. If so enter the details below." 14 75 6 \
-           $"Enable proxy:" 1 1 "$SMTP_PROXY_ENABLE" 1 24 5 5 \
-           $"Protocol (smtp/smtps):" 2 1 "$SMTP_PROXY_PROTOCOL" 2 24 5 5 \
-           $"ISP mail server:" 3 1 "$SMTP_PROXY_SERVER" 3 24 40 10000 \
-           $"Port:" 4 1 "$SMTP_PROXY_PORT" 4 24 5 5 \
-           $"Username:" 5 1 "$SMTP_PROXY_USERNAME" 5 24 40 10000 \
-           $"Password:" 6 1 "$SMTP_PROXY_PASSWORD" 6 24 40 10000 \
+           --title $"SMTP Proxy" \
+           --form $"You may need to proxy outgoing email via your ISP's mail server. If so enter the details below." 14 75 4 \
+           $"ISP mail server:" 3 1 "$SMTP_PROXY_SERVER" 1 24 40 10000 \
+           $"Port:" 4 1 "$SMTP_PROXY_PORT" 2 24 5 5 \
+           $"Username:" 5 1 "$SMTP_PROXY_USERNAME" 3 24 40 10000 \
+           $"Password:" 6 1 "" 4 24 40 10000 \
            2> "$data"
     sel=$?
     case $sel in
@@ -1565,35 +1568,22 @@ function email_smtp_proxy {
         255) rm -f "$data"
              return;;
     esac
-    SMTP_PROXY_ENABLE=$(sed -n 1p < "$data")
-    SMTP_PROXY_PROTOCOL=$(sed -n 2p < "$data")
-    SMTP_PROXY_SERVER=$(sed -n 3p < "$data")
-    SMTP_PROXY_PORT=$(sed -n 4p < "$data")
-    SMTP_PROXY_USERNAME=$(sed -n 5p < "$data")
-    SMTP_PROXY_PASSWORD=$(sed -n 6p < "$data")
+    isp_smtp_domain=$(sed -n 1p < "$data")
+    isp_smtp_port=$(sed -n 2p < "$data")
+    isp_smtp_username=$(sed -n 3p < "$data")
+    isp_smtp_password=$(sed -n 4p < "$data")
     rm -f "$data"
 
+    email_smtp_proxy_through_isp "$isp_smtp_domain" "$isp_smtp_port" "$isp_smtp_username" "$isp_smtp_password"
+
     # change muttrc
-    if [ "$SMTP_PROXY_ENABLE" != $'no' ]; then
-        if ! grep -q "set smtp_url" "$MUTTRC_FILE"; then
-            echo "set smtp_url=\"${SMTP_PROXY_PROTOCOL}://${SMTP_PROXY_USERNAME}:${SMTP_PROXY_PASSWORD}@${SMTP_PROXY_SERVER}:${SMTP_PROXY_PORT}/\"" >> "$MUTTRC_FILE"
-        else
-            sed -i "s|set smtp_url=.*|set smtp_url=\"${SMTP_PROXY_PROTOCOL}://${SMTP_PROXY_USERNAME}:${SMTP_PROXY_PASSWORD}@${SMTP_PROXY_SERVER}:${SMTP_PROXY_PORT}/\"|g" "$MUTTRC_FILE"
-        fi
-        sed -i 's|#set smtp_url|set smtp_url|g' "$MUTTRC_FILE"
-    else
+    if [ -f $MUTTRC_FILE ]; then
         if grep -q "set smtp_url" "$MUTTRC_FILE"; then
-            sed -i 's|set smtp_url|#set smtp_url|g' "$MUTTRC_FILE"
+            if ! grep -q "#set smtp_url" "$MUTTRC_FILE"; then
+                sed -i 's|set smtp_url|#set smtp_url|g' "$MUTTRC_FILE"
+            fi
         fi
     fi
-
-    # save settings within the main configuration file
-    write_config_param "SMTP_PROXY_ENABLE" "$SMTP_PROXY_ENABLE"
-    write_config_param "SMTP_PROXY_PROTOCOL" "$SMTP_PROXY_PROTOCOL"
-    write_config_param "SMTP_PROXY_SERVER" "$SMTP_PROXY_SERVER"
-    write_config_param "SMTP_PROXY_PORT" "$SMTP_PROXY_PORT"
-    write_config_param "SMTP_PROXY_USERNAME" "$SMTP_PROXY_USERNAME"
-    write_config_param "SMTP_PROXY_PASSWORD" "$SMTP_PROXY_PASSWORD"
 }
 
 function menu_backup_restore {
-- 
GitLab