VirtualMailManager/Account.py
branchv0.6.x
changeset 225 a51809f7940b
parent 216 0c8c053b451c
child 227 87db9f1f95ea
equal deleted inserted replaced
224:7e9874a50d92 225:a51809f7940b
     9 from VirtualMailManager.EmailAddress import EmailAddress
     9 from VirtualMailManager.EmailAddress import EmailAddress
    10 from VirtualMailManager.errors import AccountError as AccE
    10 from VirtualMailManager.errors import AccountError as AccE
    11 from VirtualMailManager.MailLocation import MailLocation
    11 from VirtualMailManager.MailLocation import MailLocation
    12 from VirtualMailManager.Transport import Transport
    12 from VirtualMailManager.Transport import Transport
    13 
    13 
       
    14 
       
    15 _ = lambda msg: msg
       
    16 
       
    17 
    14 class Account(object):
    18 class Account(object):
    15     """Class to manage e-mail accounts."""
    19     """Class to manage e-mail accounts."""
    16     __slots__ = ('_addr','_base','_gid','_mid','_passwd','_tid','_uid','_dbh')
    20     __slots__ = ('_addr', '_base', '_gid', '_mid', '_passwd', '_tid', '_uid',
       
    21                  '_dbh')
    17 
    22 
    18     def __init__(self, dbh, address, password=None):
    23     def __init__(self, dbh, address, password=None):
    19         self._dbh = dbh
    24         self._dbh = dbh
    20         self._base = None
    25         self._base = None
    21         if isinstance(address, EmailAddress):
    26         if isinstance(address, EmailAddress):
    43 
    48 
    44     def _exists(self):
    49     def _exists(self):
    45         dbc = self._dbh.cursor()
    50         dbc = self._dbh.cursor()
    46         dbc.execute(
    51         dbc.execute(
    47             "SELECT uid, mid, tid FROM users WHERE gid=%s AND local_part=%s",
    52             "SELECT uid, mid, tid FROM users WHERE gid=%s AND local_part=%s",
    48                     self._gid, self._addr._localpart)
    53                     self._gid, self._addr.localpart)
    49         result = dbc.fetchone()
    54         result = dbc.fetchone()
    50         dbc.close()
    55         dbc.close()
    51         if result is not None:
    56         if result is not None:
    52             self._uid, self._mid, self._tid = result
    57             self._uid, self._mid, self._tid = result
    53             return True
    58             return True
    54         else:
    59         else:
    55             return False
    60             return False
    56 
    61 
    57     def _setAddr(self):
    62     def _setAddr(self):
    58         dom = Domain(self._dbh, self._addr._domainname)
    63         dom = Domain(self._dbh, self._addr.domainname)
    59         self._gid = dom.getID()
    64         self._gid = dom.getID()
    60         if self._gid == 0:
    65         if self._gid == 0:
    61             raise AccE(_(u"The domain “%s” doesn't exist.") %
    66             raise AccE(_(u"The domain “%s” doesn't exist.") %
    62                        self._addr._domainname, ERR.NO_SUCH_DOMAIN)
    67                        self._addr.domainname, ERR.NO_SUCH_DOMAIN)
    63         self._base = dom.getDir()
    68         self._base = dom.getDir()
    64         self._tid = dom.getTransportID()
    69         self._tid = dom.getTransportID()
    65 
    70 
    66     def _setID(self):
    71     def _setID(self):
    67         dbc = self._dbh.cursor()
    72         dbc = self._dbh.cursor()
   103         dbc.close()
   108         dbc.close()
   104 
   109 
   105     def __aliaseCount(self):
   110     def __aliaseCount(self):
   106         dbc = self._dbh.cursor()
   111         dbc = self._dbh.cursor()
   107         q = "SELECT COUNT(destination) FROM alias WHERE destination = '%s'"\
   112         q = "SELECT COUNT(destination) FROM alias WHERE destination = '%s'"\
   108             %self._addr
   113             % self._addr
   109         dbc.execute(q)
   114         dbc.execute(q)
   110         a_count = dbc.fetchone()[0]
   115         a_count = dbc.fetchone()[0]
   111         dbc.close()
   116         dbc.close()
   112         return a_count
   117         return a_count
   113 
   118 
   139             else:
   144             else:
   140                 sieve_col = 'managesieve'
   145                 sieve_col = 'managesieve'
   141             self._prepare(maillocation)
   146             self._prepare(maillocation)
   142             sql = "INSERT INTO users (local_part, passwd, uid, gid, mid, tid,\
   147             sql = "INSERT INTO users (local_part, passwd, uid, gid, mid, tid,\
   143  smtp, pop3, imap, %s) VALUES ('%s', '%s', %d, %d, %d, %d, %s, %s, %s, %s)" % (
   148  smtp, pop3, imap, %s) VALUES ('%s', '%s', %d, %d, %d, %d, %s, %s, %s, %s)" % (
   144                 sieve_col, self._addr._localpart, self._passwd, self._uid,
   149                 sieve_col, self._addr.localpart, self._passwd, self._uid,
   145                 self._gid, self._mid, self._tid, smtp, pop3, imap, sieve)
   150                 self._gid, self._mid, self._tid, smtp, pop3, imap, sieve)
   146             dbc = self._dbh.cursor()
   151             dbc = self._dbh.cursor()
   147             dbc.execute(sql)
   152             dbc.execute(sql)
   148             self._dbh.commit()
   153             self._dbh.commit()
   149             dbc.close()
   154             dbc.close()
   160         dbc = self._dbh.cursor()
   165         dbc = self._dbh.cursor()
   161         if what == 'password':
   166         if what == 'password':
   162             dbc.execute('UPDATE users SET passwd = %s WHERE uid = %s',
   167             dbc.execute('UPDATE users SET passwd = %s WHERE uid = %s',
   163                     value, self._uid)
   168                     value, self._uid)
   164         elif what == 'transport':
   169         elif what == 'transport':
   165             self._tid = Transport(self._dbh, transport=value).getID()
   170             self._tid = Transport(self._dbh, transport=value).id
   166             dbc.execute('UPDATE users SET tid = %s WHERE uid = %s',
   171             dbc.execute('UPDATE users SET tid = %s WHERE uid = %s',
   167                     self._tid, self._uid)
   172                     self._tid, self._uid)
   168         else:
   173         else:
   169             dbc.execute('UPDATE users SET name = %s WHERE uid = %s',
   174             dbc.execute('UPDATE users SET name = %s WHERE uid = %s',
   170                     value, self._uid)
   175                     value, self._uid)
   200             info['address'] = self._addr
   205             info['address'] = self._addr
   201             info['maildir'] = '%s/%s/%s' % (self._base, info['uid'],
   206             info['maildir'] = '%s/%s/%s' % (self._base, info['uid'],
   202                     MailLocation(self._dbh,
   207                     MailLocation(self._dbh,
   203                         mid=info['maildir']).getMailLocation())
   208                         mid=info['maildir']).getMailLocation())
   204             info['transport'] = Transport(self._dbh,
   209             info['transport'] = Transport(self._dbh,
   205                     tid=info['transport']).getTransport()
   210                                           tid=info['transport']).transport
   206             return info
   211             return info
   207 
   212 
   208     def getAliases(self):
   213     def getAliases(self):
   209         dbc = self._dbh.cursor()
   214         dbc = self._dbh.cursor()
   210         dbc.execute("SELECT address ||'@'|| domainname FROM alias, domain_name\
   215         dbc.execute("SELECT address ||'@'|| domainname FROM alias, domain_name\
   239                     self._dbh.commit()
   244                     self._dbh.commit()
   240             else:
   245             else:
   241                 dbc.close()
   246                 dbc.close()
   242                 raise AccE(
   247                 raise AccE(
   243                   _(u"There are %(count)d aliases with the destination address\
   248                   _(u"There are %(count)d aliases with the destination address\
   244  “%(address)s”.") %{'count': a_count, 'address': self._addr}, ERR.ALIAS_PRESENT)
   249  “%(address)s”.") % {'count': a_count, 'address': self._addr},
   245         dbc.close()
   250                   ERR.ALIAS_PRESENT)
       
   251         dbc.close()
       
   252 
   246 
   253 
   247 def getAccountByID(uid, dbh):
   254 def getAccountByID(uid, dbh):
   248     try:
   255     try:
   249         uid = long(uid)
   256         uid = long(uid)
   250     except ValueError:
   257     except ValueError:
   262                 ERR.NO_SUCH_ACCOUNT)
   269                 ERR.NO_SUCH_ACCOUNT)
   263     keys = ['address', 'uid', 'gid']
   270     keys = ['address', 'uid', 'gid']
   264     info = dict(zip(keys, info))
   271     info = dict(zip(keys, info))
   265     return info
   272     return info
   266 
   273 
       
   274 
       
   275 del _