Exemple #1
0
def admin():
    """Show admin interface to add/remove/edit users"""
    # TODO: mmh, what if I choose an login equal to the admin user-login-name
    if current_user.id != config["admin"]["login"]:
        return "No access!"

    # check for config dir, create if necassary
    cfgDir = config["admin"]["config_directory"]
    if not os.path.exists(cfgDir):
        os.mkdir(cfgDir)

    error = []
    # add user config
    if "add" in request.args:
        login = request.form.get("login")
        password = request.form.get("password")
        if login and password:
            phash = User.get_password_hash(password)

            fn = "{}/{}.cfg".format(cfgDir,login)
            if phash is None:
                error.append("Could not generate password hash")
            elif os.path.exists(fn):
                error.append("user already exists")
            elif login.find(" ") != -1:
                error.append("no whitespaces allowed in username")
            elif len(config["admin"].users) > config["admin"]["paranoid.max_users"]:
                error.append("server is configured to not allow any more users!")
            else:
                timeout = 10
                chatlines = 30
                f = open(fn, "w")
                f.write("[config]\n")
                f.write("password = {}\n".format(phash))
                f.write("timeout = {}\n").format(timeout) # timeout?
                f.write("chatlines = {}\n").format(chatlines) # lines in chat window...
                f.write("\n")
                f.close()

                values = {}
                values["password"] = phash
                values["timeout"] = str(timeout)
                values["chatlines"] = str(chatlines)
                values["file"] = os.path.join(cfgDir, "{}.cfg".format(login))
                values["login"] = login
                cfg = UserConfig(values, True)
                cfg.setLogPath(config["admin"]["log_directory"])
                config[login] = cfg
                config["admin"].users.append(login)

    # delete user config
    if "del" in request.args:
        if "login" in request.args:
            login = request.args["login"]
            if login:
                fn = "{}/{}.cfg".format(cfgDir, login)
                if login and os.path.exists(fn):
                    os.unlink(fn)

    # every file inside the config-dir is an user-config-file ? mmmh, 
    cfgs = [ f[:-4] for f in os.listdir(cfgDir) \
            if os.path.isfile(os.path.join(cfgDir, f)) ]
    return render_template("admin.html", users=cfgs, error=error)
Exemple #2
0
    # setup logs
    logPath = config["admin"]["log_directory"]
    if not os.path.exists(logPath):
        os.mkdir(logPath)
        print "create log path {}".format(logPath)

    # find & setup user config dir
    cfgDir = config["admin"]["config_directory"]
    if not os.path.exists(cfgDir):
        os.mkdir(cfgDir)

    # load user config is there are any
    cfgs = [ f for f in os.listdir(cfgDir) \
            if os.path.isfile(os.path.join(cfgDir,f)) ]
    for cf in cfgs:
        conf = UserConfig(os.path.join(cfgDir, cf))
        conf.setLogPath(logPath)
        config[conf.login] = conf
        config["admin"].users.append(conf.login)

    # bring up each user's bouncer
    for user in config["admin"].users:
        print "load server settings for user {}".format(user)
        for srv in config[user].servers:
            print "start server {}".format(srv)
            config[user].srv[srv]["bouncer"] = PyIrcBouncer(config[user], srv)
            # start bouncer thread
            start_new_thread(config[user].srv[srv]["bouncer"].start, ())
            #cnt = 0
            #while not config[user].srv[srv]["bouncer"].connected:
            #    print "wait until server is ready - try: {} (user: {}, srv: {})". \