Adduser works again. :) (and a small wizard bugfix)

This commit is contained in:
Stéphan Kochen 2004-01-29 18:08:38 +00:00
parent e0f1538613
commit 9423cbc97c
2 changed files with 54 additions and 30 deletions

View File

@ -36,36 +36,63 @@ from questions import *
import os import os
import sys import sys
import logging
import optparse import optparse
import conf import registry
conf.minimumLogPriority = logging.CRITICAL
def main(): def main():
parser = optparse.OptionParser(usage='Usage: %prog [options]', from conf import version
version='supybot %s' % conf.version) parser = optparse.OptionParser(usage='Usage: %prog [options] <configFile>',
version='supybot %s' % version)
parser.add_option('-u', '--username', action='store', default='', parser.add_option('-u', '--username', action='store', default='',
dest='name', dest='name',
help='username for the user.') help='username for the user.')
parser.add_option('-p', '--password', action='store', default='', parser.add_option('-p', '--password', action='store', default='',
dest='password', dest='password',
help='password for the user.') help='password for the user.')
parser.add_option('-x', '--hashed', action='store_const', const=1,
default=0, dest='hashed',
help='hash encrypt the password.')
parser.add_option('-n', '--plain', action='store_const', const=2,
default=0, dest='hashed',
help='store the password in plain text.')
parser.add_option('-c', '--capability', action='append', parser.add_option('-c', '--capability', action='append',
dest='capabilities', metavar='CAPABILITY', dest='capabilities', metavar='CAPABILITY',
help='capability the user should have; ' help='capability the user should have; '
'this option may be given multiple times.') 'this option may be given multiple times.')
filename = os.path.join(conf.confDir, conf.userfile)
parser.add_option('-f', '--filename', action='store', default=filename,
dest='filename',
help='filename of your users.conf; '
'defaults to %s' % filename)
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
if len(args) is not 1:
parser.error('specify the configuration file you\'d like to use.')
registry.open(args[0])
import conf
import log
import ircdb
oldPriority = conf.supybot.log.minimumPriority()
conf.supybot.log.minimumPriority.set('CRITICAL')
if not options.name: if not options.name:
name = ''
while not name:
name = something('What is the user\'s name?') name = something('What is the user\'s name?')
try:
# Check to see if the user is already in the database.
_ = ircdb.users.getUser(name)
# Uh oh. That user already exists;
# otherwise we'd have KeyError'ed.
output('That user already exists. Try another name.')
name = ''
except KeyError:
# Good. No such user exists. We'll pass.
pass
else: else:
try:
# Same as above. We exit here instead.
_ = ircdb.users.getUser(options.name)
output('That user already exists. Try another name.')
sys.exit(-1)
except KeyError:
name = options.name name = options.name
if not options.password: if not options.password:
@ -73,35 +100,32 @@ def main():
else: else:
password = options.password password = options.password
if options.hashed is 0:
hashed = yn('Do you want the password to be hashed instead of '
'storing it as plain text?', default=False)
elif options.hashed is 1:
hashed = True
else:
hashed = False
if not options.capabilities: if not options.capabilities:
capabilities = [] capabilities = []
prompt = 'Would you like to give %s a capability?' % name prompt = 'Would you like to give %s a capability?' % name
while yn(prompt) == 'y': while yn(prompt):
capabilities.append(anything('What capability?')) capabilities.append(anything('What capability?'))
prompt = 'Would you like to give %s another capability?' % name prompt = 'Would you like to give %s another capability?' % name
else: else:
capabilities = options.capabilities capabilities = options.capabilities
conf.confDir = os.path.dirname(options.filename)
conf.userfile = os.path.basename(options.filename)
import ircdb
try:
# First, let's check to see if the user is already in the database.
_ = ircdb.users.getUser(name)
# Uh oh. That user already exists; otherwise we'd have KeyError'ed.
sys.stderr.write('That user already exists. Try another name.\n')
sys.exit(-1)
except KeyError:
# Good. No such user exists. We'll pass.
pass
(id, user) = ircdb.users.newUser() (id, user) = ircdb.users.newUser()
user.name = name user.name = name
user.setPassword(password) user.setPassword(password, hashed=hashed)
for capability in capabilities: for capability in capabilities:
user.addCapability(capability) user.addCapability(capability)
ircdb.users.setUser(id, user) ircdb.users.setUser(id, user)
print 'User %s added.' % name print 'User %s added.' % name
conf.supybot.log.minimumPriority.setValue(oldPriority)
registry.close(conf.supybot, args[0])
if __name__ == '__main__': if __name__ == '__main__':
try: try:

View File

@ -130,7 +130,7 @@ def main():
filename = '' filename = ''
if len(args) > 1: if len(args) > 1:
parser.error() parser.error('the wizard takes one argument at most.')
elif len(args) == 1: elif len(args) == 1:
filename = args[0] filename = args[0]
try: try: