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 # domain - All users on that homeserver
# mxid - Specific user # mxid - Specific user
permissions: permissions:
"*": "relaybot" "__LISTRELAYBOT__": "relaybot"
"__BOTUSERS__": "puppeting" "__LISTPUPPETING__": "puppeting"
"__BOTADMIN__": "admin" "__LISTADMIN__": "admin"
# Options related to the message relay Telegram bot. # Options related to the message relay Telegram bot.
relaybot: relaybot:
private_chat: private_chat:

View file

@ -17,7 +17,7 @@ services = ["__APP__"]
''' '''
type = "markdown" type = "markdown"
[main.permissions.listrelay] [main.permissions.listrelaybot]
ask = "Users bridged thanks to Relay Mode" ask = "Users bridged thanks to Relay Mode"
type = "tags" type = "tags"
help = "Relay Role: Talk on Telegram through the RelayBot in a room. No access to commands." 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 then
#ynh_systemd_action --service_name="$app" --action=stop #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. # 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. # 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" sed -i "/permissions:/,/relaybot:/{/: $role/d;}" "$install_dir/config.yaml"
# Ensure that entries with value surrounded with quotes are deleted too. E.g. "users". # 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 # 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() { get__botname() {
botname=$(ynh_app_setting_get --app $app --key botname) botname=$(ynh_app_setting_get --app $app --key botname)
echo "${botname}" echo "${botname}"
} }
get__listrelaybot() { 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 cat <<EOF
"$existingRelaybotUsers" "$existingRelaybotUsers"
@ -21,7 +27,8 @@ EOF
} }
get__listuser() { 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 cat <<EOF
"$existingUsers" "$existingUsers"
@ -29,7 +36,8 @@ EOF
} }
get__listpuppeting() { 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 cat <<EOF
"$existingPuppeting" "$existingPuppeting"
@ -37,7 +45,8 @@ EOF
} }
get__listfull() { 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 cat <<EOF
"$existingFull" "$existingFull"
@ -45,7 +54,8 @@ EOF
} }
get__listadmin() { 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 cat <<EOF
"$existingAdmins" "$existingAdmins"

View file

@ -27,47 +27,43 @@ ynh_app_setting_set --app=$app --key=appserviceid --value=$appserviceid
appserviceid=$app appserviceid=$app
async_media="false" async_media="false"
displayname="Telegram bridge bot" bot_username="$botname"
avatar="mxc://maunium.net/tJCRmUyJDsgRNgqhOgoiHWbX" bot_displayname="Telegram bridge bot"
bot_avatar="mxc://maunium.net/tJCRmUyJDsgRNgqhOgoiHWbX"
device_model="mautrix-telegram"
ephemeral_events="false" ephemeral_events="false"
enable_metrics="false" enable_metrics="false"
listen_port="8000" listen_port="8000"
os_name="Mautrix-WhatsApp bridge" alias_template="telegram_{userid}"
browser_name="unknown" displayname_template="{displayname} (Telegram)"
username_template="whatsapp_{{.}}" username_template="telegram_{userid}"
personal_filtering_spaces="false"
delivery_receipts="false" delivery_receipts="false"
send_presence_on_typing="false" telegram_link_preview="false"
url_previews="false"
encryption_default="false" encryption_default="false"
encryption_require="false" encryption_require="false"
admin_only="true" listrelaybot="*"
print_level="info" listuser=""
enable_relaybot="true" listpuppeting="$botusers"
listrelay="*" listfull=""
listadmin="$botadmin" listadmin="$botadmin"
listuser="$botusers"
ynh_app_setting_set --app=$app --key=appserviceid --value=$appserviceid 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=async_media --value=$async_media
ynh_app_setting_set --app=$app --key=displayname --value=$displayname ynh_app_setting_set --app=$app --key=bot_displayname --value=$bot_displayname
ynh_app_setting_set --app=$app --key=avatar --value=$avatar 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=ephemeral_events --value=$ephemeral_events
ynh_app_setting_set --app=$app --key=enable_metrics --value=$enable_metrics 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=listen_port --value=$listen_port
ynh_app_setting_set --app=$app --key=os_name --value=$os_name ynh_app_setting_set --app=$app --key=alias_template --value=$alias_template
ynh_app_setting_set --app=$app --key=browser_name --value=$browser_name 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=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=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=telegram_link_preview --value=$telegram_link_preview
ynh_app_setting_set --app=$app --key=url_previews --value=$url_previews
ynh_app_setting_set --app=$app --key=encryption_default --value=$encryption_default 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=encryption_require --value=$encryption_require
ynh_app_setting_set --app=$app --key=enable_relaybot --value=$enable_relaybot ynh_app_setting_set --app=$app --key=listrelaybot --value=$listrelaybot
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
#================================================= #=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE # 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" chmod 400 "$install_dir/config.yaml"
chown $app:$app "$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 # INSTALL MAUTRIX-BRIDGE PYTHON MODULE
#================================================= #=================================================
@ -118,7 +121,7 @@ mkdir -p /var/log/$app
python3 -m venv $install_dir python3 -m venv $install_dir
export HOME=$install_dir export HOME=$install_dir
$install_dir/bin/pip3 install --upgrade pip setuptools wheel $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 # 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) server_name=$(ynh_app_setting_get --app=$app --key=server_name)
synapse_db_name="matrix_$synapse_instance" 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 # CHECK VERSION
#================================================= #=================================================
@ -56,6 +81,112 @@ then
ynh_app_setting_set --app=$app --key=psqlpwd --value=$db_pwd ynh_app_setting_set --app=$app --key=psqlpwd --value=$db_pwd
fi 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 # 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 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 # main configuration
ynh_backup_if_checksum_is_different --file="$install_dir/config.yaml" ynh_backup_if_checksum_is_different --file="$install_dir/config.yaml"
# as_token hs_token are autogenerated, save them before regenerating the config # 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" chmod 400 "$install_dir/config.yaml"
chown $app:$app "$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 # UPGRADE MAUTRIX-BRIDGE PYTHON MODULE
#================================================= #=================================================