#!/usr/bin/python # (c) 2002 Michael Fischer v. Mollard (mfvm AT gmx DOT de) # # A little bit to trivial for a big license, so do what you want with # it, but remember: # This script is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. import getopt, sys, os from MySQLdb import connect # Some global variable # You may want to customize this DATABASE = 'maildb' DBUSER = 'mail' DBPASSWORD = 'secret' def main(): try: opts, args = getopt.getopt(sys.argv[1:], "hd:u:p:U:A:", ["help", "database=", "dbuser=", "dbpassword=", "user=", "aliast="]) except getopt.GetoptError: # print help information and exit: usage() sys.exit(2) database, dbuser, dbpassword, user, alias = None, None, None, None, None for o, a in opts: if o in ("-h", "--help"): usage() sys.exit() elif o in ("-d", "--database"): database = a elif o in ("-u", "--dbuser"): dbuser = a elif o in ("-p", "--dbpassword"): dbpassword = a elif o in ("-U", "--user"): user = a elif o in ("-A", "--alias"): secret = a else: usage() sys.exit() # Use silly default values if not database: database=DATABASE if not dbuser: dbuser=DBUSER if not dbpassword: dbpassword=DBPASSWORD if not user: user=prompt("Existing user (eMail): ") if not alias: alias=prompt("New eMail alias: ") # Search alias # Add user to the database connobj = connect(db=database, host='localhost', user=dbuser, passwd=dbpassword) cursobj = connobj.cursor() cursobj.execute("Select count(*) as num from passwd where email=%s",user) result=cursobj.fetchone() if int(result[0]) != 1: print "Cannot create alias for unknown email address %s"%user sys.exit(3) else: cursobj.execute("INSERT INTO aliases(email,dest) values (%s, %s)",(alias, user)) def prompt(prompt): return raw_input(prompt).strip() def usage(): print ''' maildb_addalias Script for adding users to a mail enviroment like the one described in 'Virtual Domains with Exim + Courier.IMAP + MySQL' (http://www.tty1.net/virtual_domains_en.html) See this document for further details. Parameters -h --help This text -d --database Database name (default: maildb) -u --dbuser Database user (default: mail) -p --dbpassword Database password (default: secret) -U --user Existing email -A --alias new alias for user This script is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ''' if __name__ == "__main__": main()