Better error if no mail server has been configured.
[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 "quickmail.h"
21 #include "exceptions.h"
22 #include "cinema_kdms.h"
23 #include "cinema.h"
24 #include "screen.h"
25 #include "config.h"
26 #include "util.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::string;
35 using boost::shared_ptr;
36
37 /** @param filename_first_part First part of name of KDM files inside the zip file
38  *  (perhaps the name of the film).
39  */
40 void
41 CinemaKDMs::make_zip_file (string filename_first_part, boost::filesystem::path zip_file) const
42 {
43         int error;
44         struct zip* zip = zip_open (zip_file.string().c_str(), ZIP_CREATE | ZIP_EXCL, &error);
45         if (!zip) {
46                 if (error == ZIP_ER_EXISTS) {
47                         throw FileError ("ZIP file already exists", zip_file);
48                 }
49                 throw FileError ("could not create ZIP file", zip_file);
50         }
51
52         list<shared_ptr<string> > kdm_strings;
53
54         BOOST_FOREACH (ScreenKDM const & i, screen_kdms) {
55                 shared_ptr<string> kdm (new string (i.kdm.as_xml ()));
56                 kdm_strings.push_back (kdm);
57
58                 struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
59                 if (!source) {
60                         throw StringError ("could not create ZIP source");
61                 }
62
63                 if (zip_add (zip, i.filename(filename_first_part).c_str(), source) == -1) {
64                         throw StringError ("failed to add KDM to ZIP archive");
65                 }
66         }
67
68         if (zip_close (zip) == -1) {
69                 throw StringError ("failed to close ZIP archive");
70         }
71 }
72
73 list<CinemaKDMs>
74 CinemaKDMs::collect (list<ScreenKDM> screen_kdms)
75 {
76         list<CinemaKDMs> cinema_kdms;
77
78         while (!screen_kdms.empty ()) {
79
80                 /* Get all the screens from a single cinema */
81
82                 CinemaKDMs ck;
83
84                 list<ScreenKDM>::iterator i = screen_kdms.begin ();
85                 ck.cinema = i->screen->cinema;
86                 ck.screen_kdms.push_back (*i);
87                 list<ScreenKDM>::iterator j = i;
88                 ++i;
89                 screen_kdms.remove (*j);
90
91                 while (i != screen_kdms.end ()) {
92                         if (i->screen->cinema == ck.cinema) {
93                                 ck.screen_kdms.push_back (*i);
94                                 list<ScreenKDM>::iterator j = i;
95                                 ++i;
96                                 screen_kdms.remove (*j);
97                         } else {
98                                 ++i;
99                         }
100                 }
101
102                 cinema_kdms.push_back (ck);
103         }
104
105         return cinema_kdms;
106 }
107
108 void
109 CinemaKDMs::write_zip_files (string filename_first_part, list<CinemaKDMs> cinema_kdms, boost::filesystem::path directory)
110 {
111         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
112                 boost::filesystem::path path = directory;
113                 path /= tidy_for_filename (i.cinema->name) + ".zip";
114                 i.make_zip_file (filename_first_part, path);
115         }
116 }
117
118 /* XXX: should probably get from/to from the KDMs themselves */
119 void
120 CinemaKDMs::email (string filename_first_part, string cpl_name, list<CinemaKDMs> cinema_kdms, dcp::LocalTime from, dcp::LocalTime to)
121 {
122         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
123
124                 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path ();
125                 zip_file /= boost::filesystem::unique_path().string() + ".zip";
126                 i.make_zip_file (filename_first_part, zip_file);
127
128                 /* Send email */
129
130                 quickmail_initialize ();
131
132                 SafeStringStream start;
133                 start << from.date() << " " << from.time_of_day();
134                 SafeStringStream end;
135                 end << to.date() << " " << to.time_of_day();
136
137                 string subject = Config::instance()->kdm_subject();
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                 quickmail mail = quickmail_create (Config::instance()->kdm_from().c_str(), subject.c_str ());
143
144                 quickmail_add_to (mail, i.cinema->email.c_str ());
145                 if (!Config::instance()->kdm_cc().empty ()) {
146                         quickmail_add_cc (mail, Config::instance()->kdm_cc().c_str ());
147                 }
148                 if (!Config::instance()->kdm_bcc().empty ()) {
149                         quickmail_add_bcc (mail, Config::instance()->kdm_bcc().c_str ());
150                 }
151
152                 string body = Config::instance()->kdm_email().c_str();
153                 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_name);
154                 boost::algorithm::replace_all (body, "$START_TIME", start.str ());
155                 boost::algorithm::replace_all (body, "$END_TIME", end.str ());
156                 boost::algorithm::replace_all (body, "$CINEMA_NAME", i.cinema->name);
157
158                 SafeStringStream screens;
159                 BOOST_FOREACH (ScreenKDM const & j, i.screen_kdms) {
160                         screens << j.screen->name << ", ";
161                 }
162                 boost::algorithm::replace_all (body, "$SCREENS", screens.str().substr (0, screens.str().length() - 2));
163
164                 quickmail_set_body (mail, body.c_str());
165                 quickmail_add_attachment_file (mail, zip_file.string().c_str(), "application/zip");
166
167                 char const* e = quickmail_send (
168                         mail,
169                         Config::instance()->mail_server().c_str(),
170                         Config::instance()->mail_port(),
171                         Config::instance()->mail_user().c_str(),
172                         Config::instance()->mail_password().c_str()
173                         );
174
175                 if (e) {
176                         quickmail_destroy (mail);
177
178                         string error (e);
179
180                         if (Config::instance()->mail_server().empty ()) {
181                                 error = _("no mail server set up in preferences");
182                         }
183
184                         throw KDMError (
185                                 String::compose (
186                                         _("Failed to send KDM email to %1 (%2)"),
187                                         i.cinema->email,
188                                         error
189                                         )
190                                 );
191                 }
192                 quickmail_destroy (mail);
193         }
194 }