#!/usr/bin/env python # encoding: utf-8 """ EmailDomainAdmin.py Controls creating, deleting and pulling stats from virtual email hosts. Created by Joe Topjian on 2006-07-31. """ import sys import os import os.path import shutil import subprocess import datetime class EmailDomainAdmin (object): def __init__(self, domain): self.domain = domain def _backup(self, f): """ Creates a backup copy of a given file in the same directory with the date and time appended to it. Returns True if successful """ time = datetime.datetime.now() date = "%s-%s" % (time.date(), str(time.time()).replace(':','').split('.')[0]) (directory, filename) = os.path.split(f) try: shutil.copyfile(f, "/etc/postfix/virtual/backup/%s.%s" % (filename, date)) return True except Exception, e: print "Could not copy file: %s" % e def exists(self): """ Checks for the existance of the virtual maps file. Returns True if the file exists. """ if os.path.isfile("/etc/postfix/virtual/domains/%s" % self.domain): return True else: return False def create(self, owner): """ Prepares the Postfix structure to support a new domain. The owner is a shell account. Returns True if successful """ if not self.exists(): try: # Just create an empty file f = open("/etc/postfix/virtual/domains/%s" % self.domain, 'w') f.close() # Create an empty directory for the mailboxes os.mkdir("/var/spool/vmail/%s" % self.domain) """ Add the domain to the vdomains file. Postfix only uses the left column, so using the right column would be the best place to put the owner of the domain """ self._backup('/etc/postfix/virtual/vdomains.txt') f = open('/etc/postfix/virtual/vdomains.txt','a') f.write("%s\t%s\n" % (self.domain, owner)) f.close() # Reload Postfix postfixReload = "/usr/sbin/postfix reload" output = subprocess.Popen(postfixReload.split(), stderr=subprocess.PIPE).communicate()[1] if output: raise "Could not reload Postfix: %s" % output except Exception, e: print "Could not add domain: %s" % e else: raise "Address already exists!" def delete(self): """ Deletes the domain. Will not delete unless all email addresses are deleted. This ensures the mailboxes get a proper backup. Returns True if successful """ if self.exists(): try: if len(os.listdir("/var/spool/vmail/%s" % self.domain)) == 0: os.rmdir("/var/spool/vmail/%s" % self.domain) os.remove("/etc/postfix/virtual/domains/%s" % self.domain) domains = [] f = open('/etc/postfix/virtual/vdomains.txt') for line in f: (domain, owner) = line.split() if domain != self.domain: domains.append(line) f.close() self._backup('/etc/postfix/virtual/vdomains.txt') f = open('/etc/postfix/virtual/vdomains','w') f.write("".append(domains)) f.close() else: raise "All addresses must be deleted first! See listAddresses()." except Exception, e: print "Could not remove domain: %s" % e else: raise "Domain does not exist!" def listAddresses(self): """ Lists all the addresses associated with the given domain. Returns list """ if self.exists(): try: addresses = [] for address in os.listdir("/var/spool/vmail/%s" % self.domain): addresses.append("%s@%s" % (address, self.domain)) return addresses except Exception, e: print "Could not list addresses: %s" % e else: raise "Domain does not exist!"