Add list and alter function
This commit is contained in:
parent
ce5b17a99a
commit
80d1ba68e9
|
@ -1,4 +1,9 @@
|
|||
name_bot: bot
|
||||
secrets_filepath: secrets/secrets.txt
|
||||
log_filepath: activity.log
|
||||
|
||||
email_host:
|
||||
email_port:
|
||||
email_from:
|
||||
email_fromname:
|
||||
email_username:
|
||||
email_password:
|
||||
|
|
|
@ -7,11 +7,118 @@ from mastodon import StreamListener
|
|||
from logging.handlers import RotatingFileHandler
|
||||
from pprint import pprint
|
||||
from utils.config import get_parameter, init_log, init_mastodon
|
||||
from datetime import datetime, timezone, timedelta
|
||||
|
||||
import requests, os, random, sys, time, json, logging, argparse, re, shutil
|
||||
import requests, os, sys, time, json, logging, argparse, re, smtplib, array
|
||||
config_file = "config.txt"
|
||||
secrets_filepath = get_parameter("secrets_filepath", config_file)
|
||||
log_filepath = get_parameter("log_filepath", config_file)
|
||||
log = init_log(log_filepath)
|
||||
mastodon = init_mastodon(config_file, secrets_filepath)
|
||||
|
||||
email_host = get_parameter('email_host', config_file)
|
||||
email_port = get_parameter('email_port', config_file)
|
||||
email_from = get_parameter('email_from', config_file)
|
||||
email_fromname = get_parameter('email_fromname', config_file)
|
||||
email_username = get_parameter('email_username', config_file)
|
||||
email_password = get_parameter('email_password', config_file)
|
||||
|
||||
pprint(email_fromname)
|
||||
|
||||
class bcolors:
|
||||
HEADER = '\033[95m'
|
||||
OKBLUE = '\033[94m'
|
||||
OKGREEN = '\033[92m'
|
||||
WARNING = '\033[93m'
|
||||
FAIL = '\033[91m'
|
||||
ENDC = '\033[0m'
|
||||
BOLD = '\033[1m'
|
||||
UNDERLINE = '\033[4m'
|
||||
|
||||
def list_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']
|
||||
email = 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)+" will be delete")
|
||||
|
||||
def alert_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)+" will be delete")
|
||||
sender = email_from
|
||||
receivers = [''+mail+'']
|
||||
message = """From: """+email_fromname+++""" <"""+email_from+""">
|
||||
To: """+username+""" <"""+mail+""">
|
||||
Subject: Your account has been inactive for more than 365 days
|
||||
|
||||
----
|
||||
English message bellow.
|
||||
----
|
||||
|
||||
Bonjour
|
||||
Votre compte est inactif depuis plus de 365 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.
|
||||
|
||||
Sans action de votre part, votre compte sera clôturé sous 7 jours.
|
||||
|
||||
Cordialement
|
||||
L'équipe d'administration de """+email_fromname+""".
|
||||
|
||||
--------
|
||||
Hello
|
||||
Your account has been inactive for more than 365 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.
|
||||
|
||||
Without action on your part, your account will be closed within 7 days.
|
||||
|
||||
Cordially
|
||||
The """+ email_fromname +""" administration team.
|
||||
"""
|
||||
|
||||
try:
|
||||
smtpObj = smtplib.login(email_username, email_password)
|
||||
smtpObj = smtplib.SMTP('127.0.0.1', '1025')
|
||||
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("--days", help="Number of days to keep account")
|
||||
args = parser.parse_args()
|
||||
if args.list:
|
||||
list_accounts(args.days)
|
||||
elif args.alert:
|
||||
alert_accounts(args.days)
|
||||
elif args.drop:
|
||||
print("Drop")
|
||||
else:
|
||||
print("Require an argument. Use --help to display help")
|
||||
|
||||
main()
|
||||
|
|
Loading…
Reference in New Issue