1 /*
2 ** Copyright 2002-20011, Double Precision Inc.
3 **
4 ** See COPYING for distribution information.
5 */
6 #ifndef libmail_rfcaddr_h
7 #define libmail_rfcaddr_h
8 
9 #include "libmail_config.h"
10 #include <courier-unicode.h>
11 #include "namespace.H"
12 
13 #include <stdlib.h>
14 #include <vector>
15 #include <string>
16 
17 //
18 // An RFC 2822 address, a name and user@domain.
19 //
20 
21 LIBMAIL_START
22 
23 class address {
24 
25 protected:
26 	std::string name;
27 	std::string addr;
28 public:
29 
getName()30 	std::string getName() const { return name; }
getAddr()31 	std::string getAddr() const { return addr; }
32 
33 	virtual void setName(std::string s);
34 	virtual void setAddr(std::string a);
35 
36 	// If addr.size() == 0 this is the obsolete rfc 822
37 	// group list notation, and name is either ";" or "name:"
38 
39 	address(std::string n, std::string a);
40 
41 	virtual ~address();
42 
43 	// Convert myself to "name" <addr> format.
44 
45 	std::string toString() const;
46 
47 	//
48 	// Create an RFC 2822 address header, from a vector of addresss.
49 	//
50 
51 	template<class T>
52 	static std::string toString(std::string hdr, // Name:
53 				    const std::vector<T> &h,
54 				    size_t w=76); // Max length of lines.
55 
56 	//
57 	// Create a vector of addresses from a string
58 	//
59 
60 	template<class T> static bool fromString(std::string addresses,
61 						 std::vector<T> &h,
62 						 size_t &errIndex);
63 
64 	// Addresses are appended to h
65 	// Returns false if memory allocation failed or addresses is
66 	// syntactically invalid.  errIndex points where in addresses the
67 	// syntax error is (or is string::npos if memory allocation failed)
68 
69 	std::string getCanonAddress() const;
70 	// Return addr with domain portion converted to lowercase
71 
72 	bool operator==(const address &) const;
73 	// Compare the addr portions only, ignoring domain case
74 
75 	bool operator!=(const address &a) const
76 	{
77 		return !operator==(a);
78 	}
79 
80 };
81 
82 // INPROGRESS: slowly migrate to automatic MIME encoding of names.
83 //
84 // A subclass of address that MIME-decodes the name portion of the address,
85 // and IDN-decodes the hostname portion of the address (if libidn is available).
86 //
87 // emailAddress transparently casts to mail::address, importing/exporting
88 // the name and address in its decoded form.
89 
90 class emailAddress : public address {
91 
92 	// The name field as unicode characters
93 
94 	std::u32string decodedName;
95 
96 	//! The address field, with the hostname portion decoded
97 
98 	std::u32string decodedAddr;
99 
100 	void decode();
101 public:
102 	emailAddress();
103 	emailAddress(const address &);
104 	virtual ~emailAddress();
105 
getDisplayName(const std::string & charset,bool & errflag)106 	std::string getDisplayName(const std::string &charset,
107 				   bool &errflag) const
108 	{
109 		return unicode::iconvert::convert(decodedName, charset, errflag);
110 	}
111 
getDisplayAddr(const std::string & charset,bool & errflag)112 	std::string getDisplayAddr(const std::string &charset,
113 				   bool &errflag) const
114 	{
115 		return unicode::iconvert::convert(decodedAddr, charset, errflag);
116 	}
117 
getDisplayName(const std::string & charset)118 	std::string getDisplayName(const std::string &charset) const
119 	{
120 		bool dummy;
121 
122 		return getDisplayName(charset, dummy);
123 	}
124 
getDisplayAddr(const std::string & charset)125 	std::string getDisplayAddr(const std::string &charset) const
126 	{
127 		bool dummy;
128 
129 		return getDisplayAddr(charset, dummy);
130 	}
131 
132 	std::string setDisplayName(const std::string &s,
133 				   const std::string &charset);
134 	std::string setDisplayAddr(const std::string &a,
135 				   const std::string &charset);
136 
137 	virtual void setName(std::string s);
138 	virtual void setAddr(std::string s);
139 
140 	bool operator==(const address &a) const
141 	{
142 		return mail::address::operator==(a);
143 	}
144 
145 	bool operator!=(const address &a) const
146 	{
147 		return !operator==(a);
148 	}
149 
150 };
151 
152 LIBMAIL_END
153 
154 #endif
155