Added sieve/managesieve switching stuff,
authorPascal Volk <neverseen@users.sourceforge.net>
Tue, 18 Aug 2009 03:00:00 +0000
changeset 115 21f264a88ab2
parent 114 e671210b04b8
child 116 949c5db6447a
Added sieve/managesieve switching stuff, depending on the used Dovecot version.
VirtualMailManager/Account.py
VirtualMailManager/VirtualMailManager.py
vmm
--- a/VirtualMailManager/Account.py	Tue Aug 18 02:29:15 2009 +0000
+++ b/VirtualMailManager/Account.py	Tue Aug 18 03:00:00 2009 +0000
@@ -72,30 +72,28 @@
         self._setID()
         self._mid = MailLocation(self._dbh, maillocation=maillocation).getID()
 
-    def _switchState(self, state, service):
+    def _switchState(self, state, dcvers, service):
         if not isinstance(state, bool):
             return False
-        if not service in ['smtp', 'pop3', 'imap', 'sieve', 'all', None]:
+        if not service in (None, 'all', 'imap', 'pop3', 'sieve', 'smtp'):
             raise AccE(_(u"Unknown service »%s«.") % service,
                     ERR.UNKNOWN_SERVICE)
         if self._uid < 1:
             raise AccE(_(u"The account »%s« doesn't exists.") % self._addr,
                     ERR.NO_SUCH_ACCOUNT)
+        sieve_col = 'sieve' if dcvers > 11 else 'managesieve'
+        if service in ('smtp', 'pop3', 'imap'):
+            sql = 'UPDATE users SET %s = %s WHERE uid = %d' % (service, state,
+                    self._uid)
+        elif service == 'sieve':
+            sql = 'UPDATE users SET %s = %s WHERE uid = %d' % (sieve_col,
+                    state, self._uid)
+        else:
+            sql = 'UPDATE users SET smtp = %(s)s, pop3 = %(s)s, imap = %(s)s,\
+ %(col)s = %(s)s WHERE uid = %(uid)d' % {
+                's': state, 'col': sieve_col, 'uid': self._uid}
         dbc = self._dbh.cursor()
-        if service in ['smtp', 'pop3', 'imap', 'sieve']:
-            dbc.execute(
-                    "UPDATE users SET %s=%s WHERE local_part='%s' AND gid=%s"
-                    % (service, state, self._addr._localpart, self._gid))
-        elif state:
-            # TODO
-            # add dovecotvers check
-            dbc.execute("UPDATE users SET smtp = TRUE, pop3 = TRUE,\
- imap = TRUE, managesieve = TRUE WHERE local_part = %s AND gid = %s",
-                self._addr._localpart, self._gid)
-        else:
-            dbc.execute("UPDATE users SET smtp = FALSE, pop3 = FALSE,\
- imap = FALSE, managesieve = FALSE WHERE local_part = %s AND gid = %s",
-                self._addr._localpart, self._gid)
+        dbc.execute(sql)
         if dbc.rowcount > 0:
             self._dbh.commit()
         dbc.close()
@@ -124,23 +122,22 @@
         elif directory == 'home':
             return '%s/%i' % (self._base, self._uid)
 
-    def enable(self, service=None):
-        self._switchState(True, service)
+    def enable(self, dcvers, service=None):
+        self._switchState(True, dcvers, service)
 
-    def disable(self, service=None):
-        self._switchState(False, service)
+    def disable(self, dcvers, service=None):
+        self._switchState(False, dcvers, service)
 
-    def save(self, maillocation, smtp, pop3, imap, sieve):
+    def save(self, maillocation, dcvers, smtp, pop3, imap, sieve):
         if self._uid < 1:
+            sieve_col = 'sieve' if dcvers > 11 else 'managesieve'
             self._prepare(maillocation)
+            sql = "INSERT INTO users (local_part, passwd, uid, gid, mid, tid,\
+ smtp, pop3, imap, %s) VALUES ('%s', '%s', %d, %d, %d, %d, %s, %s, %s, %s)" % (
+                sieve_col, self._addr._localpart, self._passwd, self._uid,
+                self._gid, self._mid, self._tid, smtp, pop3, imap, sieve)
             dbc = self._dbh.cursor()
-            # TODO
-            # add dovecotvers check
-            dbc.execute("""INSERT INTO users (local_part, passwd, uid, gid,\
- mid, tid, smtp, pop3, imap, managesieve)\
- VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
-                self._addr._localpart, self._passwd, self._uid, self._gid,
-                self._mid, self._tid, smtp, pop3, imap, sieve)
+            dbc.execute(sql)
             self._dbh.commit()
             dbc.close()
         else:
@@ -168,13 +165,12 @@
             self._dbh.commit()
         dbc.close()
 
-    def getInfo(self):
+    def getInfo(self, dcvers):
+        sieve_col = 'sieve' if dcvers > 11 else 'managesieve'
+        sql = 'SELECT name, uid, gid, mid, tid, smtp, pop3, imap, %s\
+ FROM users WHERE uid = %d' % (sieve_col, self._uid)
         dbc = self._dbh.cursor()
-        # TODO
-        # add dovecotvers check
-        dbc.execute("SELECT name, uid, gid, mid, tid, smtp, pop3, imap, \
- managesieve FROM users WHERE local_part=%s AND gid=%s",
-            self._addr._localpart, self._gid)
+        dbc.execute(sql)
         info = dbc.fetchone()
         dbc.close()
         if info is None:
@@ -182,9 +178,9 @@
                     ERR.NO_SUCH_ACCOUNT)
         else:
             keys = ['name', 'uid', 'gid', 'maildir', 'transport', 'smtp',
-                    'pop3', 'imap', 'sieve']
+                    'pop3', 'imap', sieve_col]
             info = dict(zip(keys, info))
-            for service in ['smtp', 'pop3', 'imap', 'sieve']:
+            for service in ('smtp', 'pop3', 'imap', sieve_col):
                 if bool(info[service]):
                     info[service] = _('enabled')
                 else:
--- a/VirtualMailManager/VirtualMailManager.py	Tue Aug 18 02:29:15 2009 +0000
+++ b/VirtualMailManager/VirtualMailManager.py	Tue Aug 18 03:00:00 2009 +0000
@@ -598,6 +598,7 @@
             password = self._readpass()
             acc.setPassword(self.__pwhash(password))
         acc.save(self.__Cfg.get('maildir', 'name'),
+                self.__Cfg.getint('misc', 'dovecotvers'),
                 self.__Cfg.getboolean('services', 'smtp'),
                 self.__Cfg.getboolean('services', 'pop3'),
                 self.__Cfg.getboolean('services', 'imap'),
@@ -651,7 +652,7 @@
             raise VMMException(_(u'Invalid argument: »%s«') % details,
                     ERR.INVALID_AGUMENT)
         acc = self.__getAccount(emailaddress)
-        info = acc.getInfo()
+        info = acc.getInfo(self.__Cfg.getint('misc', 'dovecotvers'))
         if self.__Cfg.getboolean('maildir', 'diskusage')\
         or details in ['du', 'full']:
             info['disk usage'] = self.__getDiskUsage('%(maildir)s' % info)
@@ -684,11 +685,11 @@
 
     def userDisable(self, emailaddress, service=None):
         acc = self.__getAccount(emailaddress)
-        acc.disable(service)
+        acc.disable(self.__Cfg.getint('misc', 'dovecotvers'), service)
 
     def userEnable(self, emailaddress, service=None):
         acc = self.__getAccount(emailaddress)
-        acc.enable(service)
+        acc.enable(self.__Cfg.getint('misc', 'dovecotvers'), service)
 
     def relocatedAdd(self, emailaddress, targetaddress):
         relocated = self.__getRelocated(emailaddress, targetaddress)
--- a/vmm	Tue Aug 18 02:29:15 2009 +0000
+++ b/vmm	Tue Aug 18 03:00:00 2009 +0000
@@ -85,6 +85,10 @@
 
 def _getOrder():
     order = ()
+    if vmm.cfgGetInt('misc', 'dovecotvers') > 11:
+        sieve_name = 'sieve'
+    else:
+        sieve_name = 'managesieve'
     if argv[1] in ['di', 'domaininfo']:
         order = (('domainname', 0), ('gid', 1), ('transport', 0),
                 ('domaindir', 0), ('aliasdomains', 0), ('accounts', 0),
@@ -94,11 +98,11 @@
         or vmm.cfgGetBoolean('maildir', 'diskusage'):
             order = (('address', 0), ('name', 0), ('uid', 1), ('gid', 1),
                     ('transport', 0), ('maildir', 0), ('disk usage', 0),
-                    ('smtp', 1), ('pop3', 1), ('imap', 1), ('sieve', 1))
+                    ('smtp', 1), ('pop3', 1), ('imap', 1), (sieve_name, 1))
         else:
             order = (('address', 0), ('name', 0), ('uid', 1), ('gid', 1),
                     ('transport', 0), ('maildir', 0), ('smtp', 1), ('pop3', 1),
-                    ('imap', 1), ('sieve', 1))
+                    ('imap', 1), (sieve_name, 1))
     elif argv[1] in ['gu', 'getuser']:
         order = (('uid', 1), ('gid', 1), ('address', 0))
     return order