Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  
message.h
1/***************************************************************************
2 copyright : (C) 2002-2008 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: message.h,v 1.13 2008-10-07 11:06:25 tat Exp $
6 ***************************************************************************/
7#ifndef _MIMETIC_MESSAGE_H_
8#define _MIMETIC_MESSAGE_H_
9#include <time.h>
10#include <sys/types.h>
11#include <mimetic/libconfig.h>
12#include <mimetic/mimeentity.h>
13#include <mimetic/utils.h>
14#include <mimetic/codec/codec.h>
15#ifdef HAVE_UNISTD_H
16#include <unistd.h>
17#endif
18
19namespace mimetic
20{
21
22class ContentType;
23
24/// Base class for text/* MIME entities.
25struct TextEntity: public MimeEntity
26{
27 /**
28 * Sets its content-type to "text/unknown"
29 */
31 TextEntity(const std::string& text);
32 /**
33 * Sets its content-type to "text/unknown",
34 * sets its body with the "text" variable and adds
35 * "charset=us-ascii" content-type parameter
36 */
37 TextEntity(const std::string& text, const std::string& charset);
38};
39
40/// text/plain entity class
41/*!
42 TextPlain is a MimeEntity that defaults its ContentType to "text/plain"
43 and its charset to "us-ascii".
44 */
45struct TextPlain: public TextEntity
46{
47 TextPlain(const std::string& text);
48 /**
49 * constructs a TextPlain object, assigns <i>text</i> to its body and adds
50 * a ContentType::Param("charset", <i>charset</i>) to ContentType parameter list
51 */
52 TextPlain(const std::string& text, const std::string& charset);
53};
54
55
56/// text/enriched entity class
58{
59 TextEnriched(const std::string& text);
60 /**
61 * constructs a TextPlain object, assigns <i>text</i> to its body and adds
62 * a ContentType::Param("charset", <i>charset</i>) to ContentType parameter list
63 */
64 TextEnriched(const std::string& text, const std::string& charset);
65};
66
67/// Base multipart/* class
68/**
69 Base multipart/ * class. Its constructor sets the content-type
70 to "multipart/unknown" and adds and fills a "boundary" parameter
71 */
73{
75};
76
77/// multipart/mixed entity class
79{
81};
82
83/// multipart/parallel entity class
88
89/// multipart/alternative entity class
94
95/// multipart/digest entity class
97{
99};
100
101
102/// application/octet-stream entity class
104{
106 template<typename Codec>
107 ApplicationOctStream(const std::string&, const Codec& c=Base64::Encoder());
108 std::string type() const;
109 void type(const std::string&);
110 uint padding() const;
111 void padding(unsigned int);
112 bool operator()() const { return isValid(); }
113 bool isValid() const { return m_status; }
114protected:
115 std::string m_fqn;
116 bool m_status;
117};
118
119/// Helper class to embed file attachments
120struct Attachment: public MimeEntity
121{
122 /**
123 * defaults to application/octet-stream
124 */
125 Attachment(const std::string&);
126 Attachment(const std::string&, const ContentType&);
127 template<typename Codec>
128 Attachment(const std::string&, const Codec& c );
129 template<typename Codec>
130 Attachment(const std::string&, const ContentType&, const Codec& c);
131 bool operator()() const { return isValid(); }
132 bool isValid() const { return m_status; }
133private:
134 template<typename Codec>
135 void set(const std::string&, const ContentType&, const Codec& c);
136 std::string m_fqn;
137 bool m_status;
138};
139
140/// image/jpeg attachment
141struct ImageJpeg: public Attachment
142{
143 ImageJpeg(const std::string& fqn)
144 : Attachment(fqn, ContentType("image","jpeg"))
145 {
146 }
147 template<typename Codec>
148 ImageJpeg(const std::string& fqn, const Codec& c)
149 : Attachment(fqn, ContentType("image","jpeg"), c)
150 {
151 }
152};
153
154/// audio/basic attachment
155struct AudioBasic: public Attachment
156{
157 AudioBasic(const std::string& fqn)
158 : Attachment(fqn, ContentType("audio","basic"))
159 {
160 }
161 template<typename Codec>
162 AudioBasic(const std::string& fqn, const Codec& c)
163 : Attachment(fqn, ContentType("audio","basic"), c)
164 {
165 }
166};
167
168
169
170/// message/rfc822 entity type
172{
174protected:
175 std::ostream& write(std::ostream&,const char*) const;
176private:
177 const MimeEntity& m_me;
178};
179
180
181
182/**
183 * defaults to application/octet-stream
184 */
185template<typename Codec>
186Attachment::Attachment(const std::string& fqn, const Codec& codec)
187{
188 set(fqn, ContentType("application","octet-stream"), codec);
189}
190
191template<typename Codec>
192Attachment::Attachment(const std::string& fqn, const ContentType& ctype, const Codec& codec)
193{
194 set(fqn, ctype, codec);
195}
196
197template<typename Codec>
198void Attachment::set(const std::string& fqn, const ContentType& ctype, const Codec& codec)
199{
200 Header& h = header();
201 m_fqn = fqn;
202 m_status = false;
203 std::string filename = utils::extractFilename(m_fqn);
204 // Content-Type
205 h.contentType(ctype);
206 h.contentType().paramList().push_back(ContentType::Param("name", filename));
207
208 // Content-Transfer-Encoding
209 h.contentTransferEncoding().mechanism(codec.name());
210
211 // Content-Disposition
212 h.contentDisposition().type("attachment");
213 h.contentDisposition().paramList().push_back(ContentDisposition::Param("filename", filename));
214
215 m_status = body().load(m_fqn, codec);
216}
217
218
219template<typename Codec>
220ApplicationOctStream::ApplicationOctStream(const std::string& fqn, const Codec& codec)
221{
222 Header& h = header();
223 m_fqn = fqn;
224 m_status = false;
225 std::string filename = utils::extractFilename(m_fqn);
226 // Content-Type
227 h.contentType(ContentType("application", "octet-stream"));
228 h.contentType().paramList().push_back(ContentType::Param("name", filename));
229
230 // Content-Transfer-Encoding
231 h.contentTransferEncoding().mechanism(codec.name());
232
233 // Content-Disposition
234 h.contentDisposition().type("attachment");
235 h.contentDisposition().paramList().push_back(ContentDisposition::Param("filename", filename));
236
237 m_status = body().load(m_fqn, codec);
238}
239
240}
241
242
243#endif
244
Base64 encoder.
Definition base64.h:38
bool load(const std::string &)
Content-Type field value.
Definition contenttype.h:19
Represent a MIME entity
Definition mimeentity.h:38
Definition body.h:18
application/octet-stream entity class
Definition message.h:104
Helper class to embed file attachments.
Definition message.h:121
Attachment(const std::string &)
audio/basic attachment
Definition message.h:156
image/jpeg attachment
Definition message.h:142
message/rfc822 entity type
Definition message.h:172
multipart/alternative entity class
Definition message.h:91
multipart/digest entity class
Definition message.h:97
Base multipart/* class.
Definition message.h:73
multipart/mixed entity class
Definition message.h:79
multipart/parallel entity class
Definition message.h:85
text/enriched entity class
Definition message.h:58
TextEnriched(const std::string &text, const std::string &charset)
Base class for text/* MIME entities.
Definition message.h:26
TextEntity(const std::string &text, const std::string &charset)
text/plain entity class
Definition message.h:46
TextPlain(const std::string &text, const std::string &charset)
Codecs base class.
Definition codec_base.h:24