# -*- coding: utf-8 -*-

import re

domain_re = re.compile(r'^(?!-)[a-z0-9-]{1,63}(?<!-)(?:\.(?!-)[a-z0-9-]{1,63}(?<!-)){0,125}\.(?!-)(?![0-9]+$)[a-z0-9-]{1,63}(?<!-)$')
box_re = re.compile(r'^[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*$')

class InvalidValueException(Exception):
    pass

def is_valid_domain(domain):
    return bool(domain_re.match(domain))

def is_valid_port(port):
    try:
        port = int(port)
        return port > 0 and port < 65536
    except:
        return False

def is_valid_app(app, conf):
    return app in conf['apps']

def is_valid_email(email):
    parts = email.split('@')
    if len(parts) != 2:
        return False
    return bool(box_re.match(parts[0])) and bool(domain_re.match(parts[1]))