Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  

message.h

00001 /***************************************************************************
00002     copyright            : (C) 2002-2008 by Stefano Barbato
00003     email                : stefano@codesink.org
00004 
00005     $Id: message.h,v 1.13 2008-10-07 11:06:25 tat Exp $
00006  ***************************************************************************/
00007 #ifndef _MIMETIC_MESSAGE_H_
00008 #define _MIMETIC_MESSAGE_H_
00009 #include <time.h>
00010 #include <sys/types.h>
00011 #include <mimetic/libconfig.h>
00012 #include <mimetic/mimeentity.h>
00013 #include <mimetic/utils.h>
00014 #include <mimetic/codec/codec.h>
00015 #ifdef HAVE_UNISTD_H
00016 #include <unistd.h>
00017 #endif
00018 
00019 namespace mimetic
00020 {
00021 
00022 class ContentType;
00023 
00024 /// Base class for text/* MIME entities.
00025 struct TextEntity: public MimeEntity
00026 {
00027     /**
00028      *  Sets its content-type to "text/unknown"
00029      */
00030     TextEntity();
00031     TextEntity(const std::string& text);
00032     /**
00033      *  Sets its content-type to "text/unknown",
00034      *  sets its body with the "text" variable and adds
00035      *  "charset=us-ascii" content-type parameter
00036      */
00037     TextEntity(const std::string& text, const std::string& charset);
00038 };
00039 
00040 /// text/plain entity class
00041 /*!
00042     TextPlain is a MimeEntity that defaults its ContentType to "text/plain" 
00043     and its charset to "us-ascii".
00044  */
00045 struct TextPlain: public TextEntity
00046 {
00047     TextPlain(const std::string& text);
00048     /**
00049      * constructs a TextPlain object, assigns <i>text</i> to its body and adds
00050      * a ContentType::Param("charset", <i>charset</i>) to ContentType parameter list
00051      */
00052     TextPlain(const std::string& text, const std::string& charset);
00053 };
00054 
00055 
00056 /// text/enriched entity class
00057 struct TextEnriched: public TextEntity
00058 {
00059     TextEnriched(const std::string& text);
00060     /**
00061      * constructs a TextPlain object, assigns <i>text</i> to its body and adds
00062      * a ContentType::Param("charset", <i>charset</i>) to ContentType parameter list
00063      */
00064     TextEnriched(const std::string& text, const std::string& charset);
00065 };
00066 
00067 /// Base multipart/* class
00068 /**
00069    Base multipart/ * class. Its constructor sets the content-type
00070    to "multipart/unknown" and adds and fills a "boundary" parameter
00071  */
00072 struct MultipartEntity: public MimeEntity
00073 {
00074     MultipartEntity();
00075 };
00076 
00077 /// multipart/mixed entity class
00078 struct MultipartMixed: public MultipartEntity
00079 {
00080     MultipartMixed();
00081 };
00082 
00083 /// multipart/parallel entity class
00084 struct MultipartParallel: public MultipartEntity
00085 {
00086     MultipartParallel();
00087 };
00088 
00089 /// multipart/alternative entity class
00090 struct MultipartAlternative: public MultipartEntity
00091 {
00092     MultipartAlternative();
00093 };
00094 
00095 /// multipart/digest entity class
00096 struct MultipartDigest: public MultipartEntity
00097 {
00098     MultipartDigest();
00099 };
00100 
00101 
00102 /// application/octet-stream entity class
00103 struct ApplicationOctStream: public MimeEntity
00104 {
00105     ApplicationOctStream();
00106     template<typename Codec>
00107     ApplicationOctStream(const std::string&, const Codec& c=Base64::Encoder());
00108     std::string type() const;
00109     void type(const std::string&);
00110     uint padding() const;
00111     void padding(unsigned int);
00112     bool operator()() const { return isValid(); }
00113     bool isValid() const { return m_status; }
00114 protected:
00115     std::string m_fqn;
00116     bool m_status;
00117 };
00118 
00119 /// Helper class to embed file attachments
00120 struct Attachment: public MimeEntity
00121 {
00122     /**
00123      * defaults to application/octet-stream
00124      */
00125     Attachment(const std::string&);
00126     Attachment(const std::string&, const ContentType&);
00127     template<typename Codec>
00128     Attachment(const std::string&, const Codec& c );
00129     template<typename Codec>
00130     Attachment(const std::string&, const ContentType&, const Codec& c);
00131     bool operator()() const { return isValid(); }
00132     bool isValid() const { return m_status; }
00133 private:
00134     template<typename Codec>
00135     void set(const std::string&, const ContentType&, const Codec& c);
00136     std::string m_fqn;
00137     bool m_status;
00138 };
00139 
00140 /// image/jpeg attachment
00141 struct ImageJpeg: public Attachment
00142 {
00143     ImageJpeg(const std::string& fqn)
00144     : Attachment(fqn, ContentType("image","jpeg"))
00145     {
00146     }
00147     template<typename Codec>
00148     ImageJpeg(const std::string& fqn, const Codec& c)
00149     : Attachment(fqn, ContentType("image","jpeg"), c)
00150     {
00151     }
00152 };
00153 
00154 /// audio/basic attachment
00155 struct AudioBasic: public Attachment
00156 {
00157     AudioBasic(const std::string& fqn)
00158     : Attachment(fqn, ContentType("audio","basic"))
00159     {
00160     }
00161     template<typename Codec>
00162     AudioBasic(const std::string& fqn, const Codec& c)
00163     : Attachment(fqn, ContentType("audio","basic"), c)
00164     {
00165     }
00166 };
00167 
00168 
00169 
00170 /// message/rfc822 entity type
00171 struct MessageRfc822: public MimeEntity
00172 {
00173     MessageRfc822(const MimeEntity&);
00174 protected:
00175     std::ostream& write(std::ostream&,const char*) const;
00176 private:
00177     const MimeEntity& m_me;
00178 };
00179 
00180 
00181 
00182 /**
00183  * defaults to application/octet-stream
00184  */
00185 template<typename Codec>
00186 Attachment::Attachment(const std::string& fqn, const Codec& codec)
00187 {
00188     set(fqn, ContentType("application","octet-stream"), codec);
00189 }
00190 
00191 template<typename Codec>
00192 Attachment::Attachment(const std::string& fqn, const ContentType& ctype, const Codec& codec)
00193 {
00194     set(fqn, ctype, codec);
00195 }
00196 
00197 template<typename Codec>
00198 void Attachment::set(const std::string& fqn, const ContentType& ctype, const Codec& codec)
00199 {
00200     Header& h = header();
00201     m_fqn = fqn;
00202     m_status = false;
00203     std::string filename = utils::extractFilename(m_fqn);
00204     // Content-Type
00205     h.contentType(ctype);
00206     h.contentType().paramList().push_back(ContentType::Param("name", filename));
00207     
00208     // Content-Transfer-Encoding
00209     h.contentTransferEncoding().mechanism(codec.name());
00210 
00211     // Content-Disposition
00212     h.contentDisposition().type("attachment");
00213     h.contentDisposition().paramList().push_back(ContentDisposition::Param("filename", filename));
00214     
00215     m_status = body().load(m_fqn, codec);
00216 }
00217 
00218     
00219 template<typename Codec>
00220 ApplicationOctStream::ApplicationOctStream(const std::string& fqn, const Codec& codec)
00221 {
00222     Header& h = header();
00223     m_fqn = fqn;
00224     m_status = false;
00225     std::string filename = utils::extractFilename(m_fqn);
00226     // Content-Type
00227     h.contentType(ContentType("application", "octet-stream"));
00228     h.contentType().paramList().push_back(ContentType::Param("name", filename));
00229     
00230     // Content-Transfer-Encoding
00231     h.contentTransferEncoding().mechanism(codec.name());
00232 
00233     // Content-Disposition
00234     h.contentDisposition().type("attachment");
00235     h.contentDisposition().paramList().push_back(ContentDisposition::Param("filename", filename));
00236     
00237     m_status = body().load(m_fqn, codec);
00238 }
00239 
00240 }
00241 
00242 
00243 #endif
00244