Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  

message.h

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