Add $CINEMA_SHORT_NAME variable in KDM emails.
[dcpomatic.git] / src / lib / kdm_with_metadata.cc
1 /*
2     Copyright (C) 2013-2021 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
22 #include "cinema.h"
23 #include "config.h"
24 #include "cross.h"
25 #include "dcpomatic_log.h"
26 #include "emailer.h"
27 #include "kdm_with_metadata.h"
28 #include "screen.h"
29 #include "util.h"
30 #include "zipper.h"
31
32 #include "i18n.h"
33
34
35 using std::cout;
36 using std::function;
37 using std::list;
38 using std::shared_ptr;
39 using std::string;
40 using boost::optional;
41
42
43 int
44 write_files (
45         list<KDMWithMetadataPtr> kdms,
46         boost::filesystem::path directory,
47         dcp::NameFormat name_format,
48         std::function<bool (boost::filesystem::path)> confirm_overwrite
49         )
50 {
51         int written = 0;
52
53         if (directory == "-") {
54                 /* Write KDMs to the stdout */
55                 for (auto i: kdms) {
56                         cout << i->kdm_as_xml ();
57                         ++written;
58                 }
59
60                 return written;
61         }
62
63         if (!boost::filesystem::exists (directory)) {
64                 boost::filesystem::create_directories (directory);
65         }
66
67         /* Write KDMs to the specified directory */
68         for (auto i: kdms) {
69                 auto out = fix_long_path(directory / careful_string_filter(name_format.get(i->name_values(), ".xml")));
70                 if (!boost::filesystem::exists (out) || confirm_overwrite (out)) {
71                         i->kdm_as_xml (out);
72                         ++written;
73                 }
74         }
75
76         return written;
77 }
78
79
80 optional<string>
81 KDMWithMetadata::get (char k) const
82 {
83         auto i = _name_values.find (k);
84         if (i == _name_values.end()) {
85                 return {};
86         }
87
88         return i->second;
89 }
90
91
92 void
93 make_zip_file (list<KDMWithMetadataPtr> kdms, boost::filesystem::path zip_file, dcp::NameFormat name_format)
94 {
95         Zipper zipper (zip_file);
96
97         for (auto i: kdms) {
98                 auto const name = careful_string_filter(name_format.get(i->name_values(), ".xml"));
99                 zipper.add (name, i->kdm_as_xml());
100         }
101
102         zipper.close ();
103 }
104
105
106 /** Collect a list of KDMWithMetadatas into a list of lists so that
107  *  each list contains the KDMs for one list.
108  */
109 list<list<KDMWithMetadataPtr>>
110 collect (list<KDMWithMetadataPtr> kdms)
111 {
112         list<list<KDMWithMetadataPtr>> grouped;
113
114         for (auto i: kdms) {
115
116                 auto j = grouped.begin ();
117
118                 while (j != grouped.end()) {
119                         if (j->front()->group() == i->group()) {
120                                 j->push_back (i);
121                                 break;
122                         }
123                         ++j;
124                 }
125
126                 if (j == grouped.end()) {
127                         grouped.push_back (list<KDMWithMetadataPtr>());
128                         grouped.back().push_back (i);
129                 }
130         }
131
132         return grouped;
133 }
134
135
136 /** Write one directory per list into another directory */
137 int
138 write_directories (
139         list<list<KDMWithMetadataPtr>> kdms,
140         boost::filesystem::path directory,
141         dcp::NameFormat container_name_format,
142         dcp::NameFormat filename_format,
143         function<bool (boost::filesystem::path)> confirm_overwrite
144         )
145 {
146         int written = 0;
147
148         for (auto const& i: kdms) {
149                 auto path = directory;
150                 path /= container_name_format.get(i.front()->name_values(), "", "s");
151                 if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
152                         boost::filesystem::create_directories (path);
153                         write_files (i, path, filename_format, confirm_overwrite);
154                 }
155                 written += i.size();
156         }
157
158         return written;
159 }
160
161
162 /** Write one ZIP file per cinema into a directory */
163 int
164 write_zip_files (
165         list<list<KDMWithMetadataPtr>> kdms,
166         boost::filesystem::path directory,
167         dcp::NameFormat container_name_format,
168         dcp::NameFormat filename_format,
169         function<bool (boost::filesystem::path)> confirm_overwrite
170         )
171 {
172         int written = 0;
173
174         for (auto const& i: kdms) {
175                 auto path = directory;
176                 path /= container_name_format.get(i.front()->name_values(), ".zip", "s");
177                 if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
178                         if (boost::filesystem::exists (path)) {
179                                 /* Creating a new zip file over an existing one is an error */
180                                 boost::filesystem::remove (path);
181                         }
182                         make_zip_file (i, path, filename_format);
183                         written += i.size();
184                 }
185         }
186
187         return written;
188 }
189
190
191 /** Email one ZIP file per cinema to the cinema.
192  *  @param kdms KDMs to email.
193  *  @param container_name_format Format of folder / ZIP to use.
194  *  @param filename_format Format of filenames to use.
195  *  @param name_values Values to substitute into \p container_name_format and \p filename_format.
196  *  @param cpl_name Name of the CPL that the KDMs are for.
197  */
198 void
199 send_emails (
200         list<list<KDMWithMetadataPtr>> kdms,
201         dcp::NameFormat container_name_format,
202         dcp::NameFormat filename_format,
203         string cpl_name
204         )
205 {
206         auto config = Config::instance ();
207
208         if (config->mail_server().empty()) {
209                 throw NetworkError (_("No mail server configured in preferences"));
210         }
211
212         for (auto const& i: kdms) {
213
214                 if (i.front()->emails().empty()) {
215                         continue;
216                 }
217
218                 auto zip_file = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
219                 boost::filesystem::create_directories (zip_file);
220                 zip_file /= container_name_format.get(i.front()->name_values(), ".zip");
221                 make_zip_file (i, zip_file, filename_format);
222
223                 auto substitute_variables = [cpl_name, i](string target) {
224                         boost::algorithm::replace_all (target, "$CPL_NAME", cpl_name);
225                         boost::algorithm::replace_all (target, "$START_TIME", i.front()->get('b').get_value_or(""));
226                         boost::algorithm::replace_all (target, "$END_TIME", i.front()->get('e').get_value_or(""));
227                         boost::algorithm::replace_all (target, "$CINEMA_NAME", i.front()->get('c').get_value_or(""));
228                         boost::algorithm::replace_all (target, "$CINEMA_SHORT_NAME", i.front()->get('c').get_value_or("").substr(0, 14));
229                         return target;
230                 };
231
232                 auto subject = substitute_variables(config->kdm_subject());
233                 auto body = substitute_variables(config->kdm_email());
234
235                 string screens;
236                 for (auto j: i) {
237                         auto screen_name = j->get('s');
238                         if (screen_name) {
239                                 screens += *screen_name + ", ";
240                         }
241                 }
242                 boost::algorithm::replace_all (body, "$SCREENS", screens.substr (0, screens.length() - 2));
243
244                 Emailer email (config->kdm_from(), i.front()->emails(), subject, body);
245
246                 for (auto i: config->kdm_cc()) {
247                         email.add_cc (i);
248                 }
249                 if (!config->kdm_bcc().empty()) {
250                         email.add_bcc (config->kdm_bcc());
251                 }
252
253                 email.add_attachment (zip_file, container_name_format.get(i.front()->name_values(), ".zip"), "application/zip");
254
255                 try {
256                         email.send (config->mail_server(), config->mail_port(), config->mail_protocol(), config->mail_user(), config->mail_password());
257                 } catch (...) {
258                         boost::filesystem::remove (zip_file);
259                         dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
260                         dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
261                         dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
262                         dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
263                         throw;
264                 }
265
266                 boost::filesystem::remove (zip_file);
267
268                 dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
269                 dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
270                 dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
271                 dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
272         }
273 }