00001 #ifndef _WRITEMIME_H_
00002 #define _WRITEMIME_H_
00003
00004
00005 #include <list>
00006 #include <string>
00007
00008 #ifndef NO_NAMESPACES
00009 using std::list;
00010 using std::string;
00011 #endif // NO_NAMESPACES
00012
00014 namespace WriteMime
00015 {
00016
00017
00018 class HeaderFieldParam {
00019 char *m_name;
00020 char *m_value;
00021 int m_size;
00022 void reset() {
00023 if (m_name) {free(m_name); m_name = 0;}
00024 if (m_value) {free(m_value); m_value = 0;}
00025 m_size = 0;
00026 }
00027 void buildFromFriend(const HeaderFieldParam& rh);
00028
00029 public:
00030 HeaderFieldParam(const char *nm, const char *value)
00031 : m_name(strdup(nm)), m_value(strdup(value)), m_size(0) {}
00032 HeaderFieldParam(const HeaderFieldParam& rh) {buildFromFriend(rh);}
00033 ~HeaderFieldParam() {reset();}
00034 HeaderFieldParam& operator=(const HeaderFieldParam& l);
00035
00036
00037 int size();
00038
00039 int print(char *d);
00040 };
00041
00042
00043 class HeaderField {
00044 char *m_name;
00045 char *m_value;
00046 int m_size;
00047 list<HeaderFieldParam> m_params;
00048
00049 void reset() {
00050 if (m_name) {free(m_name); m_name = 0;}
00051 if (m_value) {free(m_value); m_value = 0;}
00052 m_size = 0;
00053 }
00054 void buildFromFriend(const HeaderField &rh);
00055 public:
00056 HeaderField(const char *nm, const char *value)
00057 : m_name(strdup(nm)), m_value(strdup(value)), m_size(0) {}
00058 HeaderField(const HeaderField &rh) {buildFromFriend(rh);}
00059 ~HeaderField() {reset();}
00060 HeaderField &operator=(const HeaderField &rh);
00061 int addparam(const char *pnm, const char *pval);
00062
00063 int size();
00064
00065 int print(char *d);
00066 };
00067
00068
00069
00071 class Entity {
00072 public:
00074 enum type {TEXT,IMAGE,AUDIO,VIDEO,APPLICATION,MULTIPART,MESSAGE};
00077 enum disposition {DISP_ATTACHMENT, DISP_INLINE};
00079 enum encoding {ENC_7BIT, ENC_8BIT, ENC_BINARY, ENC_QP, ENC_BASE64};
00080
00081 private:
00082 char *m_subtype;
00083 char *m_error;
00084
00085 protected:
00086 type m_type;
00087 encoding m_encoding;
00088 list<HeaderField> m_hf;
00089 int m_size;
00090 char *m_fmt;
00091
00092 public:
00093 Entity() : m_subtype(strdup("plain")), m_error(0),
00094 m_type(TEXT), m_encoding(ENC_BINARY), m_size(0), m_fmt(0) {}
00095 virtual ~Entity() {
00096 if (m_subtype) {free(m_subtype); m_subtype = 0;}
00097 if (m_error) {free(m_error); m_error = 0;}
00098 if (m_fmt) {free(m_fmt); m_fmt = 0;}
00099 }
00100
00102 virtual void seterror(const char *s);
00104 virtual const char *geterror() {return m_error;}
00106 virtual encoding getencoding() {return m_encoding;}
00107
00109 virtual void addHeaderField(const HeaderField &f);
00110 virtual int size() = 0;
00111 virtual const char *format() = 0;
00112 virtual int format(char *buf) = 0;
00113
00114 protected:
00115 virtual int addCteHeader();
00116 virtual int allocFmt();
00117 virtual int formatHeaders(char *buf);
00118 virtual int formatHeaders();
00119 virtual void settype(type tp) {m_type = tp;}
00120 virtual int setsubtype(const char *stp);
00121 virtual const char *getsubtype() {return m_subtype;}
00122 };
00123
00127 class DiscreteEntity : public virtual Entity {
00128 char *m_body;
00129 int m_blen;
00130 encoding m_textencodepref;
00131 string m_charset;
00132
00133 int addCtpHeader();
00134
00135 public:
00136 DiscreteEntity() : m_body(0), m_blen(0), m_charset("") {}
00137 virtual ~DiscreteEntity() {if (m_body) {free(m_body); m_body = 0;}}
00138
00140 virtual void setTextEncodePref(encoding ec);
00141
00143 virtual void setCharSet(const char *cs) {m_charset = cs;}
00144
00158 virtual int setBody(const char *bp, int blen, type tp = TEXT,
00159 const char *stp = "plain");
00160
00166 virtual int setBodyAttach(const char *filename, type tp = APPLICATION,
00167 const char *stp = "octet-stream");
00169 virtual int size();
00171 virtual const char *format();
00173 virtual int format(char *buf);
00174 };
00175
00182 class CompositeEntity : public virtual Entity {
00183 list<Entity *> m_parts;
00184 int m_size;
00185 string m_boundary;
00186 public:
00187 CompositeEntity(type tp, const char *stp);
00188 virtual ~CompositeEntity();
00190 virtual int addPart(Entity *part);
00192 virtual int size();
00194 virtual const char *format();
00196 virtual int format(char *buf);
00197 };
00198
00205 extern char *readFile(const char *fname, int *outlen);
00206
00215 extern char *base64Encode(const char *from, int fromlen , int *tolen,
00216 int ll = 54);
00217
00223 extern char *base64EncodeFile(const char *fname, int *outlen);
00224
00231 extern char *qpEncode(const char *from, int fromlen, int *tolen);
00232
00233
00235
00236 class Recipient {
00237 string official;
00238 string nickname;
00239 public:
00240 Recipient(const char *off, const char *nick)
00241 : official(off), nickname(nick?nick:"")
00242 {}
00243 friend class Message;
00244 friend class SimpleMessage;
00245 friend class CompositeMessage;
00246 };
00247
00249 class Message : public virtual Entity {
00250 protected:
00251 list<Recipient> to;
00252 list<Recipient> cc;
00253 list<Recipient> bcc;
00254 static const char *recpth[3];
00255 list<Recipient> *recptlists[3];
00256
00257 void setRecipientHeaders();
00258
00259 public:
00261 enum recpt_type {TO = 0, CC = 1, BCC = 2};
00262
00263 Message();
00264 virtual ~Message() {}
00265
00267 virtual const char *format() = 0;
00268
00273 virtual int addRecipient(const char *official, const char *nick = 0,
00274 recpt_type tp = TO);
00275
00299 virtual int parseAddRecipients(const char *in, recpt_type tp = TO);
00300
00305 virtual const char **getRecipients();
00306
00308 virtual int setSubject(const char *subject);
00310 virtual int setFrom(const Recipient &from);
00311 };
00312
00317 class SimpleMessage : public Message, public DiscreteEntity {
00318 public:
00319 SimpleMessage();
00321 virtual const char *format();
00322 };
00323
00328 class CompositeMessage : public Message, public CompositeEntity {
00329 public:
00330 CompositeMessage(type tp, const char *stp);
00332 virtual const char *format();
00333 };
00334
00335 #ifdef WITH_SENDMAIL
00347 extern int sendmail(Message *msg);
00348 #endif
00349
00350 }
00351
00352 #endif