#! /usr/bin/env python

# generate email addressess, fake guaranteed
# requires the dig unix tool
#
# see http://mij.oltrelinux.com/net/hellospamcrawler.html
# this sw is public domain


# fake guarantee? or False
checkexistence=True

# domain length range
mindomainlen=4
maxdomainlen=12

# user length range
minuserlen=2
maxuserlen=10

#seconds
dnstimeout=5

# from http://en.wikipedia.org/wiki/List_of_Internet_TLDs
# with re.findall() with '<a href="/wiki/\.\w+" title="\.\w+">(\.\w+)</a>'
tlds=['.arpa', '.root', '.aero', '.biz', '.cat', '.com', '.coop', '.edu',
'.gov', '.info', '.int', '.jobs', '.mil', '.mobi', '.museum', '.name', '.net',
'.org', '.pro', '.travel', '.ac', '.ad', '.ae', '.af', '.ag', '.ai', '.al',
'.am', '.an', '.ao', '.aq', '.ar', '.as', '.at', '.au', '.aw', '.az', '.ba',
'.bb', '.bd', '.be', '.bf', '.bg', '.bh', '.bi', '.bj', '.bm', '.bn', '.bo',
'.br', '.bs', '.bt', '.bv', '.bw', '.by', '.bz', '.ca', '.cc', '.cd', '.cf',
'.cg', '.ch', '.ci', '.ck', '.cl', '.cm', '.cn', '.co', '.cr', '.cu', '.cv',
'.cx', '.cy', '.cz', '.de', '.dj', '.dk', '.dm', '.do', '.dz', '.ec', '.ee',
'.eg', '.er', '.es', '.et', '.eu', '.fi', '.fj', '.fk', '.fm', '.fo', '.fr',
'.ga', '.gb', '.uk', '.gd', '.ge', '.gf', '.gg', '.gh', '.gi', '.gl', '.gm',
'.gn', '.gp', '.gq', '.gr', '.gs', '.gt', '.gu', '.gw', '.gy', '.hk', '.hm',
'.hn', '.hr', '.ht', '.hu', '.id', '.ie', '.il', '.im', '.in', '.io', '.iq',
'.ir', '.is', '.it', '.je', '.jm', '.jo', '.jp', '.ke', '.kg', '.kh', '.ki',
'.km', '.kn', '.kr', '.kw', '.ky', '.kz', '.la', '.lb', '.lc', '.li', '.lk',
'.lr', '.ls', '.lt', '.lu', '.lv', '.ly', '.ma', '.mc', '.md', '.mg', '.mh',
'.mk', '.ml', '.mm', '.mn', '.mo', '.mp', '.mq', '.mr', '.ms', '.mt', '.mu',
'.mv', '.mw', '.mx', '.my', '.mz', '.na', '.nc', '.ne', '.nf', '.ng', '.ni',
'.nl', '.no', '.np', '.nr', '.nu', '.nz', '.om', '.pa', '.pe', '.pf', '.pg',
'.ph', '.pk', '.pl', '.pm', '.pn', '.pr', '.ps', '.pt', '.pw', '.py', '.qa',
'.re', '.ro', '.ru', '.rw', '.sa', '.sb', '.sc', '.sd', '.se', '.sg', '.sh',
'.si', '.sj', '.sk', '.sl', '.sm', '.sn', '.sr', '.st', '.su', '.sv', '.sy',
'.sz', '.tc', '.td', '.tf', '.tg', '.th', '.tj', '.tl', '.tm', '.tn', '.to',
'.tp', '.tr', '.tt', '.tv', '.tw', '.tz', '.ua', '.ug', '.uk', '.um', '.us',
'.uy', '.uz', '.va', '.vc', '.ve', '.vg', '.vi', '.vn', '.vu', '.wf', '.ws',
'.ye', '.yt', '.yu', '.cs', '.za', '.zm', '.zw']


import random
import sys
import os


def randomname(len=8):
    vows = ['a','e','i','o','u']
    cons = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']
   
    name=''
    for pos in range(len):			
        if pos % 2:
            name = name + random.choice(vows)
        else:
            name = name + random.choice(cons)

    return name
   

def domainexists(domain):
    digverb = os.popen("dig ns " + domain + " +time=" + str(dnstimeout)).read()
    if "ANSWER SECTION" in digverb:
        return True
    return False


if __name__ == "__main__":
    if len(sys.argv) != 2:
        print "Tell me how many you want me to generate."
        sys.exit(1)
    if not sys.argv[1].isdigit() or int(sys.argv[1]) == 0:
        print "Tell me how many you want me to generate, a valid positive integer."
        sys.exit(2)
    howmany=int(sys.argv[1])
    if checkexistence:
        validentries = 0
        while validentries < howmany:
            domainlen = random.randint(mindomainlen,maxdomainlen)
            domain = randomname(domainlen) + random.choice(tlds)
            if domainexists(domain):
                #print "existing", domain
                continue;
            userlen = random.randint(minuserlen,maxuserlen)
            user = randomname(userlen)
            print user + "@" + domain
            validentries = validentries+1
    else:
        for entries in range(howmany):
            domainlen = random.randint(mindomainlen,maxdomainlen)
            domain = randomname(domainlen) + random.choice(tlds)
            userlen = random.randint(minuserlen,maxuserlen)
            user = randomname(userlen)
            print user + "@" + domain
