Skip to content
Snippets Groups Projects
freedombone-app-pleroma 78.9 KiB
Newer Older
Bob Mottram's avatar
Bob Mottram committed
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud
Bob Mottram's avatar
Bob Mottram committed
#
# Pleroma backend application
Bob Mottram's avatar
Bob Mottram committed
# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2017-2019 Bob Mottram <bob@freedombone.net>
Bob Mottram's avatar
Bob Mottram committed
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

Bob Mottram's avatar
Bob Mottram committed
VARIANTS='full full-vim social'
Bob Mottram's avatar
Bob Mottram committed

Bob Mottram's avatar
Bob Mottram committed
APP_CATEGORY=social

Bob Mottram's avatar
Bob Mottram committed
IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=1
REQUIRES_APP=
Bob Mottram's avatar
Bob Mottram committed

PLEROMA_DOMAIN_NAME=
PLEROMA_CODE=
PLEROMA_PORT=4000
PLEROMA_ONION_PORT=8011
PLEROMA_REPO="https://git.pleroma.social/pleroma/pleroma.git"
Bob Mottram's avatar
Bob Mottram committed
PLEROMA_COMMIT='99fd199bda8bd90cd3e8c69d54087531ddc02eac'
Bob Mottram's avatar
Bob Mottram committed
PLEROMA_ADMIN_PASSWORD=
Bob Mottram's avatar
Bob Mottram committed
PLEROMA_DIR=/etc/pleroma
PLEROMA_SECRET_KEY=""
pleroma_secret=$PLEROMA_DIR/config/dev.secret.exs
Bob Mottram's avatar
Bob Mottram committed

PLEROMA_BACKGROUND_IMAGE_URL=

PLEROMA_TITLE='Pleroma Server'

# Number of months after which posts expire
PLEROMA_EXPIRE_MONTHS=3
Bob Mottram's avatar
Bob Mottram committed
pleroma_expire_posts_script=/usr/bin/pleroma-expire-posts
Bob Mottram's avatar
Bob Mottram committed
blocking_script_file=/usr/bin/pleroma-blocking
Bob Mottram's avatar
Bob Mottram committed

emoji_resolution='128x128'

# limit hellthreads with zillions of mentions
Bob Mottram's avatar
Bob Mottram committed
maximum_tagged_thread_participants=7
Bob Mottram's avatar
Bob Mottram committed
# whether the install function is running
pleroma_installing=

Bob Mottram's avatar
Bob Mottram committed
PLEROMA_SHORT_DESCRIPTION=$'Federated microblogging'
PLEROMA_DESCRIPTION=$'Federated microblogging'
PLEROMA_MOBILE_APP_URL='https://f-droid.org/packages/com.keylesspalace.tusky'

Bob Mottram's avatar
Bob Mottram committed
pleroma_variables=(ONION_ONLY
                   PLEROMA_DOMAIN_NAME
                   PLEROMA_CODE
                   PLEROMA_WELCOME_MESSAGE
                   PLEROMA_BACKGROUND_IMAGE_URL
                   DDNS_PROVIDER
                   PLEROMA_TITLE
                   PLEROMA_EXPIRE_MONTHS
                   MY_EMAIL_ADDRESS
                   MY_USERNAME)

function pleroma_remove_setting {
    pleroma_setting="$1"

Bob Mottram's avatar
Bob Mottram committed
    if grep -q " ${pleroma_setting}:" $pleroma_secret; then
        sed -i "/ ${pleroma_setting}:/d" $pleroma_secret
    fi
}

function pleroma_deprecate_fe {
    if ! grep -q 'config :pleroma, :fe, false' $pleroma_secret; then
        sed -i 's|config :pleroma, :fe.*|config :pleroma, :fe, false|g' $pleroma_secret
    fi
Bob Mottram's avatar
Bob Mottram committed
    pleroma_remove_setting 'theme'
    pleroma_remove_setting 'logo'
    pleroma_remove_setting 'logo_mask'
    pleroma_remove_setting 'logo_margin'
    pleroma_remove_setting 'background'
    pleroma_remove_setting 'redirect_root_no_login'
    pleroma_remove_setting 'redirect_root_login'
    pleroma_remove_setting 'show_instance_panel'
    pleroma_remove_setting 'collapse_message_with_subject'
    pleroma_remove_setting 'formatting_options_enabled'
    pleroma_remove_setting 'hide_user_stats'
    pleroma_remove_setting 'scope_options_enabled'
Bob Mottram's avatar
Bob Mottram committed
function pleroma_enable_web_push_encryption {
    if grep -q 'web_push_encryption' "$pleroma_secret"; then
        return
    fi

    cd $PLEROMA_DIR || return
    sudo -u pleroma mix web_push.gen.keypair | sudo tee -a "$pleroma_secret"
    sed -i "s|administrator@example.com|$MY_EMAIL_ADDRESS|g" "$pleroma_secret"
    sed -i '/# Put the following/d' "$pleroma_secret"
    if [ $pleroma_installing ]; then
        increment_app_install_progress
    fi
    sudo -u postgres psql pleroma -c "create extension if not exists pg_trgm"
    if [ $pleroma_installing ]; then
        increment_app_install_progress
    fi
    sudo -u postgres psql pleroma -c 'create extension if not exists "uuid-ossp"'
    if [ $pleroma_installing ]; then
        increment_app_install_progress
    fi
    sudo -u pleroma mix ecto.migrate --force
    if [ $pleroma_installing ]; then
        increment_app_install_progress
    fi
    pleroma_recompile
    if [ $pleroma_installing ]; then
        increment_app_install_progress
    fi

    PLEROMA_PUSH_PUBLIC_KEY=$(grep "public_key:" "$pleroma_secret" | head -n 1 | awk -F '"' '{print $2}')
    PLEROMA_PUSH_PRIVATE_KEY=$(grep "private_key:" "$pleroma_secret" | head -n 1 | awk -F '"' '{print $2}')
    if [ "$PLEROMA_PUSH_PUBLIC_KEY" ]; then
        write_config_param PLEROMA_PUSH_PUBLIC_KEY "$PLEROMA_PUSH_PUBLIC_KEY"
    else
        echo $'web_push_encryption failed: no public key'
        return
    fi

    if [ "$PLEROMA_PUSH_PRIVATE_KEY" ]; then
        write_config_param PLEROMA_PUSH_PRIVATE_KEY "$PLEROMA_PUSH_PRIVATE_KEY"
    else
        echo $'web_push_encryption failed: no private key'
        return
    fi
}

function pleroma_set_permissions {
    # set permissions on directories
    if [ -d "$PLEROMA_DIR/config" ]; then
        chmod 644 "$PLEROMA_DIR/config"/*
    fi
    if [ -d "$PLEROMA_DIR/deps" ]; then
        chmod -R 755 "$PLEROMA_DIR/deps"
    fi
}

Bob Mottram's avatar
Bob Mottram committed
function pleroma_create_recompile_command {
    { echo '#!/bin/bash';
      echo "cd $PLEROMA_DIR || exit 1";
      echo "chown -R pleroma:pleroma $PLEROMA_DIR";
      echo 'sudo -u pleroma mix clean';
      echo 'sudo -u pleroma mix deps.compile';
      echo 'sudo -u pleroma mix compile';
      echo "chmod 644 $PLEROMA_DIR/config/*";
      echo "chmod -R 755 $PLEROMA_DIR/deps/*";
Bob Mottram's avatar
Bob Mottram committed
      echo 'systemctl restart pleroma'; } > /usr/bin/pleroma-recompile
    chmod +x /usr/bin/pleroma-recompile
}

Bob Mottram's avatar
Bob Mottram committed
function pleroma_recompile {
    # necessary after parameter changes

Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f /usr/bin/pleroma-recompile ]; then
        pleroma_create_recompile_command
Bob Mottram's avatar
Bob Mottram committed
    fi
    if ! grep -q 'chmod' /usr/bin/pleroma-recompile; then
        pleroma_create_recompile_command
    fi

Bob Mottram's avatar
Bob Mottram committed
    /usr/bin/pleroma-recompile
Bob Mottram's avatar
Bob Mottram committed
}

function pleroma_setting_background {
    pleroma_new_background="$1"

    if [ "$pleroma_new_background" ]; then
        url="$pleroma_new_background"

        ext=
        if [ ${#url} -gt 0 ]; then
            if [[ "$url" == *".jpeg" || "$url" == *".jpg" ]]; then
                ext="jpg"
            fi
            if [[ "$url" == *".png" ]]; then
                ext="png"
            fi
            if [[ "$url" == *".gif" ]]; then
                ext="gif"
            fi
        fi

        if [ ${#ext} -gt 0 ]; then
            cd "$PLEROMA_DIR" || return

            # remove any existing image
            if [ -f "bg_custom.${ext}" ]; then
                rm "bg_custom.${ext}"
            fi

            # get the new image
            wget "$url" -O "bg_custom.${ext}"
            if [ ! -f "bg_custom.${ext}" ]; then
                echo "$url"
                echo $'Custom background image for pleroma could not be downloaded'
                return
            fi

            if [ -d priv/static/static ]; then
                cp "bg_custom.${ext}" "priv/static/static/bg_custom.${ext}"
                sed -i "s|\"background\":.*|\"background\": \"/static/bg_custom.${ext}\",|g" priv/static/static/config.json
                if [ -f "$pleroma_secret" ]; then
                    if grep -q "\"background\":" "$pleroma_secret"; then
                        sed -i "s|\"background\":.*|\"background\": \"/static/bg_custom.${ext}\",|g" "$pleroma_secret"
                    fi
                fi
Bob Mottram's avatar
Bob Mottram committed
                chown pleroma:pleroma "priv/static/static/bg_custom.${ext}"
            fi

            rm "bg_custom.${ext}"
            pleroma_recompile
        fi
    fi
}

Bob Mottram's avatar
Bob Mottram committed
function pleroma_setting_emoji {
    shortcode="$1"
    image_url="$2"
    if [ ! "$shortcode" ]; then
        return
    fi
    if [[ "$shortcode" == *' '* || "$shortcode" == *':'* ]]; then
        return
    fi
    if [ ${#shortcode} -gt 32 ]; then
        return
    fi
    if [[ "$image_url" != 'http'* ]]; then
        return
    fi
    if [ ${#image_url} -gt 256 ]; then
        return
    fi

    image_extension=
    if [[ "$image_url" == *'.jpg' || "$image_url" == *'.jpeg' ]]; then
        image_extension='jpg'
    fi
    if [[ "$image_url" == *'.gif' ]]; then
        image_extension='gif'
    fi
    if [[ "$image_url" == *'.png' ]]; then
        image_extension='png'
    fi
    if [ ! $image_extension ]; then
        echo $'Invalid image type'
Bob Mottram's avatar
Bob Mottram committed
        return
    fi

    if [ ! -d $PLEROMA_DIR/priv/static/emoji ]; then
        mkdir -p $PLEROMA_DIR/priv/static/emoji
    fi

    image_filename=$PLEROMA_DIR/priv/static/emoji/${shortcode}.${image_extension}
    if [ -f "$image_filename" ]; then
        mv "$image_filename" "${image_filename}.prev"
    fi
Bob Mottram's avatar
Bob Mottram committed
    wget "$image_url" -O "$image_filename"
    if [ ! -f "$image_filename" ]; then
Bob Mottram's avatar
Bob Mottram committed
        if [ -f "${image_filename}.prev" ]; then
            mv "${image_filename}.prev" "${image_filename}"
        fi
Bob Mottram's avatar
Bob Mottram committed
        return
    fi
    rm "${image_filename}.prev"
Bob Mottram's avatar
Bob Mottram committed

    if [[ "$image_url" == *'.jpg' || "$image_url" == *'.jpeg' || "$image_url" == *'.gif' ]]; then
        convert "$image_filename" -resize "$emoji_resolution" "$PLEROMA_DIR/priv/static/emoji/${shortcode}.png"
        if [ ! -f "$PLEROMA_DIR/priv/static/emoji/${shortcode}.png" ]; then
            return
        fi

        # remove the original
        rm "$image_filename"

        image_extension='png'
        image_filename=$PLEROMA_DIR/priv/static/emoji/${shortcode}.${image_extension}
    else
        convert "$image_filename" -resize "$emoji_resolution" "$image_filename"
    fi

    if ! grep -q "${shortcode}," $PLEROMA_DIR/config/emoji.txt; then
        echo "${shortcode}, /emoji/${shortcode}.${image_extension}" >> $PLEROMA_DIR/config/emoji.txt
    else
        sed -i "s|${shortcode},.*|${shortcode}, /emoji/${shortcode}.${image_extension}|g" $PLEROMA_DIR/config/emoji.txt
    fi

    chown -R pleroma:pleroma $PLEROMA_DIR
    cd $PLEROMA_DIR || exit 26
    pleroma_recompile
Bob Mottram's avatar
Bob Mottram committed
}

function pleroma_setting_registration {
    allow_registration="$1"

Bob Mottram's avatar
Bob Mottram committed
    if [[ "$allow_registration" == '1' ]]; then
        if ! grep -q 'registrations_open' $pleroma_secret; then
            sed -i '/config :pleroma, :instance/a registrations_open: true,' $pleroma_secret
            sed -i 's|registrations_open:|  registrations_open:|g' $pleroma_secret
Bob Mottram's avatar
Bob Mottram committed
            sed -i 's|registrationOpen:.*|registrationOpen: true,|g' $pleroma_secret
Bob Mottram's avatar
Bob Mottram committed
        sed -i 's|registrations_open: false|registrations_open: true|g' $PLEROMA_DIR/config/config.exs
        sed -i 's|registrations_open: False|registrations_open: true|g' $PLEROMA_DIR/config/config.exs
        sed -i 's|registrations_open: false|registrations_open: true|g' $pleroma_secret
        sed -i 's|registrations_open: False|registrations_open: true|g' $pleroma_secret
Bob Mottram's avatar
Bob Mottram committed
        sed -i 's|"registrationOpen": false|"registrationOpen": true|g' $PLEROMA_DIR/priv/static/static/config.json
        sed -i 's|"registrationOpen": False|"registrationOpen": true|g' $PLEROMA_DIR/priv/static/static/config.json
        cd $PLEROMA_DIR || exit 56
Bob Mottram's avatar
Bob Mottram committed
        pleroma_recompile
    fi

Bob Mottram's avatar
Bob Mottram committed
    if [[ "$allow_registration" == '0' ]]; then
        if ! grep -q 'registrations_open' $pleroma_secret; then
            sed -i '/config :pleroma, :instance/a registrations_open: false,' $pleroma_secret
            sed -i 's|registrations_open:|  registrations_open:|g' $pleroma_secret
Bob Mottram's avatar
Bob Mottram committed
            sed -i 's|registrationOpen:.*|registrationOpen: false,|g' $pleroma_secret
Bob Mottram's avatar
Bob Mottram committed
        sed -i 's|registrations_open: true|registrations_open: false|g' $PLEROMA_DIR/config/config.exs
        sed -i 's|registrations_open: True|registrations_open: false|g' $PLEROMA_DIR/config/config.exs
        sed -i 's|registrations_open: true|registrations_open: false|g' $pleroma_secret
        sed -i 's|registrations_open: True|registrations_open: false|g' $pleroma_secret
Bob Mottram's avatar
Bob Mottram committed
        sed -i 's|"registrationOpen": true|"registrationOpen": false|g' $PLEROMA_DIR/priv/static/static/config.json
        sed -i 's|"registrationOpen": True|"registrationOpen": false|g' $PLEROMA_DIR/priv/static/static/config.json
        cd $PLEROMA_DIR || exit 45
Bob Mottram's avatar
Bob Mottram committed
        pleroma_recompile
    fi
}

function pleroma_enable_chat {
    if [[ "$1" == 't'* || "$1" == 'y'* || "$1" == 'T'* || "$1" == 'Y'* ]]; then
        sed -i 's|"chatDisabled":.*|"chatDisabled": false,|g' $PLEROMA_DIR/priv/static/static/config.json
        sed -i 's|:chat, enabled:.*|:chat, enabled: true|g' $PLEROMA_DIR/config/config.exs
    else
        sed -i 's|"chatDisabled":.*|"chatDisabled": true,|g' $PLEROMA_DIR/priv/static/static/config.json
        sed -i 's|:chat, enabled:.*|:chat, enabled: false|g' $PLEROMA_DIR/config/config.exs
    fi
    pleroma_recompile
}

Bob Mottram's avatar
Bob Mottram committed
function create_pleroma_blocklist {
Bob Mottram's avatar
Bob Mottram committed
    { echo '#!/bin/bash';
      echo '';
      echo 'websub_server_subscriptions_query="DELETE FROM websub_server_subscriptions WHERE"';
      echo 'websub_server_subscriptions_updated='
      echo '';
Bob Mottram's avatar
Bob Mottram committed
      echo "if [ ! -f /root/${PROJECT_NAME}-firewall-domains.cfg ]; then";
      echo '    exit 0';
      echo 'fi';
      echo '';
Bob Mottram's avatar
Bob Mottram committed
      echo 'if [ -f /tmp/pleroma-blocking.lock ]; then';
      echo '    cd /tmp';
      echo '    find ./pleroma*.lock -type f -mmin +5 -exec rm {} \;';
      echo '    if [ -f /tmp/pleroma-blocking.lock ]; then';
      echo '        exit 0';
      echo '    fi';
      echo 'fi';
      echo '';
Bob Mottram's avatar
Bob Mottram committed
      echo 'touch /tmp/pleroma-blocking.lock';
      echo 'filter_str=';
      echo 'muted_words_str=';
      echo '';
Bob Mottram's avatar
Bob Mottram committed
      echo 'while read blocked; do';
      echo "    if [[ \"\$blocked\" == *\".\"* && \"\$blocked\" != *\"@\"* ]]; then";
Bob Mottram's avatar
Bob Mottram committed
      echo "        if [ \${#blocked} -gt 4 ]; then";
      echo '            # Create a filter string for the pleroma configuration';
      echo "            if [ \"\$filter_str\" ]; then";
      echo "                filter_str=\"\${filter_str}, \\\"\$blocked\\\"\"";
      echo '            else';
      echo "                filter_str=\"\\\"\${blocked}\\\"\"";
Bob Mottram's avatar
Bob Mottram committed
      echo '            fi';
      echo "            if ! grep -q \"127.0.0.1  \$blocked\" /etc/hosts; then";
      echo "                echo \"127.0.0.1  \$blocked\" >> /etc/hosts";
      echo '            fi';
      echo '';
      echo "            if [ \$websub_server_subscriptions_updated ]; then";
      echo "                websub_server_subscriptions_query=\"\${websub_server_subscriptions_query} or\"";
      echo '            fi';
      echo "            websub_server_subscriptions_query=\"\${websub_server_subscriptions_query} callback like '%\${blocked}%'\"";
      echo '        fi';
      echo '    fi';
      echo "done </root/${PROJECT_NAME}-firewall-domains.cfg";
      echo '';
      echo 'while read blocked; do';
      echo "    if [ \${#blocked} -gt 2 ]; then";
      echo "        if [[ \"\$blocked\" != '#'* ]]; then";
      echo '            # Create a filter string for the pleroma configuration';
      echo "            blocked_regex=\"~r/\${blocked}/iu\"";
      echo "            if [ \"\$muted_words_str\" ]; then";
      echo "                muted_words_str=\"\${muted_words_str}, \\\"\${blocked_regex}\\\"\"";
      echo '            else';
      echo "                muted_words_str=\"\\\"\${blocked_regex}\\\"\"";
      echo '            fi';
      echo '        fi';
      echo '    fi';
      echo "done </root/${PROJECT_NAME}-firewall-words.cfg";
      echo '';
      echo 'while read blocked; do';
      echo "    if [[ \"\$blocked\" == *\".\"* && \"\$blocked\" == *\"@\"* ]]; then";
      echo "        if [ \${#blocked} -gt 4 ]; then";
      echo "            curr_user_query=\"SELECT nickname FROM users where nickname ilike '%\${blocked}%'\"";
      echo "            curr_user_present=\$(sudo -u postgres psql -d pleroma -c \"\$curr_user_query\")";
      echo "            if [[ \"\$curr_user_present\" != *'(0 '* ]]; then";
      echo "                /bin/bash -c \"cd /etc/pleroma;sudo -u pleroma mix deactivate_user \${blocked}\" &";
      echo '                sleep 1';
Bob Mottram's avatar
Bob Mottram committed
      echo '            fi';
      echo '        fi';
      echo '    fi';
      echo "done </root/${PROJECT_NAME}-firewall-domains.cfg";
Bob Mottram's avatar
Bob Mottram committed
      echo '';
      echo 'recompile=';
      echo "if [ \"\$filter_str\" ]; then";
      echo "    if ! grep -q \" \$filter_str \" $pleroma_secret; then";
Bob Mottram's avatar
Bob Mottram committed
      echo "        sed -i \"s| media_removal:.*| media_removal: [ \$filter_str ],|g\" $pleroma_secret";
      echo "        sed -i \"s| quarantined_instances:.*| quarantined_instances: [ \$filter_str ],|g\" $pleroma_secret";

Bob Mottram's avatar
Bob Mottram committed
      echo "        sed -i \"/mrf_simple/{N;s| federated_timeline_removal:.*| federated_timeline_removal: [ \$filter_str ],|}\" $pleroma_secret";
      echo "        sed -i \"/mrf_simple/{N;N;s| federated_timeline_removal:.*| federated_timeline_removal: [ \$filter_str ],|}\" $pleroma_secret";
      echo "        sed -i \"/mrf_simple/{N;N;N;s| federated_timeline_removal:.*| federated_timeline_removal: [ \$filter_str ],|}\" $pleroma_secret";
      echo "        sed -i \"/mrf_simple/{N;N;N;N;s| federated_timeline_removal:.*| federated_timeline_removal: [ \$filter_str ],|}\" $pleroma_secret";
      echo "        sed -i \"/mrf_simple/{N;s| reject:.*| reject: [ \$filter_str ]|}\" $pleroma_secret";
      echo "        sed -i \"/mrf_simple/{N;N;s| reject:.*| reject: [ \$filter_str ]|}\" $pleroma_secret";
      echo "        sed -i \"/mrf_simple/{N;N;N;s| reject:.*| reject: [ \$filter_str ]|}\" $pleroma_secret";
      echo "        sed -i \"/mrf_simple/{N;N;N;N;s| reject:.*| reject: [ \$filter_str ]|}\" $pleroma_secret";
      echo "        sed -i \"/mrf_simple/{N;N;N;N;N;s| reject:.*| reject: [ \$filter_str ]|}\" $pleroma_secret";
      echo '        recompile=1';
      echo '    fi';
      echo 'fi';
      echo '';
      echo "if [ \"\$muted_words_str\" ]; then";
      echo "    if ! grep -q \" \$muted_words_str \" $pleroma_secret; then";
Bob Mottram's avatar
Bob Mottram committed
      echo "        sed -i \"/mrf_keyword/{N;s| federated_timeline_removal:.*| federated_timeline_removal: [ \$muted_words_str ],|}\" $pleroma_secret";
      echo "        sed -i \"/mrf_keyword/{N;N;s| federated_timeline_removal:.*| federated_timeline_removal: [ \$muted_words_str ],|}\" $pleroma_secret";
      echo "        sed -i \"/mrf_keyword/{N;N;N;s| federated_timeline_removal:.*| federated_timeline_removal: [ \$muted_words_str ],|}\" $pleroma_secret";
      echo "        sed -i \"/mrf_keyword/{N;N;N;N;s| federated_timeline_removal:.*| federated_timeline_removal: [ \$muted_words_str ],|}\" $pleroma_secret";
      echo "        sed -i \"/mrf_keyword/{N;s| reject:.*| reject: [ \$muted_words_str ],|}\" $pleroma_secret";
      echo "        sed -i \"/mrf_keyword/{N;N;s| reject:.*| reject: [ \$muted_words_str ],|}\" $pleroma_secret";
      echo "        sed -i \"/mrf_keyword/{N;N;N;s| reject:.*| reject: [ \$muted_words_str ],|}\" $pleroma_secret";
      echo "        sed -i \"/mrf_keyword/{N;N;N;N;s| reject:.*| reject: [ \$muted_words_str ],|}\" $pleroma_secret";
      echo "        sed -i \"/mrf_keyword/{N;N;N;N;N;s| reject:.*| reject: [ \$muted_words_str ],|}\" $pleroma_secret";
      echo '        recompile=1';
      echo '    fi';
      echo 'fi';
      echo '';
      echo "if [ \$recompile ]; then";
      echo '    systemctl stop pleroma';
      echo "    cd $PLEROMA_DIR";
      echo "    chown -R pleroma:pleroma $PLEROMA_DIR";
      echo '    sudo -u pleroma mix clean';
      echo '    sudo -u pleroma mix deps.compile';
      echo '    sudo -u pleroma mix compile';
      echo '    systemctl restart pleroma';
      echo 'fi';
      echo '';
      echo "if [ \$websub_server_subscriptions_updated ]; then";
      echo '    cd /etc/postgresql';
      echo "    sudo -u postgres psql -d pleroma -c \"\$websub_server_subscriptions_query\"";
      echo 'fi';
      echo '';
Bob Mottram's avatar
Bob Mottram committed
      echo 'rm /tmp/pleroma-blocking.lock'; } > $blocking_script_file
Bob Mottram's avatar
Bob Mottram committed
    chmod +x $blocking_script_file

    if ! grep -q "$blocking_script_file" /etc/crontab; then
Bob Mottram's avatar
Bob Mottram committed
        cron_add_mins 1 "$blocking_script_file 2> /dev/null"
Bob Mottram's avatar
Bob Mottram committed
function expire_pleroma_posts {
    domain_name=$1
    expire_months=$3

Bob Mottram's avatar
Bob Mottram committed
    if [ ! "$expire_months" ]; then
Bob Mottram's avatar
Bob Mottram committed
        expire_months=3
    fi

Bob Mottram's avatar
Bob Mottram committed
    #expire_days=$((expire_months * 30))
Bob Mottram's avatar
Bob Mottram committed

    # files are what take up most of the backup time, so don't keep them for very long
    expire_days_files=7

    # To prevent the database size from growing endlessly this script expires posts
    # after a number of months
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d $PLEROMA_DIR ]; then
Bob Mottram's avatar
Bob Mottram committed
    { echo '#!/bin/bash';
      echo "plmonths=\"$PLEROMA_EXPIRE_MONTHS\"";
      echo "if [ \${#plmonths} -eq 0 ]; then";
      echo '    exit 1';
      echo 'fi';
      echo "if [[ \"\$plmonths\" == \"0\" ]]; then";
      echo '    exit 2';
      echo 'fi';
      echo "oldate=\$(date +%Y-%m-%d --date=\"\$plmonths months ago\")";
      echo 'cd /etc/postgresql';
      echo "sudo -u postgres psql -d pleroma -c \"DELETE FROM notifications WHERE inserted_at <= '\$oldate 01:01:01'\"";
      echo "sudo -u postgres psql -d pleroma -c \"DELETE FROM objects WHERE inserted_at <= '\$oldate 01:01:01'\""; } > $pleroma_expire_posts_script
Bob Mottram's avatar
Bob Mottram committed
    chmod +x $pleroma_expire_posts_script

    pleroma_expire_script=/etc/cron.daily/pleroma-expire
Bob Mottram's avatar
Bob Mottram committed
    expire_days_files_threshold=$((expire_days_files - 1))
    { echo '#!/bin/bash';
      echo 'cd /etc/postgresql';
      echo "if [ -d $PLEROMA_DIR/tempfiles ]; then";
      echo "    rm -rf $PLEROMA_DIR/tempfiles";
Bob Mottram's avatar
Bob Mottram committed
      echo 'fi';
      echo '';
      echo '# make directory to temporarily store local avatars';
      echo "mkdir $PLEROMA_DIR/tempfiles";
Bob Mottram's avatar
Bob Mottram committed
      echo '';
      echo '# get the local avatar files';
      echo "avatars=\$(sudo -u postgres psql -d pleroma -c \"select avatar->>'url' from users where avatar->>'url' like '%${domain_name}%'\")";
      echo '';
      echo '# copy the avatar files to a temporary directory';
      echo "arr=( \$avatars )";
Bob Mottram's avatar
Bob Mottram committed
      echo "for i in \${arr[@]}; do";
Bob Mottram's avatar
Bob Mottram committed
      echo "    if [[ \"\$i\" == *'/media/'* ]]; then";
      echo "        imagefile=$PLEROMA_DIR/uploads/\$(echo \$i | sed 's|\"||g' | sed 's|,||g' | awk -F '/media/' '{print \$2}');";
Bob Mottram's avatar
Bob Mottram committed
      echo "        if [ -f \$imagefile ]; then";
Bob Mottram's avatar
Bob Mottram committed
      echo "            imagedir=$PLEROMA_DIR/uploads/\$(echo \$imagefile | awk -F '/' '{print \$5}')";
Bob Mottram's avatar
Bob Mottram committed
      echo "            nowdate=\$(date +%s)";
      echo "            sinceepoch=\$(date +%s -r \$imagefile)";
      echo "            daysold=\$(((\$nowdate - \$sinceepoch) / 86400))";
      echo "            if [ \$daysold -ge ${expire_days_files_threshold} ]; then";
      echo "                cp -rp \$imagedir $PLEROMA_DIR/tempfiles";
Bob Mottram's avatar
Bob Mottram committed
      echo '            fi';
      echo '        fi';
      echo '    fi';
      echo 'done';
      echo '';
      echo '# get the local banner files';
      echo "banners=\$(sudo -u postgres psql -d pleroma -c \"select avatar->>'banner' from users where avatar->>'url' like '%${domain_name}%'\")";
Bob Mottram's avatar
Bob Mottram committed
      echo '';
      echo '# copy the banner files to a temporary directory';
      echo "arr=( \$banners )";
Bob Mottram's avatar
Bob Mottram committed
      echo "for i in \${arr[@]}; do";
Bob Mottram's avatar
Bob Mottram committed
      echo "    if [[ \"\$i\" == *'/media/'* ]]; then";
Bob Mottram's avatar
Bob Mottram committed
      echo "        imagefile=$PLEROMA_DIR/uploads/\$(echo \$i | sed 's|\"||g' | sed 's|,||g' | awk -F '/media/' '{print \$2}');";
Bob Mottram's avatar
Bob Mottram committed
      echo "        if [ -f \$imagefile ]; then";
Bob Mottram's avatar
Bob Mottram committed
      echo "            imagedir=$PLEROMA_DIR/uploads/\$(echo \$imagefile | awk -F '/' '{print \$5}')";
Bob Mottram's avatar
Bob Mottram committed
      echo "            nowdate=\$(date +%s)";
      echo "            sinceepoch=\$(date +%s -r \$imagefile)";
      echo "            daysold=\$(((\$nowdate - \$sinceepoch) / 86400))";
      echo "            if [ \$daysold -ge ${expire_days_files_threshold} ]; then";
      echo "                cp -rp \$imagedir $PLEROMA_DIR/tempfiles";
Bob Mottram's avatar
Bob Mottram committed
      echo '            fi';
      echo '        fi';
      echo '    fi';
      echo 'done';
      echo '';
      echo '# delete old files';
      echo "find $PLEROMA_DIR/uploads/* -mtime +${expire_days_files} -exec rm -rf {} +";
Bob Mottram's avatar
Bob Mottram committed
      echo '';
      echo '# move avatar files back to uploads';
      echo "chown -R pleroma:pleroma $PLEROMA_DIR/tempfiles";
      echo "mv $PLEROMA_DIR/tempfiles/* $PLEROMA_DIR/uploads";
      echo "rm -rf $PLEROMA_DIR/tempfiles";
Bob Mottram's avatar
Bob Mottram committed
      echo '';
      echo "$pleroma_expire_posts_script 2> /dev/null"; } > $pleroma_expire_script
Bob Mottram's avatar
Bob Mottram committed
    chmod +x $pleroma_expire_script

    # remove any old cron job
    if grep -q "pleroma-expire" /etc/crontab; then
        sed -i "/pleroma-expire/d" /etc/crontab
        rm /usr/bin/pleroma-expire
    fi

    # remove old expire script
    if [ -f /etc/cron.weekly/clear-pleroma-database ]; then
        rm /etc/cron.weekly/clear-pleroma-database
    fi
}

Bob Mottram's avatar
Bob Mottram committed
function logging_on_pleroma {
    echo -n ''
}

function logging_off_pleroma {
    echo -n ''
}

function remove_user_pleroma {
    remove_username="$1"

    if [ ! "$remove_username" ]; then
        return
    fi

    cd $PLEROMA_DIR || exit 25
    sudo -u pleroma mix pleroma.user rm "$remove_username"

    cd /etc/postgresql || exit 26
    sudo -u postgres psql -d pleroma -c "delete from users where nickname is null and local is true;"
    sudo -u postgres psql -d pleroma -c "delete from users where nickname = '$remove_username' and local is true;"

Bob Mottram's avatar
Bob Mottram committed
    "${PROJECT_NAME}-pass" -u "$remove_username" --rmapp pleroma
Bob Mottram's avatar
Bob Mottram committed
}

function add_user_pleroma {
    new_username="$1"
    new_user_password="$2"

    cd $PLEROMA_DIR || exit 34
    echo 'y' | sudo -u pleroma mix pleroma.user new "$new_username" "$new_username@$HOSTNAME" --bio $"Your bio goes here" --password "$new_user_password"
Bob Mottram's avatar
Bob Mottram committed
    "${PROJECT_NAME}-pass" -u "$new_username" -a pleroma -p "$new_user_password"
Bob Mottram's avatar
Bob Mottram committed
    echo '0'
Bob Mottram's avatar
Bob Mottram committed
}

function install_interactive_pleroma {
Bob Mottram's avatar
Bob Mottram committed
    if [ ! "$ONION_ONLY" ]; then
Bob Mottram's avatar
Bob Mottram committed
        ONION_ONLY='no'
    fi

    if [[ $ONION_ONLY != "no" ]]; then
        PLEROMA_DOMAIN_NAME='pleroma.local'
    else
        PLEROMA_DETAILS_COMPLETE=
        while [ ! $PLEROMA_DETAILS_COMPLETE ]
        do
Bob Mottram's avatar
Bob Mottram committed
            data=$(mktemp 2>/dev/null)
Bob Mottram's avatar
Bob Mottram committed
            dialog --backtitle $"Freedombone Configuration" \
                   --title $"Pleroma Configuration" \
                   --form $"\\nPlease enter your Pleroma details. The background image URL can be left blank.\\n\\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 16 65 4 \
                   $"Domain:" 1 1 "$(grep 'PLEROMA_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
                   $"Title:" 2 1 "$(grep "$PLEROMA_TITLE" temp.cfg | awk -F '=' '{print $2}')" 2 25 255 255 \
                   $"Background image URL:" 3 1 "$(grep "$PLEROMA_BACKGROUND_IMAGE_URL" temp.cfg | awk -F '=' '{print $2}')" 3 25 255 255 \
                   2> "$data"
Bob Mottram's avatar
Bob Mottram committed
            sel=$?
            case $sel in
Bob Mottram's avatar
Bob Mottram committed
                1) rm -f "$data"
                   exit 1;;
                255) rm -f "$data"
                     exit 1;;
Bob Mottram's avatar
Bob Mottram committed
            esac
Bob Mottram's avatar
Bob Mottram committed
            PLEROMA_DOMAIN_NAME=$(sed -n 1p < "$data")
            title=$(sed -n 2p < "$data")
Bob Mottram's avatar
Bob Mottram committed
            if [ ${#title} -gt 1 ]; then
Bob Mottram's avatar
Bob Mottram committed
                PLEROMA_TITLE="$title"
Bob Mottram's avatar
Bob Mottram committed
            fi
Bob Mottram's avatar
Bob Mottram committed
            img_url=$(sed -n 3p < "$data")
Bob Mottram's avatar
Bob Mottram committed
            if [ ${#img_url} -gt 1 ]; then
                PLEROMA_BACKGROUND_IMAGE_URL=$img_url
            fi
Bob Mottram's avatar
Bob Mottram committed
            if [ "$PLEROMA_DOMAIN_NAME" ]; then
Bob Mottram's avatar
Bob Mottram committed
                if [[ $PLEROMA_DOMAIN_NAME == "$HUBZILLA_DOMAIN_NAME" ]]; then
                    PLEROMA_DOMAIN_NAME=""
                fi
                TEST_DOMAIN_NAME=$PLEROMA_DOMAIN_NAME
                validate_domain_name
Bob Mottram's avatar
Bob Mottram committed
                if [[ "$TEST_DOMAIN_NAME" != "$PLEROMA_DOMAIN_NAME" ]]; then
Bob Mottram's avatar
Bob Mottram committed
                    PLEROMA_DOMAIN_NAME=
                    dialog --title $"Domain name validation" --msgbox "$TEST_DOMAIN_NAME" 15 50
                fi
            fi
            if [ $PLEROMA_DOMAIN_NAME ]; then
                PLEROMA_DETAILS_COMPLETE="yes"
            fi
Bob Mottram's avatar
Bob Mottram committed
            rm -f "$data"
Bob Mottram's avatar
Bob Mottram committed
        done

        # remove any invalid characters
        if [ ${#PLEROMA_TITLE} -gt 0 ]; then
Bob Mottram's avatar
Bob Mottram committed
            new_title=${PLEROMA_TITLE//\'/}
Bob Mottram's avatar
Bob Mottram committed
            PLEROMA_TITLE="$new_title"
        fi

        # save the results in the config file
        write_config_param "PLEROMA_TITLE" "$PLEROMA_TITLE"
        write_config_param "PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_BACKGROUND_IMAGE_URL"
    fi
    write_config_param "PLEROMA_DOMAIN_NAME" "$PLEROMA_DOMAIN_NAME"
    APP_INSTALLED=1
}

function change_password_pleroma {
Bob Mottram's avatar
Bob Mottram committed
#    curr_username="$1"
Bob Mottram's avatar
Bob Mottram committed
    new_user_password="$2"

    #${PROJECT_NAME}-pass -u "$curr_username" -a pleroma -p "$new_user_password"
}

Bob Mottram's avatar
Bob Mottram committed
function pleroma_create_database_failed {
    run_system_query_postgresql "ALTER USER pleroma NOSUPERUSER;"
    run_system_query_postgresql "ALTER USER pleroma NOCREATEDB;"
}

function pleroma_create_config {
    config_filename="$1"
    domain=$PLEROMA_DOMAIN_NAME
    domain_scheme='https'
    domain_port=443

    pleroma_http_security='true'
    if [[ "$ONION_ONLY" != 'no' ]]; then
        domain=$PLEROMA_ONION_HOSTNAME
        domain_scheme='http'
        domain_port=80
        pleroma_http_security='false'
    pleroma_background_image='aurora_borealis.jpg'

    { echo 'use Mix.Config';
      echo '';
      echo 'config :pleroma, Pleroma.Web.Endpoint,';
      echo '  pubsub: [name: Pleroma.Web.PubSub, adapter: Phoenix.PubSub.PG2],';
      echo "  secret_key_base: \"$PLEROMA_SECRET_KEY\",";
Bob Mottram's avatar
Bob Mottram committed
      echo "  signing_salt: \"$PLEROMA_SIGNING_SALT\",";
      echo "  secure_cookie_flag: true,";
      echo "  http: [port: $PLEROMA_PORT],";
      echo '  protocol: "http",';
      echo '  debug_errors: true,';
      echo '  code_reloader: true,';
      echo '  watchers: [],';
      echo "  url: [host: \"$domain\", scheme: \"$domain_scheme\", port: $domain_port]";
      echo '';
      echo "config :pleroma, :http, proxy_url: {:socks5, :localhost, 9050}";
      echo '';
      # shellcheck disable=SC2028
      echo "config :logger, :console, format: \"[\$level] \$message\\n\", level: :warn";
      echo '';
      echo 'config :comeonin, :pbkdf2_rounds, 1';
      echo '';
      echo 'config :pleroma, Pleroma.Upload,';
      echo '  uploads: "uploads",';
      echo '  strip_exif: true';
      echo '';
      echo '# begin filtering';
Bob Mottram's avatar
Bob Mottram committed
      echo 'config :pleroma, :instance,';
      echo '  registrations_open: false,';
Bob Mottram's avatar
Bob Mottram committed
      echo '  federating: true,';
      echo '  limit: 5000,';
      echo '  public: true,';
      echo '  allow_relay: false,';
      echo '  finmoji_enabled: true,';
      echo '  quarantined_instances: [],';
      echo '  mrf_transparency: false,';
Bob Mottram's avatar
Bob Mottram committed
      echo '  allowed_post_formats: [';
      echo '    "text/plain",';
      echo '    "text/html",';
      echo '    "text/markdown"';
      echo '  ],';
Bob Mottram's avatar
Bob Mottram committed
      echo '  rewrite_policy: Pleroma.Web.ActivityPub.MRF.SimplePolicy';
      echo '';
      echo 'config :pleroma, :mrf_simple,';
      echo '  media_removal: [],';
      echo '  media_nsfw: [],';
      echo '  federated_timeline_removal: [],';
      echo '  accept: [],';
      echo '  reject: []';
      echo '';
      echo 'config :phoenix, :stacktrace_depth, 20';
      echo '';
Bob Mottram's avatar
Bob Mottram committed
      echo 'config :pleroma, :markup,';
      echo '  allow_inline_images: true,';
      echo '  allow_headings: true,';
      echo '  allow_tables: true,';
      echo '  allow_fonts: false,';
      echo '  scrub_policy: [';
      echo '    Pleroma.HTML.Transform.MediaProxy,';
      echo '    Pleroma.HTML.Scrubber.Default';
      echo '  ]';
      echo '';
      echo 'config :pleroma, Pleroma.Repo,';
      echo '  adapter: Ecto.Adapters.Postgres,';
      echo '  username: "pleroma",';
      echo "  password: \"$PLEROMA_ADMIN_PASSWORD\",";
      echo '  database: "pleroma",';
      echo '  hostname: "localhost",';
      echo '  pool_size: 10';
      echo '';
      echo 'config :pleroma, :mrf_rejectnonpublic,';
      echo '  allow_followersonly: true,';
      echo '  allow_direct: true';
      echo '';
      echo "config :pleroma, :mrf_hellthread, delist_threshold: $maximum_tagged_thread_participants, reject_threshold: $maximum_tagged_thread_participants";
      echo 'config :pleroma, :fe, false';
      echo '';
Bob Mottram's avatar
Bob Mottram committed
      echo "config :pleroma, :frontend_configurations, pleroma_fe: %{";
      echo '  redirectRootNoLogin: "/main/all",';
      echo "  name: \"${PROJECT_NAME}\",";
      echo '  theme: "mammal",';
      echo '  logo: "/static/logo_custom.png",';
      echo '  logoMask: true,';
      echo '  logoMargin: ".1em",';
      echo '  registrationOpen: false,';
      echo '  defaultPath: "/main/all",';
      echo '  chatDisabled: true,';
      echo '  redirectRootLogin: "/main/friends",';
      echo '  scopeOptionsEnabled: true,';
      echo '  formattingOptionsEnabled: false,';
      echo '  media_proxy: true,';
      echo '  collapseMessageWithSubject: true,';
      echo '  scopeCopy: false,';
      echo '  subjectLineBehavior: "email",';
      echo '  hidePostStats: true,';
      echo '  hideUserStats: true,';
      echo '  loginMethod: "password"';
      echo '}';
      echo '';
      echo 'config :pleroma, :activitypub,';
      echo '  accept_blocks: true,';
      echo '  unfollow_blocked: true,';
      echo '  outgoing_blocks: true,';
      echo '  follow_handshake_timeout: 500';
      echo '';
      echo 'config :pleroma, :http_security,';
      echo "  enabled: ${pleroma_http_security},";
      echo '  sts: false,';
      echo '  sts_max_age: 31_536_000,';
      echo '  ct_max_age: 2_592_000,';
      echo '  referrer_policy: "same-origin"';
      echo '';
      echo 'config :cors_plug,';
      echo '  max_age: 86_400,';
      echo '  methods: ["POST", "PUT", "DELETE", "GET", "PATCH", "OPTIONS"],';
      echo '  expose: [';
      echo '    "Link",';
      echo '    "X-RateLimit-Reset",';
      echo '    "X-RateLimit-Limit",';
      echo '    "X-RateLimit-Remaining",';
      echo '    "X-Request-Id",';
      echo '    "Idempotency-Key"';
      echo '  ],';
      echo '  credentials: true,';
      echo '  headers: ["Authorization", "Content-Type", "Idempotency-Key"]';
      echo '';
      echo 'config :pleroma, :mrf_keyword,';
      echo "  reject: [],";
      echo "  federated_timeline_removal: [],";
      echo '  replace: []';
      echo '';
      echo 'try do';
      echo '  # import_config "dev.secret.exs"';
      echo 'rescue';
      # shellcheck disable=SC2028
      echo '  _-> IO.puts("!!! RUNNING IN LOCALHOST DEV MODE! !!!\nFEDERATION WONT WORK UNTIL YOU CONFIGURE A dev.secret.exs")';
      echo 'end'; } > "$config_filename"
}

Bob Mottram's avatar
Bob Mottram committed
function pleroma_create_database {
Bob Mottram's avatar
Bob Mottram committed
    if [ -f "$IMAGE_PASSWORD_FILE" ]; then
        PLEROMA_ADMIN_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
Bob Mottram's avatar
Bob Mottram committed
    else
Bob Mottram's avatar
Bob Mottram committed
        if [ ! "$PLEROMA_ADMIN_PASSWORD" ]; then
            PLEROMA_ADMIN_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
Bob Mottram's avatar
Bob Mottram committed
        fi
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! "$PLEROMA_ADMIN_PASSWORD" ]; then
Bob Mottram's avatar
Bob Mottram committed
        return
    fi

Bob Mottram's avatar
Bob Mottram committed
    systemctl restart postgresql
Bob Mottram's avatar
Bob Mottram committed
    add_postgresql_user pleroma "$PLEROMA_ADMIN_PASSWORD" encrypted
    run_system_query_postgresql "create database pleroma;"
    # temporarily allow the user to create databases
    run_system_query_postgresql "ALTER USER pleroma CREATEDB;"
    run_system_query_postgresql "ALTER USER pleroma SUPERUSER;"
Bob Mottram's avatar
Bob Mottram committed
    run_system_query_postgresql "GRANT ALL ON ALL tables IN SCHEMA public TO pleroma;"
    run_system_query_postgresql "GRANT ALL ON ALL sequences IN SCHEMA public TO pleroma;"
Bob Mottram's avatar
Bob Mottram committed
    run_system_query_postgresql "CREATE EXTENSION citext;"
Bob Mottram's avatar
Bob Mottram committed
    run_system_query_postgresql "set statement_timeout to 40000;"
Bob Mottram's avatar
Bob Mottram committed

    read_config_param "PLEROMA_SECRET_KEY"
    if [ ${#PLEROMA_SECRET_KEY} -lt 64 ]; then
Bob Mottram's avatar
Bob Mottram committed
        PLEROMA_SECRET_KEY="$(create_random_string 30)$(create_random_string 30)$(create_random_string 30)"
        if [ ${#PLEROMA_SECRET_KEY} -lt 64 ]; then
Bob Mottram's avatar
Bob Mottram committed
            pleroma_create_database_failed
            echo $'Pleroma secret key not created'
        fi
        write_config_param "PLEROMA_SECRET_KEY" "$PLEROMA_SECRET_KEY"
    fi

    pleroma_create_signing_salt
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d $PLEROMA_DIR/config ]; then
        echo $"Missing directory $PLEROMA_DIR/config"
Bob Mottram's avatar
Bob Mottram committed
    fi
    if [ ! -f $PLEROMA_DIR/config/dev.exs ]; then
        echo $"Did not find $PLEROMA_DIR/config/dev.exs"
Bob Mottram's avatar
Bob Mottram committed
    fi

    pleroma_create_config $pleroma_secret
    cd "$PLEROMA_DIR" || exit 67
Bob Mottram's avatar
Bob Mottram committed
    chown -R pleroma:pleroma $PLEROMA_DIR/*
Bob Mottram's avatar
Bob Mottram committed
    if ! sudo -u pleroma mix local.rebar --force; then
Bob Mottram's avatar
Bob Mottram committed
        pleroma_create_database_failed
Bob Mottram's avatar
Bob Mottram committed
        echo $'mix local.rebar failed'
Bob Mottram's avatar
Bob Mottram committed
    fi
    sudo -u pleroma mix local.hex --force
    sudo -u pleroma mix deps.compile mimerl
    systemctl restart postgresql
Bob Mottram's avatar
Bob Mottram committed
    if ! sudo -u pleroma mix ecto.create --force; then
Bob Mottram's avatar
Bob Mottram committed
        pleroma_create_database_failed
Bob Mottram's avatar
Bob Mottram committed
        echo $'mix ecto.create failed'
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
    if ! sudo -u pleroma mix ecto.migrate --force; then
Bob Mottram's avatar
Bob Mottram committed
        pleroma_create_database_failed
Bob Mottram's avatar
Bob Mottram committed
        echo $'mix ecto.migrate failed'
Bob Mottram's avatar
Bob Mottram committed
    fi

    # revoke the ability to create databases for this user
    run_system_query_postgresql "ALTER USER pleroma NOSUPERUSER;"
    run_system_query_postgresql "ALTER USER pleroma NOCREATEDB;"
Bob Mottram's avatar
Bob Mottram committed
}

function reconfigure_pleroma {
    echo -n ''
}

function pleroma_set_background_image {
    PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")

Bob Mottram's avatar
Bob Mottram committed
    data=$(mktemp 2>/dev/null)
Bob Mottram's avatar
Bob Mottram committed
    dialog --title $"Pleroma" \
           --backtitle $"Freedombone Control Panel" \
Bob Mottram's avatar
Bob Mottram committed
           --inputbox $'Set a background image URL' 10 60 2>"$data"
Bob Mottram's avatar
Bob Mottram committed
    sel=$?
    case $sel in
        0)
Bob Mottram's avatar
Bob Mottram committed
            temp_background=$(<"$data")
Bob Mottram's avatar
Bob Mottram committed
            if [ ${#temp_background} -gt 0 ]; then
                PLEROMA_BACKGROUND_IMAGE_URL="$temp_background"
                write_config_param "PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_BACKGROUND_IMAGE_URL"
                if [[ $(pleroma_set_background_image_from_url $PLEROMA_DIR "$PLEROMA_DOMAIN_NAME" "$PLEROMA_BACKGROUND_IMAGE_URL" "$PLEROMA_TITLE" | tail -n 1) == "0" ]]; then
                    chown -R pleroma:pleroma "$PLEROMA_DIR/priv/static/static"
                    pleroma_recompile
Bob Mottram's avatar
Bob Mottram committed
                    dialog --title $"Set Pleroma login background" \
                           --msgbox $"The background image has been set" 6 60
                fi
            fi
           ;;
    esac
Bob Mottram's avatar
Bob Mottram committed
    rm -f "$data"
Bob Mottram's avatar
Bob Mottram committed
}

function pleroma_set_title {
Bob Mottram's avatar
Bob Mottram committed
    data=$(mktemp 2>/dev/null)
Bob Mottram's avatar
Bob Mottram committed
    dialog --title $"Pleroma" \
           --backtitle $"Freedombone Control Panel" \
Bob Mottram's avatar
Bob Mottram committed
           --inputbox $'Set a title' 10 60 2>"$data"
Bob Mottram's avatar
Bob Mottram committed
    sel=$?
    case $sel in
        0)
Bob Mottram's avatar
Bob Mottram committed
            new_title=$(<"$data")
Bob Mottram's avatar
Bob Mottram committed
            if [ ${#new_title} -gt 0 ]; then
                PLEROMA_TITLE="$new_title"
                PLEROMA_DOMAIN_NAME=$(get_completion_param "pleroma domain")
                write_config_param "PLEROMA_TITLE" "$PLEROMA_TITLE"
                sed -i "s|\"name\":.*|\"name\": \"${PLEROMA_TITLE}\",|g" $PLEROMA_DIR/static/config.json
                sed -i "s|\"name\":.*|\"name\": \"${PLEROMA_TITLE}\",|g" $PLEROMA_DIR/priv/static/static/config.json
Bob Mottram's avatar
Bob Mottram committed
                sed -i "s|name: .*|name: \"${PLEROMA_TITLE}\",|g" $PLEROMA_DIR/config/config.exs
                systemctl restart pleroma
Bob Mottram's avatar
Bob Mottram committed
                dialog --title $"Set Pleroma title" \
                       --msgbox $"The title has been set" 6 60
            fi
           ;;
    esac
Bob Mottram's avatar
Bob Mottram committed
    rm -f "$data"