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