From e545d7d76939a511f614e5a88e7590013173c48e Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 13 Jul 2019 21:23:44 +0200 Subject: [PATCH] Added extract_mrf_simple_domains.sh. --- extract_mrf_simple_domains.sh | 93 +++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100755 extract_mrf_simple_domains.sh diff --git a/extract_mrf_simple_domains.sh b/extract_mrf_simple_domains.sh new file mode 100755 index 0000000..23e96d2 --- /dev/null +++ b/extract_mrf_simple_domains.sh @@ -0,0 +1,93 @@ +#!/bin/zsh +# Parses MRF.SimplePolicy from Pleroma config and outputs federation +# restrictions in human-readable plain text. First argument is prod.secret.exs. + +# Version: 2019-07-13_1 + +# The format in prod.secret.exs should be: +# +# config :pleroma, :mrf_simple, +# reject: +# [ +# "example.com" # Reason you blocked them +# ], +# federated_timeline_removal: [], +# +# And so on. White-space doesn't matter. + +function cut_mrfsimple() # Output the :mrf_simple-block of the config. +{ + local line + declare -i local start=0 + declare -i local stop=0 + + # Start at line containing ":mrf_simple", stop at empty newline. + while read line; do + if [[ ${line} =~ ":mrf_simple" ]]; then + start=1 + fi + if [[ start -eq 1 && "${line}" == "" ]]; then + stop=1 + fi + + if [[ ${start} -eq 1 && ${stop} -eq 0 ]]; then + echo ${line} + fi + done +} + +function parse() # Output the domain list for ${1}. +{ + local word=${1} + local line + declare -i local start=0 + declare -i local stop=0 + + # Start at line containing "${word}:", stop at line containing "]" followed + # by "," or newline. + while read line; do + if [[ ${line} =~ "${word}:" ]]; then + start=1 + fi + if [[ start -eq 1 && "${line}" =~ "\][,\n]" ]]; then + stop=1 + fi + + if [[ ${start} -eq 1 && ${stop} -eq 0 ]]; then + if [[ "${line}" =~ "\".+\"" ]]; then + echo ${line} | sed -E 's/"([^ ]+\.[^ ]+)",?/\1/g' + fi + fi + done +} + +function pretty() # Output a pretty table. +{ + # Replacing the first "#" with "§" and using it as separator, because "#" + # could occur in the comment and "§" hopefully not. + echo ${1} | sed 's/[ \t]# */ §/' \ + | column -t -s '§' -N "Domain(s),Reason(s)" | sed 's/^/ /' +} + +function main() +{ + local file="${1}" + local mrfsimple=$(cat ${file} | cut_mrfsimple) + + for word in \ + "media_removal" "media_nsfw" "federated_timeline_removal" "reject" + do + local text=$(echo ${mrfsimple} | parse "${word}") + if [[ -n ${text} ]]; then # Only print table if entries exist. + # Convert to uppercase and replace "_" with " ". + echo -e "${word:u:gs/_/ }:" + pretty "${text}" + echo + fi + done +} + +main "${1}" + +echo -e "\nGenerated with:\n" +cat ${0}