|
@@ -0,0 +1,141 @@
|
|
|
+#!/bin/bash
|
|
|
+
|
|
|
+# This program is free software: you can redistribute it and/or modify
|
|
|
+# it under the terms of the GNU 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 General Public License for more details.
|
|
|
+# You should have received a copy of the GNU General Public License
|
|
|
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
+
|
|
|
+MASTHOME='/var/lib/mastodon'
|
|
|
+MASTENVFP="$MASTHOME/live/.env.production"
|
|
|
+SISTERSFP="istanzesorelle"
|
|
|
+
|
|
|
+HELP="SINTASSI
|
|
|
+ mastblocksmerge.sh [opzioni]
|
|
|
+DESCRIZIONE
|
|
|
+ Questo script integra i dati di una o più blocklist esterne nella
|
|
|
+ tabella domain_blocks di mastodon.
|
|
|
+ Legge i domini delle istanze di cui recuperare la blocklist da un file,
|
|
|
+ per default \"$SISTERSP\" (formato del file: un dominio per riga),
|
|
|
+ prova a recuperare ciascuna blocklist da
|
|
|
+ https://[dominio]/domain_blocks.txt, costruisce dalle liste recuperate
|
|
|
+ un\'unica lista senza duplicati, si collega al db di mastodon e scrive
|
|
|
+ nella tabella domain_blocks i dati relativi alle istanze bloccate che
|
|
|
+ non sono già presenti nella tabella stessa.
|
|
|
+ Per la connessione al db di mastodon legge i dati necessari dal file
|
|
|
+ di configurazione di mastodon, per default
|
|
|
+ \"$MASTENVFP\"
|
|
|
+ È pensato per essere eseguito periodicamente da un cron job, come
|
|
|
+ utente mastodon oppure root oppure altro utente che abbia accesso in
|
|
|
+ lettura al file di configurazione di mastodon.
|
|
|
+OPZIONI
|
|
|
+ -H, --home
|
|
|
+ Definisce la home di mastodon (per default \"$MASTHOME\")
|
|
|
+ e di conseguenza il percorso del suo file di configurazione
|
|
|
+ (per default \"$MASTENVFP\").
|
|
|
+ È comunque possibile specificare individualmente il percorso
|
|
|
+ del file di configurazione di mastodon con l\'opzione che segue.
|
|
|
+ -e, --envfp
|
|
|
+ Definisce il percorso del file di configurazione di mastodon in uso.
|
|
|
+ -s, --sistersfp
|
|
|
+ Definisce il percorso del file da cui leggere la lista delle istanze
|
|
|
+ sorelle.
|
|
|
+ -h, --help
|
|
|
+ Mostra questo aiuto ed esce."
|
|
|
+
|
|
|
+args=("$@")
|
|
|
+i=0
|
|
|
+while [ $i -lt ${#args[@]} ]; do
|
|
|
+ if [ "${args[$i]:0:1}" == "-" ]; then
|
|
|
+ case "${args[$i]}" in
|
|
|
+ "-H" | "--home" )
|
|
|
+ if [ -z "${args[$i+1]}" ]; then
|
|
|
+ echo "L'opzione \"${args[$i]}\" richiede un parametro (usa \"-h\" per l'aiuto)."
|
|
|
+ exit 1
|
|
|
+ else
|
|
|
+ ((i++))
|
|
|
+ MASTHOME=$(echo "${args[$i]}" | sed -e 's/\/$//')
|
|
|
+ MASTENVFP="$MASTHOME/live/.env.production"
|
|
|
+ fi
|
|
|
+ ;;
|
|
|
+ "-e" | "--envfp" )
|
|
|
+ if [ -z "${args[$i+1]}" ]; then
|
|
|
+ echo "L'opzione \"${args[$i]}\" richiede un parametro (usa \"-h\" per l'aiuto)."
|
|
|
+ exit 1
|
|
|
+ else
|
|
|
+ ((i++))
|
|
|
+ MASTENVFP="${args[$i]}"
|
|
|
+ fi
|
|
|
+ ;;
|
|
|
+ "-s" | "--sistersfp" )
|
|
|
+ if [ -z "${args[$i+1]}" ]; then
|
|
|
+ echo "L'opzione \"${args[$i]}\" richiede un parametro (usa \"-h\" per l'aiuto)."
|
|
|
+ exit 1
|
|
|
+ else
|
|
|
+ ((i++))
|
|
|
+ SISTERSFP="${args[$i]}"
|
|
|
+ fi
|
|
|
+ ;;
|
|
|
+ "-h" | "--help" )
|
|
|
+ echo "$HELP"
|
|
|
+ exit 0
|
|
|
+ ;;
|
|
|
+ *)
|
|
|
+ echo "\"${args[$i]}\": opzione sconosciuta (usa \"-h\" per l'aiuto)."
|
|
|
+ exit 1
|
|
|
+ ;;
|
|
|
+ esac
|
|
|
+ else
|
|
|
+ echo "\"${args[$i]}\": opzione sconosciuta (usa \"-h\" per l'aiuto)."
|
|
|
+ exit 1
|
|
|
+ fi
|
|
|
+ ((i++))
|
|
|
+done
|
|
|
+
|
|
|
+[ ! -e "$MASTENVFP" ] && echo "\"$MASTENVFP\" non esiste, muoio (usa \"-h\" per l'aiuto)." && exit 1
|
|
|
+[ ! -f "$MASTENVFP" ] && echo "\"$MASTENVFP\" non è un file, muoio (usa \"-h\" per l'aiuto)." && exit 1
|
|
|
+[ ! -r "$MASTENVFP" ] && echo "\"$MASTENVFP\" non è leggibile, muoio (usa \"-h\" per l'aiuto)." && exit 1
|
|
|
+
|
|
|
+[ ! -e "$SISTERSFP" ] && echo "\"$SISTERSFP\" non esiste, muoio (usa \"-h\" per l'aiuto)." && exit 1
|
|
|
+[ ! -f "$SISTERSFP" ] && echo "\"$SISTERSFP\" non è un file, muoio (usa \"-h\" per l'aiuto)." && exit 1
|
|
|
+[ ! -r "$SISTERSFP" ] && echo "\"$SISTERSFP\" non è leggibile, muoio (usa \"-h\" per l'aiuto)." && exit 1
|
|
|
+
|
|
|
+DB_HOST=`grep 'DB_HOST' "$MASTENVFP"|sed -e 's/[^=]*=//'`
|
|
|
+DB_PORT=`grep 'DB_PORT' "$MASTENVFP"|sed -e 's/[^=]*=//'`
|
|
|
+DB_NAME=`grep 'DB_NAME' "$MASTENVFP"|sed -e 's/[^=]*=//'`
|
|
|
+DB_USER=`grep 'DB_USER' "$MASTENVFP"|sed -e 's/[^=]*=//'`
|
|
|
+DB_PASS=`grep 'DB_PASS' "$MASTENVFP"|sed -e 's/[^=]*=//'`
|
|
|
+
|
|
|
+IFS=$'\n'
|
|
|
+
|
|
|
+for sisdom in $(grep -P '^[^#]+[^\s]+' "$SISTERSFP"); do
|
|
|
+ blocks="$(curl -s "https://$sisdom/domain_blocks.txt" | grep -P '^[^#]+[^\s]+')"
|
|
|
+# blocks=$(cat "$sisdom" | grep -P '^[^#]+[^\s]+')
|
|
|
+ for line in $blocks; do
|
|
|
+ dom=$(echo "$line" | sed -e 's/\t.*//' -e 's/\./\\./g')
|
|
|
+ echo "$allblocks" | grep -P "^$dom\t" &>/dev/null
|
|
|
+ [ $? -ne 0 ] && allblocks+="$line$IFS"
|
|
|
+ done
|
|
|
+done
|
|
|
+allblocks=$(echo "$allblocks" | head -n -1 | sort)
|
|
|
+
|
|
|
+blocks=$(PGPASSWORD="$DB_PASS" psql -h "$DB_HOST" -p "$DB_PORT" -d "$DB_NAME" -U "$DB_USER" -c 'SELECT domain, updated_at, severity, public_comment FROM domain_blocks' -A -t -F $'\t')
|
|
|
+
|
|
|
+i=0
|
|
|
+for line in $allblocks; do
|
|
|
+ dom=$(echo "$line" | sed -e 's/\t.*//' -e 's/\./\\./g')
|
|
|
+ echo "$blocks" | grep -P "^$dom\t" &>/dev/null
|
|
|
+ [ $? -ne 0 ] && newblocks+="$line$IFS" && ((i++))
|
|
|
+done
|
|
|
+newblocks=$(echo "$newblocks" | head -n -1 | sort)
|
|
|
+
|
|
|
+[ $i -eq 0 ] && echo "Non ho trovato nessuna nuova istanza bloccata." && exit 0
|
|
|
+
|
|
|
+echo "Ho trovato $i istanza/e bloccata da aggiungere."
|
|
|
+
|
|
|
+echo "$newblocks" | PGPASSWORD="$DB_PASS" psql -h "$DB_HOST" -p "$DB_PORT" -d "$DB_NAME" -U "$DB_USER" -A -t -c "COPY domain_blocks ( domain, created_at, updated_at, severity, reject_media, reject_reports, public_comment ) FROM STDIN WITH ( FORMAT text, DELIMITER ' ' )"
|