Skip to content

Show users an SMTP protocol configuration option, preset based on the given port #881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 73 additions & 10 deletions discourse-setup
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,17 @@ check_port() {
## read a variable from the config file
##
read_config() {
local config_line
config_line=`grep -E "^ #?$1:" $web_file`
read_config_result=`echo $config_line | awk -F":" '{print $2}'`
read_config_result=`echo $read_config_result | sed "s/^\([\"']\)\(.*\)\1\$/\2/g"`
}

read_default() {
config_line=`grep -E "^ #?$1:" samples/standalone.yml`
read_default_result=`echo $config_line | awk -F":" '{print $2}'`
read_default_result=`echo $read_config_result | sed "s/^\([\"']\)\(.*\)\1\$/\2/g"`
local default_line
default_line=`grep -E "^ #?$1:" samples/standalone.yml`
read_default_result=`echo $default_line | awk -F":" '{print $2}'`
read_default_result=`echo $read_default_result | sed "s/^\([\"']\)\(.*\)\1\$/\2/g"`
}

assert_maxmind_envs() {
Expand Down Expand Up @@ -445,6 +447,10 @@ ask_user_for_config() {
local developer_emails=$read_config_result
read_config "DISCOURSE_SMTP_PASSWORD"
local smtp_password=$read_config_result
read_config "DISCOURSE_SMTP_ENABLE_START_TLS"
local smtp_enable_start_tls=$read_config_result
read_config "DISCOURSE_SMTP_FORCE_TLS"
local smtp_force_tls=$read_config_result
read_config "DISCOURSE_SMTP_PORT"
local smtp_port=$read_config_result
read_config "DISCOURSE_SMTP_USER_NAME"
Expand Down Expand Up @@ -557,6 +563,46 @@ ask_user_for_config() {
fi
fi

##
## automatically suggest TLS if port 465, otherwise keep STARTTLS
##
if [ ! -z "$smtp_enable_start_tls" ]
then
if [ "$smtp_port" == 465 ]
then
smtp_protocol="TLS"
else
smtp_protocol="STARTTLS"
fi
local smtp_protocol_valid="n"
until [ "$smtp_protocol_valid" == "y" ]
do
read -p "SMTP protocol? [$smtp_protocol]: " new_value
if [ ! -z "$new_value" ]
then
if [ "$new_value" == 'TLS' ] || [ "$new_value" == 'STARTTLS' ]
then
smtp_protocol="$new_value"
smtp_protocol_valid="y"
else
echo
echo "[Error] Invalid SMTP protocol (TLS or STARTTLS)"
echo
fi
else
smtp_protocol_valid="y"
fi
done
if [ "$smtp_protocol" == "STARTTLS" ]
then
smtp_enable_start_tls='true'
smtp_force_tls='false'
else
smtp_enable_start_tls='false'
smtp_force_tls='true'
fi
fi

##
## automatically set correct user name based on common mail providers unless it's been set
##
Expand Down Expand Up @@ -644,6 +690,7 @@ ask_user_for_config() {
echo "Email : $developer_emails"
echo "SMTP address : $smtp_address"
echo "SMTP port : $smtp_port"
echo "SMTP protocol : $smtp_protocol"
echo "SMTP username : $smtp_user_name"
echo "SMTP password : $smtp_password"
echo "Notification email: $notification_email"
Expand Down Expand Up @@ -707,6 +754,28 @@ ask_user_for_config() {
update_ok="n"
fi

sed -i -e "s/^ #\?DISCOURSE_SMTP_ENABLE_START_TLS:.*/ DISCOURSE_SMTP_ENABLE_START_TLS: $smtp_enable_start_tls/w $changelog" $web_file
if [ -s $changelog ]
then
rm $changelog
else
echo "DISCOURSE_SMTP_ENABLE_START_TLS change failed."
update_ok="n"
fi

sed -i -e "s/^ #\?DISCOURSE_SMTP_FORCE_TLS:.*/ DISCOURSE_SMTP_FORCE_TLS: $smtp_force_tls/w $changelog" $web_file
if [ -s $changelog ]
then
rm $changelog
else
sed -i "/^.*DISCOURSE_SMTP_ENABLE_START_TLS:.*/a \ \ DISCOURSE_SMTP_FORCE_TLS: $smtp_force_tls" $web_file
if ! grep -q '^ DISCOURSE_SMTP_FORCE_TLS:' "$web_file"
then
echo "DISCOURSE_SMTP_FORCE_TLS change failed."
update_ok="n"
fi
fi

sed -i -e "s/^ #\?DISCOURSE_SMTP_USER_NAME:.*/ DISCOURSE_SMTP_USER_NAME: $smtp_user_name/w $changelog" $web_file
if [ -s $changelog ]
then
Expand Down Expand Up @@ -866,17 +935,11 @@ validate_config() {

if [ ! -z "$result" ]
then
if [[ "$config_line" = *"$default"* ]]
if [[ "$result" = "$default" ]]
then
echo "$x left at incorrect default of $default"
valid_config="n"
fi
config_val=`echo $config_line | awk '{print $2}'`
if [ -z $config_val ]
then
echo "$x was not configured"
valid_config="n"
fi
else
echo "$x not present"
valid_config="n"
Expand Down