Add dropping accounts

This commit is contained in:
Tristan Le Chanony 2020-01-03 13:45:14 +01:00
parent 0752c4436c
commit 0258bb66f6
1 changed files with 72 additions and 4 deletions

View File

@ -69,14 +69,14 @@ def alert_accounts(days):
receivers = [''+mail+'']
message = """From: """+email['fromname']+""" <"""+email['from']+""">
To: """+username+""" <"""+mail+""">
Subject: Your account has been inactive for more than 365 days
Subject: Your account has been inactive for more than """+days+""" days
----
English message bellow.
----
Bonjour
Votre compte est inactif depuis plus de 365 jours.
Votre compte est inactif depuis plus de """+days+""" jours.
Si vous ne souhaitez pas conserver ce compte sur l'instance Mastodon miaou.drycat.fr, oubliez ce mail.
Sinon, connectez vous et par messure de sécurité, envoyez un message privée à vous même.
@ -87,7 +87,7 @@ L'équipe d'administration de """+email['fromname']+""".
--------
Hello
Your account has been inactive for more than 365 days.
Your account has been inactive for more than """+days+""" days.
If you do not wish to keep this account on the instance Mastodon miaou.drycat.fr, forget this mail.
Otherwise, log in and by security measure, send a private message to yourself.
@ -113,11 +113,76 @@ The """+email['fromname']+"""'s administration team.
log.debug(e)
log.error("Error: unable to send email")
def drop_accounts(days):
users_count = mastodon.instance()['stats']['user_count']
mastodon_accounts = mastodon.admin_accounts(remote=False, status='active', limit=users_count)
for account in mastodon_accounts:
uid = account['id']
username = account['username']
mail = account['email']
if account['account']['last_status_at'] == None:
last_activity = account['account']['created_at']
else:
last_activity = account['account']['last_status_at']
today = datetime.now(timezone.utc)
if today - last_activity > timedelta(int(days)):
log.info("User "+username+" https://miaou.drycat.fr/admin/accounts/"+str(uid)+" is deleted")
mastodon.admin_account_moderate(uid, action="suspend", report_id=None, warning_preset_id=None, text="Autodeleting old account", send_email_notification=False)
sender = email['from']
receivers = [''+mail+'']
message = """From: """+email['fromname']+""" <"""+email['from']+""">
To: """+username+""" <"""+mail+""">
Subject: Your account has been deleted
----
English message bellow.
----
Bonjour
Votre compte est inactif depuis plus de """+days+""" jours.
Vous n'avez pas donné suite à notre mail d'hier.
Nous avons donc clôturé votre compte.
Cordialement
L'équipe d'administration de """+email['fromname']+""".
--------
Hello
Your account has been inactive for more than """+days+""" days.
You did not respond to our email yesterday.
We have therefore closed your account.
Without action on your part, your account will be closed within 7 days.
Cordially
The """+email['fromname']+"""'s administration team.
"""
try:
smtpObj = smtplib.SMTP(email['host'], email['port'])
smtpObj.ehlo()
if email['encryption'] == "SSL":
smtpObj.ssl()
elif email['encryption'] == "STARTTLS":
smtpObj.starttls()
else:
log.warning("No encryption select. It's dangerous")
smtpObj.login(email['username'], email['password'])
smtpObj.sendmail(sender, receivers, message.replace("\xe9", "").encode("ascii", errors="ignore"))
log.info("Successfully sent email")
except smtplib.SMTPException as e:
log.debug(e)
log.error("Error: unable to send email")
def main():
parser = argparse.ArgumentParser(description='Choose between alert, list or drop old accounts')
parser.add_argument("-a", "--alert", action='store_true', help="Alert account by email")
parser.add_argument("-l", "--list", action='store_true', help="Only list old accounts (dry run mode)")
parser.add_argument("-d", "--drop", action='store_true', help="Drop old accounts")
parser.add_argument("--forceyes", action='store_true', help="Accept dropping old accounts")
parser.add_argument("--days", help="Number of days to keep account")
args = parser.parse_args()
if args.list:
@ -125,7 +190,10 @@ def main():
elif args.alert:
alert_accounts(args.days)
elif args.drop:
print("Drop")
if args.forceyes:
drop_accounts
else:
print("Please use --forceyes to confirm dropping accounts")
else:
print("Require an argument. Use --help to display help")