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
|
//
// VMime library (http://vmime.sourceforge.net)
// Copyright (C) 2002-2004 Vincent Richard <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#include "headerField.hpp"
#include "headerFieldFactory.hpp"
namespace vmime
{
headerField::headerField()
: m_type(Custom), m_name("Undefined")
{
}
headerField::headerField(const string& fieldName)
: m_type(Custom), m_name(fieldName)
{
}
headerField::~headerField()
{
}
headerField* headerField::clone() const
{
headerField* field = NULL;
if (m_type == Custom)
field = headerFieldFactory::getInstance()->create(m_name);
else
field = headerFieldFactory::getInstance()->create(m_type);
field->copyFrom(*this);
return (field);
}
const bool headerField::operator<(const headerField& field) const
{
return (m_type < field.m_type);
}
headerField& headerField::operator=(const headerField& field)
{
copyFrom(field);
return (*this);
}
void headerField::copyFrom(const headerField& field)
{
m_type = field.m_type;
m_name = field.m_name;
}
void headerField::generate(utility::outputStream& os, const string::size_type /* maxLineLength */,
const string::size_type curLinePos, string::size_type* newLinePos) const
{
if (m_type == Custom)
{
os << m_name + ": ";
if (newLinePos)
*newLinePos = curLinePos + m_name.length() + 2;
}
else
{
const string name = typeToName(m_type);
os << name + ": ";
if (newLinePos)
*newLinePos = curLinePos + name.length() + 2;
}
}
/** Return the field type corresponding to the specified name.
*
* @param name field name
* @return field type (see headerField::Types) or headerField::custom
* if this is a custom field
*/
const headerField::Types headerField::nameToType(const string& name)
{
switch (name[0])
{
case 'B':
case 'b':
{
if (isStringEqualNoCase(name, "bcc", 3)) return (Bcc);
break;
}
case 'C':
case 'c':
{
if (isStringEqualNoCase(name, "cc", 2)) return (Cc);
else if (isStringEqualNoCase(name, "content-type", 12)) return (ContentType);
else if (isStringEqualNoCase(name, "content-transfer-encoding", 25)) return (ContentTransferEncoding);
else if (isStringEqualNoCase(name, "content-description", 19)) return (ContentDescription);
else if (isStringEqualNoCase(name, "content-disposition", 19)) return (ContentDisposition);
else if (isStringEqualNoCase(name, "content-id", 10)) return (ContentId);
else if (isStringEqualNoCase(name, "content-location", 16)) return (ContentLocation);
break;
}
case 'd':
case 'D':
{
if (isStringEqualNoCase(name, "date", 4)) return (Date);
else if (isStringEqualNoCase(name, "delivered-to", 12)) return (DeliveredTo);
break;
}
case 'f':
case 'F':
{
if (isStringEqualNoCase(name, "from", 4)) return (From);
break;
}
case 'm':
case 'M':
{
if (isStringEqualNoCase(name, "mime-version", 12)) return (MimeVersion);
else if (isStringEqualNoCase(name, "message-id", 10)) return (MessageId);
break;
}
case 'o':
case 'O':
{
if (isStringEqualNoCase(name, "organization", 12)) return (Organization);
break;
}
case 'r':
case 'R':
{
if (isStringEqualNoCase(name, "received", 8)) return (Received);
else if (isStringEqualNoCase(name, "reply-to", 8)) return (ReplyTo);
else if (isStringEqualNoCase(name, "return-path", 11)) return (ReturnPath);
break;
}
case 's':
case 'S':
{
if (isStringEqualNoCase(name, "sender", 6)) return (Sender);
else if (isStringEqualNoCase(name, "subject", 7)) return (Subject);
break;
}
case 't':
case 'T':
{
if (isStringEqualNoCase(name, "to", 2)) return (To);
break;
}
case 'u':
case 'U':
{
if (isStringEqualNoCase(name, "user-agent", 10)) return (UserAgent);
break;
}
}
return (Custom);
}
/** Return the name for the specified field type.
* Eg: returns "From" for headerField::From.
*
* @param type field type
* @return name for the specified field type
*/
const string headerField::typeToName(const Types type)
{
switch (type)
{
case From: return "From";
case Sender: return "Sender";
case To: return "To";
case Cc: return "Cc";
case Bcc: return "Bcc";
case Date: return "Date";
case Received: return "Received";
case Subject: return "Subject";
case ReplyTo: return "Reply-To";
case Organization: return "Organization";
case DeliveredTo: return "Delivered-To";
case UserAgent: return "User-Agent";
case ReturnPath: return "Return-Path";
case ContentType: return "Content-Type";
case ContentTransferEncoding: return "Content-Transfer-Encoding";
case ContentDescription: return "Content-Description";
case MimeVersion: return "Mime-Version";
case ContentDisposition: return "Content-Disposition";
case ContentId: return "Content-Id";
case MessageId: return "Message-Id";
case ContentLocation: return "Content-Location";
case Custom:
case Last:
return "?";
};
return "?";
}
/** Return the type of this field.
*
* @return field type (see headerField::Types)
*/
const headerField::Types headerField::type() const
{
return (m_type);
}
/** Return the name of this field.
*
* @return field name
*/
const string headerField::name() const
{
return ((m_type == Custom) ? m_name : typeToName(m_type));
}
/** Check whether this field is a custom field.
*
* @return true if the field is a custom field, false otherwise
*/
const bool headerField::isCustom() const
{
return (m_type == Custom);
}
} // vmime
|