mastblocksdump.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. mastblocksdump.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, per default
  22. \"$MASTENVFP\"
  23. poi esegue tramite psql una query sulla tabella domain_blocks
  24. e ne salva i risultati in un file di testo, per default
  25. \"$DOMBLOCKSFP\"
  26. che sarà accessibile da \"https://[dominio]/domain_blocks.txt\".
  27. È pensato per essere eseguito periodicamente da un cron job, come
  28. utente mastodon oppure root oppure altro utente che abbia accesso in
  29. lettura al file di configurazione di mastodon e in scrittura a quello
  30. in cui scrivere la lista delle istanze bloccate.
  31. OPZIONI
  32. -H, --home
  33. Definisce la home di mastodon (per default \"$MASTHOME\")
  34. e di conseguenza il percorso del suo file di configurazione
  35. (per default \"$MASTENVFP\") e del file
  36. in cui scrivere la lista delle istanze bloccate (per default
  37. \"$DOMBLOCKSFP\").
  38. È comunque possibile specificare individualmente il percorso
  39. del file di configurazione di mastodon e di quello in cui scrivere
  40. la lista delle istanze bloccate con le due opzioni che seguono.
  41. -e, --envfp
  42. Definisce il percorso del file di configurazione di mastodon in uso.
  43. -b, --blocksfp
  44. Definisce il percorso del file in cui scrivere la lista delle istanze
  45. bloccate.
  46. -h, --help
  47. Mostra questo aiuto ed esce."
  48. args=("$@")
  49. i=0
  50. while [ $i -lt ${#args[@]} ]; do
  51. if [ "${args[$i]:0:1}" == "-" ]; then
  52. case "${args[$i]}" in
  53. "-H" | "--home" )
  54. if [ -z "${args[$i+1]}" ]; then
  55. echo "L'opzione \"${args[$i]}\" richiede un parametro (usa \"-h\" per l'aiuto)."
  56. exit 1
  57. else
  58. ((i++))
  59. MASTHOME=$(echo "${args[$i]}" | sed -e 's/\/$//')
  60. MASTENVFP="$MASTHOME/live/.env.production"
  61. DOMBLOCKSFP="$MASTHOME/live/public/domain_blocks.txt"
  62. fi
  63. ;;
  64. "-e" | "--envfp" )
  65. if [ -z "${args[$i+1]}" ]; then
  66. echo "L'opzione \"${args[$i]}\" richiede un parametro (usa \"-h\" per l'aiuto)."
  67. exit 1
  68. else
  69. ((i++))
  70. MASTENVFP="${args[$i]}"
  71. fi
  72. ;;
  73. "-b" | "--blocksfp" )
  74. if [ -z "${args[$i+1]}" ]; then
  75. echo "L'opzione \"${args[$i]}\" richiede un parametro (usa \"-h\" per l'aiuto)."
  76. exit 1
  77. else
  78. ((i++))
  79. DOMBLOCKSFP="${args[$i]}"
  80. fi
  81. ;;
  82. "-h" | "--help" )
  83. echo "$HELP"
  84. exit 0
  85. ;;
  86. *)
  87. echo "\"${args[$i]}\": opzione sconosciuta (usa \"-h\" per l'aiuto)."
  88. exit 1
  89. ;;
  90. esac
  91. else
  92. echo "\"${args[$i]}\": opzione sconosciuta (usa \"-h\" per l'aiuto)."
  93. exit 1
  94. fi
  95. ((i++))
  96. done
  97. [ ! -e "$MASTENVFP" ] && echo "\"$MASTENVFP\" non esiste, muoio (usa \"-h\" per l'aiuto)." && exit 1
  98. [ ! -f "$MASTENVFP" ] && echo "\"$MASTENVFP\" non è un file, muoio (usa \"-h\" per l'aiuto)." && exit 1
  99. [ ! -r "$MASTENVFP" ] && echo "\"$MASTENVFP\" non è leggibile, muoio (usa \"-h\" per l'aiuto)." && exit 1
  100. DB_HOST=`grep 'DB_HOST' "$MASTENVFP"|sed -e 's/[^=]*=//'`
  101. DB_PORT=`grep 'DB_PORT' "$MASTENVFP"|sed -e 's/[^=]*=//'`
  102. DB_NAME=`grep 'DB_NAME' "$MASTENVFP"|sed -e 's/[^=]*=//'`
  103. DB_USER=`grep 'DB_USER' "$MASTENVFP"|sed -e 's/[^=]*=//'`
  104. DB_PASS=`grep 'DB_PASS' "$MASTENVFP"|sed -e 's/[^=]*=//'`
  105. touch "$DOMBLOCKSFP" 2>/dev/null
  106. [ ! $? -eq 0 ] && echo "Non posso creare/modificare il file \"$DOMBLOCKSFP\", muoio (usa \"-h\" per l'aiuto)." && exit 1
  107. echo "# generato $(date -u)" > "$DOMBLOCKSFP"
  108. 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"
  109. echo "# \"tipo blocco\" può essere 0=silenziato, 1=sospeso, 2=nessuno (solo, eventualmente, rifiuto media e reports)" >> "$DOMBLOCKSFP"
  110. 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"