1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 2004-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 
18 #include <mailutils/cpp/address.h>
19 
20 using namespace mailutils;
21 
22 //
23 // Address
24 //
25 
Address()26 Address :: Address ()
27 {
28   addr = NULL;
29 }
30 
Address(const std::string & str)31 Address :: Address (const std::string& str)
32 {
33   int status = mu_address_create (&addr, str.c_str ());
34   if (status)
35     throw Exception ("Address::Address", status);
36 }
37 
Address(const char * sv[],size_t len)38 Address :: Address (const char *sv[], size_t len)
39 {
40   int status = mu_address_createv (&addr, sv, len);
41   if (status)
42     throw Exception ("Address::Address", status);
43 }
44 
Address(const mu_address_t addr)45 Address :: Address (const mu_address_t addr)
46 {
47   if (addr == 0)
48     throw Exception ("Address::Address", EINVAL);
49 
50   this->addr = addr;
51 }
52 
~Address()53 Address :: ~Address ()
54 {
55   if (addr)
56     mu_address_destroy (&addr);
57 }
58 
59 Address&
operator =(const Address & a)60 Address :: operator = (const Address& a)
61 {
62   if (this != &a)
63     {
64       if (this->addr)
65 	mu_address_destroy (&this->addr);
66       this->addr = mu_address_dup (a.addr);
67     }
68   return *this;
69 }
70 
71 bool
is_group(size_t n)72 Address :: is_group (size_t n)
73 {
74   int isgroup;
75   int status = mu_address_is_group (addr, n, &isgroup);
76   if (status == EINVAL)
77     throw Address::EInval ("Address::is_group", status);
78   else if (status == ENOENT)
79     throw Address::ENoent ("Address::is_group", status);
80 
81   return (bool) isgroup;
82 }
83 
84 size_t
get_count()85 Address :: get_count ()
86 {
87   size_t count;
88   mu_address_get_count (addr, &count);
89   return count;
90 }
91 
92 std::string
get_email(size_t n)93 Address :: get_email (size_t n)
94 {
95   const char* buf = NULL;
96   int status = mu_address_sget_email (addr, n, &buf);
97   if (status == EINVAL)
98     throw Address::EInval ("Address::get_email", status);
99   else if (status == ENOENT)
100     throw Address::ENoent ("Address::get_email", status);
101 
102   return std::string (mu_prstr (buf));
103 }
104 
105 std::string
get_local_part(size_t n)106 Address :: get_local_part (size_t n)
107 {
108   const char* buf = NULL;
109   int status = mu_address_sget_local_part (addr, n, &buf);
110   if (status == EINVAL)
111     throw Address::EInval ("Address::get_local_part", status);
112   else if (status == ENOENT)
113     throw Address::ENoent ("Address::get_local_part", status);
114 
115   return std::string (mu_prstr (buf));
116 }
117 
118 std::string
get_domain(size_t n)119 Address :: get_domain (size_t n)
120 {
121   const char* buf = NULL;
122   int status = mu_address_sget_domain (addr, n, &buf);
123   if (status == EINVAL)
124     throw Address::EInval ("Address::get_domain", status);
125   else if (status == ENOENT)
126     throw Address::ENoent ("Address::get_domain", status);
127 
128   return std::string (mu_prstr (buf));
129 }
130 
131 std::string
get_personal(size_t n)132 Address :: get_personal (size_t n)
133 {
134   const char* buf = NULL;
135   int status = mu_address_sget_personal (addr, n, &buf);
136   if (status == EINVAL)
137     throw Address::EInval ("Address::get_personal", status);
138   else if (status == ENOENT)
139     throw Address::ENoent ("Address::get_personal", status);
140 
141   return std::string (mu_prstr (buf));
142 }
143 
144 std::string
get_comments(size_t n)145 Address :: get_comments (size_t n)
146 {
147   const char* buf = NULL;
148   int status = mu_address_sget_comments (addr, n, &buf);
149   if (status == EINVAL)
150     throw Address::EInval ("Address::get_comments", status);
151   else if (status == ENOENT)
152     throw Address::ENoent ("Address::get_comments", status);
153 
154   return std::string (mu_prstr (buf));
155 }
156 
157 std::string
get_route(size_t n)158 Address :: get_route (size_t n)
159 {
160   const char* buf = NULL;
161   int status = mu_address_sget_route (addr, n, &buf);
162   if (status == EINVAL)
163     throw Address::EInval ("Address::get_route", status);
164   else if (status == ENOENT)
165     throw Address::ENoent ("Address::get_route", status);
166 
167   return std::string (mu_prstr (buf));
168 }
169 
170 std::string
to_string()171 Address :: to_string ()
172 {
173   size_t n;
174   char const *sptr;
175 
176   int status = mu_address_sget_printable (addr, &sptr);
177   if (status)
178     throw Exception ("Address::to_string", status);
179 
180   return std::string (sptr);
181 }
182 
183 namespace mailutils
184 {
operator <<(std::ostream & os,Address & addr)185   std::ostream& operator << (std::ostream& os, Address& addr) {
186     return os << addr.to_string ();
187   };
188 }
189 
190