1
0
Fork 0
gnuviechadmin-historic/backend/mailtools.py

136 lines
4.9 KiB
Python
Raw Normal View History

#! /usr/bin/env python
import os, string
import gnuviech
#from GNVAdm import GNVDomain
from gnuviech import GNVPrefs, tools
from gnuviech.gnvdomain import GNVDomain
# if [ -n $USERPREFIX ]; then
# USERPREFIX="usr"
# fi
# if [ $1 == "" ]; then
# echo "give <UserNumber>p<pop3Account> as parameter"
# exit
# fi
# NEWUSER="$USERPREFIX$1"
# NEWHOME="/home/mail/$NEWUSER"
# NEWPASS=$(apg -n 1 -a 1 -CL -m 8 -x 12)
# echo $NEWHOME
# adduser --home "$NEWHOME" --shell /bin/true --no-create-home --firstuid 20000 --ingroup poponly --disabled-password --disabled-login --gecos "Popuser $NEWUSER" $NEWUSER
# mkdir -p "$NEWHOME"
# chown -Rc $NEWUSER.poponly "$NEWHOME"
# echo "${NEWUSER}:${NEWPASS}" | chpasswd
# echo "Herzlich willkommen auf dem GNU-Viech" | mail -s "Willkommen auf dem GNU-Viech" ${NEWUSER}
# echo added new pop3 user $NEWUSER with password $NEWPASS
class MailAliasExists(Exception): pass
class POP3AccountExists(Exception): pass
class MailAccount:
def __init__(self, domain, localpart):
"Initialize a MailAccount instance for a given domain"
if (not os.access(gnuviech.GNVPrefs.VIRTUALDOMDIR, os.R_OK & os.X_OK)):
self.setupDirs()
self.domain = domain
self.localpart = localpart
self.prefs = domain.prefs
self.logger = domain.prefs.getLogger(self)
def __repr__(self):
return "%s@%s" % (self.localpart, self.domain.name)
class MailAlias(MailAccount):
"""This represents a mail alias"""
def __init__(self, domain, localpart, target):
"Initialize the POPAccount class for a given domain"
if localpart in domain.mailaliases.keys():
raise MailAliasExists
MailAccount.__init__(self, domain, localpart)
self.setTarget(target)
def setTarget(self, target):
self.target = target
self.logger.debug("setting target for alias %s to %s." %
(str(self), self.target))
# self.aliases = {}
# self.readAll()
# def readAll():
# """reads the aliasfile for the given domain"""
# self.aliases = {}
# if (os.access(gnuviech.GNVPrefs.VIRTUALDOMDIR, os.R_OK)):
# try:
# aliasfile = open(gnuviech.GNVPrefs.VIRTUALDOMDIR+self.domain.name , 'r')
# for line in aliasfile.readlines():
# keyvals = string.split(line,":",1)
# self.aliases[keyvals[0]] = keyvals[1].strip()
# aliasfile.close()
# except IOError:
# self.logger.error("couldn't read the aliasfile for "+self.domain.name+".")
# else:
# self.logger.error("couldn't read from "+gnuviech.GNVPrefs.VIRTUALDOMDIR+".")
# def writeAll(self):
# """writes the aliasfile for the given domain with the aliases defined
# in the dictionary object aliases"""
# if (os.access(gnuviech.GNVPrefs.VIRTUALDOMDIR, os.W_OK)):
# try:
# aliasfile = open(gnuviech.GNVPrefs.VIRTUALDOMDIR+self.domain.name, 'w')
# keys = self.aliases.keys();
# keys.sort();
# for key in keys:
# aliasfile.write("%s:%s" % (key, self.aliases[key]) + "\n")
# aliasfile.close()
# except IOError:
# self.logger.error("writing to aliasfile failed.")
# else:
# self.logger.error("no write access to directory "+gnuviech.GNVPrefs.VIRTUALDOMDIR+".")
# 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()
class POP3Account(MailAccount):
"""This represents a pop 3 account"""
def __init__(self, domain, localpart):
"""Creates a new pop3 mail account"""
if localpart in domain.pop3accounts.keys():
raise POP3AccountExists
MailAccount.__init__(self, domain, localpart)
self.logger.debug("adding address %s@%s." % (self.localpart,
self.domain.name))
self.setPassword(tools.generatePassword())
self.setSysUser(domain.getNextUser("pop3"))
self.domain.addMailAlias(MailAlias(self.domain,
self.localpart, self.sysuser))
def setPassword(self, newpassword):
self.password = newpassword
self.logger.debug("set password for %s to %s." %
(str(self), self.password))
def setSysUser(self, username):
self.sysuser = username
self.logger.debug("set system user for %s to %s" %
(str(self), self.sysuser))
if __name__ == "__main__":
prefs = GNVPrefs()
domain = GNVDomain("dittberner.info", prefs)
domain.addPOP3Account(POP3Account(domain, "test"))
domain.addMailAlias(MailAlias(domain, "klaus", "klaus@test.de"))