1 # -*- coding: UTF-8 -*- |
|
2 # Copyright (c) 2008 - 2010, Pascal Volk |
|
3 # See COPYING for distribution information. |
|
4 |
|
5 """Virtual Mail Manager's Relocated class to manage relocated users.""" |
|
6 |
|
7 from __main__ import ERR |
|
8 from Exceptions import VMMRelocatedException as VMMRE |
|
9 from Domain import Domain |
|
10 from EmailAddress import EmailAddress |
|
11 import VirtualMailManager as VMM |
|
12 |
|
13 class Relocated(object): |
|
14 """Class to manage e-mail addresses of relocated users.""" |
|
15 __slots__ = ('_addr', '_dest', '_gid', '_isNew', '_dbh') |
|
16 def __init__(self, dbh, address, destination=None): |
|
17 if isinstance(address, EmailAddress): |
|
18 self._addr = address |
|
19 else: |
|
20 raise TypeError("Argument 'address' is not an EmailAddress") |
|
21 if destination is None: |
|
22 self._dest = None |
|
23 elif isinstance(destination, EmailAddress): |
|
24 self._dest = destination |
|
25 else: |
|
26 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 |
|
45 def _exists(self): |
|
46 dbc = self._dbh.cursor() |
|
47 dbc.execute("SELECT gid FROM relocated WHERE gid = %s AND address = %s", |
|
48 self._gid, self._addr._localpart) |
|
49 gid = dbc.fetchone() |
|
50 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 |
|
77 def getInfo(self): |
|
78 dbc = self._dbh.cursor() |
|
79 dbc.execute('SELECT destination FROM relocated WHERE gid=%s\ |
|
80 AND address=%s', |
|
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: |
|
87 raise VMMRE( |
|
88 _(u"The relocated user “%s” doesn't exist.") % self._addr, |
|
89 ERR.NO_SUCH_RELOCATED) |
|
90 |
|
91 def delete(self): |
|
92 dbc = self._dbh.cursor() |
|
93 dbc.execute("DELETE FROM relocated WHERE gid = %s AND address = %s", |
|
94 self._gid, self._addr._localpart) |
|
95 rowcount = dbc.rowcount |
|
96 dbc.close() |
|
97 if rowcount > 0: |
|
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 |
|