Line data Source code
1 : // Snap Websites Servers -- create a feed where you can write an email
2 : // Copyright (c) 2016-2019 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // https://snapwebsites.org/
5 : // contact@m2osw.com
6 : //
7 : // This program is free software; you can redistribute it and/or modify
8 : // it under the terms of the GNU General Public License as published by
9 : // the Free Software Foundation; either version 2 of the License, or
10 : // (at your option) any later version.
11 : //
12 : // This program is distributed in the hope that it will be useful,
13 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : // GNU General Public License for more details.
16 : //
17 : // You should have received a copy of the GNU General Public License
18 : // along with this program; if not, write to the Free Software
19 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 : //
21 : #pragma once
22 :
23 : // snapwebsites lib
24 : //
25 : #include "snapwebsites/snap_exception.h"
26 : #include "snapwebsites/quoted_printable.h"
27 :
28 :
29 : // snapdev lib
30 : //
31 : #include <snapdev/qcaseinsensitivestring.h>
32 :
33 :
34 : // qt serialization lib
35 : //
36 : #include <QtSerialization/QSerializationFieldTag.h>
37 : #include <QtSerialization/QSerializationWriter.h>
38 :
39 : // C++ lib
40 : //
41 : #include <map>
42 : #include <memory>
43 :
44 :
45 : namespace snap
46 : {
47 :
48 0 : class email_exception : public snap_exception
49 : {
50 : public:
51 : // no sub-name
52 0 : explicit email_exception(char const * what_msg) : snap_exception("email", what_msg) {}
53 : explicit email_exception(std::string const & what_msg) : snap_exception("email", what_msg) {}
54 : explicit email_exception(QString const & what_msg) : snap_exception("email", what_msg) {}
55 : };
56 :
57 :
58 : class email_exception_called_multiple_times : public email_exception
59 : {
60 : public:
61 : // no sub-name
62 : explicit email_exception_called_multiple_times(char const * what_msg) : email_exception(what_msg) {}
63 : explicit email_exception_called_multiple_times(std::string const & what_msg) : email_exception(what_msg) {}
64 : explicit email_exception_called_multiple_times(QString const & what_msg) : email_exception(what_msg) {}
65 : };
66 :
67 :
68 : class email_exception_called_after_end_header : public email_exception
69 : {
70 : public:
71 : // no sub-name
72 : explicit email_exception_called_after_end_header(char const * what_msg) : email_exception(what_msg) {}
73 : explicit email_exception_called_after_end_header(std::string const & what_msg) : email_exception(what_msg) {}
74 : explicit email_exception_called_after_end_header(QString const & what_msg) : email_exception(what_msg) {}
75 : };
76 :
77 :
78 0 : class email_exception_too_many_levels : public email_exception
79 : {
80 : public:
81 : // no sub-name
82 0 : explicit email_exception_too_many_levels(char const * what_msg) : email_exception(what_msg) {}
83 : explicit email_exception_too_many_levels(std::string const & what_msg) : email_exception(what_msg) {}
84 : explicit email_exception_too_many_levels(QString const & what_msg) : email_exception(what_msg) {}
85 : };
86 :
87 :
88 :
89 :
90 :
91 :
92 :
93 : // create email then use the sendemail() function to send it.
94 : // if you want to save it and send it later, you can serialize/unserialize it too
95 : //
96 : class email
97 : : public QtSerialization::QSerializationObject
98 : {
99 : public:
100 : static int const EMAIL_MAJOR_VERSION = 1;
101 : static int const EMAIL_MINOR_VERSION = 0;
102 :
103 : typedef std::map<QCaseInsensitiveString, QString> header_map_t;
104 : typedef std::map<QString, QString> parameter_map_t;
105 :
106 : enum class priority_t
107 : {
108 : EMAIL_PRIORITY_BULK = 1,
109 : EMAIL_PRIORITY_LOW,
110 : EMAIL_PRIORITY_NORMAL,
111 : EMAIL_PRIORITY_HIGH,
112 : EMAIL_PRIORITY_URGENT
113 : };
114 :
115 0 : class attachment
116 : : public QtSerialization::QSerializationObject
117 : {
118 : public:
119 : typedef std::vector<attachment> vector_t;
120 :
121 : attachment();
122 : virtual ~attachment();
123 :
124 : // data ("matter" of this attachment)
125 : //
126 : void set_data(QByteArray const & data, QString mime_type = QString());
127 : void quoted_printable_encode_and_set_data(
128 : QByteArray const & data
129 : , QString mime_type = QString()
130 : , int flags = quoted_printable::QUOTED_PRINTABLE_FLAG_LFONLY
131 : | quoted_printable::QUOTED_PRINTABLE_FLAG_NO_LONE_PERIOD);
132 : QByteArray get_data() const;
133 :
134 : // header for this header
135 : //
136 : void set_content_disposition(QString const & filename, int64_t modification_date = 0, QString const & attachment_type = "attachment");
137 : void add_header(QString const & name, QString const & value);
138 : void remove_header(QString const & name);
139 : bool has_header(QString const & name) const;
140 : QString get_header(QString const & name) const;
141 : header_map_t const & get_all_headers() const;
142 :
143 : // sub-attachment (one level available only)
144 : //
145 : void add_related(attachment const & data);
146 : int get_related_count() const;
147 : attachment & get_related(int index) const;
148 :
149 : // "internal" functions used to save the data serialized
150 : //
151 : void unserialize(QtSerialization::QReader & r);
152 : virtual void readTag(QString const & name, QtSerialization::QReader & r);
153 : void serialize(QtSerialization::QWriter & w, bool is_sub_attachment) const;
154 :
155 : bool operator == (attachment const & rhs) const;
156 :
157 : private:
158 : header_map_t f_headers = header_map_t();
159 : QByteArray f_data = QByteArray();
160 : bool f_is_sub_attachment = false;
161 : vector_t f_sub_attachments = vector_t(); // for HTML data (images, css, ...)
162 : };
163 :
164 : email();
165 : virtual ~email();
166 :
167 : // basic flags and strings
168 : //
169 : void set_branding(bool branding = true);
170 : bool get_branding() const;
171 : void set_cumulative(QString const & object);
172 : QString const & get_cumulative() const;
173 : void set_site_key(QString const & site_key);
174 : QString const & get_site_key() const;
175 : void set_email_path(QString const & email_path);
176 : QString const & get_email_path() const;
177 : void set_email_key(QString const & site_key);
178 : QString const & get_email_key() const;
179 : time_t get_time() const;
180 :
181 : // headers
182 : //
183 : void set_from(QString const & from);
184 : void set_to(QString const & to);
185 : void set_priority(priority_t priority = priority_t::EMAIL_PRIORITY_NORMAL);
186 : void set_subject(QString const & subject);
187 : void add_header(QString const & name, QString const & value);
188 : void remove_header(QString const & name);
189 : bool has_header(QString const & name) const;
190 : QString get_header(QString const & name) const;
191 : header_map_t const & get_all_headers() const;
192 :
193 : // attachments
194 : //
195 : void set_body_attachment(attachment const & data);
196 : void add_attachment(attachment const & data);
197 : int get_attachment_count() const;
198 : attachment & get_attachment(int index) const;
199 :
200 : // parameters (like headers but not included in email and names are
201 : // case sensitive)
202 : //
203 : void add_parameter(QString const & name, QString const & value);
204 : QString get_parameter(QString const & name) const;
205 : parameter_map_t const & get_all_parameters() const;
206 :
207 : // functions used to save the data serialized (used by the sendmail plugin)
208 : //
209 : void unserialize(QString const & data);
210 : virtual void readTag(QString const & name, QtSerialization::QReader & r);
211 : QString serialize() const;
212 :
213 : bool send() const;
214 :
215 : bool operator == (email const & rhs) const;
216 :
217 : private:
218 : bool f_branding = true;
219 : QString f_cumulative = QString();
220 : QString f_site_key = QString();
221 : QString f_email_path = QString();
222 : QString f_email_key = QString(); // set on post_email()
223 : time_t f_time = static_cast<time_t>(-1);
224 : header_map_t f_headers = header_map_t();
225 : attachment::vector_t f_attachments = attachment::vector_t();
226 : parameter_map_t f_parameters = parameter_map_t();
227 : };
228 :
229 :
230 :
231 :
232 : } // namespace snap
233 : // vim: ts=4 sw=4 et
|