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', '_mail', '_new', '_passwd', |
30 __slots__ = ('_addr', '_dbh', '_domain', '_mail', '_new', '_passwd', |
31 '_transport', '_uid') |
31 '_transport', '_uid') |
|
32 _services = ('imap', 'pop3', 'smtp', 'sieve') |
32 |
33 |
33 def __init__(self, dbh, address): |
34 def __init__(self, dbh, address): |
34 """Creates a new Account instance. |
35 """Creates a new Account instance. |
35 |
36 |
36 When an account with the given *address* could be found in the |
37 When an account with the given *address* could be found in the |
101 {'transport': self._transport, |
102 {'transport': self._transport, |
102 'mbfmt': maillocation.mbformat}, INVALID_MAIL_LOCATION) |
103 'mbfmt': maillocation.mbformat}, INVALID_MAIL_LOCATION) |
103 self._mail = maillocation |
104 self._mail = maillocation |
104 self._set_uid() |
105 self._set_uid() |
105 |
106 |
106 def _switch_state(self, state, service): |
107 def _update_services(self, activate, *services): |
107 """Switch the state of the Account's services on or off. See |
108 """Activate or deactivate the Account's services. |
108 Account.enable()/Account.disable() for more information.""" |
109 |
|
110 Arguments: |
|
111 |
|
112 `activate`: bool |
|
113 When `True` the Account's user will be able to login to the |
|
114 services, otherwise the login will fail. |
|
115 `*services` |
|
116 No or one or more of the services: imap, pop3, smtp and sieve |
|
117 """ |
109 self._chk_state() |
118 self._chk_state() |
110 if service not in (None, 'all', 'imap', 'pop3', 'sieve', 'smtp'): |
119 if services: |
111 raise AErr(_(u"Unknown service: '%s'.") % service, UNKNOWN_SERVICE) |
120 services = set(services) |
112 if cfg_dget('misc.dovecot_version') >= 0x10200b02: |
121 for service in services: |
113 sieve_col = 'sieve' |
122 if service not in self.__class__._services: |
|
123 raise AErr(_(u"Unknown service: '%s'.") % service, |
|
124 UNKNOWN_SERVICE) |
114 else: |
125 else: |
115 sieve_col = 'managesieve' |
126 services = self.__class__._services |
116 if service in ('smtp', 'pop3', 'imap'): |
127 state = ('FALSE', 'TRUE')[activate] |
117 sql = 'UPDATE users SET %s = %s WHERE uid = %d' % (service, state, |
128 sql = 'UPDATE users SET %s WHERE uid = %u' % ( |
118 self._uid) |
129 (' = %(s)s, '.join(services) + ' = %(s)s') % {'s': state}, |
119 elif service == 'sieve': |
130 self._uid) |
120 sql = 'UPDATE users SET %s = %s WHERE uid = %d' % (sieve_col, |
131 if 'sieve' in services and \ |
121 state, |
132 cfg_dget('misc.dovecot_version') < 0x10200b02: |
122 self._uid) |
133 sql = sql.replace('sieve', 'managesieve') |
123 else: |
|
124 sql = 'UPDATE users SET smtp = %(s)s, pop3 = %(s)s, imap = %(s)s,\ |
|
125 %(col)s = %(s)s WHERE uid = %(uid)d' % \ |
|
126 {'s': state, 'col': sieve_col, 'uid': self._uid} |
|
127 dbc = self._dbh.cursor() |
134 dbc = self._dbh.cursor() |
128 dbc.execute(sql) |
135 dbc.execute(sql) |
129 if dbc.rowcount > 0: |
136 if dbc.rowcount > 0: |
130 self._dbh.commit() |
137 self._dbh.commit() |
131 dbc.close() |
138 dbc.close() |
211 `transport` : basestring |
218 `transport` : basestring |
212 The string representation of the transport, e.g.: 'dovecot:' |
219 The string representation of the transport, e.g.: 'dovecot:' |
213 """ |
220 """ |
214 self._transport = Transport(self._dbh, transport=transport) |
221 self._transport = Transport(self._dbh, transport=transport) |
215 |
222 |
216 def enable(self, service=None): |
223 def enable(self, *services): |
217 """Enable a/all service/s for the Account. |
224 """Enable all or the given service/s for the Account. |
218 |
225 |
219 Possible values for the *service* are: 'imap', 'pop3', 'sieve' and |
226 Possible *services* are: 'imap', 'pop3', 'sieve' and 'smtp'. |
220 'smtp'. When all services should be enabled, use 'all' or the |
227 When all services should be enabled, give no service name. |
221 default value `None`. |
|
222 |
228 |
223 Arguments: |
229 Arguments: |
224 |
230 |
225 `service` : basestring |
231 `*services` : basestring |
226 The name of a service ('imap', 'pop3', 'smtp', 'sieve'), 'all' |
232 No or one or more of the services 'imap', 'pop3', 'smtp', and |
227 or `None`. |
233 'sieve'. |
228 """ |
234 """ |
229 self._switch_state(True, service) |
235 self._update_services(True, *services) |
230 |
236 |
231 def disable(self, service=None): |
237 def disable(self, *services): |
232 """Disable a/all service/s for the Account. |
238 """Disable all or the given service/s for the Account. |
233 |
239 |
234 For more information see: Account.enable().""" |
240 For more information see: Account.enable().""" |
235 self._switch_state(False, service) |
241 self._update_services(False, *services) |
236 |
242 |
237 def save(self): |
243 def save(self): |
238 """Save the new Account in the database.""" |
244 """Save the new Account in the database.""" |
239 if not self._new: |
245 if not self._new: |
240 raise AErr(_(u"The account '%s' already exists.") % self._addr, |
246 raise AErr(_(u"The account '%s' already exists.") % self._addr, |