
# autentificate from 4SS repository
from Ft.Server.Server.SCore import GetRepository
from Ft.Server.Server import FtServerServerException, Error

#userContainer='/localhost/users'
import os, sha

def got_user(username, session, sessions):
    group = None
    #repo=GetRepository()
    #userCont=repo.fetchContainer(userContainer)
    #if username in map(lambda x: os.path.basename(x.uri),userCont[:]):
    #    repo.txRollback()
    return 331, "Password required for user %s"%username, username, group, 0, 1
    #repo.txRollback()
    #return 331, "User %s doesnt exist"%username, "", "", -1, 1

    # return "message", username, groupname, X, Y
    # X == 0: deny access
    # X == 1: grant access
    # X == -1: does not concern this module
    # Y == 1: continue with other modules
    # Y == 0: definitive answer

def got_pass(username, password, session, sessions):
    if not username:
        #return 503, "Login with USER first.", 0, 0
        return 530, "Username or password invalid", 0, 0

    try:
        repo = GetRepository(username,
                             sha.new(password).hexdigest(),
                             session.logFile,
                             session.properties)        
        repo.txRollback()                             
        return 230, "You may proceed, "+username, 1, 1
    except FtServerServerException, e:
        if e.errorCode == Error.INVALID_LOGIN:
            return 530, "Username or password invalid", 0, 0
        raise
    
        

