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