File: rfcaddr.C

package info (click to toggle)
cone 0.75-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 31,040 kB
  • ctags: 13,930
  • sloc: ansic: 90,648; cpp: 79,781; sh: 18,355; perl: 3,218; makefile: 1,611; yacc: 289; sed: 16
file content (294 lines) | stat: -rw-r--r-- 4,994 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* $Id: rfcaddr.C,v 1.5 2004/05/30 02:43:00 mrsam Exp $
**
** Copyright 2002-2004, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "libmail_config.h"
#include <ctype.h>
#include <string.h>
#include "rfcaddr.H"
#include "mail.H"
#include "misc.H"
#include "rfc2047encode.H"
#include "rfc2047decode.H"
#include "rfc822/rfc822.h"
#include "unicode/unicode.h"

using namespace std;

mail::address::address(string n, string a): name(n), addr(a)
{
}

mail::address::~address()
{
}

string mail::address::toString() const
{
	if (addr.length() == 0)
		return name;

	string q=addr;

	// Only add < > if there's a name

	if (name.length() > 0)
	{
		q=" <" + q + ">";

		// If name is all alphabetics, don't bother with quotes.

		string::const_iterator b=name.begin(), e=name.end();

		while (b != e)
		{
			if (!strchr(" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789?*+-/=_",
				    *b))
			{
				string s="\"";

				b=name.begin(), e=name.end();

				while (b != e)
				{
					if (strchr("\\\"()<>", *b))
						s += "\\";
					// These chars must be escaped.

					s.append(&*b, 1);
					b++;
				}
				return s + "\"" + q;
			}
			b++;
		}
		q=name + q;
	}
	return q;
}

template<class T>
string mail::address::toString(string hdr, const vector<T> &h,
			       size_t w)
{
	int len=hdr.length();
	string comma="";
	bool first=true;

	string spc="";

	spc.insert(spc.end(), len < 20 ? len:20, ' ');
	// Leading whitespace for folded lines.

	typename std::vector<T>::const_iterator b=h.begin(), e=h.end();

	while (b != e)
	{
		string s= b->toString();

		if (!first && len + comma.length() + s.length() > w)
		{
			// Line too long, wrap it.

			if (comma.size() > 0)
				comma=",";

			hdr=hdr + comma + "\n" + spc;
			len=spc.size();
			comma="";
		}
		first=false;
		hdr = hdr + comma + s;

		len += comma.length() + s.length();
		comma=", ";

		if (b->addr.length() == 0)
			comma="";
		b++;
	}
	return hdr;
}

template
string mail::address::toString(string hdr, const vector<mail::address> &h,
			       size_t w);

template
string mail::address::toString(string hdr,
			       const vector<mail::emailAddress> &h,
			       size_t w);

string mail::address::getCanonAddress() const
{
	string a=addr;

	size_t n=a.find_last_of('@');

	if (n < a.size())
	{
		char *p=
			(*unicode_ISO8859_1.tolower_func)(&unicode_ISO8859_1,
							  a.c_str() + n, NULL);

		if (!p)
			LIBMAIL_THROW("Out of memory.");

		try {
			a=a.substr(0, n) + p;
			free(p);
		} catch (...) {
			free(p);
			LIBMAIL_THROW();
		}
	}

	return a;
}

bool mail::address::operator==(const mail::address &o) const
{
	return getCanonAddress() == o.getCanonAddress();
}

static void parseCallback(const char *str, int pos, void *voidarg)
{
	*(size_t *)voidarg=pos;
}

template<class T> bool mail::address::fromString(string addresses,
						 vector<T> &h,
						 size_t &errIndex)
{
	errIndex=addresses.npos;

	struct rfc822t *t=rfc822t_alloc_new(addresses.c_str(),
					    &parseCallback, &errIndex);
	if (!t)
		return false;

	struct rfc822a *a=rfc822a_alloc(t);

	if (!a)
	{
		rfc822t_free(t);
		return false;
	}

	try {
		int i;

		for (i=0; i<a->naddrs; i++)
		{
			string name;
			string addr;

			char *n=rfc822_getname_orlist(a, i);

			if (!n)
			{
				rfc822a_free(a);
				rfc822t_free(t);
				return false;
			}

			try {
				name=n;
				free(n);
			} catch (...) {
				free(n);
				LIBMAIL_THROW();
			}


			n=rfc822_getaddr(a, i);

			if (!n)
			{
				rfc822a_free(a);
				rfc822t_free(t);
				return false;
			}

			try {
				addr=n;
				free(n);
			} catch (...) {
				free(n);
				LIBMAIL_THROW();
			}

			if (a->addrs[i].name == 0)
				name="";

			h.push_back( mail::address(name, addr));
		}
		rfc822a_free(a);
		rfc822t_free(t);
	} catch (...) {
		rfc822a_free(a);
		rfc822t_free(t);
		LIBMAIL_THROW();
	}
	return true;
}

template
bool mail::address::fromString(string addresses,
			       vector<mail::address> &h,
			       size_t &errIndex);

template
bool mail::address::fromString(string addresses,
			       vector<mail::emailAddress> &h,
			       size_t &errIndex);


void mail::address::setName(std::string s)
{
	name=s;
}

/////////////////////////////////////////////////////////////////////////

mail::emailAddress::emailAddress(string n, string a)
	: mail::address(n, a)
{
	setAddrName(n);
}

mail::emailAddress::~emailAddress()
{
}

mail::emailAddress::emailAddress(const mail::address &a)
	: mail::address(a.getName(), a.getAddr())
{
	decode();
}

void mail::emailAddress::setAddrName(string s)
{
	encodedName=s;
	if (appcharset == NULL)
		appcharset=unicode_find(unicode_default_chset());

	name=mail::rfc2047::encodeAddrName(s, appcharset->chset);
}

void mail::emailAddress::setName(string s)
{
	mail::address::setName(s);
	decode();
}

void mail::emailAddress::decode()
{
	if (appcharset == NULL)
		appcharset=unicode_find(unicode_default_chset());
	encodedName=mail::rfc2047::decoder::decodeEnhanced(name,
							   *appcharset);
}