1#  GNU Mailutils -- a suite of utilities for electronic mail
2#  Copyright (C) 2009-2021 Free Software Foundation, Inc.
3#
4#  This library is free software; you can redistribute it and/or
5#  modify it under the terms of the GNU Lesser General Public
6#  License as published by the Free Software Foundation; either
7#  version 3 of the License, or (at your option) any later version.
8#
9#  This library is distributed in the hope that it will be useful,
10#  but WITHOUT ANY WARRANTY; without even the implied warranty of
11#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12#  Lesser General Public License for more details.
13#
14#  You should have received a copy of the GNU Lesser General
15#  Public License along with this library.  If not, see
16#  <http://www.gnu.org/licenses/>.
17
18from mailutils.c_api import address
19from mailutils.error import AddressError
20
21class Address:
22    def __init__ (self, addr):
23        self.addr = address.AddressType ()
24        if isinstance(addr, list):
25            status = address.createv (self.addr, addr)
26        else:
27            status = address.create (self.addr, addr)
28        if status:
29            raise AddressError (status)
30
31    def __del__ (self):
32        address.destroy (self.addr)
33        del self.addr
34
35    def __len__ (self):
36        return self.get_count ()
37
38    def __str__ (self):
39        status, str = address.to_string (self.addr)
40        if status:
41            raise AddressError (status)
42        return str
43
44    def __iter__ (self):
45        self.__count = 0
46        self.__len = self.get_count ()
47        return self
48
49    def __next__ (self):
50        if self.__count >= self.__len:
51            self.__count = 0
52            raise StopIteration
53        else:
54            self.__count += 1
55            return self.get_email (self.__count)
56
57    def is_group (self, n):
58        """Return True if this address is just the name of a group,
59        False otherwise."""
60        status, isgroup = address.is_group (self.addr, n)
61        if status:
62            raise AddressError (status)
63        return isgroup
64
65    def get_count (self):
66        """Return a count of the addresses in the address list."""
67        return address.get_count (self.addr)
68
69    def get_email (self, n):
70        """Return email part of the Nth email address."""
71        status, email = address.get_email (self.addr, n)
72        if status:
73            raise AddressError (status)
74        return email
75
76    def get_local_part (self, n):
77        """Return local part of the Nth email address."""
78        status, local_part = address.get_local_part (self.addr, n)
79        if status:
80            raise AddressError (status)
81        return local_part
82
83    def get_domain (self, n):
84        """Return domain part of the Nth email address."""
85        status, domain = address.get_domain (self.addr, n)
86        if status:
87            raise AddressError (status)
88        return domain
89
90    def get_personal (self, n):
91        """Return personal part of the Nth email address."""
92        status, personal = address.get_personal (self.addr, n)
93        if status:
94            raise AddressError (status)
95        return personal
96
97    def get_comments (self, n):
98        """Return comment part of the Nth email address."""
99        status, comments = address.get_comments (self.addr, n)
100        if status:
101            raise AddressError (status)
102        return comments
103
104    def get_route (self, n):
105        """Return the route part of the Nth email address."""
106        status, route = address.get_route (self.addr, n)
107        if status:
108            raise AddressError (status)
109        return route
110