From 0258bb66f6247b3742766d7fa2107c97d2572015 Mon Sep 17 00:00:00 2001 From: Tristan Le Chanony Date: Fri, 3 Jan 2020 13:45:14 +0100 Subject: [PATCH] Add dropping accounts --- drop-account.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 4 deletions(-) diff --git a/drop-account.py b/drop-account.py index 5066ffe..3e3c88d 100755 --- a/drop-account.py +++ b/drop-account.py @@ -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")