d62f1e66035494696bc4c6a11f1aa0440b57bca2
[dcpomatic.git] / src / lib / kdm.cc
1 /*
2     Copyright (C) 2013-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 "kdm.h"
21 #include "cinema.h"
22 #include "screen.h"
23 #include "exceptions.h"
24 #include "util.h"
25 #include "film.h"
26 #include "config.h"
27 #include "safe_stringstream.h"
28 #include "quickmail.h"
29 #include "compose.hpp"
30 #include <zip.h>
31 #include <dcp/encrypted_kdm.h>
32 #include <dcp/types.h>
33 #include <boost/shared_ptr.hpp>
34 #include <list>
35
36 using std::list;
37 using std::string;
38 using std::cout;
39 using boost::shared_ptr;
40
41 struct ScreenKDM
42 {
43         ScreenKDM (shared_ptr<Screen> s, dcp::EncryptedKDM k)
44                 : screen (s)
45                 , kdm (k)
46         {}
47
48         shared_ptr<Screen> screen;
49         dcp::EncryptedKDM kdm;
50 };
51
52 static string
53 kdm_filename (shared_ptr<const Film> film, ScreenKDM kdm)
54 {
55         return tidy_for_filename (film->name()) + "_" + tidy_for_filename (kdm.screen->cinema->name) + "_" + tidy_for_filename (kdm.screen->name) + ".kdm.xml";
56 }
57
58 struct CinemaKDMs
59 {
60         shared_ptr<Cinema> cinema;
61         list<ScreenKDM> screen_kdms;
62
63         void make_zip_file (shared_ptr<const Film> film, boost::filesystem::path zip_file) const
64         {
65                 int error;
66                 struct zip* zip = zip_open (zip_file.string().c_str(), ZIP_CREATE | ZIP_EXCL, &error);
67                 if (!zip) {
68                         if (error == ZIP_ER_EXISTS) {
69                                 throw FileError ("ZIP file already exists", zip_file);
70                         }
71                         throw FileError ("could not create ZIP file", zip_file);
72                 }
73
74                 list<shared_ptr<string> > kdm_strings;
75
76                 for (list<ScreenKDM>::const_iterator i = screen_kdms.begin(); i != screen_kdms.end(); ++i) {
77                         shared_ptr<string> kdm (new string (i->kdm.as_xml ()));
78                         kdm_strings.push_back (kdm);
79
80                         struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
81                         if (!source) {
82                                 throw StringError ("could not create ZIP source");
83                         }
84
85                         if (zip_add (zip, kdm_filename (film, *i).c_str(), source) == -1) {
86                                 throw StringError ("failed to add KDM to ZIP archive");
87                         }
88                 }
89
90                 if (zip_close (zip) == -1) {
91                         throw StringError ("failed to close ZIP archive");
92                 }
93         }
94 };
95
96 /* Not complete but sufficient for our purposes (we're using
97    ScreenKDM in a list where all the screens will be unique).
98 */
99 bool
100 operator== (ScreenKDM const & a, ScreenKDM const & b)
101 {
102         return a.screen == b.screen;
103 }
104
105 static list<ScreenKDM>
106 make_screen_kdms (
107         shared_ptr<const Film> film,
108         list<shared_ptr<Screen> > screens,
109         boost::filesystem::path cpl,
110         dcp::LocalTime from,
111         dcp::LocalTime to,
112         dcp::Formulation formulation
113         )
114 {
115         list<dcp::EncryptedKDM> kdms = film->make_kdms (screens, cpl, from, to, formulation);
116
117         list<ScreenKDM> screen_kdms;
118
119         list<shared_ptr<Screen> >::iterator i = screens.begin ();
120         list<dcp::EncryptedKDM>::iterator j = kdms.begin ();
121         while (i != screens.end() && j != kdms.end ()) {
122                 screen_kdms.push_back (ScreenKDM (*i, *j));
123                 ++i;
124                 ++j;
125         }
126
127         return screen_kdms;
128 }
129
130 static list<CinemaKDMs>
131 make_cinema_kdms (
132         shared_ptr<const Film> film,
133         list<shared_ptr<Screen> > screens,
134         boost::filesystem::path cpl,
135         dcp::LocalTime from,
136         dcp::LocalTime to,
137         dcp::Formulation formulation
138         )
139 {
140         list<ScreenKDM> screen_kdms = make_screen_kdms (film, screens, cpl, from, to, formulation);
141         list<CinemaKDMs> cinema_kdms;
142
143         while (!screen_kdms.empty ()) {
144
145                 /* Get all the screens from a single cinema */
146
147                 CinemaKDMs ck;
148
149                 list<ScreenKDM>::iterator i = screen_kdms.begin ();
150                 ck.cinema = i->screen->cinema;
151                 ck.screen_kdms.push_back (*i);
152                 list<ScreenKDM>::iterator j = i;
153                 ++i;
154                 screen_kdms.remove (*j);
155
156                 while (i != screen_kdms.end ()) {
157                         if (i->screen->cinema == ck.cinema) {
158                                 ck.screen_kdms.push_back (*i);
159                                 list<ScreenKDM>::iterator j = i;
160                                 ++i;
161                                 screen_kdms.remove (*j);
162                         } else {
163                                 ++i;
164                         }
165                 }
166
167                 cinema_kdms.push_back (ck);
168         }
169
170         return cinema_kdms;
171 }
172
173 /** @param from KDM from time in local time.
174  *  @param to KDM to time in local time.
175  */
176 void
177 write_kdm_files (
178         shared_ptr<const Film> film,
179         list<shared_ptr<Screen> > screens,
180         boost::filesystem::path cpl,
181         dcp::LocalTime from,
182         dcp::LocalTime to,
183         dcp::Formulation formulation,
184         boost::filesystem::path directory
185         )
186 {
187         list<ScreenKDM> screen_kdms = make_screen_kdms (film, screens, cpl, from, to, formulation);
188
189         /* Write KDMs to the specified directory */
190         for (list<ScreenKDM>::iterator i = screen_kdms.begin(); i != screen_kdms.end(); ++i) {
191                 boost::filesystem::path out = directory;
192                 out /= kdm_filename (film, *i);
193                 i->kdm.as_xml (out);
194         }
195 }
196
197 void
198 write_kdm_zip_files (
199         shared_ptr<const Film> film,
200         list<shared_ptr<Screen> > screens,
201         boost::filesystem::path cpl,
202         dcp::LocalTime from,
203         dcp::LocalTime to,
204         dcp::Formulation formulation,
205         boost::filesystem::path directory
206         )
207 {
208         list<CinemaKDMs> cinema_kdms = make_cinema_kdms (film, screens, cpl, from, to, formulation);
209
210         for (list<CinemaKDMs>::const_iterator i = cinema_kdms.begin(); i != cinema_kdms.end(); ++i) {
211                 boost::filesystem::path path = directory;
212                 path /= tidy_for_filename (i->cinema->name) + ".zip";
213                 i->make_zip_file (film, path);
214         }
215 }
216
217 void
218 email_kdms (
219         shared_ptr<const Film> film,
220         list<shared_ptr<Screen> > screens,
221         boost::filesystem::path cpl,
222         dcp::LocalTime from,
223         dcp::LocalTime to,
224         dcp::Formulation formulation
225         )
226 {
227         list<CinemaKDMs> cinema_kdms = make_cinema_kdms (film, screens, cpl, from, to, formulation);
228
229         for (list<CinemaKDMs>::const_iterator i = cinema_kdms.begin(); i != cinema_kdms.end(); ++i) {
230
231                 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path ();
232                 zip_file /= boost::filesystem::unique_path().string() + ".zip";
233                 i->make_zip_file (film, zip_file);
234
235                 /* Send email */
236
237                 quickmail_initialize ();
238
239                 SafeStringStream start;
240                 start << from.date() << " " << from.time_of_day();
241                 SafeStringStream end;
242                 end << to.date() << " " << to.time_of_day();
243
244                 string subject = Config::instance()->kdm_subject();
245                 boost::algorithm::replace_all (subject, "$CPL_NAME", film->dcp_name ());
246                 boost::algorithm::replace_all (subject, "$START_TIME", start.str ());
247                 boost::algorithm::replace_all (subject, "$END_TIME", end.str ());
248                 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i->cinema->name);
249                 quickmail mail = quickmail_create (Config::instance()->kdm_from().c_str(), subject.c_str ());
250
251                 quickmail_add_to (mail, i->cinema->email.c_str ());
252                 if (!Config::instance()->kdm_cc().empty ()) {
253                         quickmail_add_cc (mail, Config::instance()->kdm_cc().c_str ());
254                 }
255                 if (!Config::instance()->kdm_bcc().empty ()) {
256                         quickmail_add_bcc (mail, Config::instance()->kdm_bcc().c_str ());
257                 }
258
259                 string body = Config::instance()->kdm_email().c_str();
260                 boost::algorithm::replace_all (body, "$CPL_NAME", film->dcp_name ());
261                 boost::algorithm::replace_all (body, "$START_TIME", start.str ());
262                 boost::algorithm::replace_all (body, "$END_TIME", end.str ());
263                 boost::algorithm::replace_all (body, "$CINEMA_NAME", i->cinema->name);
264
265                 SafeStringStream screens;
266                 for (list<ScreenKDM>::const_iterator j = i->screen_kdms.begin(); j != i->screen_kdms.end(); ++j) {
267                         screens << j->screen->name << ", ";
268                 }
269                 boost::algorithm::replace_all (body, "$SCREENS", screens.str().substr (0, screens.str().length() - 2));
270
271                 quickmail_set_body (mail, body.c_str());
272                 quickmail_add_attachment_file (mail, zip_file.string().c_str(), "application/zip");
273
274                 char const* error = quickmail_send (
275                         mail,
276                         Config::instance()->mail_server().c_str(),
277                         Config::instance()->mail_port(),
278                         Config::instance()->mail_user().c_str(),
279                         Config::instance()->mail_password().c_str()
280                         );
281
282                 if (error) {
283                         quickmail_destroy (mail);
284                         throw KDMError (
285                                 String::compose (
286                                         "Failed to send KDM email to %1 (%2)",
287                                         Config::instance()->mail_server(),
288                                         error
289                                         )
290                                 );
291                 }
292                 quickmail_destroy (mail);
293         }
294 }