add some improvements

Signed-off-by: Tealk <tealk@anzah.email>
This commit is contained in:
Tealk 2023-10-25 15:46:46 +02:00
parent 179885ec00
commit b5f438e09d
No known key found for this signature in database
GPG Key ID: 5FE349ABC863E7F9

View File

@ -4,22 +4,26 @@
# UPPERLIMIT="'7 months'" or "'28 weeks'" # UPPERLIMIT="'7 months'" or "'28 weeks'"
# this defines the range of inactivity where users get notified # this defines the range of inactivity where users get notified
# before the account will be deleted when UPPERLIMIT is surpassed. # before the account will be deleted when UPPERLIMIT is surpassed.
LOWERLIMIT="6 months" # all accounts below 6 months inactivity are safe, start notifiying them when >6 months readonly LOWERLIMIT="6 months" # all accounts below 6 months inactivity are safe, start notifiying them when >6 months
UPPERLIMIT="7 months" # inactive accounts older than 7 months will be deactivated readonly UPPERLIMIT="7 months" # inactive accounts older than 7 months will be deactivated
DELDELIMIT="1 year" # all accounts not used within a year will get deleted readonly DELDELIMIT="1 year" # all accounts not used within a year will get deleted
readonly LIVE_PATH="${MASTO_PATH}" # Path to live data from mastodon
readonly TOOTCTL="bin/tootctl"
TOOTCTL="~/live/bin/tootctl" cd "${LIVE_PATH}" || exit
# shellcheck source=/dev/null
source "${LIVE_PATH}/.env.production"
run_tootctl() {
"${TOOTCTL}" "$@"
}
set -f set -f
# set the following variables accordingly to your site # set the following variables accordingly to your site
# the admin will get a notification mail in BCC # the admin will get a notification mail in BCC
grep -e ^"DB_" -e ^"LOCAL_DOMAIN" /home/mastodon/live/.env.production > /home/mastodon/bin/cleanup-mastodon-users.conf
. /home/mastodon/bin/cleanup-mastodon-users.conf
# the following lines should be moved to a config file, eg. /usr/local/etc/cleanup_friendica.conf # the following lines should be moved to a config file, eg. /usr/local/etc/cleanup_friendica.conf
mastodonpath="/home/mastodon/live"
#DB_HOST=127.0.0.1 #DB_HOST=127.0.0.1
#DB_PORT=6432 #DB_PORT=6432
#DB_NAME=mastodon #DB_NAME=mastodon
@ -27,9 +31,7 @@ mastodonpath="/home/mastodon/live"
#DB_PASS= #DB_PASS=
site="Nerdculture.de" site="Nerdculture.de"
siteurl="https://nerdculture.de/" #siteadmin="ij@bluespice.org"
siteadmin="ij@bluespice.org"
sitefrom="no-reply@nerdculture.de"
protectedusers="ij kirschwipfel xmppcompliance order kirschwipfeltest" protectedusers="ij kirschwipfel xmppcompliance order kirschwipfeltest"
sqlprotectedusers="'ij', 'kirschwipfel', 'xmppcompliance', 'order', 'kirschwipfeltest'" sqlprotectedusers="'ij', 'kirschwipfel', 'xmppcompliance', 'order', 'kirschwipfeltest'"
limit_delete=2 limit_delete=2
@ -61,85 +63,85 @@ esac
#. /usr/local/etc/cleanup_friendica.conf #. /usr/local/etc/cleanup_friendica.conf
# make a list to be used for grep -E # make a list to be used for grep -E
protected=$(echo $protectedusers | sed 's/\"//g' | sed 's/\ /\\\|/g') protected=$(echo "$protectedusers" | sed 's/\"//g' | sed 's/\ /\\\|/g')
#echo $protected #echo $protected
cd ${mastodonpath} || exit 0
# notify the user that s/he needs to re-login after 6 months to prevent account deletion # notify the user that s/he needs to re-login after 6 months to prevent account deletion
notifyUser () { notifyUser() {
( cat <<EOF (
Dear ${dispname}, cat <<EOF
Dear ${dispname},
you have registered your account on ${siteurl} at ${registered} and last time you logged in was at ${lastlogin}. you have registered your account on ${LOCAL_DOMAIN} at ${registered} and last time you logged in was at ${lastlogin}.
If you want to continue to keep your Mastodon account on Nerdculture then please log in at least every 6 months via web browser to keep your account alive. Otherwise we assume that you don't want to use it anymore and will deactivate your account 7 months after your last login (and delete it later). If you want to continue to keep your Mastodon account on Nerdculture then please log in at least every 6 months via web browser to keep your account alive. Otherwise we assume that you don't want to use it anymore and will deactivate your account 7 months after your last login (and delete it later).
You can access your profile at ${profileurl} or you can cancel your account on your own when logged in at ${siteurl}removeme - however we would like to see you become an active user again and contribute to the Fediverse, but of course it's up to you. You can access your profile at ${profileurl} or you can cancel your account on your own when logged in at ${LOCAL_DOMAIN}removeme - however we would like to see you become an active user again and contribute to the Fediverse, but of course it's up to you.
Sincerely, Sincerely,
your ${site} admins your ${site} admins
EOF EOF
) | sed 's/_/\ /g' | /usr/bin/mail -s "The Fediverse misses you, ${username}!" -r "${sitefrom}" -- "${usermail}" ) | sed 's/_/\ /g' | /usr/bin/mail -s "The Fediverse misses you, ${username}!" -r "${SMTP_FROM_ADDRESS}" -- "${usermail}"
# add '-b "$siteadmin"' before the "--" above to receive BCC mails # add '-b "$siteadmin"' before the "--" above to receive BCC mails
#((num_notified++)) #((num_notified++))
} }
# notify user that the account has been deleted because of inactivity # notify user that the account has been deleted because of inactivity
notifyUserDisable () { notifyUserDisable() {
( cat <<EOF (
Dear ${dispname}, cat <<EOF
Dear ${dispname},
you have registered your account on ${siteurl} at ${registered} and last time you logged in was at ${lastlogin}. you have registered your account on ${LOCAL_DOMAIN} at ${registered} and last time you logged in was at ${lastlogin}.
Since you haven't reacted to the previous mails and didn't login again, your account including all your data has now been deactivated and will be deleted at a (random) later time. Since you haven't reacted to the previous mails and didn't login again, your account including all your data has now been deactivated and will be deleted at a (random) later time.
Sincerely, Sincerely,
your ${site} admins your ${site} admins
EOF EOF
) | sed 's/_/\ /g' | /usr/bin/mail -s "Your account ${username} on ${site} has been deleted!" -r "${sitefrom}" -- "${usermail}" ) | sed 's/_/\ /g' | /usr/bin/mail -s "Your account ${username} on ${site} has been deleted!" -r "${SMTP_FROM_ADDRESS}" -- "${usermail}"
# add '-b "$siteadmin"' before the "--" above to receive BCC mails # add '-b "$siteadmin"' before the "--" above to receive BCC mails
} }
# notify user that the account has been deleted because of inactivity # notify user that the account has been deleted because of inactivity
notifyUserDeletion () { notifyUserDeletion() {
( cat <<EOF (
Dear ${dispname}, cat <<EOF
Dear ${dispname},
you have registered your account on ${siteurl} at ${registered} and last time you logged in was at ${lastlogin}. you have registered your account on ${LOCAL_DOMAIN} at ${registered} and last time you logged in was at ${lastlogin}.
Since you haven't reacted to the previous mails and didn't login again, your account including all your data has now been deleted. Since you haven't reacted to the previous mails and didn't login again, your account including all your data has now been deleted.
Sincerely, Sincerely,
your ${site} admins your ${site} admins
EOF EOF
) | sed 's/_/\ /g' | /usr/bin/mail -s "Your account ${username} on ${site} has been deleted!" -r "${sitefrom}" -- "${usermail}" ) | sed 's/_/\ /g' | /usr/bin/mail -s "Your account ${username} on ${site} has been deleted!" -r "${SMTP_FROM_ADDRESS}" -- "${usermail}"
# add '-b "$siteadmin"' before the "--" above to receive BCC mails # add '-b "$siteadmin"' before the "--" above to receive BCC mails
} }
# delete users that never logged in and never posted content # delete users that never logged in and never posted content
# filtering for "weeks" will result in accounts with 2 weeks old accounts, # filtering for "weeks" will result in accounts with 2 weeks old accounts,
# filter for just "week" will do the same after 1 week. # filter for just "week" will do the same after 1 week.
# same should apply to "month" and "months", but untested. # same should apply to "month" and "months", but untested.
#for username in $( ${mastodonpath}/bin/tootctl user list active -c 10000 | grep 'never.*never' | grep weeks | awk '{print $2}') ; do #for username in $( "${TOOTCTL}" user list active -c 10000 | grep 'never.*never' | grep weeks | awk '{print $2}') ; do
# select a.id, username, email, current_sign_in_at from accounts a, users u where domain is null and a.id=u.account_id and current_sign_in_at <'2019-01-01' # select a.id, username, email, current_sign_in_at from accounts a, users u where domain is null and a.id=u.account_id and current_sign_in_at <'2019-01-01'
case ${mode} in case ${mode} in
"cron") "cron")
# get the total number of deactivated accounts # get the total number of deactivated accounts
# the intention is that you can better judge how often you need to invoke the cron option # the intention is that you can better judge how often you need to invoke the cron option
# or by increasing the limit_delete variable # or by increasing the limit_delete variable
# the backlog queue shouldn't pile up but also not run empty to fast to reduce the load # the backlog queue shouldn't pile up but also not run empty to fast to reduce the load
num_deactivated_overgrace=$(psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select count(username) from accounts a, users u where disabled is true and a.id=u.account_id and current_sign_in_at < now()-'${DELDELIMIT}'::interval and username not in ($sqlprotectedusers)" | tr -d " " ) num_deactivated_overgrace=$(psql -U "${DB_USER}" -w -h "${DB_HOST}" -p "${DB_PORT}" -t "${DB_NAME}" -c "select count(username) from accounts a, users u where disabled is true and a.id=u.account_id and current_sign_in_at < now()-'${DELDELIMIT}'::interval and username not in ($sqlprotectedusers)" | tr -d " " )
num_deactivated_total=$(psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select count(username) from accounts a, users u where disabled is true and a.id=u.account_id and username not in ($sqlprotectedusers)" | tr -d " " ) num_deactivated_total=$(psql -U "${DB_USER}" -w -h "${DB_HOST}" -p "${DB_PORT}" -t "${DB_NAME}" -c "select count(username) from accounts a, users u where disabled is true and a.id=u.account_id and username not in ($sqlprotectedusers)" | tr -d " " )
# when there is less then 1 user to delete (=0) then exit # when there is less then 1 user to delete (=0) then exit
if [ ${num_deactivated_overgrace} -lt 1 ]; then if [ "${num_deactivated_overgrace}" -lt 1 ]; then
exit 0 exit 0
fi fi
echo "===================================" echo "==================================="
@ -147,7 +149,7 @@ case ${mode} in
echo "Number deactivated accounts over grace: ${num_deactivated_overgrace}" echo "Number deactivated accounts over grace: ${num_deactivated_overgrace}"
echo "Deleting this many accounts: ${limit_delete}" echo "Deleting this many accounts: ${limit_delete}"
echo "===================================" echo "==================================="
for u in $(psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select concat(username||';'||display_name||';'||email||';'||to_char(a.created_at, 'YYYY-MM-DD')||';'||to_char(current_sign_in_at,'YYYY-MM-DD')) from accounts a, users u where disabled is true and a.id=u.account_id and current_sign_in_at < now()-'${DELDELIMIT}'::interval and username not in ($sqlprotectedusers) order by current_sign_in_at limit ${limit_delete}" | tr -d " " ); do for u in $(psql -U "${DB_USER}" -w -h "${DB_HOST}" -p "${DB_PORT}" -t "${DB_NAME}" -c "select concat(username||';'||display_name||';'||email||';'||to_char(a.created_at, 'YYYY-MM-DD')||';'||to_char(current_sign_in_at,'YYYY-MM-DD')) from accounts a, users u where disabled is true and a.id=u.account_id and current_sign_in_at < now()-'${DELDELIMIT}'::interval and username not in ($sqlprotectedusers) order by current_sign_in_at limit ${limit_delete}" | tr -d " " ); do
#echo ${u} #echo ${u}
username=$(echo "${u}" | awk -F ";" '{print $1}') username=$(echo "${u}" | awk -F ";" '{print $1}')
dispname=$(echo "${u}" | awk -F ";" '{print $2}') dispname=$(echo "${u}" | awk -F ";" '{print $2}')
@ -157,29 +159,29 @@ case ${mode} in
lastlogin=$(echo "${u}" | awk -F ";" '{print $5}') lastlogin=$(echo "${u}" | awk -F ";" '{print $5}')
# delete account when last login is older than 7 months and send mail about deletion # delete account when last login is older than 7 months and send mail about deletion
# you should copy & paste the text from 6 months for the first runs of this script # you should copy & paste the text from 6 months for the first runs of this script
# and later change the text to a notification that the account has been deleted. # and later change the text to a notification that the account has been deleted.
# if username is a protected user do nothing, else delete user # if username is a protected user do nothing, else delete user
echo -n "${username} : " echo -n "${username} : "
${mastodonpath}/bin/tootctl accounts delete "${username}" run_tootctl accounts delete "${username}"
notifyUserDeletion notifyUserDeletion
num_deleted=$((num_deleted+1)) num_deleted=$((num_deleted+1))
RND=$(date +%s) RND=$(date +%s)
sec=$(( $RND %60 )) sec=$(( $RND %60 ))
ms=$(( $RND %23 )) ms=$(( $RND %23 ))
sleep ${sec}.${ms} sleep ${sec}.${ms}
done done
;; ;;
*) *)
# find & notify users that didn't logged in >6 months and send mail to log in again#psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select concat(username||';'||email) from accounts a, users u where domain is null and a.id=u.account_id and current_sign_in_at is null and u.created_at < now()-'2 weeks'::interval" | tr -d " " # find & notify users that didn't logged in >6 months and send mail to log in again#psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select concat(username||';'||email) from accounts a, users u where domain is null and a.id=u.account_id and current_sign_in_at is null and u.created_at < now()-'2 weeks'::interval" | tr -d " "
#for username in $(psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select a.id, username, email, current_sign_in_at from accounts a, users u where domain is null and a.id=u.account_id and current_sign_in_at <'2019-01-01'" ) #for username in $(psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select a.id, username, email, current_sign_in_at from accounts a, users u where domain is null and a.id=u.account_id and current_sign_in_at <'2019-01-01'" )
for line in $(psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select concat(username||','||email) from accounts a, users u where domain is null and a.id=u.account_id and current_sign_in_at is null and u.created_at < now()-'2 weeks'::interval and username not in (${sqlprotectedusers})"| tr -d " "); do for line in $(psql -U "${DB_USER}" -w -h "${DB_HOST}" -p "${DB_PORT}" -t "${DB_NAME}" -c "select concat(username||','||email) from accounts a, users u where domain is null and a.id=u.account_id and current_sign_in_at is null and u.created_at < now()-'2 weeks'::interval and username not in (${sqlprotectedusers})"| tr -d " "); do
#echo ${line} #echo ${line}
username=$(echo ${line} | cut -f1 -d"," ) username=$(echo "${line}" | cut -f1 -d"," )
mail=$(echo ${line} | cut -f2 -d"," ) mail=$(echo "${line}" | cut -f2 -d"," )
# if username is a protected user do nothing, else delete user # if username is a protected user do nothing, else delete user
if [ -n "${protectedusers}" ]; then if [ -n "${protectedusers}" ]; then
pcheck=0 pcheck=0
for s in $(echo ${protectedusers}) ; do for s in ${protectedusers} ; do
if [ "${s}" = "${username}" ]; then if [ "${s}" = "${username}" ]; then
pcheck=1 pcheck=1
fi fi
@ -187,20 +189,20 @@ case ${mode} in
if [ ${pcheck} -eq 0 ]; then if [ ${pcheck} -eq 0 ]; then
echo "Delete unconfirmed user ${username}" echo "Delete unconfirmed user ${username}"
if [ "${mode}" = "hotrun" ]; then if [ "${mode}" = "hotrun" ]; then
${mastodonpath}/bin/tootctl accounts delete "${username}" run_tootctl accounts delete "${username}"
elif [ "${mode}" = "dryrun" ]; then elif [ "${mode}" = "dryrun" ]; then
echo "${username}: skipped because of dryrun." echo "${username}: skipped because of dryrun."
fi fi
fi fi
fi fi
done done
#for u in $( ${mastodonpath}/bin/console user list active -c 10000 | grep -v '.*---.*' | sed 's/|/;/g' | tr -s "\ " | sed 's/^;\ //g' | sed 's/\ ;\ /;/g' | sed 's/\ /_/g' | tail -n +2 | grep -i -v -E ${protected} ); do #for u in $( ${LIVE_PATH}/bin/console user list active -c 10000 | grep -v '.*---.*' | sed 's/|/;/g' | tr -s "\ " | sed 's/^;\ //g' | sed 's/\ ;\ /;/g' | sed 's/\ /_/g' | tail -n +2 | grep -i -v -E ${protected} ); do
for mode2 in $(echo "warn disable"); do for mode2 in $(echo "warn disable"); do
#echo "mode2: $mode2" #echo "mode2: $mode2"
case ${mode2} in case ${mode2} in
"warn") "warn")
#echo "in warn" #echo "in warn"
SQLSTATE="current_sign_in_at between now()-'${UPPERLIMIT}'::interval and now()-'${LOWERLIMIT}'::interval" SQLSTATE="current_sign_in_at between now()-'${UPPERLIMIT}'::interval and now()-'${LOWERLIMIT}'::interval"
;; ;;
"disable") "disable")
#echo "in disable" #echo "in disable"
@ -212,7 +214,7 @@ case ${mode} in
# ;; # ;;
esac esac
#echo "SQL: $SQLSTATE" #echo "SQL: $SQLSTATE"
for u in $( psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select concat(username||';'||display_name||';'||email||';'||to_char(a.created_at, 'YYYY-MM-DD')||';'||to_char(current_sign_in_at,'YYYY-MM-DD')) from accounts a, users u where domain is null and a.id=u.account_id and ${SQLSTATE} and username not in ($sqlprotectedusers) order by current_sign_in_at" | tr -d " " ); do for u in $( psql -U "${DB_USER}" -w -h "${DB_HOST}" -p "${DB_PORT}" -t "${DB_NAME}" -c "select concat(username||';'||display_name||';'||email||';'||to_char(a.created_at, 'YYYY-MM-DD')||';'||to_char(current_sign_in_at,'YYYY-MM-DD')) from accounts a, users u where domain is null and a.id=u.account_id and ${SQLSTATE} and username not in ($sqlprotectedusers) order by current_sign_in_at" | tr -d " " ); do
#echo ${u} #echo ${u}
username=$(echo "${u}" | awk -F ";" '{print $1}') username=$(echo "${u}" | awk -F ";" '{print $1}')
dispname=$(echo "${u}" | awk -F ";" '{print $2}') dispname=$(echo "${u}" | awk -F ";" '{print $2}')
@ -233,13 +235,13 @@ case ${mode} in
"disable") "disable")
# delete account when last login is older than 7 months and send mail about deletion # delete account when last login is older than 7 months and send mail about deletion
# you should copy & paste the text from 6 months for the first runs of this script # you should copy & paste the text from 6 months for the first runs of this script
# and later change the text to a notification that the account has been deleted. # and later change the text to a notification that the account has been deleted.
# if username is a protected user do nothing, else delete user # if username is a protected user do nothing, else delete user
echo -n "${username} : " echo -n "${username} : "
if [ "${mode}" = "hotrun" ]; then if [ "${mode}" = "hotrun" ]; then
#echo -n "hotrun " #echo -n "hotrun "
#${mastodonpath}/bin/tootctl accounts delete "${username}" #run_tootctl accounts delete "${username}"
${mastodonpath}/bin/tootctl accounts modify "${username}" --disable run_tootctl accounts modify "${username}" --disable
#notifyUserDeletion #notifyUserDeletion
notifyUserDisable notifyUserDisable
#echo "deleted." #echo "deleted."
@ -260,7 +262,6 @@ esac
ENDDATE=$(date +"%d.%m.%Y %H:%M:%S") ENDDATE=$(date +"%d.%m.%Y %H:%M:%S")
echo "===================================" echo "==================================="
echo "Starting time : ${STARTDATE}" echo "Starting time : ${STARTDATE}"
echo "Ending time : ${ENDDATE}" echo "Ending time : ${ENDDATE}"
@ -268,4 +269,3 @@ echo "Notified Users: $num_notified"
echo "Disabled Users: $num_disabled" echo "Disabled Users: $num_disabled"
echo "Deleted Users : $num_deleted" echo "Deleted Users : $num_deleted"
echo "===================================" echo "==================================="