'1st Stage Parser
- called by msg.parse()
Sets the appropriate Authorisation level and then calls the
parser function from the template'

@staticmethod
def parser(function_name, message_id, **kwargs):

    reply = None
    s3db = current.s3db
    table = s3db.msg_message
    message = current.db((table.message_id == message_id)).select(limitby=(0, 1)).first()
    from_address = message.from_address
    if ('<' in from_address):
        from_address = from_address.split('<')[1].split('>')[0]
    email = S3Parsing.is_session_alive(from_address)
    if email:
        current.auth.s3_impersonate(email)
    else:
        (email, password) = S3Parsing.parse_login(message)
        if (email and password):
            current.auth.login_bare(email, password)
            expiration = current.session.auth['expiration']
            table = s3db.msg_session
            table.insert(email=email, expiration_time=expiration, from_address=from_address)
            reply = 'Login    succesful'
    template = current.deployment_settings.get_msg_parser()
    module_name = ('applications.%s.modules.templates.%s.parser' % (current.request.application, template))
    __import__(module_name)
    mymodule = sys.modules[module_name]
    S3Parser = mymodule.S3Parser()
    try:
        fn = getattr(S3Parser, function_name)
    except:
        current.log.error(('Parser    not    found:    %s' % function_name))
        return None
    reply = (fn(message, **kwargs) or reply)
    if (not reply):
        return None
    current.msg.send(from_address, reply)
