1 # -*- coding: UTF-8 -*- |
1 # -*- coding: UTF-8 -*- |
2 # Copyright (c) 2008 - 2010, Pascal Volk |
2 # Copyright (c) 2008 - 2010, Pascal Volk |
3 # See COPYING for distribution information. |
3 # See COPYING for distribution information. |
4 |
4 |
5 """Virtual Mail Manager's Relocated class to manage relocated users.""" |
5 """ |
|
6 VirtualMailManager.Relocated |
6 |
7 |
7 import VirtualMailManager.constants.ERROR as ERR |
8 Virtual Mail Manager's Relocated class to handle relocated users. |
|
9 """ |
|
10 |
8 from VirtualMailManager.Domain import Domain |
11 from VirtualMailManager.Domain import Domain |
9 from VirtualMailManager.EmailAddress import EmailAddress |
12 from VirtualMailManager.EmailAddress import EmailAddress |
10 from VirtualMailManager.Exceptions import VMMRelocatedException as VMMRE |
13 from VirtualMailManager.Exceptions import VMMRelocatedException as VMMRE |
11 import VirtualMailManager as VMM |
14 from VirtualMailManager.constants.ERROR import NO_SUCH_DOMAIN, \ |
|
15 NO_SUCH_RELOCATED, RELOCATED_ADDR_DEST_IDENTICAL, RELOCATED_EXISTS |
|
16 |
|
17 |
|
18 _ = lambda msg: msg |
|
19 |
12 |
20 |
13 class Relocated(object): |
21 class Relocated(object): |
14 """Class to manage e-mail addresses of relocated users.""" |
22 """Class to handle e-mail addresses of relocated users.""" |
15 __slots__ = ('_addr', '_dest', '_gid', '_isNew', '_dbh') |
23 __slots__ = ('_addr', '_dest', '_gid', '_dbh') |
16 def __init__(self, dbh, address, destination=None): |
24 |
|
25 def __init__(self, dbh, address): |
|
26 """Creates a new *Relocated* instance. The ``address`` is the |
|
27 old e-mail address of the user. |
|
28 |
|
29 Use `setDestination()` to set/update the new address, where the |
|
30 user has moved to.""" |
17 if isinstance(address, EmailAddress): |
31 if isinstance(address, EmailAddress): |
18 self._addr = address |
32 self._addr = address |
19 else: |
33 else: |
20 raise TypeError("Argument 'address' is not an EmailAddress") |
34 raise TypeError("Argument 'address' is not an EmailAddress") |
21 if destination is None: |
35 self._dbh = dbh |
22 self._dest = None |
36 self._gid = 0 |
23 elif isinstance(destination, EmailAddress): |
37 self._dest = None |
24 self._dest = destination |
38 |
|
39 self.__set_gid() |
|
40 self.__load() |
|
41 |
|
42 def __set_gid(self): |
|
43 """Sets the `_gid` attribute, based on the `_addr.domainname`.""" |
|
44 dom = Domain(self._dbh, self._addr.domainname) |
|
45 self._gid = dom.getID() |
|
46 if self._gid == 0: |
|
47 raise VMMRE(_(u"The domain “%s” doesn't exist.") % |
|
48 self._addr.domainname, NO_SUCH_DOMAIN) |
|
49 |
|
50 def __load(self): |
|
51 """Loads the destination address from the database into the |
|
52 `_dest` attribute.""" |
|
53 dbc = self._dbh.cursor() |
|
54 dbc.execute( |
|
55 'SELECT destination FROM relocated WHERE gid=%s AND address=%s', |
|
56 self._gid, self._addr.localpart) |
|
57 destination = dbc.fetchone() |
|
58 dbc.close() |
|
59 if destination: |
|
60 self._dest = EmailAddress(destination[0]) |
|
61 |
|
62 def setDestination(self, destination): |
|
63 """Sets/updates the new address of the relocated user.""" |
|
64 update = False |
|
65 if isinstance(destination, EmailAddress): |
|
66 if self._addr == destination: |
|
67 raise VMMRE(_(u'Address and destination are identical.'), |
|
68 RELOCATED_ADDR_DEST_IDENTICAL) |
|
69 if self._dest: |
|
70 if self._dest == destination: |
|
71 raise VMMRE( |
|
72 _(u'The relocated user “%s” already exists.') % |
|
73 self._addr, RELOCATED_EXISTS) |
|
74 else: |
|
75 self._dest = destination |
|
76 update = True |
|
77 else: |
|
78 self._dest = destination |
25 else: |
79 else: |
26 raise TypeError("Argument 'destination' is not an EmailAddress") |
80 raise TypeError("Argument 'destination' is not an EmailAddress") |
27 if address == destination: |
|
28 raise VMMRE(_(u"Address and destination are identical."), |
|
29 ERR.RELOCATED_ADDR_DEST_IDENTICAL) |
|
30 self._dbh = dbh |
|
31 self._gid = 0 |
|
32 self._isNew = False |
|
33 self._setAddr() |
|
34 self._exists() |
|
35 if self._isNew and VMM.VirtualMailManager.accountExists(self._dbh, |
|
36 self._addr): |
|
37 raise VMMRE(_(u"There is already an account with address “%s”.") %\ |
|
38 self._addr, ERR.ACCOUNT_EXISTS) |
|
39 if self._isNew and VMM.VirtualMailManager.aliasExists(self._dbh, |
|
40 self._addr): |
|
41 raise VMMRE( |
|
42 _(u"There is already an alias with the address “%s”.") %\ |
|
43 self._addr, ERR.ALIAS_EXISTS) |
|
44 |
81 |
45 def _exists(self): |
|
46 dbc = self._dbh.cursor() |
82 dbc = self._dbh.cursor() |
47 dbc.execute("SELECT gid FROM relocated WHERE gid = %s AND address = %s", |
83 if not update: |
48 self._gid, self._addr._localpart) |
84 dbc.execute('INSERT INTO relocated VALUES (%s, %s, %s)', |
49 gid = dbc.fetchone() |
85 self._gid, self._addr.localpart, str(self._dest)) |
|
86 else: |
|
87 dbc.execute('UPDATE relocated SET destination=%s \ |
|
88 WHERE gid=%s AND address=%s', |
|
89 str(self._dest), self._gid, self._addr.localpart) |
|
90 self._dbh.commit() |
50 dbc.close() |
91 dbc.close() |
51 if gid is None: |
|
52 self._isNew = True |
|
53 |
|
54 def _setAddr(self): |
|
55 dom = Domain(self._dbh, self._addr._domainname) |
|
56 self._gid = dom.getID() |
|
57 if self._gid == 0: |
|
58 raise VMMRE(_(u"The domain “%s” doesn't exist.") %\ |
|
59 self._addr._domainname, ERR.NO_SUCH_DOMAIN) |
|
60 |
|
61 def save(self): |
|
62 if self._dest is None: |
|
63 raise VMMRE( |
|
64 _(u"No destination address specified for relocated user."), |
|
65 ERR.RELOCATED_MISSING_DEST) |
|
66 if self._isNew: |
|
67 dbc = self._dbh.cursor() |
|
68 dbc.execute("INSERT INTO relocated VALUES (%s, %s, %s)", |
|
69 self._gid, self._addr._localpart, str(self._dest)) |
|
70 self._dbh.commit() |
|
71 dbc.close() |
|
72 else: |
|
73 raise VMMRE( |
|
74 _(u"The relocated user “%s” already exists.") % self._addr, |
|
75 ERR.RELOCATED_EXISTS) |
|
76 |
92 |
77 def getInfo(self): |
93 def getInfo(self): |
78 dbc = self._dbh.cursor() |
94 """Returns the address to which mails should be sent.""" |
79 dbc.execute('SELECT destination FROM relocated WHERE gid=%s\ |
95 if self._dest: |
80 AND address=%s', |
96 return self._dest |
81 self._gid, self._addr._localpart) |
|
82 destination = dbc.fetchone() |
|
83 dbc.close() |
|
84 if destination is not None: |
|
85 return destination[0] |
|
86 else: |
97 else: |
87 raise VMMRE( |
98 raise VMMRE(_(u"The relocated user “%s” doesn't exist.") % |
88 _(u"The relocated user “%s” doesn't exist.") % self._addr, |
99 self._addr, NO_SUCH_RELOCATED) |
89 ERR.NO_SUCH_RELOCATED) |
|
90 |
100 |
91 def delete(self): |
101 def delete(self): |
|
102 """Deletes the relocated entry from the database.""" |
|
103 if not self._dest: |
|
104 raise VMMRE(_(u"The relocated user “%s” doesn't exist.") % |
|
105 self._addr, NO_SUCH_RELOCATED) |
92 dbc = self._dbh.cursor() |
106 dbc = self._dbh.cursor() |
93 dbc.execute("DELETE FROM relocated WHERE gid = %s AND address = %s", |
107 dbc.execute("DELETE FROM relocated WHERE gid = %s AND address = %s", |
94 self._gid, self._addr._localpart) |
108 self._gid, self._addr.localpart) |
95 rowcount = dbc.rowcount |
109 if dbc.rowcount > 0: |
|
110 self._dbh.commit() |
96 dbc.close() |
111 dbc.close() |
97 if rowcount > 0: |
112 self._dest = None |
98 self._dbh.commit() |
|
99 else: |
|
100 raise VMMRE( |
|
101 _(u"The relocated user “%s” doesn't exist.") % self._addr, |
|
102 ERR.NO_SUCH_RELOCATED) |
|
103 |
113 |
|
114 |
|
115 del _ |