Barely-functioning GL playback with new arrangement.
[dcpomatic.git] / src / lib / emailer.cc
1 /*
2     Copyright (C) 2015-2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "compose.hpp"
22 #include "config.h"
23 #include "emailer.h"
24 #include "exceptions.h"
25 #include <curl/curl.h>
26 #include <boost/algorithm/string.hpp>
27 #include <boost/date_time/c_local_time_adjustor.hpp>
28 #include <boost/foreach.hpp>
29
30 #include "i18n.h"
31
32 using std::string;
33 using std::min;
34 using std::list;
35 using std::cout;
36 using std::pair;
37 using boost::shared_ptr;
38 using dcp::Data;
39
40 Emailer::Emailer (string from, list<string> to, string subject, string body)
41         : _from (from)
42         , _to (to)
43         , _subject (subject)
44         , _body (fix (body))
45         , _offset (0)
46 {
47
48 }
49
50 string
51 Emailer::fix (string s) const
52 {
53         boost::algorithm::replace_all (s, "\n", "\r\n");
54         boost::algorithm::replace_all (s, "\0", " ");
55         return s;
56 }
57
58 void
59 Emailer::add_cc (string cc)
60 {
61         _cc.push_back (cc);
62 }
63
64 void
65 Emailer::add_bcc (string bcc)
66 {
67         _bcc.push_back (bcc);
68 }
69
70 void
71 Emailer::add_attachment (boost::filesystem::path file, string name, string mime_type)
72 {
73         Attachment a;
74         a.file = file;
75         a.name = name;
76         a.mime_type = mime_type;
77         _attachments.push_back (a);
78 }
79
80 static size_t
81 curl_data_shim (void* ptr, size_t size, size_t nmemb, void* userp)
82 {
83         return reinterpret_cast<Emailer*>(userp)->get_data (ptr, size, nmemb);
84 }
85
86 static int
87 curl_debug_shim (CURL* curl, curl_infotype type, char* data, size_t size, void* userp)
88 {
89         return reinterpret_cast<Emailer*>(userp)->debug (curl, type, data, size);
90 }
91
92 size_t
93 Emailer::get_data (void* ptr, size_t size, size_t nmemb)
94 {
95         size_t const t = min (_email.length() - _offset, size * nmemb);
96         memcpy (ptr, _email.substr (_offset, t).c_str(), t);
97         _offset += t;
98         return t;
99 }
100
101 void
102 Emailer::send (string server, int port, EmailProtocol protocol, string user, string password)
103 {
104         char date_buffer[128];
105         time_t now = time (0);
106         strftime (date_buffer, sizeof(date_buffer), "%a, %d %b %Y %H:%M:%S ", localtime (&now));
107
108         boost::posix_time::ptime const utc_now = boost::posix_time::second_clock::universal_time ();
109         boost::posix_time::ptime const local_now = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local (utc_now);
110         boost::posix_time::time_duration offset = local_now - utc_now;
111         sprintf (date_buffer + strlen(date_buffer), "%s%02d%02d", (offset.hours() >= 0 ? "+" : "-"), int(abs(offset.hours())), int(offset.minutes()));
112
113         _email = "Date: " + string(date_buffer) + "\r\n"
114                 "To: " + address_list (_to) + "\r\n"
115                 "From: " + _from + "\r\n";
116
117         if (!_cc.empty ()) {
118                 _email += "Cc: " + address_list (_cc) + "\r\n";
119         }
120
121         if (!_bcc.empty ()) {
122                 _email += "Bcc: " + address_list (_bcc) + "\r\n";
123         }
124
125         string const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
126         string boundary;
127         for (int i = 0; i < 32; ++i) {
128                 boundary += chars[rand() % chars.length()];
129         }
130
131         if (!_attachments.empty ()) {
132                 _email += "MIME-Version: 1.0\r\n"
133                         "Content-Type: multipart/mixed; boundary=" + boundary + "\r\n";
134         }
135
136         _email += "Subject: " + _subject + "\r\n"
137                 "User-Agent: DCP-o-matic\r\n"
138                 "\r\n";
139
140         if (!_attachments.empty ()) {
141                 _email += "--" + boundary + "\r\n"
142                         + "Content-Type: text/plain; charset=utf-8\r\n\r\n";
143         }
144
145         _email += _body;
146
147         BOOST_FOREACH (Attachment i, _attachments) {
148                 _email += "\r\n\r\n--" + boundary + "\r\n"
149                         "Content-Type: " + i.mime_type + "; name=" + i.name + "\r\n"
150                         "Content-Transfer-Encoding: Base64\r\n"
151                         "Content-Disposition: attachment; filename=" + i.name + "\r\n\r\n";
152
153                 BIO* b64 = BIO_new (BIO_f_base64());
154
155                 BIO* bio = BIO_new (BIO_s_mem());
156                 bio = BIO_push (b64, bio);
157
158                 Data data (i.file);
159                 BIO_write (bio, data.data().get(), data.size());
160                 (void) BIO_flush (bio);
161
162                 char* out;
163                 long int bytes = BIO_get_mem_data (bio, &out);
164                 _email += fix (string (out, bytes));
165
166                 BIO_free_all (b64);
167         }
168
169         if (!_attachments.empty ()) {
170                 _email += "\r\n--" + boundary + "--\r\n";
171         }
172
173         curl_global_init (CURL_GLOBAL_DEFAULT);
174
175         CURL* curl = curl_easy_init ();
176         if (!curl) {
177                 throw NetworkError ("Could not initialise libcurl");
178         }
179
180         if ((protocol == EMAIL_PROTOCOL_AUTO && port == 465) || protocol == EMAIL_PROTOCOL_SSL) {
181                 /* "SSL" or "Implicit TLS"; I think curl wants us to use smtps here */
182                 curl_easy_setopt (curl, CURLOPT_URL, String::compose("smtps://%1:%2", server, port).c_str());
183         } else {
184                 curl_easy_setopt (curl, CURLOPT_URL, String::compose("smtp://%1:%2", server, port).c_str());
185         }
186
187         if (!user.empty ()) {
188                 curl_easy_setopt (curl, CURLOPT_USERNAME, user.c_str ());
189         }
190         if (!password.empty ()) {
191                 curl_easy_setopt (curl, CURLOPT_PASSWORD, password.c_str());
192         }
193
194         curl_easy_setopt (curl, CURLOPT_MAIL_FROM, _from.c_str());
195
196         struct curl_slist* recipients = 0;
197         BOOST_FOREACH (string i, _to) {
198                 recipients = curl_slist_append (recipients, i.c_str());
199         }
200         BOOST_FOREACH (string i, _cc) {
201                 recipients = curl_slist_append (recipients, i.c_str());
202         }
203         BOOST_FOREACH (string i, _bcc) {
204                 recipients = curl_slist_append (recipients, i.c_str());
205         }
206
207         curl_easy_setopt (curl, CURLOPT_MAIL_RCPT, recipients);
208
209         curl_easy_setopt (curl, CURLOPT_READFUNCTION, curl_data_shim);
210         curl_easy_setopt (curl, CURLOPT_READDATA, this);
211         curl_easy_setopt (curl, CURLOPT_UPLOAD, 1L);
212
213         if (protocol == EMAIL_PROTOCOL_AUTO || protocol == EMAIL_PROTOCOL_STARTTLS) {
214                 curl_easy_setopt (curl, CURLOPT_USE_SSL, (long) CURLUSESSL_TRY);
215         }
216         curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
217         curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
218         curl_easy_setopt (curl, CURLOPT_VERBOSE, 1L);
219         curl_easy_setopt (curl, CURLOPT_DEBUGFUNCTION, curl_debug_shim);
220         curl_easy_setopt (curl, CURLOPT_DEBUGDATA, this);
221
222         CURLcode const r = curl_easy_perform (curl);
223         if (r != CURLE_OK) {
224                 throw KDMError (_("Failed to send email"), curl_easy_strerror (r));
225         }
226
227         curl_slist_free_all (recipients);
228         curl_easy_cleanup (curl);
229         curl_global_cleanup ();
230 }
231
232 string
233 Emailer::address_list (list<string> addresses)
234 {
235         string o;
236         BOOST_FOREACH (string i, addresses) {
237                 o += i + ", ";
238         }
239
240         return o.substr (0, o.length() - 2);
241 }
242
243 int
244 Emailer::debug (CURL *, curl_infotype type, char* data, size_t size)
245 {
246         if (type == CURLINFO_TEXT) {
247                 _notes += string (data, size);
248         } else if (type == CURLINFO_HEADER_IN) {
249                 _notes += "<- " + string (data, size);
250         } else if (type == CURLINFO_HEADER_OUT) {
251                 _notes += "-> " + string (data, size);
252         }
253         return 0;
254 }