summaryrefslogtreecommitdiff
path: root/src/lib/kdm_with_metadata.cc
blob: f7ff8443536d8ad7cfa92b70c082946d461beb17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
    Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "cinema.h"
#include "config.h"
#include "cross.h"
#include "dcpomatic_log.h"
#include "email.h"
#include "kdm_with_metadata.h"
#include "screen.h"
#include "util.h"
#include "zipper.h"
#include <dcp/file.h>
#include <dcp/filesystem.h>

#include "i18n.h"


using std::cout;
using std::function;
using std::list;
using std::shared_ptr;
using std::string;
using std::vector;
using boost::optional;


int
write_files (
	list<KDMWithMetadataPtr> kdms,
	boost::filesystem::path directory,
	dcp::NameFormat name_format,
	std::function<bool (boost::filesystem::path)> confirm_overwrite
	)
{
	int written = 0;

	if (directory == "-") {
		/* Write KDMs to the stdout */
		for (auto i: kdms) {
			cout << i->kdm_as_xml ();
			++written;
		}

		return written;
	}

	if (!dcp::filesystem::exists(directory)) {
		dcp::filesystem::create_directories(directory);
	}

	/* Write KDMs to the specified directory */
	for (auto i: kdms) {
		auto out = directory / careful_string_filter(name_format.get(i->name_values(), ".xml"));
		if (!dcp::filesystem::exists(out) || confirm_overwrite(out)) {
			i->kdm_as_xml (out);
			++written;
		}
	}

	return written;
}


optional<string>
KDMWithMetadata::get (char k) const
{
	auto i = _name_values.find (k);
	if (i == _name_values.end()) {
		return {};
	}

	return i->second;
}


void
make_zip_file (list<KDMWithMetadataPtr> kdms, boost::filesystem::path zip_file, dcp::NameFormat name_format)
{
	Zipper zipper (zip_file);

	for (auto i: kdms) {
		auto const name = careful_string_filter(name_format.get(i->name_values(), ".xml"));
		zipper.add (name, i->kdm_as_xml());
	}

	zipper.close ();
}


/** Collect a list of KDMWithMetadatas into a list of lists so that
 *  each list contains the KDMs for one list.
 */
list<list<KDMWithMetadataPtr>>
collect (list<KDMWithMetadataPtr> kdms)
{
	list<list<KDMWithMetadataPtr>> grouped;

	for (auto i: kdms) {

		auto j = grouped.begin ();

		while (j != grouped.end()) {
			if (j->front()->group() == i->group()) {
				j->push_back (i);
				break;
			}
			++j;
		}

		if (j == grouped.end()) {
			grouped.push_back (list<KDMWithMetadataPtr>());
			grouped.back().push_back (i);
		}
	}

	return grouped;
}


/** Write one directory per list into another directory */
int
write_directories (
	list<list<KDMWithMetadataPtr>> kdms,
	boost::filesystem::path directory,
	dcp::NameFormat container_name_format,
	dcp::NameFormat filename_format,
	function<bool (boost::filesystem::path)> confirm_overwrite
	)
{
	int written = 0;

	for (auto const& kdm: kdms) {
		auto path = directory;
		path /= container_name_format.get(kdm.front()->name_values(), "", "s");
		if (!dcp::filesystem::exists(path) || confirm_overwrite(path)) {
			dcp::filesystem::create_directories(path);
			write_files(kdm, path, filename_format, confirm_overwrite);
			written += kdm.size();
		}
	}

	return written;
}


/** Write one ZIP file per cinema into a directory */
int
write_zip_files (
	list<list<KDMWithMetadataPtr>> kdms,
	boost::filesystem::path directory,
	dcp::NameFormat container_name_format,
	dcp::NameFormat filename_format,
	function<bool (boost::filesystem::path)> confirm_overwrite
	)
{
	int written = 0;

	for (auto const& kdm: kdms) {
		auto path = directory;
		path /= container_name_format.get(kdm.front()->name_values(), ".zip", "s");
		if (!dcp::filesystem::exists(path) || confirm_overwrite(path)) {
			if (dcp::filesystem::exists(path)) {
				/* Creating a new zip file over an existing one is an error */
				dcp::filesystem::remove(path);
			}
			make_zip_file(kdm, path, filename_format);
			written += kdm.size();
		}
	}

	return written;
}


/** Email one ZIP file per cinema to the cinema.
 *  @param kdms KDMs to email.
 *  @param container_name_format Format of folder / ZIP to use.
 *  @param filename_format Format of filenames to use.
 *  @param name_values Values to substitute into \p container_name_format and \p filename_format.
 *  @param cpl_name Name of the CPL that the KDMs are for.
 */
void
send_emails (
	list<list<KDMWithMetadataPtr>> kdms,
	dcp::NameFormat container_name_format,
	dcp::NameFormat filename_format,
	string cpl_name,
	vector<string> extra_addresses
	)
{
	auto config = Config::instance ();

	if (config->mail_server().empty()) {
		throw MissingConfigurationError(_("No outgoing mail server configured in the Email tab of preferences"));
	}

	if (config->kdm_from().empty()) {
		throw MissingConfigurationError(_("No from address configured in the KDM Email tab of preferences"));
	}

	for (auto const& kdms_for_cinema: kdms) {

		auto first = kdms_for_cinema.front();

		auto zip_file = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
		dcp::filesystem::create_directories(zip_file);
		zip_file /= container_name_format.get(first->name_values(), ".zip");
		make_zip_file (kdms_for_cinema, zip_file, filename_format);

		auto substitute_variables = [cpl_name, first](string target) {
			boost::algorithm::replace_all(target, "$CPL_NAME", cpl_name);
			boost::algorithm::replace_all(target, "$START_TIME", first->get('b').get_value_or(""));
			boost::algorithm::replace_all(target, "$END_TIME", first->get('e').get_value_or(""));
			boost::algorithm::replace_all(target, "$CINEMA_NAME", first->get('c').get_value_or(""));
			boost::algorithm::replace_all(target, "$CINEMA_SHORT_NAME", first->get('c').get_value_or("").substr(0, 14));
			return target;
		};

		auto subject = substitute_variables(config->kdm_subject());
		auto body = substitute_variables(config->kdm_email());

		vector<string> screens;
		for (auto kdm: kdms_for_cinema) {
			if (auto screen_name = kdm->get('s')) {
				screens.push_back(*screen_name);
			}
		}
		boost::algorithm::replace_all(body, "$SCREENS", screen_names_to_string(screens));

		auto emails = first->emails();
		std::copy(extra_addresses.begin(), extra_addresses.end(), std::back_inserter(emails));
		if (emails.empty()) {
			continue;
		}

		Email email(config->kdm_from(), { emails.front() }, subject, body);

		/* Use CC for the second and subsequent email addresses, so we seem less spammy (#2310) */
		for (auto cc = std::next(emails.begin()); cc != emails.end(); ++cc) {
			email.add_cc(*cc);
		}

		for (auto cc: config->kdm_cc()) {
			email.add_cc (cc);
		}
		if (!config->kdm_bcc().empty()) {
			email.add_bcc (config->kdm_bcc());
		}

		email.add_attachment (zip_file, container_name_format.get(first->name_values(), ".zip"), "application/zip");
		dcp::filesystem::remove(zip_file);

		auto log_details = [](Email& email) {
			dcpomatic_log->log("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
			dcpomatic_log->log(email.email(), LogEntry::TYPE_DEBUG_EMAIL);
			dcpomatic_log->log("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
			dcpomatic_log->log(email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
		};

		try {
			email.send (config->mail_server(), config->mail_port(), config->mail_protocol(), config->mail_user(), config->mail_password());
		} catch (...) {
			log_details (email);
			throw;
		}

		log_details (email);
	}
}