1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- #!/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
- domain_blocks_dump.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, esegue tramite
- psql una query sulla tabella domain_blocks e ne salva i risultati in un
- file di testo pubblicamente accessibile
- (da https://[dominio]/domain_blocks.txt).
- È pensato per essere eseguito periodicamente da un cron job.
- OPZIONI
- -H, --home
- Definisce la home di mastodon (per default \"$MASTHOME\")
- e di conseguenza la posizione del suo file di configurazione
- (per default \"$MASTENVFP\") e del file in cui scrivere la
- -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]}" ] || [ ! -d "${args[$i+1]}" ]; then
- echo "L'opzione \"${args[$i]}\" richiede un parametro di tipo directory (usa \"-h\" per l'aiuto)."
- exit 1
- else
- ((i++))
- MASTHOME=$(echo "${args[$i]}" | sed -e 's/\/$//')
- 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
- MASTENVFP="$MASTHOME/live/.env.production"
- [ ! -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
- DOMBLOCKSFP="$MASTHOME/live/public/domain_blocks.txt"
- touch "$DOMBLOCKSFP" 2>/dev/null
- [ ! $? -eq 0 ] && echo "Non ho potuto creare/modificare il file \"$DOMBLOCKSFP\", muoio (usa \"-h\" per l'aiuto)." && exit 1
- DB_HOST=`cat "$MASTENVFP"|grep 'DB_HOST'|sed -e 's/[^=]*=//'`
- DB_PORT=`cat "$MASTENVFP"|grep 'DB_PORT'|sed -e 's/[^=]*=//'`
- DB_NAME=`cat "$MASTENVFP"|grep 'DB_NAME'|sed -e 's/[^=]*=//'`
- DB_USER=`cat "$MASTENVFP"|grep 'DB_USER'|sed -e 's/[^=]*=//'`
- DB_PASS=`cat "$MASTENVFP"|grep 'DB_PASS'|sed -e 's/[^=]*=//'`
- echo "# generato $(date -u)" > "$DOMBLOCKSFP"
- echo "# formato di output: dominio bloccato|data ultima modifica blocco|tipo blocco|commento pubblico" >> "$DOMBLOCKSFP"
- echo "# \"tipo blocco\" può essere 0=silenziato, 1=sospeso, 2=nessuno (solo, eventualmente, file media e rapporti)" >> "$DOMBLOCKSFP"
- 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 >> "$DOMBLOCKSFP"
|