From b0cedd492fceabc46f40bdaaf3dc14885102b9c4 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Wed, 4 Dec 2024 15:28:20 -0500 Subject: [PATCH] Initial implementation of bin/nntp-tiny-adduser --- bin/nntp-tiny-adduser | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100755 bin/nntp-tiny-adduser diff --git a/bin/nntp-tiny-adduser b/bin/nntp-tiny-adduser new file mode 100755 index 0000000..25b831f --- /dev/null +++ b/bin/nntp-tiny-adduser @@ -0,0 +1,40 @@ +#! /usr/bin/env python3 + +import sys +import os +import argparse + +from nntp.tiny.config import Config +from nntp.tiny.db import Database +from nntp.tiny.user import User + +parser = argparse.ArgumentParser(description='Create new account') +parser.add_argument('--config-file', '-f', type=str, help='Specify a configuration file location') +parser.add_argument('username', type=str, help='Username of account') + +args = parser.parse_args() + +config = Config.load(args.config_file) +db = Database.connect(config.get('database', 'path')) + +user = db.get(User, {'username': args.username}) + +if user is not None: + print(f"{sys.argv[0]}: user '{args.username}' already exists", file=sys.stderr) + exit(1) + +user = User() +user.active = True +user.username = args.username +user.password = '*' +user.fullname = input('Full name: ') +user.mail = input('Email address: ') + +try: + db.add(user) + db.commit() +except Exception as e: + print(f"Failed to create user '{args.username}': {e}", file=sys.stderr) + exit(1) + +print(f"Created user '{args.username}' successfully.")