def prepare_list():
    # pre-check before handling mailman core service
    if DEFAULT_DOMAIN_NAME == "":
        print("Must specify 'DEFAULT_DOMAIN_NAME' for mail list preparation.")
        exit(1)

    lists = str.split(str(DEFAULT_MAIL_LISTS).lower(), ",")
    if not os.path.exists(TEMPLATE_FOLDER_PATH):
        print("The template file folder 'TEMPLATE_FOLDER_PATH' must exits on"
              " local.")
        exit(1)

    if len(lists) == 0:
        # find out all of the lists from local folder.
        local_file = []
        for _, _, f in os.walk(os.path.join(os.getcwd(),
                                            TEMPLATE_FOLDER_PATH)):
            for file in f:
                if file.endswith(".txt") and not file.endswith("base.txt"):
                    local_file.append(os.path.splitext(file)[0])
        lists = list(set(local_file))

    client = Client(MAILMAN_CORE_ENDPOINT, MAILMAN_CORE_USER,
                    MAILMAN_CORE_PASSWORD)

    try:
        # Create default domain if not exists
        default_domain = client.get_domain(DEFAULT_DOMAIN_NAME)
    except HTTPError as err:
        if err.code == 404:
            default_domain = client.create_domain(DEFAULT_DOMAIN_NAME)
        else:
            print("unable to find domain {0}".format(err))
            exit(1)
    # Create default mail lists
    existing_lists = [el.list_name for el in client.lists]
    for l in lists:
        if l in existing_lists:
            print("skip creating list {0}, since it's already exist".format(l))
            continue
        else:
            print("starting to create mail list {0}".format(l))
            default_domain.create_list(l)

    # Patch template for lists
    for l in lists:
        # browse all of the dirs and find out the template files
        existing_folders = [
            f for f in os.listdir(
                os.path.join(os.getcwd(), TEMPLATE_FOLDER_PATH))
        ]
        for d in existing_folders:
            if not os.path.isdir(
                    os.path.join(os.getcwd(), TEMPLATE_FOLDER_PATH, d)):
                continue
            # check the list file exists
            local_file = get_template_file(d, l)
            if os.path.exists(local_file):
                patch_content = {
                    convert_name_to_substitution(d): get_templates_url(d, l)
                }
            elif os.path.exists(get_base_template_file(d)):
                patch_content = {
                    convert_name_to_substitution(d):
                    get_templates_url(d, "base")
                }
            else:
                continue
            patch_uri = "{0}/lists/{1}.{2}/uris".format(
                MAILMAN_CORE_ENDPOINT, l, DEFAULT_DOMAIN_NAME)
            response = requests.patch(patch_uri,
                                      patch_content,
                                      auth=(MAILMAN_CORE_USER,
                                            MAILMAN_CORE_PASSWORD))
            print("patching list {0} with template file {1}, result {2} {3}"
                  "".format(l, local_file, response.status_code,
                            response.text))
예제 #2
0
            action = row[1].strip()
            if action == 'add':
                members = to_add
            else:
                members = to_del
            mail = row[2].strip()
            if '@' in mail:
                members.add(mail)

client = Client('http://localhost:8001/3.1', api_user, api_pass)
try:
    dom = client.get_domain(domain)
except (HTTPError, ) as exc:
    if exc.code != 404:
        raise
    dom = client.create_domain(domain)


def accept_request(ml, member):
    done = None
    for request in ml.requests:
        if request['email'] != member:
            continue
        if member not in [m.email for m in ml.members]:
            ml.accept_request(request['token'])
        else:
            ml.discard_request(request['token'])
        done = True
    return done