mirror of
				https://codeberg.org/Windfluechter/cleanup-mastodon-users.sh.git
				synced 2025-11-04 13:30:23 +00:00 
			
		
		
		
	Added configuration file and other changes
This commit is contained in:
		
							parent
							
								
									8a60709bfd
								
							
						
					
					
						commit
						246212c8e1
					
				@ -1,14 +1,38 @@
 | 
			
		||||
#!/bin/sh
 | 
			
		||||
 | 
			
		||||
# LOWERLIMIT="'6 months'" or "'24 weeks'"
 | 
			
		||||
# UPPERLIMIT="'7 months'" or "'28 weeks'"
 | 
			
		||||
# this defines the range of inactivity where users get notified
 | 
			
		||||
# before the account will be deleted when UPPERLIMIT is surpassed.
 | 
			
		||||
readonly LOWERLIMIT="6 months"     # all accounts below 6 months inactivity are safe, start notifiying them when >6 months
 | 
			
		||||
readonly UPPERLIMIT="7 months"     # inactive accounts older than 7 months will be deactivated
 | 
			
		||||
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"
 | 
			
		||||
set -f
 | 
			
		||||
 | 
			
		||||
if [ -f ${HOME}/.cleanup-mastodon-users.conf ]; then
 | 
			
		||||
	. ${HOME}/.cleanup-mastodon-users.conf
 | 
			
		||||
else 
 | 
			
		||||
	(
 | 
			
		||||
	cat <<EOF
 | 
			
		||||
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
 | 
			
		||||
DELDELIMIT="1 year"       # all accounts not used within a year will get deleted
 | 
			
		||||
LIVE_PATH="${HOME}/live/" # Path to live data from mastodon
 | 
			
		||||
TOOTCTL="${HOME}/live/bin/tootctl"
 | 
			
		||||
SITE="Mastodon"
 | 
			
		||||
SITEADMIN="root@localhost"
 | 
			
		||||
PROTECTEDUSERS="user foo bar baz"
 | 
			
		||||
SQLPROTECTEDUSERS="'user', 'foo', 'bar', 'baz'"
 | 
			
		||||
LIMIT_DELETE="2"
 | 
			
		||||
 | 
			
		||||
EOF
 | 
			
		||||
	) > ${HOME}/.cleanup-mastodon-users.conf
 | 
			
		||||
	sleep 1
 | 
			
		||||
	. ${HOME}/.cleanup-mastodon-users.conf
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
#LOWERLIMIT="${LOWERLIMIT:-'6 months'}"     # all accounts below 6 months inactivity are safe, start notifiying them when >6 months
 | 
			
		||||
#UPPERLIMIT="${UPPERLIMIT:-'7 months'}"     # inactive accounts older than 7 months will be deactivated
 | 
			
		||||
#DELDELIMIT="${DELDELIMIT:-'1 year'}"       # all accounts not used within a year will get deleted
 | 
			
		||||
#LIVE_PATH="${LIVE_PATH:-${HOME}/live/}" # Path to live data from mastodon
 | 
			
		||||
#TOOTCTL="${TOOTCTL:-${HOME}/bin/tootctl}"
 | 
			
		||||
#SITE="${SITE:-$LOCAL_DOMAIN}"
 | 
			
		||||
#SITEADMIN="${SITEADMIN:-root@localhost}"
 | 
			
		||||
#LIMIT_DELETE="${LIMIT_DELETE:-2}"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
cd "${LIVE_PATH}" || exit
 | 
			
		||||
 | 
			
		||||
@ -19,22 +43,6 @@ run_tootctl() {
 | 
			
		||||
	"${TOOTCTL}" "$@"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
set -f
 | 
			
		||||
# set the following variables accordingly to your site
 | 
			
		||||
# the admin will get a notification mail in BCC
 | 
			
		||||
 | 
			
		||||
# the following lines should be moved to a config file, eg. /usr/local/etc/cleanup_friendica.conf
 | 
			
		||||
#DB_HOST=127.0.0.1
 | 
			
		||||
#DB_PORT=6432
 | 
			
		||||
#DB_NAME=mastodon
 | 
			
		||||
#DB_USER=mastodon
 | 
			
		||||
#DB_PASS=
 | 
			
		||||
 | 
			
		||||
site="Nerdculture.de"
 | 
			
		||||
#siteadmin="ij@bluespice.org"
 | 
			
		||||
protectedusers="ij kirschwipfel xmppcompliance order kirschwipfeltest"
 | 
			
		||||
sqlprotectedusers="'ij', 'kirschwipfel', 'xmppcompliance', 'order', 'kirschwipfeltest'"
 | 
			
		||||
limit_delete=2
 | 
			
		||||
 | 
			
		||||
num_notified=0
 | 
			
		||||
num_disabled=0
 | 
			
		||||
@ -61,11 +69,9 @@ case $1 in
 | 
			
		||||
		;;
 | 
			
		||||
esac
 | 
			
		||||
 | 
			
		||||
#. /usr/local/etc/cleanup_friendica.conf
 | 
			
		||||
 | 
			
		||||
# make a list to be used for grep -E
 | 
			
		||||
protected=$(echo "$protectedusers" | sed 's/\"//g' | sed 's/\ /\\\|/g')
 | 
			
		||||
#echo $protected
 | 
			
		||||
PROTECTED=$(echo "$PROTECTEDUSERS" | sed 's/\"//g' | sed 's/\ /\\\|/g')
 | 
			
		||||
#echo $PROTECTED
 | 
			
		||||
 | 
			
		||||
# notify the user that s/he needs to re-login after 6 months to prevent account deletion
 | 
			
		||||
notifyUser() {
 | 
			
		||||
@ -80,11 +86,11 @@ If you want to continue to keep your Mastodon account on Nerdculture then please
 | 
			
		||||
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,
 | 
			
		||||
your ${site} admins
 | 
			
		||||
your ${SITE} admins
 | 
			
		||||
 | 
			
		||||
EOF
 | 
			
		||||
	) | 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++))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -99,11 +105,11 @@ you have registered your account on ${LOCAL_DOMAIN} at ${registered} and last ti
 | 
			
		||||
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,
 | 
			
		||||
your ${site} admins
 | 
			
		||||
your ${SITE} admins
 | 
			
		||||
 | 
			
		||||
EOF
 | 
			
		||||
	) | 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
 | 
			
		||||
	) | 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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# notify user that the account has been deleted because of inactivity
 | 
			
		||||
@ -117,11 +123,11 @@ you have registered your account on ${LOCAL_DOMAIN} at ${registered} and last ti
 | 
			
		||||
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,
 | 
			
		||||
your ${site} admins
 | 
			
		||||
your ${SITE} admins
 | 
			
		||||
 | 
			
		||||
EOF
 | 
			
		||||
	) | 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
 | 
			
		||||
	) | 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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# delete users that never logged in and never posted content
 | 
			
		||||
@ -138,8 +144,8 @@ case ${mode} in
 | 
			
		||||
		# the intention is that you can better judge how often you need to invoke the cron option
 | 
			
		||||
		# 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
 | 
			
		||||
		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_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 " " )
 | 
			
		||||
		# when there is less then 1 user to delete (=0) then exit
 | 
			
		||||
		if [ "${num_deactivated_overgrace}" -lt 1 ]; then
 | 
			
		||||
			exit 0
 | 
			
		||||
@ -149,7 +155,7 @@ case ${mode} in
 | 
			
		||||
		echo "Number deactivated accounts over grace: ${num_deactivated_overgrace}"
 | 
			
		||||
		echo "Deleting this many accounts: ${limit_delete}"
 | 
			
		||||
		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}
 | 
			
		||||
			username=$(echo "${u}" | awk -F ";" '{print $1}')
 | 
			
		||||
			dispname=$(echo "${u}" | awk -F ";" '{print $2}')
 | 
			
		||||
@ -160,7 +166,7 @@ case ${mode} in
 | 
			
		||||
			# 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
 | 
			
		||||
			# 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} : "
 | 
			
		||||
			run_tootctl accounts delete "${username}"
 | 
			
		||||
			notifyUserDeletion
 | 
			
		||||
@ -174,14 +180,14 @@ case ${mode} in
 | 
			
		||||
	*)
 | 
			
		||||
		# 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 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}
 | 
			
		||||
			username=$(echo "${line}" | cut -f1 -d"," )
 | 
			
		||||
			mail=$(echo "${line}" | cut -f2 -d"," )
 | 
			
		||||
			# if username is a protected user do nothing, else delete user
 | 
			
		||||
			if [ -n "${protectedusers}" ]; then
 | 
			
		||||
			# if username is a PROTECTED user do nothing, else delete user
 | 
			
		||||
			if [ -n "${PROTECTEDUSERS}" ]; then
 | 
			
		||||
				pcheck=0
 | 
			
		||||
				for s in ${protectedusers} ; do
 | 
			
		||||
				for s in ${PROTECTEDUSERS} ; do
 | 
			
		||||
					if [ "${s}" = "${username}" ]; then
 | 
			
		||||
						pcheck=1
 | 
			
		||||
					fi
 | 
			
		||||
@ -196,7 +202,7 @@ case ${mode} in
 | 
			
		||||
				fi
 | 
			
		||||
			fi
 | 
			
		||||
		done
 | 
			
		||||
		#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 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
 | 
			
		||||
			#echo "mode2: $mode2"
 | 
			
		||||
			case ${mode2} in
 | 
			
		||||
@ -214,7 +220,7 @@ case ${mode} in
 | 
			
		||||
				#	;;
 | 
			
		||||
			esac
 | 
			
		||||
			#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}
 | 
			
		||||
				username=$(echo "${u}" | awk -F ";" '{print $1}')
 | 
			
		||||
				dispname=$(echo "${u}" | awk -F ";" '{print $2}')
 | 
			
		||||
@ -236,7 +242,7 @@ case ${mode} in
 | 
			
		||||
						# 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
 | 
			
		||||
						# 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} : "
 | 
			
		||||
						if [ "${mode}" = "hotrun" ]; then
 | 
			
		||||
							#echo -n "hotrun  "
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user