123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- #!/bin/sh
- # 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"
- DOMBLOCKSFP="$MASTHOME/live/public/domain_blocks.txt"
- HELP="SINTASSI
- mastblocksdump.sh [opzioni]
- DESCRIZIONE
- Questo script rimedia all'attuale mancanza, in mastodon, di un endpoint
- dell'API che ritorni una lista dei domini bloccati da un'istanza in un
- formato univoco e parsabile. Legge i dati necessari alla connessione al
- db di mastodon dal file di configurazione di mastodon, per default
- \"$MASTENVFP\"
- poi esegue tramite psql una query sulla tabella domain_blocks
- e ne salva i risultati in un file di testo, per default
- \"$DOMBLOCKSFP\"
- che sarà accessibile da \"https://[dominio]/domain_blocks.txt\".
- È 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 e in scrittura a quello
- in cui scrivere la lista delle istanze bloccate.
- OPZIONI
- -H, --home
- Definisce la home di mastodon (per default \"$MASTHOME\")
- e di conseguenza il percorso del suo file di configurazione
- (per default \"$MASTENVFP\") e del file
- in cui scrivere la lista delle istanze bloccate (per default
- \"$DOMBLOCKSFP\").
- È comunque possibile specificare individualmente il percorso
- del file di configurazione di mastodon e di quello in cui scrivere
- la lista delle istanze bloccate con le due opzioni che seguono.
- -e, --envfp
- Definisce il percorso del file di configurazione di mastodon in uso.
- -b, --blocksfp
- Definisce il percorso del file in cui scrivere la lista delle istanze
- bloccate.
- -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"
- DOMBLOCKSFP="$MASTHOME/live/public/domain_blocks.txt"
- 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
- ;;
- "-b" | "--blocksfp" )
- if [ -z "${args[$i+1]}" ]; then
- echo "L'opzione \"${args[$i]}\" richiede un parametro (usa \"-h\" per l'aiuto)."
- exit 1
- else
- ((i++))
- DOMBLOCKSFP="${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
- 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/[^=]*=//'`
- touch "$DOMBLOCKSFP" 2>/dev/null
- [ ! $? -eq 0 ] && echo "Non posso creare/modificare il file \"$DOMBLOCKSFP\", muoio (usa \"-h\" per l'aiuto)." && exit 1
- echo "# generato $(date -u)" > "$DOMBLOCKSFP"
- echo "# formato di output: dominio bloccato[tab]data creazione blocco[tab]data ultima modifica blocco[tab]tipo blocco[tab]rifiuto media[tab]rifiuto reports[tab]commento pubblico" >> "$DOMBLOCKSFP"
- echo "# \"tipo blocco\" può essere 0=silenziato, 1=sospeso, 2=nessuno (solo, eventualmente, rifiuto media e reports)" >> "$DOMBLOCKSFP"
- PGPASSWORD="$DB_PASS" psql -h "$DB_HOST" -p "$DB_PORT" -d "$DB_NAME" -U "$DB_USER" -c 'SELECT domain, created_at, updated_at, severity, reject_media, reject_reports, public_comment FROM domain_blocks' -A -t -F $'\t' >> "$DOMBLOCKSFP"
|