Last active
August 29, 2015 13:57
-
-
Save mike-perdide/9834142 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # License : copyleft | |
| import json | |
| import requests | |
| import string | |
| import os | |
| import sys | |
| CHARACTER_BASE = string.letters + string.digits | |
| API_URL = 'https://api.alwaysdata.com/v1/' | |
| MAILBOXES_URL = API_URL + "mailbox/" | |
| API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx' | |
| ACCOUNT_NAME = 'bla' | |
| AUTH = ('/%s' % ACCOUNT_NAME, API_KEY) | |
| DOMAIN_NAME = "bla.fr" | |
| ALIASES = { | |
| '[email protected]': ( | |
| 'bobby', | |
| 'bonbon', | |
| 'fred', | |
| 'john' | |
| ), | |
| '[email protected]': ( | |
| 'my_team', | |
| 'ze_team', | |
| 'sales', | |
| 'contact' | |
| ), | |
| } | |
| EXISTING_ACCOUNTS = [ | |
| account['name'] | |
| for account in requests.get(MAILBOXES_URL, auth=AUTH).json() | |
| ] | |
| def get_domain_id(): | |
| domain_list = [ | |
| domain | |
| for domain in requests.get(API_URL + "domain/", auth=AUTH).json() | |
| if domain["name"] == DOMAIN_NAME | |
| ] | |
| if len(domain_list) == 0: | |
| print "No domain is configured yet. Cannot continue." | |
| sys.exit(1) | |
| elif len(domain_list) != 1: | |
| print "More than one match to the given DOMAIN_NAME: %s" % DOMAIN_NAME | |
| print " ".join([domain["name"] for domain in domain_list]) | |
| sys.exit(2) | |
| return domain_list[0]["href"].split("/")[3] | |
| DOMAIN_ID = get_domain_id() | |
| REQUIRED_PARAMS = { | |
| "antispam_folder": "Trash", | |
| "antispam_score": 5, | |
| "antispam_score_delete": 10, | |
| "antivirus_folder": "Trash", | |
| "domain": DOMAIN_ID, | |
| "purge_days": 30, | |
| "autoresponder_days": 7, | |
| "purge_folders": "Trash", | |
| } | |
| def gen_password(): | |
| return "".join([ | |
| CHARACTER_BASE[ord(position) % len(CHARACTER_BASE)] | |
| for position in os.urandom(20) | |
| ]) | |
| def add_alias(alias, redirect_to): | |
| print "Adding %s: " % alias, | |
| if alias in EXISTING_ACCOUNTS: | |
| print "skipping, account already created." | |
| return False | |
| this_alias_data = { | |
| 'name': alias, | |
| 'redirect_enabled': True, | |
| 'redirect_to': redirect_to, | |
| 'password': gen_password(), | |
| } | |
| data = dict(REQUIRED_PARAMS.items() + this_alias_data.items()) | |
| response = requests.post(MAILBOXES_URL, auth=AUTH, data=json.dumps(data)) | |
| if response.status_code == 201: | |
| print "OK" | |
| else: | |
| print "NOK, return code is %d" % response.status_code | |
| for redirect_to in ALIASES: | |
| for alias in ALIASES[redirect_to]: | |
| add_alias(alias, redirect_to) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment