Skip to content
Snippets Groups Projects
freedombone-app-pleroma 54.2 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
# =======
#
# Copyright (C) 2017-2018 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

IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=1

PLEROMA_DOMAIN_NAME=
PLEROMA_CODE=
PLEROMA_PORT=4000
PLEROMA_ONION_PORT=8011
PLEROMA_REPO="https://git.pleroma.social/pleroma/pleroma.git"
PLEROMA_COMMIT='d7368ea272c86a0b6b22d57d0cb5fb01d502978a'
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

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_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 '';
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 '';
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 '        fi';
      echo '    fi';
      echo 'done </root/freedombone-firewall-domains.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/freedombone-firewall-domains.cfg';
      echo '';
      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| federated_timeline_removal:.*| federated_timeline_removal: [ \$filter_str ],|g\" $pleroma_secret";
Bob Mottram's avatar
Bob Mottram committed
      echo "        sed -i \"s| reject:.*| reject: [ \$filter_str ]|g\" $pleroma_secret";
      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 '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";
Loading
Loading full blame...