From e18a12269f116d829e4935462491139014e26fb2 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Tue, 21 Dec 2004 19:01:59 +0000 Subject: [PATCH] - start for object oriented infrastructure - default preferencs file - ignore development files git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/gnuviech.info/gnuviechadmin/trunk@81 a67ec6bc-e5d5-0310-a910-815c51eb3124 --- backend/GVAdm.py | 37 ++++++++++ backend/gvadm.preferences | 14 ++-- backend/gvadm.preferences.default | 10 +++ backend/mailtools.py | 115 +++++++++++++++++++----------- 4 files changed, 129 insertions(+), 47 deletions(-) create mode 100644 backend/GVAdm.py create mode 100644 backend/gvadm.preferences.default diff --git a/backend/GVAdm.py b/backend/GVAdm.py new file mode 100644 index 0000000..2b7a698 --- /dev/null +++ b/backend/GVAdm.py @@ -0,0 +1,37 @@ +"""Package for GNUViech Admin main types and functions + +(c) Copyright 2004 Jan Dittberner, IT-Consulting & Solutions + Germany +""" + +import os + +execfile('gvadm.preferences') + +class GNVDomain: + """Represents a domain in the GNUViech admin tool""" + + def __init__(self, domain): + """Initializes the domain object""" + self.domainname = domain + self.findUser() + + def __repr__(self): + return "Domain "+self.domainname+", User "+self.username + + def findUser(self): + """Finds the user for the domain or creates a new one.""" + if (os.access(GVADMDIR, os.R_OK)): + try: + domainsfile = open(GVADMDIR+"domains", "r") + self.username = "" + except IOError: + print "domain file not accessible" + self.username = "" + else: + print "The directory "+GVADMDIR+" is not accessible!" + self.username = "" + +if __name__ == "__main__": + dom = GNVDomain("test.de") + print dom diff --git a/backend/gvadm.preferences b/backend/gvadm.preferences index b9986c0..f0043f6 100644 --- a/backend/gvadm.preferences +++ b/backend/gvadm.preferences @@ -1,4 +1,10 @@ -USERPREFIX="usr" -EXIMCONFDIR="etc/exim/" -VIRTUALDOMDIR=EXIMCONFDIR+"virtual/" -POPHOMEDIR="home/mail/" +USERPREFIX = "usr" +BASEPREFIX = "../tmp" +GVADMDIR = BASEPREFIX+"/etc/gvadm/" +EXIMCONFDIR = BASEPREFIX+"/etc/exim/" +VIRTUALDOMDIR = EXIMCONFDIR+"virtual/" +HOMEDIR = BASEPREFIX+"/home" +POPHOMEDIR = HOMEDIR+"/mail/" +WEBHOMEDIR = HOMEDIR+"/www/" +WEBLOGDIR = WEBHOMEDIR+"logs/" +WEBSTATSDIR = WEBHOMEDIR+"stats/" diff --git a/backend/gvadm.preferences.default b/backend/gvadm.preferences.default new file mode 100644 index 0000000..24c4f50 --- /dev/null +++ b/backend/gvadm.preferences.default @@ -0,0 +1,10 @@ +USERPREFIX = "usr" +BASEPREFIX = "" +GVADMDIR = BASEPREFIX+"/etc/gvadm/" +EXIMCONFDIR = BASEPREFIX+"/etc/exim/" +VIRTUALDOMDIR = EXIMCONFDIR+"virtual/" +HOMEDIR = BASEPREFIX+"/home" +POPHOMEDIR = HOMEDIR+"/mail/" +WEBHOMEDIR = HOMEDIR+"/www/" +WEBLOGDIR = WEBHOMEDIR+"logs/" +WEBSTATSDIR = WEBHOMEDIR+"stats/" diff --git a/backend/mailtools.py b/backend/mailtools.py index a310d52..e04882c 100755 --- a/backend/mailtools.py +++ b/backend/mailtools.py @@ -1,6 +1,8 @@ #! /usr/bin/env python import os, string +import GVAdm +from UserDict import UserDict execfile('gvadm.preferences') @@ -27,50 +29,77 @@ execfile('gvadm.preferences') # echo "Herzlich willkommen auf dem GNU-Viech" | mail -s "Willkommen auf dem GNU-Viech" ${NEWUSER} # echo added new pop3 user $NEWUSER with password $NEWPASS -def createpopaccount(domainname, sysuser): - """Creates a pop3/imap account for given domain and system user""" - "addpopuser sysusrp" +class MailAccount(UserDict): + def __init__(self, domain): + "Initialize a MailAccount instance for a given domain" + UserDict.__init__(self) + self["domainname"] = domain -def readaliasfile(domainname): - """reads the aliasfile for the given domain and returns a dictionary - object with the aliases - """ - aliases={} - if (os.access(VIRTUALDOMDIR, os.R_OK)): - try: - aliasfile=open(VIRTUALDOMDIR+domainname, 'r') - for line in aliasfile.readlines(): - keyvals = string.split(line,":",1) - aliases[keyvals[0]]=keyvals[1].strip() - aliasfile.close() - except IOError: - print "couldn't read the aliasfile for "+domainname+"." - else: - print "couldn't read from "+VIRTUALDOMDIR+"." - return aliases +class MailAlias(MailAccount): + """This represents a mail alias""" + + def __init__(self, domain): + "Initialize the POPAccount class for a given domain" + MailAccount.__init__(self, domain) + self["aliases"] = {} + self.readAll() + + def readAll(self): + """reads the aliasfile for the given domain""" + self["aliases"] = {} + if (os.access(VIRTUALDOMDIR, os.R_OK)): + try: + aliasfile = open(VIRTUALDOMDIR+self["domainname"], 'r') + for line in aliasfile.readlines(): + keyvals = string.split(line,":",1) + self["aliases"][keyvals[0]] = keyvals[1].strip() + aliasfile.close() + except IOError: + print "couldn't read the aliasfile for "+self["domainname"]+"." + else: + print "couldn't read from "+VIRTUALDOMDIR+"." -def writealiasfile(domainname, aliases): - """writes the aliasfile for the given domain with the aliases defined - in the dictionary object aliases - """ - if (os.access(VIRTUALDOMDIR, os.W_OK)): - try: - aliasfile=open(VIRTUALDOMDIR+domainname, 'w') - for key in aliases.keys(): - aliasfile.write(key+":"+aliases[key]+"\n") - aliasfile.close() - except IOError: - print "writing to aliasfile failed." - else: - print "no write access to directory "+VIRTUALDOMDIR+"." + def writeAll(self): + """writes the aliasfile for the given domain with the aliases defined +in the dictionary object aliases""" + if (os.access(VIRTUALDOMDIR, os.W_OK)): + try: + aliasfile = open(VIRTUALDOMDIR+self["domainname"], 'w') + keys = self["aliases"].keys(); + keys.sort(); + for key in keys: + aliasfile.write(key+":"+self["aliases"][key]+"\n") + aliasfile.close() + except IOError: + print "writing to aliasfile failed." + else: + print "no write access to directory "+VIRTUALDOMDIR+"." -def setmailalias(domainname, alias, target): - """sets a mail alias for given domain which directs the MTA to the - given target - """ - aliases=readaliasfile(domainname) - aliases[alias]=target - writealiasfile(domainname, aliases) + def setAlias(self, alias, target): + """sets a mail alias for given domain which directs the MTA to the +given target +""" + self.readAll() + self["aliases"][alias]=target + self.writeAll() -createpopaccount("test.de", "usr03") -setmailalias("test.de", "doedel", "horst@dittberner.info") +class POP3Account(MailAccount): + """This represents a pop 3 account""" + + def create(self, address): + """Creates a pop3/imap account for the domain""" + print self + print "adding address "+address + alias = MailAlias(self["domainname"]) + alias.setAlias(address, "usr03") + print alias + +if __name__ == "__main__": + popacc = POP3Account("test.de") + print popacc + popacc.create("webmaster") + + alias = MailAlias("test.de") + print alias + alias.setAlias("klaus", "klaus@dittberner.info") + print alias