Finish install and upgrade logic for restoring permissions

This commit is contained in:
Dante 2024-02-28 17:29:21 +00:00
parent aa99d1514f
commit 69a96a0348
6 changed files with 204 additions and 35 deletions

View file

@ -500,9 +500,9 @@ bridge:
# domain - All users on that homeserver
# mxid - Specific user
permissions:
"*": "relaybot"
"__BOTUSERS__": "puppeting"
"__BOTADMIN__": "admin"
"__LISTRELAYBOT__": "relaybot"
"__LISTPUPPETING__": "puppeting"
"__LISTADMIN__": "admin"
# Options related to the message relay Telegram bot.
relaybot:
private_chat:

View file

@ -17,7 +17,7 @@ services = ["__APP__"]
'''
type = "markdown"
[main.permissions.listrelay]
[main.permissions.listrelaybot]
ask = "Users bridged thanks to Relay Mode"
type = "tags"
help = "Relay Role: Talk on Telegram through the RelayBot in a room. No access to commands."

View file

@ -15,7 +15,7 @@ apply_permissions() {
then
#ynh_systemd_action --service_name="$app" --action=stop
# Get all entries between "permissions:" and "relay:" keys, remove the role part, remove commented parts, format it with newlines and clean whitespaces and double quotes.
allDefinedEntries=$(awk '/permissions:/{flag=1; next} /relay:/{flag=0} flag' "$install_dir/config.yaml" | sed "/: $role/d" | sed -r 's/: (admin|user|relay)//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr ',' '\n' )
allDefinedEntries=$(awk '/permissions:/{flag=1; next} /relaybot:/{flag=0} flag' "$install_dir/config.yaml" | sed "/: $role/d" | sed -r 's/: (admin|user|relaybot|full|puppeting)//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr ',' '\n' )
# Delete everything from the corresponding role to insert the new defined values. This way we also handle deletion of users.
sed -i "/permissions:/,/relaybot:/{/: $role/d;}" "$install_dir/config.yaml"
# Ensure that entries with value surrounded with quotes are deleted too. E.g. "users".

View file

@ -7,13 +7,19 @@ ynh_abort_if_errors
# SPECIFIC GETTERS FOR TOML SHORT KEY
#=================================================
get_config_permission() {
result=$(grep -- ".*: $role" "$install_dir/config.yaml" | sed -r "s/: $role//" | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr -d "'" | tr '\n' ',')
echo $result
}
get__botname() {
botname=$(ynh_app_setting_get --app $app --key botname)
echo "${botname}"
}
get__listrelaybot() {
existingRelaybotUsers=$(grep -- "\".*: relaybot" "$install_dir/config.yaml" | sed -r 's/: relaybot//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr '\n' ',')
role="relaybot"
existingRelaybotUsers=$(get_config_permission)
cat <<EOF
"$existingRelaybotUsers"
@ -21,7 +27,8 @@ EOF
}
get__listuser() {
existingUsers=$(grep -- "\".*: user" "$install_dir/config.yaml" | sed -r 's/: user//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr '\n' ',')
role="user"
existingUsers=$(get_config_permission)
cat <<EOF
"$existingUsers"
@ -29,7 +36,8 @@ EOF
}
get__listpuppeting() {
existingPuppeting=$(grep -- "\".*: puppeting" "$install_dir/config.yaml" | sed -r 's/: puppeting//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr '\n' ',')
role="puppeting"
existingPuppeting=$(get_config_permission)
cat <<EOF
"$existingPuppeting"
@ -37,7 +45,8 @@ EOF
}
get__listfull() {
existingFull=$(grep -- "\".*: full" "$install_dir/config.yaml" | sed -r 's/: full//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr '\n' ',')
role="full"
existingFull=$(get_config_permission)
cat <<EOF
"$existingFull"
@ -45,7 +54,8 @@ EOF
}
get__listadmin() {
existingAdmins=$(grep -- "\".*: admin" "$install_dir/config.yaml" | sed -r 's/: admin//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr '\n' ',')
role="admin"
existingAdmins=$(get_config_permission)
cat <<EOF
"$existingAdmins"

View file

@ -27,47 +27,43 @@ ynh_app_setting_set --app=$app --key=appserviceid --value=$appserviceid
appserviceid=$app
async_media="false"
displayname="Telegram bridge bot"
avatar="mxc://maunium.net/tJCRmUyJDsgRNgqhOgoiHWbX"
bot_username="$botname"
bot_displayname="Telegram bridge bot"
bot_avatar="mxc://maunium.net/tJCRmUyJDsgRNgqhOgoiHWbX"
device_model="mautrix-telegram"
ephemeral_events="false"
enable_metrics="false"
listen_port="8000"
os_name="Mautrix-WhatsApp bridge"
browser_name="unknown"
username_template="whatsapp_{{.}}"
personal_filtering_spaces="false"
alias_template="telegram_{userid}"
displayname_template="{displayname} (Telegram)"
username_template="telegram_{userid}"
delivery_receipts="false"
send_presence_on_typing="false"
url_previews="false"
telegram_link_preview="false"
encryption_default="false"
encryption_require="false"
admin_only="true"
print_level="info"
enable_relaybot="true"
listrelay="*"
listrelaybot="*"
listuser=""
listpuppeting="$botusers"
listfull=""
listadmin="$botadmin"
listuser="$botusers"
ynh_app_setting_set --app=$app --key=appserviceid --value=$appserviceid
ynh_app_setting_set --app=$app --key=async_media --value=$async_media
ynh_app_setting_set --app=$app --key=displayname --value=$displayname
ynh_app_setting_set --app=$app --key=avatar --value=$avatar
ynh_app_setting_set --app=$app --key=bot_displayname --value=$bot_displayname
ynh_app_setting_set --app=$app --key=bot_username --value=$bot_username
ynh_app_setting_set --app=$app --key=bot_avatar --value=$bot_avatar
ynh_app_setting_set --app=$app --key=device_model --value=$device_model
ynh_app_setting_set --app=$app --key=ephemeral_events --value=$ephemeral_events
ynh_app_setting_set --app=$app --key=enable_metrics --value=$enable_metrics
ynh_app_setting_set --app=$app --key=listen_port --value=$listen_port
ynh_app_setting_set --app=$app --key=os_name --value=$os_name
ynh_app_setting_set --app=$app --key=browser_name --value=$browser_name
ynh_app_setting_set --app=$app --key=alias_template --value=$alias_template
ynh_app_setting_set --app=$app --key=displayname_template --value=$displayname_template
ynh_app_setting_set --app=$app --key=username_template --value=$username_template
ynh_app_setting_set --app=$app --key=personal_filtering_spaces --value=$personal_filtering_spaces
ynh_app_setting_set --app=$app --key=delivery_receipts --value=$delivery_receipts
ynh_app_setting_set --app=$app --key=send_presence_on_typing --value=$send_presence_on_typing
ynh_app_setting_set --app=$app --key=url_previews --value=$url_previews
ynh_app_setting_set --app=$app --key=telegram_link_preview --value=$telegram_link_preview
ynh_app_setting_set --app=$app --key=encryption_default --value=$encryption_default
ynh_app_setting_set --app=$app --key=encryption_require --value=$encryption_require
ynh_app_setting_set --app=$app --key=enable_relaybot --value=$enable_relaybot
ynh_app_setting_set --app=$app --key=admin_only --value=$admin_only
ynh_app_setting_set --app=$app --key=print_level --value=$print_level
ynh_app_setting_set --app=$app --key=listrelay --value=$listrelay
ynh_app_setting_set --app=$app --key=listrelaybot --value=$listrelaybot
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
@ -108,6 +104,13 @@ ynh_add_config --template="config.yaml" --destination="$install_dir/config.yaml"
chmod 400 "$install_dir/config.yaml"
chown $app:$app "$install_dir/config.yaml"
# This calls allows to set multiple users during install question "botusers" specifying them separated by a comma
set__listrelaybot
set__listuser
set__listpuppeting
set__listfull
set__listadmin
#=================================================
# INSTALL MAUTRIX-BRIDGE PYTHON MODULE
#=================================================
@ -118,7 +121,7 @@ mkdir -p /var/log/$app
python3 -m venv $install_dir
export HOME=$install_dir
$install_dir/bin/pip3 install --upgrade pip setuptools wheel
$install_dir/bin/pip3 install --upgrade $install_dir/src/mautrix-telegram.tar.gz[e2be,speedups,qr_login] # metrics,hq_thumbnails,sqlite,formattednumbers
$install_dir/bin/pip3 install --upgrade $install_dir/src/mautrix-telegram.tar.gz[e2be,speedups] # metrics,hq_thumbnails,sqlite,formattednumbers
#=================================================
# REGISTER SYNAPSE APP-SERVICE

View file

@ -12,6 +12,31 @@ source /usr/share/yunohost/helpers
server_name=$(ynh_app_setting_get --app=$app --key=server_name)
synapse_db_name="matrix_$synapse_instance"
#=================================================
# GET CONFIG PANEL SETTINGS
#=================================================
async_media=$(ynh_app_setting_get --app=$app --key=async_media)
bot_displayname=$(ynh_app_setting_get --app=$app --key=bot_displayname)
bot_username=$(ynh_app_setting_get --app=$app --key=bot_username)
bot_avatar=$(ynh_app_setting_get --app=$app --key=bot_avatar)
device_model=$(ynh_app_setting_get --app=$app --key=device_model)
ephemeral_events=$(ynh_app_setting_get --app=$app --key=ephemeral_events)
enable_metrics=$(ynh_app_setting_get --app=$app --key=enable_metrics)
listen_port=$(ynh_app_setting_get --app=$app --key=listen_port)
alias_template=$(ynh_app_setting_get --app=$app --key=alias_template)
displayname_template=$(ynh_app_setting_get --app=$app --key=displayname_template)
username_template=$(ynh_app_setting_get --app=$app --key=username_template)
delivery_receipts=$(ynh_app_setting_get --app=$app --key=delivery_receipts)
telegram_link_preview=$(ynh_app_setting_get --app=$app --key=telegram_link_preview)
encryption_default=$(ynh_app_setting_get --app=$app --key=encryption_default)
encryption_require=$(ynh_app_setting_get --app=$app --key=encryption_require)
listrelaybot=$(ynh_app_setting_get --app=$app --key=listrelaybot)
listuser=$(ynh_app_setting_get --app=$app --key=listuser)
listpuppeting=$(ynh_app_setting_get --app=$app --key=listpuppeting)
listfull=$(ynh_app_setting_get --app=$app --key=listfull)
listadmin=$(ynh_app_setting_get --app=$app --key=listadmin)
#=================================================
# CHECK VERSION
#=================================================
@ -56,6 +81,112 @@ then
ynh_app_setting_set --app=$app --key=psqlpwd --value=$db_pwd
fi
#=================================================
# SET STANDARD SETTINGS FROM DEFAULT CONFIG
#=================================================
if [ -z "$async_media" ]
then
async_media="false"
ynh_app_setting_set --app=$app --key=async_media --value=$async_media
fi
if [ -z "$bot_username" ]
then
bot_username="$botname"
ynh_app_setting_set --app=$app --key=bot_username --value=$bot_username
fi
if [ -z "$bot_displayname" ]
then
bot_displayname="Telegram bridge bot"
ynh_app_setting_set --app=$app --key=bot_displayname --value=$bot_displayname
fi
if [ -z "$bot_avatar" ]
then
bot_avatar="mxc://maunium.net/tJCRmUyJDsgRNgqhOgoiHWbX"
ynh_app_setting_set --app=$app --key=bot_avatar --value=$bot_avatar
fi
if [ -z "$device_model" ]
then
device_model="mautrix-telegram"
ynh_app_setting_set --app=$app --key=device_model --value=$device_model
fi
if [ -z "$ephemeral_events" ]
then
ephemeral_events="false"
ynh_app_setting_set --app=$app --key=ephemeral_events --value=$ephemeral_events
fi
if [ -z "$enable_metrics" ]
then
enable_metrics="false"
ynh_app_setting_set --app=$app --key=enable_metrics --value=$enable_metrics
fi
if [ -z "$listen_port" ]
then
listen_port="8000"
ynh_app_setting_set --app=$app --key=listen_port --value=$listen_port
fi
if [ -z "$alias_template" ]
then
alias_template="telegram_{userid}"
ynh_app_setting_set --app=$app --key=alias_template --value=$alias_template
fi
if [ -z "$displayname_template" ]
then
displayname_template="{displayname} (Telegram)"
ynh_app_setting_set --app=$app --key=displayname_template --value=$displayname_template
fi
if [ -z "$username_template" ]
then
username_template="telegram_{userid}"
ynh_app_setting_set --app=$app --key=username_template --value=$username_template
fi
if [ -z "$delivery_receipts" ]
then
delivery_receipts="false"
ynh_app_setting_set --app=$app --key=delivery_receipts --value=$delivery_receipts
fi
if [ -z "$telegram_link_preview" ]
then
telegram_link_preview="false"
ynh_app_setting_set --app=$app --key=telegram_link_preview --value=$telegram_link_preview
fi
if [ -z "$encryption_default" ]
then
encryption_default="false"
ynh_app_setting_set --app=$app --key=encryption_default --value=$encryption_default
fi
if [ -z "$encryption_require" ]
then
encryption_require="false"
ynh_app_setting_set --app=$app --key=encryption_require --value=$encryption_require
fi
if [ -z "$listrelaybot" ]
then
listrelaybot="*"
ynh_app_setting_set --app=$app --key=listrelaybot --value=$listrelaybot
fi
if [ -z "$listuser" ]
then
listuser=""
ynh_app_setting_set --app=$app --key=listuser --value=$listuser
fi
if [ -z "$listpuppeting" ]
then
listpuppeting=$(ynh_app_setting_get --app=$app --key=botusers)
ynh_app_setting_set --app=$app --key=listpuppeting --value=$listpuppeting
ynh_app_setting_delete --app=$app --key=botusers
fi
if [ -z "$listfull" ]
then
listfull=""
ynh_app_setting_set --app=$app --key=listfull --value=$listfull
fi
if [ -z "$listadmin" ]
then
listadmin=$(ynh_app_setting_get --app=$app --key=botadmin)
ynh_app_setting_set --app=$app --key=listadmin --value=$listadmin
ynh_app_setting_delete --app=$app --key=botadmin
fi
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
@ -79,6 +210,18 @@ chown -R $app:$app "$install_dir"
#=================================================
ynh_script_progression --message="Updating a configuration file..." --weight=2
# reset permissions to be able to apply_permissions with app_setting values after upgrade
listrelaybot=$listrelaybot
listuser=$listuser
listpuppeting=$listpuppeting
listfull=$listfull
listadmin=$listadmin
listrelaybot="*"
listuser="@user:domain.tld"
listpuppeting="@puppet:domain.tld"
listfull="@full:domain.tld"
listadmin="@admin:domain.tld"
# main configuration
ynh_backup_if_checksum_is_different --file="$install_dir/config.yaml"
# as_token hs_token are autogenerated, save them before regenerating the config
@ -95,6 +238,19 @@ ynh_add_config --template="../conf/config.yaml" --destination="$install_dir/conf
chmod 400 "$install_dir/config.yaml"
chown $app:$app "$install_dir/config.yaml"
listrelaybot=$listrelaybot_
listuser=$listuser_
listpuppeting=$listpuppeting_
listfull=$listfull_
listadmin=$listadmin_
# apply_permissions to have correct syntax in config file
set__listrelaybot
set__listuser
set__listpuppeting
set__listfull
set__listadmin
#=================================================
# UPGRADE MAUTRIX-BRIDGE PYTHON MODULE
#=================================================