2025-01-24 19:16:21 +00:00
#!/bin/bash
db = "friendica"
tmpfile = "/tmp/sitesdown.txt"
idsdownfile = "/tmp/idsdown.txt"
2025-02-02 03:25:37 +00:00
url = friendica.example.net
avatarfolder = /var/www/friendica/avatar
avatarfolderescaped = ${ avatarfolder //// \\ / }
2025-01-24 19:16:21 +00:00
loop_1( ) {
2025-02-02 21:15:23 +00:00
sitereq = $( curl -s -L --head -m 30 --request GET " ${ a } " )
#Skip check if the message contains a reference to Cloudflare
2025-01-24 19:34:48 +00:00
status = $( echo " ${ sitereq } " | grep -e "200" -e "cloudflare" )
2025-02-05 14:59:57 +00:00
if [ [ -z ${ status } ] ] ; then
echo " ${ a } " >>" ${ tmpfile } "
2025-01-24 19:34:48 +00:00
echo " Added ${ a } "
2025-01-24 19:16:21 +00:00
fi
}
loop_2( ) {
2025-01-24 19:34:48 +00:00
echo " Finding users for ${ b } "
2025-02-02 21:15:23 +00:00
" ${ dbengine } " " ${ db } " -N -B -q -e " select \`id\`, \`nick\`, \`baseurl\` from contact c where c.\`id\` not in (select \`contact-id\` from group_member) and (c.baseurl = \" ${ b } \" or c.url = \" ${ b } \") " | sudo tee -a " ${ idsdownfile } " #&> /dev/null
2025-01-24 19:16:21 +00:00
}
loop_3( ) {
2025-02-01 23:12:02 +00:00
baseurltrimmed = $( echo " ${ baseurl } " | sed -e "s/http[s]*:\/\///g" )
echo " Deleting user ${ lineb } - ${ nick } @ ${ baseurltrimmed } "
2025-02-02 03:25:37 +00:00
#Find the pictures in the avatar folders and delete them
2025-02-05 14:59:57 +00:00
" ${ dbengine } " " ${ db } " -N -B -q -e " select \`photo\`, \`thumb\`, \`micro\` from \`contact\` where \`id\` = ${ lineb } " | while read -r photo thumb micro; do
2025-02-02 03:25:37 +00:00
#If stored in avatar folder
2025-02-05 14:59:57 +00:00
if $( echo " ${ photo } " | grep -q " ${ url } /avatar " ) ; then #isavatar=$(grep -q "${url}/avatar" <<< "${photo}")
#if [[ -z "${isavatar}" ]]
2025-02-02 03:25:37 +00:00
phototrimmed = $( echo " ${ photo } " | sed -e " s/https:\/\/ ${ url } \/avatar/ ${ avatarfolderescaped } /g " -e "s/\?ts.*//g" )
2025-02-02 21:15:23 +00:00
echo " ${ phototrimmed } "
2025-02-02 03:25:37 +00:00
rm -rfv " ${ phototrimmed } "
thumbtrimmed = $( echo " ${ thumb } " | sed -e " s/https:\/\/ ${ url } \/avatar/ ${ avatarfolderescaped } /g " -e "s/\?ts.*//g" )
rm -rfv " ${ thumbtrimmed } "
microtrimmed = $( echo " ${ micro } " | sed -e " s/https:\/\/ ${ url } \/avatar/ ${ avatarfolderescaped } /g " -e "s/\?ts.*//g" )
rm -rfv " ${ microtrimmed } "
fi
done
2025-02-01 23:12:02 +00:00
" ${ dbengine } " " ${ db } " -N -B -q -e " delete from \`post-thread\` where \`author-id\` = ${ lineb } or \`causer-id\` = ${ lineb } or \`owner-id\` = ${ lineb } "
" ${ dbengine } " " ${ db } " -N -B -q -e " delete from \`post-thread-user\` where \`author-id\` = ${ lineb } or \`causer-id\` = ${ lineb } or \`owner-id\` = ${ lineb } "
" ${ dbengine } " " ${ db } " -N -B -q -e " delete from \`post-user\` where \`author-id\` = ${ lineb } or \`causer-id\` = ${ lineb } or \`owner-id\` = ${ lineb } "
" ${ dbengine } " " ${ db } " -N -B -q -e " delete from \`post-tag\` where cid = ${ lineb } "
" ${ dbengine } " " ${ db } " -N -B -q -e " delete from \`post\` where \`owner-id\` = ${ lineb } or \`author-id\` = ${ lineb } or \`causer-id\` = ${ lineb } "
" ${ dbengine } " " ${ db } " -N -B -q -e " delete from \`photo\` where \`contact-id\` = ${ lineb } "
" ${ dbengine } " " ${ db } " -N -B -q -e " delete from \`contact\` where \`id\` = ${ lineb } "
2025-01-24 19:16:21 +00:00
}
2025-02-01 23:12:02 +00:00
#Check for mariadb vs. mysql
dbengine = ""
2025-02-05 14:59:57 +00:00
if [ [ -n $( type mariadb) ] ] ; then
dbengine = "mariadb"
elif [ [ -n $( type mysql) ] ] ; then
dbengine = "mysql"
2025-02-01 23:12:02 +00:00
fi
2025-01-24 19:16:21 +00:00
#Check if our dependencies are installed
2025-02-05 14:59:57 +00:00
if [ [ -n $( type curl) && -n " ${ dbengine } " && -n $( type " ${ dbengine } " ) && -n $( type date) ] ] ; then
2025-01-24 19:16:21 +00:00
date
2025-02-05 14:59:57 +00:00
if [ [ ! -f " ${ tmpfile } " ] ] ; then
2025-01-24 19:16:21 +00:00
echo "Listing sites"
2025-02-02 21:15:23 +00:00
#sites=($("${dbengine}" "${db}" -N -B -q -e "select distinct baseurl from contact where baseurl != \"\"" | sort -n | uniq ))
sites = ( )
2025-02-05 14:59:57 +00:00
mapfile -t sites < <( " ${ dbengine } " " ${ db } " -N -B -q -e "select distinct baseurl from contact where baseurl != \"\"" | sort -b -f -n | uniq -i)
2025-01-24 19:16:21 +00:00
echo " Amount of unique sites: ${# sites [@] } "
2025-02-05 14:59:57 +00:00
for a in " ${ sites [@] } " ; do
2025-01-24 19:34:48 +00:00
loop_1 " ${ a } " &
2025-02-05 14:59:57 +00:00
if [ [ $( jobs -r -p | wc -l) -ge $(( $( getconf _NPROCESSORS_ONLN) * 2 )) ] ] ; then
2025-01-24 19:16:21 +00:00
wait -n
fi
done
wait
fi
sitesdown = ( )
while read -r line; do
2025-02-02 21:15:23 +00:00
sitesdown += ( " ${ line } " )
2025-02-05 14:59:57 +00:00
done <" ${ tmpfile } "
2025-01-24 19:16:21 +00:00
echo " Amount of sites down: ${# sitesdown [@] } / ${# sites [@] } "
2025-02-05 14:59:57 +00:00
if [ [ ! -f " ${ idsdownfile } " ] ] ; then
for b in " ${ sitesdown [@] } " ; do
2025-01-24 19:34:48 +00:00
loop_2 " ${ b } " &
2025-02-05 14:59:57 +00:00
if [ [ $( jobs -r -p | wc -l) -ge $(( $( getconf _NPROCESSORS_ONLN) / 2 )) ] ] ; then
2025-01-24 19:16:21 +00:00
wait -n
fi
done
wait
#cat "$idsdownfile" | sort -n | uniq > "$idsdownfile"
fi
2025-02-01 03:59:59 +00:00
#idsdown=()
#echo "$idsdownfile" | sort | uniq > "$idsdownfile"
2025-02-01 23:12:02 +00:00
while read -r lineb nick baseurl; do
2025-02-01 03:59:59 +00:00
#idsdown+=($lineb)
2025-01-24 19:16:21 +00:00
#The community no longer exists, delete
2025-02-01 23:12:02 +00:00
loop_3 " ${ lineb } " " ${ nick } " " ${ baseurl } " &
2025-02-05 14:59:57 +00:00
if [ [ $( jobs -r -p | wc -l) -ge $(( $( getconf _NPROCESSORS_ONLN) / 2 )) ] ] ; then
2025-01-24 19:16:21 +00:00
wait -n
fi
wait
2025-02-05 14:59:57 +00:00
done <" ${ idsdownfile } "
rm " ${ tmpfile } " 2>/dev/null
rm " ${ idsdownfile } " 2>/dev/null
2025-01-24 19:16:21 +00:00
date
fi