domain_blocks_dump.sh 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/sh
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. MASTHOME='/var/lib/mastodon'
  13. MASTENVFP="$MASTHOME/live/.env.production"
  14. DOMBLOCKSFP="$MASTHOME/live/public/domain_blocks.txt"
  15. HELP="SINTASSI
  16. domain_blocks_dump.sh [opzioni]
  17. DESCRIZIONE
  18. Questo script rimedia all'attuale mancanza, in mastodon, di un endpoint
  19. dell'API che ritorni una lista dei domini bloccati da un'istanza in un
  20. formato univoco e parsabile. Legge i dati necessari alla connessione al
  21. db di mastodon dal file di configurazione di mastodon, esegue tramite
  22. psql una query sulla tabella domain_blocks e ne salva i risultati in un
  23. file di testo pubblicamente accessibile
  24. (da https://[dominio]/domain_blocks.txt).
  25. È pensato per essere eseguito periodicamente da un cron job.
  26. OPZIONI
  27. -H, --home
  28. Definisce la home di mastodon (per default \"$MASTHOME\")
  29. e di conseguenza la posizione del suo file di configurazione
  30. (per default \"$MASTENVFP\") e del file in cui scrivere la
  31. -h, --help
  32. Mostra questo aiuto ed esce."
  33. args=("$@")
  34. i=0
  35. while [ $i -lt ${#args[@]} ]; do
  36. if [ "${args[$i]:0:1}" == "-" ]; then
  37. case "${args[$i]}" in
  38. "-H" | "--home" )
  39. if [ -z "${args[$i+1]}" ] || [ ! -d "${args[$i+1]}" ]; then
  40. echo "L'opzione \"${args[$i]}\" richiede un parametro di tipo directory (usa \"-h\" per l'aiuto)."
  41. exit 1
  42. else
  43. ((i++))
  44. MASTHOME=$(echo "${args[$i]}" | sed -e 's/\/$//')
  45. fi
  46. ;;
  47. "-h" | "--help" )
  48. echo "$HELP"
  49. exit 0
  50. ;;
  51. *)
  52. echo "\"${args[$i]}\": opzione sconosciuta (usa \"-h\" per l'aiuto)."
  53. exit 1
  54. ;;
  55. esac
  56. else
  57. echo "\"${args[$i]}\": opzione sconosciuta (usa \"-h\" per l'aiuto)."
  58. exit 1
  59. fi
  60. ((i++))
  61. done
  62. MASTENVFP="$MASTHOME/live/.env.production"
  63. [ ! -e "$MASTENVFP" ] && echo "\"$MASTENVFP\" non esiste, muoio (usa \"-h\" per l'aiuto)." && exit 1
  64. [ ! -f "$MASTENVFP" ] && echo "\"$MASTENVFP\" non è un file, muoio (usa \"-h\" per l'aiuto)." && exit 1
  65. [ ! -r "$MASTENVFP" ] && echo "\"$MASTENVFP\" non è leggibile, muoio (usa \"-h\" per l'aiuto)." && exit 1
  66. DOMBLOCKSFP="$MASTHOME/live/public/domain_blocks.txt"
  67. touch "$DOMBLOCKSFP" 2>/dev/null
  68. [ ! $? -eq 0 ] && echo "Non ho potuto creare/modificare il file \"$DOMBLOCKSFP\", muoio (usa \"-h\" per l'aiuto)." && exit 1
  69. DB_HOST=`cat "$MASTENVFP"|grep 'DB_HOST'|sed -e 's/[^=]*=//'`
  70. DB_PORT=`cat "$MASTENVFP"|grep 'DB_PORT'|sed -e 's/[^=]*=//'`
  71. DB_NAME=`cat "$MASTENVFP"|grep 'DB_NAME'|sed -e 's/[^=]*=//'`
  72. DB_USER=`cat "$MASTENVFP"|grep 'DB_USER'|sed -e 's/[^=]*=//'`
  73. DB_PASS=`cat "$MASTENVFP"|grep 'DB_PASS'|sed -e 's/[^=]*=//'`
  74. echo "# generato $(date -u)" > "$DOMBLOCKSFP"
  75. echo "# formato di output: dominio bloccato|data ultima modifica blocco|tipo blocco|commento pubblico" >> "$DOMBLOCKSFP"
  76. echo "# \"tipo blocco\" può essere 0=silenziato, 1=sospeso, 2=nessuno (solo, eventualmente, file media e rapporti)" >> "$DOMBLOCKSFP"
  77. 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"