17 from Domain import Domain |
17 from Domain import Domain |
18 import constants.ERROR as ERR |
18 import constants.ERROR as ERR |
19 |
19 |
20 class Alias: |
20 class Alias: |
21 """Class to manage e-mail accounts.""" |
21 """Class to manage e-mail accounts.""" |
22 def __init__(self, dbh, address, basedir, destination=None): |
22 def __init__(self, dbh, address, destination=None): |
23 if address == destination: |
23 if address == destination: |
24 raise VMMAliasException(('Address and destination are identical.', |
24 raise VMMAliasException(('Address and destination are identical.', |
25 ERR.ALIAS_ADDR_DEST_IDENTICAL)) |
25 ERR.ALIAS_ADDR_DEST_IDENTICAL)) |
26 self._dbh = dbh |
26 self._dbh = dbh |
27 self._addr = address |
27 self._addr = address |
28 self._dest = destination |
28 self._dest = destination |
29 self._localpart = None |
29 self._localpart = None |
30 self._gid = 0 |
30 self._gid = 0 |
31 self._aid = 0 |
31 self._isNew = False |
32 self._setAddr(basedir) |
32 self._setAddr() |
33 if not self._dest is None: |
33 if not self._dest is None: |
34 self._exists() |
34 self._exists() |
35 if self._isAccount(): |
35 if self._isAccount(): |
36 raise VMMAliasException( |
36 raise VMMAliasException( |
37 ('There is already an account with address »%s«' % self._addr, |
37 ('There is already an account with address »%s«' % self._addr, |
38 ERR.ACCOUNT_EXISTS)) |
38 ERR.ACCOUNT_EXISTS)) |
39 |
39 |
40 def _exists(self): |
40 def _exists(self): |
41 dbc = self._dbh.cursor() |
41 dbc = self._dbh.cursor() |
42 dbc.execute("SELECT id FROM alias WHERE gid=%s AND address=%s\ |
42 dbc.execute("SELECT gid FROM alias WHERE gid=%s AND address=%s\ |
43 AND destination=%s", self._gid, self._localpart, self._dest) |
43 AND destination=%s", self._gid, self._localpart, self._dest) |
44 aid = dbc.fetchone() |
44 gid = dbc.fetchone() |
45 dbc.close() |
45 dbc.close() |
46 if aid is not None: |
46 if gid is None: |
47 self._aid = aid[0] |
47 self._isNew = True |
48 return True |
|
49 else: |
48 else: |
50 return False |
49 self._isNew = False |
51 |
50 |
52 def _isAccount(self): |
51 def _isAccount(self): |
53 dbc = self._dbh.cursor() |
52 dbc = self._dbh.cursor() |
54 dbc.execute("SELECT uid FROM users WHERE gid=%s AND local_part=%s", |
53 dbc.execute("SELECT uid FROM users WHERE gid=%s AND local_part=%s", |
55 self._gid, self._localpart) |
54 self._gid, self._localpart) |
58 if uid is not None: |
57 if uid is not None: |
59 return True |
58 return True |
60 else: |
59 else: |
61 return False |
60 return False |
62 |
61 |
63 def _setAddr(self, basedir): |
62 def _setAddr(self): |
64 self._localpart, d = self._addr.split('@') |
63 self._localpart, d = self._addr.split('@') |
65 dom = Domain(self._dbh, d, basedir) |
64 dom = Domain(self._dbh, d) |
66 self._gid = dom.getID() |
65 self._gid = dom.getID() |
67 if self._gid == 0: |
66 if self._gid == 0: |
68 raise VMMAliasException(("Domain »%s« doesn't exist." % d, |
67 raise VMMAliasException(("Domain »%s« doesn't exist." % d, |
69 ERR.NO_SUCH_DOMAIN)) |
68 ERR.NO_SUCH_DOMAIN)) |
70 |
69 |
71 def save(self): |
70 def save(self): |
72 if self._dest is None: |
71 if self._dest is None: |
73 raise VMMAliasException(('No destination address for alias denoted.', |
72 raise VMMAliasException(('No destination address for alias denoted.', |
74 ERR.ALIAS_MISSING_DEST)) |
73 ERR.ALIAS_MISSING_DEST)) |
75 if self._aid < 1: |
74 if self._isNew: |
76 dbc = self._dbh.cursor() |
75 dbc = self._dbh.cursor() |
77 dbc.execute("INSERT INTO alias (gid, address, destination) VALUES\ |
76 dbc.execute("INSERT INTO alias (gid, address, destination) VALUES\ |
78 (%s, %s, %s)", self._gid, self._localpart, self._dest) |
77 (%s, %s, %s)", self._gid, self._localpart, self._dest) |
79 self._dbh.commit() |
78 self._dbh.commit() |
80 dbc.close() |
79 dbc.close() |