mastblocksmerge.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/bin/bash
  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. SISTERSFP="istanzesorelle"
  15. INTERACTIVE=0
  16. HELP="SINTASSI
  17. mastblocksmerge.sh [opzioni]
  18. DESCRIZIONE
  19. Questo script integra i dati di una o più blocklist esterne nella
  20. tabella domain_blocks di mastodon.
  21. Legge i domini delle istanze di cui recuperare la blocklist da un file,
  22. per default \"$SISTERSP\" (formato del file: un dominio per riga),
  23. prova a recuperare ciascuna blocklist da
  24. https://[dominio]/domain_blocks.txt, costruisce dalle liste recuperate
  25. un\'unica lista senza duplicati, si collega al db di mastodon e scrive
  26. nella tabella domain_blocks i dati relativi alle istanze bloccate che
  27. non sono già presenti nella tabella stessa.
  28. Per la connessione al db di mastodon legge i dati necessari dal file
  29. di configurazione di mastodon, per default
  30. \"$MASTENVFP\"
  31. È pensato per essere eseguito periodicamente da un cron job, come
  32. utente mastodon oppure root oppure altro utente che abbia accesso in
  33. lettura al file di configurazione di mastodon.
  34. OPZIONI
  35. -H, --home
  36. Definisce la home di mastodon (per default \"$MASTHOME\")
  37. e di conseguenza il percorso del suo file di configurazione
  38. (per default \"$MASTENVFP\").
  39. È comunque possibile specificare individualmente il percorso
  40. del file di configurazione di mastodon con l\'opzione che segue.
  41. -e, --envfp
  42. Definisce il percorso del file di configurazione di mastodon in uso.
  43. -s, --sistersfp
  44. Definisce il percorso del file da cui leggere la lista delle istanze
  45. sorelle.
  46. -i, --interactive
  47. Modalità interattiva: se vengono nelle blocklist vengono trovate
  48. istanze ancora non presenti nel database di mastodon, viene chiesto
  49. per ciascuna se aggiungerla o meno.
  50. -h, --help
  51. Mostra questo aiuto ed esce."
  52. args=("$@")
  53. i=0
  54. while [ $i -lt ${#args[@]} ]; do
  55. if [ "${args[$i]:0:1}" == "-" ]; then
  56. case "${args[$i]}" in
  57. "-H" | "--home" )
  58. if [ -z "${args[$i+1]}" ]; then
  59. echo "L'opzione \"${args[$i]}\" richiede un parametro (usa \"-h\" per l'aiuto)."
  60. exit 1
  61. else
  62. ((i++))
  63. MASTHOME=$(echo "${args[$i]}" | sed -e 's/\/$//')
  64. MASTENVFP="$MASTHOME/live/.env.production"
  65. fi
  66. ;;
  67. "-e" | "--envfp" )
  68. if [ -z "${args[$i+1]}" ]; then
  69. echo "L'opzione \"${args[$i]}\" richiede un parametro (usa \"-h\" per l'aiuto)."
  70. exit 1
  71. else
  72. ((i++))
  73. MASTENVFP="${args[$i]}"
  74. fi
  75. ;;
  76. "-s" | "--sistersfp" )
  77. if [ -z "${args[$i+1]}" ]; then
  78. echo "L'opzione \"${args[$i]}\" richiede un parametro (usa \"-h\" per l'aiuto)."
  79. exit 1
  80. else
  81. ((i++))
  82. SISTERSFP="${args[$i]}"
  83. fi
  84. ;;
  85. "-i" | "--interactive" )
  86. INTERACTIVE=1
  87. ;;
  88. "-h" | "--help" )
  89. echo "$HELP"
  90. exit 0
  91. ;;
  92. *)
  93. echo "\"${args[$i]}\": opzione sconosciuta (usa \"-h\" per l'aiuto)."
  94. exit 1
  95. ;;
  96. esac
  97. else
  98. echo "\"${args[$i]}\": opzione sconosciuta (usa \"-h\" per l'aiuto)."
  99. exit 1
  100. fi
  101. ((i++))
  102. done
  103. [ ! -e "$MASTENVFP" ] && echo "\"$MASTENVFP\" non esiste, muoio (usa \"-h\" per l'aiuto)." && exit 1
  104. [ ! -f "$MASTENVFP" ] && echo "\"$MASTENVFP\" non è un file, muoio (usa \"-h\" per l'aiuto)." && exit 1
  105. [ ! -r "$MASTENVFP" ] && echo "\"$MASTENVFP\" non è leggibile, muoio (usa \"-h\" per l'aiuto)." && exit 1
  106. [ ! -e "$SISTERSFP" ] && echo "\"$SISTERSFP\" non esiste, muoio (usa \"-h\" per l'aiuto)." && exit 1
  107. [ ! -f "$SISTERSFP" ] && echo "\"$SISTERSFP\" non è un file, muoio (usa \"-h\" per l'aiuto)." && exit 1
  108. [ ! -r "$SISTERSFP" ] && echo "\"$SISTERSFP\" non è leggibile, muoio (usa \"-h\" per l'aiuto)." && exit 1
  109. DB_HOST=`grep 'DB_HOST' "$MASTENVFP"|sed -e 's/[^=]*=//'`
  110. DB_PORT=`grep 'DB_PORT' "$MASTENVFP"|sed -e 's/[^=]*=//'`
  111. DB_NAME=`grep 'DB_NAME' "$MASTENVFP"|sed -e 's/[^=]*=//'`
  112. DB_USER=`grep 'DB_USER' "$MASTENVFP"|sed -e 's/[^=]*=//'`
  113. DB_PASS=`grep 'DB_PASS' "$MASTENVFP"|sed -e 's/[^=]*=//'`
  114. IFS=$'\n'
  115. for sisdom in $(grep -P '^[^#]+[^\s]+' "$SISTERSFP"); do
  116. blocks="$(curl -s "https://$sisdom/domain_blocks.txt" | grep -P '^[^#]+[^\s]+')"
  117. # blocks=$(cat "$sisdom" | grep -P '^[^#]+[^\s]+')
  118. for line in $blocks; do
  119. echo "$allblocks"|grep -F "$line" &>/dev/null
  120. if [ $? -ne 0 ]; then
  121. dom=$(echo "$line" | sed -e 's/\t.*//' -e 's/\./\\./g')
  122. echo "$allblocks" | grep -P "^$dom\t" &>/dev/null
  123. [ $? -ne 0 ] && allblocks+="$line$IFS"
  124. fi
  125. done
  126. done
  127. allblocks=$(echo "$allblocks" | head -n -1 | sort)
  128. 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')
  129. i=0
  130. for line in $allblocks; do
  131. dom=$(echo "$line" | sed -e 's/\t.*//' -e 's/\./\\./g')
  132. echo "$blocks" | grep -P "^$dom\t" &>/dev/null
  133. [ $? -ne 0 ] && newblocks+="$line$IFS" && ((i++))
  134. done
  135. newblocks=$(echo "$newblocks" | head -n -1 | sort)
  136. [ $i -eq 0 ] && echo "Non ho trovato nessuna nuova istanza bloccata." && exit 0
  137. echo "Ho trovato $i istanza/e bloccata da aggiungere."
  138. if [ $INTERACTIVE -eq 1 ]; then
  139. for line in $newblocks; do
  140. echo "$line"
  141. ask=1
  142. while [ $ask -eq 1 ]; do
  143. read -p "Vuoi aggiungere questa istanza alla tabella delle istanze bloccate? [S/n] " inp
  144. echo "$inp" | grep -P '^[sSnN]{0,1}$' &>/dev/null
  145. [ $? -eq 0 ] && ask=0
  146. done
  147. if [ "$inp" == "" ] || [ "$inp" == "s" ] || [ "$inp" == "S" ]; then
  148. buf+="$line$IFS"
  149. fi
  150. done
  151. newblocks=$(echo "$buf" | head -n -1)
  152. fi
  153. 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 ' ' )"