* 'VirtualMailManager/VirtualMailManager.py'
authorPascal Volk <neverseen@users.sourceforge.net>
Sat, 16 Aug 2008 02:19:32 +0000
changeset 45 9e66138aad0b
parent 44 c9ab6900ede9
child 46 7ece710c559d
* 'VirtualMailManager/VirtualMailManager.py' - Implemented: + VirtualMailManager.domain_alias_add() + VirtualMailManager.domain_alias_delete() * 'VirtualMailManager/Domain.py' - Implemented: + Domain._aliasExists() + Domain.saveAlias() + deleteAlias() - Fixed Domain._exists(); returns only True when the domain exists AND it's the primary domain - Fixed table order in Domain.delete() * 'vmm' - _printList() added ace2idna support for alias domains - Implemented: + domain_alias_add() + domain_alias_delete()
VirtualMailManager/Domain.py
VirtualMailManager/VirtualMailManager.py
VirtualMailManager/constants/ERROR.py
vmm
--- a/VirtualMailManager/Domain.py	Fri Aug 15 21:00:26 2008 +0000
+++ b/VirtualMailManager/Domain.py	Sat Aug 16 02:19:32 2008 +0000
@@ -54,9 +54,9 @@
         will be returned.
         """
         dbc = self._dbh.cursor()
-        # XXX check for primary
         dbc.execute("SELECT gid, tid, domaindir FROM domain_data WHERE gid =\
- (SELECT gid FROM domain_name WHERE domainname = %s)", self._name)
+ (SELECT gid FROM domain_name WHERE domainname = %s AND is_primary)",
+            self._name)
         result = dbc.fetchone()
         dbc.close()
         if result is not None:
@@ -66,6 +66,21 @@
         else:
             return False
 
+    def _aliasExists(self, aliasname):
+        dbc = self._dbh.cursor()
+        dbc.execute("SELECT gid, is_primary FROM domain_name\
+ WHERE domainname = %s", aliasname)
+        result = dbc.fetchone()
+        dbc.close()
+        if result is None:
+            return False
+        elif result[1]:
+            raise VMMDomainException((_('Domain already exists.'),
+                ERR.DOMAIN_EXISTS))
+        else:
+            raise VMMDomainException((_('Domain alias already exists.'),
+                ERR.DOMAIN_ALIAS_EXISTS))
+
     def _setID(self):
         """Sets the ID of the domain."""
         dbc = self._dbh.cursor()
@@ -151,11 +166,8 @@
         if self._id > 0:
             self._chkDelete(delUser, delAlias)
             dbc = self._dbh.cursor()
-            dbc.execute('DELETE FROM alias WHERE gid=%s', self._id)
-            dbc.execute('DELETE FROM users WHERE gid=%s', self._id)
-            dbc.execute('DELETE FROM relocated WHERE gid=%s', self._id)
-            dbc.execute('DELETE FROM domain_name WHERE gid=%s', self._id)
-            dbc.execute('DELETE FROM domain_data WHERE gid=%s', self._id)
+            for t in ('alias','users','relocated','domain_name','domain_data'):
+                dbc.execute("DELETE FROM %s WHERE gid = %d" % (t, self._id))
             self._dbh.commit()
             dbc.close()
         else:
@@ -186,6 +198,23 @@
             raise VMMDomainException((_("Domain doesn't exist yet."),
                 ERR.NO_SUCH_DOMAIN))
 
+    def saveAlias(self, aliasname):
+        """Stores the alias name for the domain in the database.
+
+        Keyword arguments:
+        aliasname -- the alias name of the domain (str)
+        """
+        if self._id > 0 and not self._aliasExists(aliasname):
+            dbc = self._dbh.cursor()
+            dbc.execute('INSERT INTO domain_name VALUES (%s, %s, %s)',
+                    aliasname, self._id, False)
+            if dbc.rowcount == 1:
+                self._dbh.commit()
+            dbc.close()
+        else:
+            raise VMMDomainException((_("Domain doesn't exist yet."),
+                ERR.NO_SUCH_DOMAIN))
+
     def getID(self):
         """Returns the ID of the domain."""
         return self._id
@@ -289,3 +318,11 @@
                 domdict[gid] = [None, dom]
     del doms
     return order, domdict
+
+def deleteAlias(dbh, aliasname):
+    dbc = dbh.cursor()
+    dbc.execute('DELETE FROM domain_name WHERE domainname = %s', aliasname)
+    if dbc.rowcount > 0:
+        dbh.commit()
+    dbc.close()
+
--- a/VirtualMailManager/VirtualMailManager.py	Fri Aug 15 21:00:26 2008 +0000
+++ b/VirtualMailManager/VirtualMailManager.py	Sat Aug 16 02:19:32 2008 +0000
@@ -506,6 +506,28 @@
             raise VMMDomainException(("%s: '%s'" % (_('Invalid argument'),
                 detailed),  ERR.INVALID_OPTION))
 
+    def domain_alias_add(self, aliasname, domainname):
+        """Adds an alias name to the domain.
+        
+        Keyword arguments:
+        aliasname -- the alias name of the domain (str)
+        domainname -- name of the target domain (str)
+        """
+        dom = self.__getDomain(domainname)
+        aliasname = self.__chkDomainname(aliasname)
+        dom.saveAlias(aliasname)
+
+    def domain_alias_delete(self, aliasname):
+        """Deletes the specified alias name.
+        
+        Keyword arguments:
+        aliasname -- the alias name of the domain (str)
+        """
+        from Domain import deleteAlias
+        aliasname = self.__chkDomainname(aliasname)
+        self.__dbConnect()
+        deleteAlias(self.__dbh, aliasname)
+
     def domain_list(self, pattern=None):
         from Domain import search
         like = False
--- a/VirtualMailManager/constants/ERROR.py	Fri Aug 15 21:00:26 2008 +0000
+++ b/VirtualMailManager/constants/ERROR.py	Sat Aug 16 02:19:32 2008 +0000
@@ -11,31 +11,32 @@
 ALIAS_EXISTS = 24
 ALIAS_MISSING_DEST = 25
 ALIAS_PRESENT = 26
-CONF_WRONGPERM = 27
-CONF_NOPERM = 28
-CONF_NOFILE = 29
-CONF_ERROR = 30
+CONF_ERROR = 27
+CONF_NOFILE = 28
+CONF_NOPERM = 29
+CONF_WRONGPERM = 30
 DATABASE_ERROR = 31
 DOMAINDIR_GROUP_MISMATCH = 32
-DOMAIN_EXISTS = 33
-DOMAIN_INVALID = 34
-DOMAIN_TOO_LONG = 35
-FOUND_DOTS_IN_PATH = 36
-INVALID_ADDRESS = 37
-INVALID_AGUMENT = 38
-INVALID_OPTION = 39
-INVALID_SECTION = 40
-LOCALPART_INVALID = 41
-LOCALPART_TOO_LONG = 42
-MAILDIR_PERM_MISMATCH = 43
-MAILLOCATION_INIT = 44
-NOT_EXECUTABLE = 45
-NO_SUCH_ACCOUNT = 46
-NO_SUCH_ALIAS = 47
-NO_SUCH_BINARY = 48
-NO_SUCH_DIRECTORY = 49
-NO_SUCH_DOMAIN = 50
-TRANSPORT_INIT = 51
-UNKNOWN_MAILLOCATION_ID = 52
-UNKNOWN_SERVICE = 53
-UNKNOWN_TRANSPORT_ID = 54
+DOMAIN_ALIAS_EXISTS = 33
+DOMAIN_EXISTS = 34
+DOMAIN_INVALID = 35
+DOMAIN_TOO_LONG = 36
+FOUND_DOTS_IN_PATH = 37
+INVALID_ADDRESS = 38
+INVALID_AGUMENT = 39
+INVALID_OPTION = 40
+INVALID_SECTION = 41
+LOCALPART_INVALID = 42
+LOCALPART_TOO_LONG = 43
+MAILDIR_PERM_MISMATCH = 44
+MAILLOCATION_INIT = 45
+NOT_EXECUTABLE = 46
+NO_SUCH_ACCOUNT = 47
+NO_SUCH_ALIAS = 48
+NO_SUCH_BINARY = 49
+NO_SUCH_DIRECTORY = 50
+NO_SUCH_DOMAIN = 51
+TRANSPORT_INIT = 52
+UNKNOWN_MAILLOCATION_ID = 53
+UNKNOWN_SERVICE = 54
+UNKNOWN_TRANSPORT_ID = 55
--- a/vmm	Fri Aug 15 21:00:26 2008 +0000
+++ b/vmm	Sat Aug 16 02:19:32 2008 +0000
@@ -33,26 +33,29 @@
     sys.stderr.write(_("""\
 Usage: %s SUBCOMMAND OBJECT ARGS*
   short long
-  subcommand            object           args (* = optional)\n\n""")% __prog__)
+  subcommand               object            args (* = optional)\n\n""")%
+  __prog__)
     sys.stderr.write("""\
-  da    domainadd       domain.tld       transport*
-  di    domaininfo      domain.tld       detailed*
-  dt    domaintransport domain.tld       transport force*
-  dd    domaindelete    domain.tld       delalias*|deluser*|delall*
-  ua    useradd         user@domain.tld  password*
-  ui    userinfo        user@domain.tld  du*
-  un    username        user@domain.tld  'Users Name'
-  up    userpassword    user@domain.tld  password*
-  ut    usertransport   user@domain.tld  transport
-  u0    userdisable     user@domain.tld  smtp*|pop3*|imap*|managesieve*|all*
-  u1    userenable      user@domain.tld  smtp*|pop3*|imap*|managesieve*|all*
-  ud    userdelete      user@domain.tld
-  aa    aliasadd        alias@domain.tld user@domain.tld
-  ai    aliasinfo       alias@domain.tld
-  ad    aliasdelete     alias@domain.tld user@domain.tld*
-  gu    getuser         userid
-  ld    listdomains                      pattern*
-  cf    configure                        section*
+  da    domainadd          domain.tld        transport*
+  di    domaininfo         domain.tld        detailed*
+  dt    domaintransport    domain.tld        transport force*
+  dd    domaindelete       domain.tld        delalias*|deluser*|delall*
+  daa   domainaliasadd     aliasdomain.tld   domain.tld
+  dad   domainaliasdelete  aliasdomain.tld
+  ua    useradd            user@domain.tld   password*
+  ui    userinfo           user@domain.tld   du*
+  un    username           user@domain.tld   'Users Name'
+  up    userpassword       user@domain.tld   password*
+  ut    usertransport      user@domain.tld   transport
+  u0    userdisable        user@domain.tld   smtp*|pop3*|imap*|managesieve*|all*
+  u1    userenable         user@domain.tld   smtp*|pop3*|imap*|managesieve*|all*
+  ud    userdelete         user@domain.tld
+  aa    aliasadd           alias@domain.tld  user@domain.tld
+  ai    aliasinfo          alias@domain.tld
+  ad    aliasdelete        alias@domain.tld  user@domain.tld*
+  gu    getuser            userid
+  ld    listdomains                          pattern*
+  cf    configure                            section*
   h     help
   v     version
 
@@ -117,8 +120,15 @@
     msg = '%s %s' % (_('Available'), title)
     print '%s\n%s' % (msg, '-'*len(msg))
     if len(alist) > 0:
-        for val in alist:
-            print '\t%s' % val
+        if title != _('alias domains'):
+            for val in alist:
+                print '\t%s' % val
+        else:
+            for dom in alist:
+                if not dom.startswith('xn--'):
+                    print '\t%s' % dom
+                else:
+                    print '\t%s (%s)' % (dom, vmm.ace2idna(dom))
     else:
         print _('\tNone')
     print
@@ -142,7 +152,6 @@
     else:
         return '\t[-]     %s' % domain
 
-
 def _printDomList(dids, domains):
     if argc < 3:
         msg = _('Available domains')
@@ -198,6 +207,21 @@
     else:
         vmm.domain_transport(sys.argv[2].lower(), sys.argv[3], sys.argv[4])
 
+def domain_alias_add():
+    if argc < 3:
+        usage(EXIT.MISSING_ARGS,
+                _('Missing alias domain name and target domain name.'))
+    elif argc < 4:
+        usage(EXIT.MISSING_ARGS, _('Missing target domain name.'))
+    else:
+        vmm.domain_alias_add(sys.argv[2].lower(), sys.argv[3].lower())
+
+def domain_alias_delete():
+    if argc < 3:
+        usage(EXIT.MISSING_ARGS, _('Missing alias domain name.'))
+    else:
+        vmm.domain_alias_delete(sys.argv[2].lower())
+
 def user_add():
     if argc < 3:
         usage(EXIT.MISSING_ARGS, _('Missing e-mail address.'))
@@ -325,6 +349,10 @@
             domain_transport()
         elif sys.argv[1] in ['dd', 'domaindelete']:
             domain_delete()
+        elif sys.argv[1] in ['daa', 'domainaliasadd']:
+            domain_alias_add()
+        elif sys.argv[1] in ['dad', 'domainaliasdelete']:
+            domain_alias_delete()
         elif sys.argv[1] in ['ua', 'useradd']:
             user_add()
         elif sys.argv[1] in ['ui', 'userinfo']: