2 Copyright (C) 2013-2017 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 /** @file src/tools/dcpomatic_kdm_cli.cc
22 * @brief Command-line program to generate KDMs.
26 #include "lib/cinema.h"
27 #include "lib/screen_kdm.h"
28 #include "lib/cinema_kdms.h"
29 #include "lib/config.h"
30 #include "lib/exceptions.h"
31 #include "lib/emailer.h"
32 #include "lib/dkdm_wrapper.h"
33 #include "lib/screen.h"
34 #include <dcp/certificate.h>
35 #include <dcp/decrypted_kdm.h>
36 #include <dcp/encrypted_kdm.h>
45 using boost::shared_ptr;
46 using boost::optional;
48 using boost::dynamic_pointer_cast;
53 cerr << "Syntax: " << program_name << " [OPTION] <FILM|CPL-ID|DKDM>\n"
54 " -h, --help show this help\n"
55 " -o, --output output file or directory\n"
56 " -f, --valid-from valid from time (in local time zone of the cinema) (e.g. \"2013-09-28 01:41:51\") or \"now\"\n"
57 " -t, --valid-to valid to time (in local time zone of the cinema) (e.g. \"2014-09-28 01:41:51\")\n"
58 " -d, --valid-duration valid duration (e.g. \"1 day\", \"4 hours\", \"2 weeks\")\n"
59 " -F, --formulation modified-transitional-1, multiple-modified-transitional-1, dci-any or dci-specific [default modified-transitional-1]\n"
60 " -z, --zip ZIP each cinema's KDMs into its own file\n"
61 " -v, --verbose be verbose\n"
62 " -c, --cinema specify a cinema, either by name or email address\n"
63 " -S, --screen screen description\n"
64 " -C, --certificate file containing projector certificate\n"
65 " -T, --trusted-device file containing a trusted device's certificate\n"
66 " --list-cinemas list known cinemas from the DCP-o-matic settings\n"
67 " --list-dkdm-cpls list CPLs for which DCP-o-matic has DKDMs\n\n"
68 "CPL-ID must be the ID of a CPL that is mentioned in DCP-o-matic's DKDM list.\n\n"
70 "Create KDMs for my_great_movie to play in all of Fred's Cinema's screens for the next two weeks and zip them up.\n"
71 "(Fred's Cinema must have been set up in DCP-o-matic's KDM window)\n\n"
72 "\tdcpomatic_kdm -c \"Fred's Cinema\" -f now -d \"2 weeks\" -z my_great_movie\n\n";
78 cerr << program_name << ": " << m << "\n";
82 static boost::posix_time::ptime
83 time_from_string (string t)
86 return boost::posix_time::second_clock::local_time ();
89 return boost::posix_time::time_from_string (t);
92 static boost::posix_time::time_duration
93 duration_from_string (string d)
96 char unit_buf[64] = "\0";
97 sscanf (d.c_str(), "%d %63s", &N, unit_buf);
98 string const unit (unit_buf);
101 cerr << "Could not understand duration \"" << d << "\"\n";
105 if (unit == "year" || unit == "years") {
106 return boost::posix_time::time_duration (N * 24 * 365, 0, 0, 0);
107 } else if (unit == "week" || unit == "weeks") {
108 return boost::posix_time::time_duration (N * 24 * 7, 0, 0, 0);
109 } else if (unit == "day" || unit == "days") {
110 return boost::posix_time::time_duration (N * 24, 0, 0, 0);
111 } else if (unit == "hour" || unit == "hours") {
112 return boost::posix_time::time_duration (N, 0, 0, 0);
115 cerr << "Could not understand duration \"" << d << "\"\n";
126 write_files (list<ScreenKDM> screen_kdms, bool zip, boost::filesystem::path output, dcp::NameFormat::Map values, bool verbose)
129 int const N = CinemaKDMs::write_zip_files (
130 CinemaKDMs::collect (screen_kdms),
132 Config::instance()->kdm_container_name_format(),
133 Config::instance()->kdm_filename_format(),
135 bind (&always_overwrite)
139 cout << "Wrote " << N << " ZIP files to " << output << "\n";
142 int const N = ScreenKDM::write_files (
143 screen_kdms, output, Config::instance()->kdm_filename_format(), values,
144 bind (&always_overwrite)
148 cout << "Wrote " << N << " KDM files to " << output << "\n";
154 find_cinema (string cinema_name)
156 list<shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
157 list<shared_ptr<Cinema> >::const_iterator i = cinemas.begin();
159 i != cinemas.end() &&
160 (*i)->name != cinema_name &&
161 find ((*i)->emails.begin(), (*i)->emails.end(), cinema_name) == (*i)->emails.end()) {
166 if (i == cinemas.end ()) {
167 cerr << program_name << ": could not find cinema \"" << cinema_name << "\"\n";
176 list<shared_ptr<Screen> > screens,
177 boost::filesystem::path film_dir,
179 optional<boost::filesystem::path> output,
180 boost::posix_time::ptime valid_from,
181 boost::posix_time::ptime valid_to,
182 dcp::Formulation formulation,
186 shared_ptr<Film> film;
188 film.reset (new Film (film_dir));
189 film->read_metadata ();
191 cout << "Read film " << film->name () << "\n";
193 } catch (std::exception& e) {
194 cerr << program_name << ": error reading film `" << film_dir.string() << "' (" << e.what() << ")\n";
198 /* XXX: allow specification of this */
199 vector<CPLSummary> cpls = film->cpls ();
201 error ("no CPLs found in film");
202 } else if (cpls.size() > 1) {
203 error ("more than one CPL found in film");
206 boost::filesystem::path cpl = cpls.front().cpl_file;
212 dcp::NameFormat::Map values;
213 values['f'] = film->name();
214 values['b'] = dcp::LocalTime(valid_from).date() + " " + dcp::LocalTime(valid_from).time_of_day(true, false);
215 values['e'] = dcp::LocalTime(valid_to).date() + " " + dcp::LocalTime(valid_to).time_of_day(true, false);
218 list<ScreenKDM> screen_kdms = film->make_kdms (
219 screens, cpl, valid_from, valid_to, formulation
222 write_files (screen_kdms, zip, *output, values, verbose);
223 } catch (FileError& e) {
224 cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
226 } catch (KDMError& e) {
227 cerr << program_name << ": " << e.what() << "\n";
232 optional<dcp::EncryptedKDM>
233 sub_find_dkdm (shared_ptr<DKDMGroup> group, string cpl_id)
235 BOOST_FOREACH (shared_ptr<DKDMBase> i, group->children()) {
236 shared_ptr<DKDMGroup> g = dynamic_pointer_cast<DKDMGroup>(i);
238 optional<dcp::EncryptedKDM> dkdm = sub_find_dkdm (g, cpl_id);
243 shared_ptr<DKDM> d = dynamic_pointer_cast<DKDM>(i);
245 if (d->dkdm().cpl_id() == cpl_id) {
251 return optional<dcp::EncryptedKDM>();
254 optional<dcp::EncryptedKDM>
255 find_dkdm (string cpl_id)
257 return sub_find_dkdm (Config::instance()->dkdms(), cpl_id);
262 dcp::DecryptedKDM dkdm,
263 dcp::Certificate target,
264 vector<dcp::Certificate> trusted_devices,
265 dcp::LocalTime valid_from,
266 dcp::LocalTime valid_to,
267 dcp::Formulation formulation
270 /* Signer for new KDM */
271 shared_ptr<const dcp::CertificateChain> signer = Config::instance()->signer_chain ();
272 if (!signer->valid ()) {
273 error ("signing certificate chain is invalid.");
276 /* Make a new empty KDM and add the keys from the DKDM to it */
277 dcp::DecryptedKDM kdm (
280 dkdm.annotation_text().get_value_or(""),
281 dkdm.content_title_text(),
282 dcp::LocalTime().as_string()
285 BOOST_FOREACH (dcp::DecryptedKDMKey const & j, dkdm.keys()) {
289 return kdm.encrypt (signer, target, trusted_devices, formulation);
294 list<shared_ptr<Screen> > screens,
295 dcp::DecryptedKDM dkdm,
297 optional<boost::filesystem::path> output,
298 boost::posix_time::ptime valid_from,
299 boost::posix_time::ptime valid_to,
300 dcp::Formulation formulation,
308 dcp::NameFormat::Map values;
309 values['f'] = dkdm.annotation_text().get_value_or("");
310 values['b'] = dcp::LocalTime(valid_from).date() + " " + dcp::LocalTime(valid_from).time_of_day(true, false);
311 values['e'] = dcp::LocalTime(valid_to).date() + " " + dcp::LocalTime(valid_to).time_of_day(true, false);
314 list<ScreenKDM> screen_kdms;
315 BOOST_FOREACH (shared_ptr<Screen> i, screens) {
319 screen_kdms.push_back (
326 dcp::LocalTime(valid_from, i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute()),
327 dcp::LocalTime(valid_to, i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute()),
333 write_files (screen_kdms, zip, *output, values, verbose);
334 } catch (FileError& e) {
335 cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
337 } catch (KDMError& e) {
338 cerr << program_name << ": " << e.what() << "\n";
344 dump_dkdm_group (shared_ptr<DKDMGroup> group, int indent)
347 for (int i = 0; i < indent; ++i) {
350 cout << group->name() << "\n";
352 BOOST_FOREACH (shared_ptr<DKDMBase> i, group->children()) {
353 shared_ptr<DKDMGroup> g = dynamic_pointer_cast<DKDMGroup>(i);
355 dump_dkdm_group (g, indent + 2);
357 for (int j = 0; j < indent; ++j) {
360 shared_ptr<DKDM> d = dynamic_pointer_cast<DKDM>(i);
362 cout << d->dkdm().cpl_id() << "\n";
367 int main (int argc, char* argv[])
369 optional<boost::filesystem::path> output;
370 optional<string> cinema_name;
371 shared_ptr<Cinema> cinema;
372 optional<string> screen_description;
373 list<shared_ptr<Screen> > screens;
374 optional<dcp::Certificate> certificate;
375 vector<dcp::Certificate> trusted_devices;
376 optional<dcp::EncryptedKDM> dkdm;
377 optional<boost::posix_time::ptime> valid_from;
378 optional<boost::posix_time::ptime> valid_to;
380 bool list_cinemas = false;
381 bool list_dkdm_cpls = false;
382 optional<string> duration_string;
383 bool verbose = false;
384 dcp::Formulation formulation = dcp::MODIFIED_TRANSITIONAL_1;
386 program_name = argv[0];
388 int option_index = 0;
390 static struct option long_options[] = {
391 { "help", no_argument, 0, 'h'},
392 { "output", required_argument, 0, 'o'},
393 { "valid-from", required_argument, 0, 'f'},
394 { "valid-to", required_argument, 0, 't'},
395 { "valid-duration", required_argument, 0, 'd'},
396 { "formulation", required_argument, 0, 'F' },
397 { "zip", no_argument, 0, 'z' },
398 { "verbose", no_argument, 0, 'v' },
399 { "cinema", required_argument, 0, 'c' },
400 { "screen", required_argument, 0, 'S' },
401 { "certificate", required_argument, 0, 'C' },
402 { "trusted-device", required_argument, 0, 'T' },
403 { "list-cinemas", no_argument, 0, 'B' },
404 { "list-dkdm-cpls", no_argument, 0, 'D' },
408 int c = getopt_long (argc, argv, "ho:f:t:d:F:zvc:S:C:T:BD", long_options, &option_index);
422 valid_from = time_from_string (optarg);
425 valid_to = time_from_string (optarg);
428 duration_string = optarg;
431 if (string (optarg) == "modified-transitional-1") {
432 formulation = dcp::MODIFIED_TRANSITIONAL_1;
433 } else if (string (optarg) == "multiple-modified-transitional-1") {
434 formulation = dcp::MULTIPLE_MODIFIED_TRANSITIONAL_1;
435 } else if (string (optarg) == "dci-any") {
436 formulation = dcp::DCI_ANY;
437 } else if (string (optarg) == "dci-specific") {
438 formulation = dcp::DCI_SPECIFIC;
440 error ("unrecognised KDM formulation " + string (optarg));
451 if (!screen_description) {
452 screen_description = "";
455 shared_ptr<Screen> screen (new Screen (*screen_description, certificate, trusted_devices));
457 cinema->add_screen (screen);
459 screens.push_back (screen);
461 certificate = boost::none;
462 screen_description = boost::none;
463 trusted_devices = vector<dcp::Certificate> ();
466 cinema_name = optarg;
467 cinema = shared_ptr<Cinema> (new Cinema (optarg, list<string> (), "", 0, 0 ));
470 screen_description = optarg;
473 certificate = dcp::Certificate (dcp::file_to_string (optarg));
476 trusted_devices.push_back (dcp::Certificate (dcp::file_to_string (optarg)));
482 list_dkdm_cpls = true;
488 list<boost::shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
489 for (list<boost::shared_ptr<Cinema> >::const_iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
490 cout << (*i)->name << " (" << Emailer::address_list ((*i)->emails) << ")\n";
495 if (list_dkdm_cpls) {
496 dump_dkdm_group (Config::instance()->dkdms(), 0);
501 if (!screen_description) {
502 screen_description = "";
505 shared_ptr<Screen> screen (new Screen (*screen_description, certificate, trusted_devices));
507 cinema->add_screen (screen);
509 screens.push_back (screen);
512 if (!duration_string && !valid_to) {
513 error ("you must specify a --valid-duration or --valid-to");
517 error ("you must specify --valid-from");
521 if (optind >= argc) {
526 if (screens.empty()) {
528 error ("you must specify either a cinema or one or more screens using certificate files");
531 screens = find_cinema (*cinema_name)->screens ();
532 } else if (!cinema_name && !output) {
533 error ("you must specify either a cinema name or output format");
536 if (duration_string) {
537 valid_to = valid_from.get() + duration_from_string (*duration_string);
540 dcpomatic_setup_path_encoding ();
544 cout << "Making KDMs valid from " << valid_from.get() << " to " << valid_to.get() << "\n";
547 string const thing = argv[optind];
548 if (boost::filesystem::is_directory(thing) && boost::filesystem::is_regular_file(boost::filesystem::path(thing) / "metadata.xml")) {
549 from_film (screens, thing, verbose, output, *valid_from, *valid_to, formulation, zip);
551 if (boost::filesystem::is_regular_file(thing)) {
552 dkdm = dcp::EncryptedKDM (dcp::file_to_string (thing));
554 dkdm = find_dkdm (thing);
558 error ("could not find film or CPL ID corresponding to " + thing);
561 from_dkdm (screens, dcp::DecryptedKDM (*dkdm, Config::instance()->decryption_chain()->key().get()), verbose, output, *valid_from, *valid_to, formulation, zip);