Add debug option to log SMTP session transcripts.
[dcpomatic.git] / src / lib / cinema_kdms.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 "exceptions.h"
21 #include "cinema_kdms.h"
22 #include "cinema.h"
23 #include "screen.h"
24 #include "config.h"
25 #include "util.h"
26 #include "emailer.h"
27 #include "compose.hpp"
28 #include "log.h"
29 #include <zip.h>
30 #include <boost/foreach.hpp>
31
32 #include "i18n.h"
33
34 using std::list;
35 using std::cout;
36 using std::string;
37 using boost::shared_ptr;
38
39 void
40 CinemaKDMs::make_zip_file (string film_name, boost::filesystem::path zip_file) const
41 {
42         int error;
43         struct zip* zip = zip_open (zip_file.string().c_str(), ZIP_CREATE | ZIP_EXCL, &error);
44         if (!zip) {
45                 if (error == ZIP_ER_EXISTS) {
46                         throw FileError ("ZIP file already exists", zip_file);
47                 }
48                 throw FileError ("could not create ZIP file", zip_file);
49         }
50
51         list<shared_ptr<string> > kdm_strings;
52
53         BOOST_FOREACH (ScreenKDM const & i, screen_kdms) {
54                 shared_ptr<string> kdm (new string (i.kdm.as_xml ()));
55                 kdm_strings.push_back (kdm);
56
57                 struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
58                 if (!source) {
59                         throw StringError ("could not create ZIP source");
60                 }
61
62                 if (zip_add (zip, i.filename(film_name).c_str(), source) == -1) {
63                         throw StringError ("failed to add KDM to ZIP archive");
64                 }
65         }
66
67         if (zip_close (zip) == -1) {
68                 throw StringError ("failed to close ZIP archive");
69         }
70 }
71
72 list<CinemaKDMs>
73 CinemaKDMs::collect (list<ScreenKDM> screen_kdms)
74 {
75         list<CinemaKDMs> cinema_kdms;
76
77         while (!screen_kdms.empty ()) {
78
79                 /* Get all the screens from a single cinema */
80
81                 CinemaKDMs ck;
82
83                 list<ScreenKDM>::iterator i = screen_kdms.begin ();
84                 ck.cinema = i->screen->cinema;
85                 ck.screen_kdms.push_back (*i);
86                 list<ScreenKDM>::iterator j = i;
87                 ++i;
88                 screen_kdms.remove (*j);
89
90                 while (i != screen_kdms.end ()) {
91                         if (i->screen->cinema == ck.cinema) {
92                                 ck.screen_kdms.push_back (*i);
93                                 list<ScreenKDM>::iterator j = i;
94                                 ++i;
95                                 screen_kdms.remove (*j);
96                         } else {
97                                 ++i;
98                         }
99                 }
100
101                 cinema_kdms.push_back (ck);
102         }
103
104         return cinema_kdms;
105 }
106
107 void
108 CinemaKDMs::write_zip_files (string film_name, list<CinemaKDMs> cinema_kdms, boost::filesystem::path directory)
109 {
110         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
111                 boost::filesystem::path path = directory;
112                 path /= tidy_for_filename (i.cinema->name) + ".zip";
113                 i.make_zip_file (film_name, path);
114         }
115 }
116
117 /** @param log Log to write email session transcript to, or 0 */
118 /* XXX: should probably get from/to from the KDMs themselves */
119 void
120 CinemaKDMs::email (
121         string film_name, string cpl_name, list<CinemaKDMs> cinema_kdms, dcp::LocalTime from, dcp::LocalTime to, shared_ptr<Job> job, shared_ptr<Log> log
122         )
123 {
124         Config* config = Config::instance ();
125
126         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
127
128                 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path ();
129                 zip_file /= boost::filesystem::unique_path().string() + ".zip";
130                 i.make_zip_file (film_name, zip_file);
131
132                 string subject = config->kdm_subject();
133                 SafeStringStream start;
134                 start << from.date() << " " << from.time_of_day();
135                 SafeStringStream end;
136                 end << to.date() << " " << to.time_of_day();
137                 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
138                 boost::algorithm::replace_all (subject, "$START_TIME", start.str ());
139                 boost::algorithm::replace_all (subject, "$END_TIME", end.str ());
140                 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i.cinema->name);
141
142                 string body = config->kdm_email().c_str();
143                 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_name);
144                 boost::algorithm::replace_all (body, "$START_TIME", start.str ());
145                 boost::algorithm::replace_all (body, "$END_TIME", end.str ());
146                 boost::algorithm::replace_all (body, "$CINEMA_NAME", i.cinema->name);
147
148                 SafeStringStream screens;
149                 BOOST_FOREACH (ScreenKDM const & j, i.screen_kdms) {
150                         screens << j.screen->name << ", ";
151                 }
152                 boost::algorithm::replace_all (body, "$SCREENS", screens.str().substr (0, screens.str().length() - 2));
153
154                 Emailer email (config->kdm_from(), i.cinema->email, subject, body);
155
156                 if (!config->kdm_cc().empty ()) {
157                         email.add_cc (config->kdm_cc ());
158                 }
159                 if (!config->kdm_bcc().empty ()) {
160                         email.add_bcc (config->kdm_bcc ());
161                 }
162
163                 string const name = tidy_for_filename(i.cinema->name) + "_" + tidy_for_filename(film_name) + ".zip";
164                 email.add_attachment (zip_file, name, "application/zip");
165                 email.send (job);
166
167                 if (log) {
168                         log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
169                 }
170         }
171 }