#!/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-20_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 -en "\nGenerated with :\n" cat ${0}