wip: stuff.
[dcpomatic.git] / src / lib / recipient_with_dkdm.cc
1 /*
2     Copyright (C) 2020 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 #include "recipient_with_dkdm.h"
22 #include "config.h"
23 #include "dcpomatic_log.h"
24 #include "emailer.h"
25 #include "exceptions.h"
26 #include "zipper.h"
27 #include "util.h"
28 #include <boost/foreach.hpp>
29
30 #include "i18n.h"
31
32 using std::list;
33 using std::vector;
34 using std::string;
35 using boost::shared_ptr;
36
37 void
38 RecipientWithDKDM::make_zip_file (boost::filesystem::path zip_file, dcp::NameFormat name_format, dcp::NameFormat::Map name_values) const
39 {
40         Zipper zipper (zip_file);
41
42         name_values['i'] = kdm.cpl_id ();
43         string const name = careful_string_filter(name_format.get(name_values, ".xml"));
44         zipper.add (name, kdm.as_xml());
45
46         zipper.close ();
47 }
48
49
50 /** Email one ZIP file per recipient.
51  *  @param kdms KDMs to email.
52  *  @param filename_format Format of filenames to use.
53  *  @param name_values Values to substitute into \p container_name_format and \p filename_format.
54  *  @param cpl_name Name of the CPL that the KDMs are for.
55  */
56 void
57 RecipientWithDKDM::email (
58         vector<RecipientWithDKDM> kdms,
59         dcp::NameFormat filename_format,
60         dcp::NameFormat::Map name_values,
61         string cpl_name
62         )
63 {
64         Config* config = Config::instance ();
65
66         if (config->mail_server().empty()) {
67                 throw NetworkError (_("No mail server configured in preferences"));
68         }
69
70         BOOST_FOREACH (RecipientWithDKDM const & i, kdms) {
71
72                 if (i.recipient->emails.empty()) {
73                         continue;
74                 }
75
76                 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
77                 boost::filesystem::create_directories (zip_file);
78                 zip_file /= filename_format.get(name_values, ".zip");
79                 i.make_zip_file (zip_file, filename_format, name_values);
80
81                 string subject = config->kdm_subject();
82                 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
83                 boost::algorithm::replace_all (subject, "$START_TIME", name_values['b']);
84                 boost::algorithm::replace_all (subject, "$END_TIME", name_values['e']);
85
86                 string body = config->kdm_email().c_str();
87                 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_name);
88                 boost::algorithm::replace_all (body, "$START_TIME", name_values['b']);
89                 boost::algorithm::replace_all (body, "$END_TIME", name_values['e']);
90
91                 Emailer email (config->kdm_from(), i.recipient->emails, subject, body);
92
93                 BOOST_FOREACH (string i, config->kdm_cc()) {
94                         email.add_cc (i);
95                 }
96                 if (!config->kdm_bcc().empty ()) {
97                         email.add_bcc (config->kdm_bcc ());
98                 }
99
100                 email.add_attachment (zip_file, filename_format.get(name_values, ".zip"), "application/zip");
101
102                 try {
103                         email.send (config->mail_server(), config->mail_port(), config->mail_protocol(), config->mail_user(), config->mail_password());
104                 } catch (...) {
105                         boost::filesystem::remove (zip_file);
106                         dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
107                         dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
108                         dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
109                         dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
110                         throw;
111                 }
112
113                 boost::filesystem::remove (zip_file);
114
115                 dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
116                 dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
117                 dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
118                 dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
119         }
120 }
121
122
123 int
124 RecipientWithDKDM::write_files (
125         vector<RecipientWithDKDM> kdms,
126         boost::filesystem::path directory,
127         dcp::NameFormat name_format,
128         dcp::NameFormat::Map name_values,
129         boost::function<bool (boost::filesystem::path)> confirm_overwrite
130         )
131 {
132         int written = 0;
133
134         if (!boost::filesystem::exists (directory)) {
135                 boost::filesystem::create_directories (directory);
136         }
137
138         /* Write KDMs to the specified directory */
139         BOOST_FOREACH (RecipientWithDKDM const& i, kdms) {
140                 name_values['i'] = i.kdm.cpl_id ();
141                 boost::filesystem::path out = directory / careful_string_filter(name_format.get(name_values, ".xml"));
142                 if (!boost::filesystem::exists (out) || confirm_overwrite (out)) {
143                         i.kdm.as_xml (out);
144                         ++written;
145                 }
146         }
147
148         return written;
149 }