VirtualMailManager/Account.py
branchv0.6.x
changeset 293 a77b67673aa6
parent 290 e2785e04f92e
child 302 32b4a39b5640
equal deleted inserted replaced
292:619dadc0fd25 293:a77b67673aa6
    25 cfg_dget = lambda option: None
    25 cfg_dget = lambda option: None
    26 
    26 
    27 
    27 
    28 class Account(object):
    28 class Account(object):
    29     """Class to manage e-mail accounts."""
    29     """Class to manage e-mail accounts."""
    30     __slots__ = ('_addr', '_dbh', '_domain', '_mid', '_new', '_passwd',
    30     __slots__ = ('_addr', '_dbh', '_domain', '_mail', '_new', '_passwd',
    31                  '_transport', '_uid')
    31                  '_transport', '_uid')
    32 
    32 
    33     def __init__(self, dbh, address):
    33     def __init__(self, dbh, address):
    34         """Creates a new Account instance.
    34         """Creates a new Account instance.
    35 
    35 
    50         self._domain = Domain(self._dbh, self._addr.domainname)
    50         self._domain = Domain(self._dbh, self._addr.domainname)
    51         if not self._domain.gid:
    51         if not self._domain.gid:
    52             raise AErr(_(u"The domain '%s' doesn't exist.") %
    52             raise AErr(_(u"The domain '%s' doesn't exist.") %
    53                        self._addr.domainname, NO_SUCH_DOMAIN)
    53                        self._addr.domainname, NO_SUCH_DOMAIN)
    54         self._uid = 0
    54         self._uid = 0
    55         self._mid = 0
    55         self._mail = None
    56         self._transport = self._domain.transport
    56         self._transport = self._domain.transport
    57         self._passwd = None
    57         self._passwd = None
    58         self._new = True
    58         self._new = True
    59         self._load()
    59         self._load()
    60 
    60 
    69         dbc.execute('SELECT uid, mid, tid FROM users WHERE gid = %s AND '
    69         dbc.execute('SELECT uid, mid, tid FROM users WHERE gid = %s AND '
    70                     'local_part = %s', self._domain.gid, self._addr.localpart)
    70                     'local_part = %s', self._domain.gid, self._addr.localpart)
    71         result = dbc.fetchone()
    71         result = dbc.fetchone()
    72         dbc.close()
    72         dbc.close()
    73         if result:
    73         if result:
    74             self._uid, self._mid, _tid = result
    74             self._uid, _mid, _tid = result
    75             if _tid != self._transport.tid:
    75             if _tid != self._transport.tid:
    76                 self._transport = Transport(self._dbh, tid=_tid)
    76                 self._transport = Transport(self._dbh, tid=_tid)
       
    77             self._mail = MailLocation(mid=_mid)
    77             self._new = False
    78             self._new = False
    78 
    79 
    79     def _set_uid(self):
    80     def _set_uid(self):
    80         """Set the unique ID for the new Account."""
    81         """Set the unique ID for the new Account."""
    81         assert self._uid == 0
    82         assert self._uid == 0
    98           self._transport.transport.lower() in ('virtual:', 'virtual'):
    99           self._transport.transport.lower() in ('virtual:', 'virtual'):
    99             raise AErr(_(u"Invalid transport '%(transport)s' for mail_location"
   100             raise AErr(_(u"Invalid transport '%(transport)s' for mail_location"
   100                          u" prefix '%(prefix)s'") %
   101                          u" prefix '%(prefix)s'") %
   101                        {'transport': self._transport,
   102                        {'transport': self._transport,
   102                         'prefix': maillocation.prefix}, INVALID_MAIL_LOCATION)
   103                         'prefix': maillocation.prefix}, INVALID_MAIL_LOCATION)
   103         self._mid = maillocation.mid
   104         self._mail = maillocation
   104         self._set_uid()
   105         self._set_uid()
   105 
   106 
   106     def _switch_state(self, state, service):
   107     def _switch_state(self, state, service):
   107         """Switch the state of the Account's services on or off. See
   108         """Switch the state of the Account's services on or off. See
   108         Account.enable()/Account.disable() for more information."""
   109         Account.enable()/Account.disable() for more information."""
   173         if not self._new:
   174         if not self._new:
   174             return '%s/%s' % (self._domain.directory, self._uid)
   175             return '%s/%s' % (self._domain.directory, self._uid)
   175         return None
   176         return None
   176 
   177 
   177     @property
   178     @property
       
   179     def mail_location(self):
       
   180         """The Account's MailLocation."""
       
   181         return self._mail
       
   182 
       
   183     @property
   178     def uid(self):
   184     def uid(self):
   179         """The Account's unique ID."""
   185         """The Account's unique ID."""
   180         return self._uid
   186         return self._uid
   181 
   187 
   182     def set_password(self, password):
   188     def set_password(self, password):
   244         self._prepare(MailLocation(format=cfg_dget('mailbox.format')))
   250         self._prepare(MailLocation(format=cfg_dget('mailbox.format')))
   245         sql = "INSERT INTO users (local_part, passwd, uid, gid, mid, tid,\
   251         sql = "INSERT INTO users (local_part, passwd, uid, gid, mid, tid,\
   246  smtp, pop3, imap, %s) VALUES ('%s', '%s', %d, %d, %d, %d, %s, %s, %s, %s)" % (
   252  smtp, pop3, imap, %s) VALUES ('%s', '%s', %d, %d, %d, %d, %s, %s, %s, %s)" % (
   247             sieve_col, self._addr.localpart, pwhash(self._passwd,
   253             sieve_col, self._addr.localpart, pwhash(self._passwd,
   248                                                     user=self._addr),
   254                                                     user=self._addr),
   249             self._uid, self._domain.gid, self._mid, self._transport.tid,
   255             self._uid, self._domain.gid, self._mail.mid, self._transport.tid,
   250             cfg_dget('account.smtp'), cfg_dget('account.pop3'),
   256             cfg_dget('account.smtp'), cfg_dget('account.pop3'),
   251             cfg_dget('account.imap'), cfg_dget('account.sieve'))
   257             cfg_dget('account.imap'), cfg_dget('account.sieve'))
   252         dbc = self._dbh.cursor()
   258         dbc = self._dbh.cursor()
   253         dbc.execute(sql)
   259         dbc.execute(sql)
   254         self._dbh.commit()
   260         self._dbh.commit()
   316                     # TP: A service (pop3/imap) isn't enabled/usable for a user
   322                     # TP: A service (pop3/imap) isn't enabled/usable for a user
   317                     info[service] = _('disabled')
   323                     info[service] = _('disabled')
   318             info['address'] = self._addr
   324             info['address'] = self._addr
   319             info['gid'] = self._domain.gid
   325             info['gid'] = self._domain.gid
   320             info['home'] = '%s/%s' % (self._domain.directory, self._uid)
   326             info['home'] = '%s/%s' % (self._domain.directory, self._uid)
   321             info['mail_location'] = MailLocation(mid=self._mid).mail_location
   327             info['mail_location'] = self._mail.mail_location
   322             info['transport'] = self._transport.transport
   328             info['transport'] = self._transport.transport
   323             info['uid'] = self._uid
   329             info['uid'] = self._uid
   324             return info
   330             return info
   325         # nearly impossibleā€½
   331         # nearly impossibleā€½
   326         raise AErr(_(u"Couldn't fetch information for account: '%s'") %
   332         raise AErr(_(u"Couldn't fetch information for account: '%s'") %
   372                            ALIAS_PRESENT)
   378                            ALIAS_PRESENT)
   373             dbc.execute('DELETE FROM users WHERE uid = %s', self._uid)
   379             dbc.execute('DELETE FROM users WHERE uid = %s', self._uid)
   374             self._dbh.commit()
   380             self._dbh.commit()
   375         dbc.close()
   381         dbc.close()
   376         self._new = True
   382         self._new = True
   377         self._uid = self._mid = 0
   383         self._uid = 0
   378         self._addr = self._dbh = self._domain = self._passwd = None
   384         self._addr = self._dbh = self._domain = self._passwd = None
   379         self._transport = None
   385         self._mail = self._transport = None
   380 
   386 
   381 
   387 
   382 def get_account_by_uid(uid, dbh):
   388 def get_account_by_uid(uid, dbh):
   383     """Search an Account by its UID.
   389     """Search an Account by its UID.
   384 
   390