#!/usr/bin/perl

# SReview, a web-based video review and transcoding system
# Copyright (c) 2016-2017, Wouter Verhelst <w@uter.be>
#
# SReview is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program 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
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program.  If not, see
# <http://www.gnu.org/licenses/>.

use strict;
use warnings;

use Email::Sender::Simple qw(sendmail);
use Email::Simple;
use DBI;

our $config;

require './config.pl';

my $dbh = DBI->connect($config->{dbistring}, '', '') or die "Cannot connect to database!";

my $talkid = $ARGV[0];

my $recps = $dbh->prepare("SELECT speakers.name, speakers.email FROM speakers JOIN speakers_talks ON speakers_talks.speaker = speakers.id WHERE speakers_talks.talk = ? AND speakers.email IS NOT NULL");
my $ccs = $dbh->prepare("SELECT tracks.name, tracks.email FROM tracks JOIN talks ON (talks.track = tracks.id) WHERE talks.id = ?");
my $talk = $dbh->prepare("SELECT title, nonce FROM talks WHERE id = ?");

$recps->execute($talkid);
my $recplist = $recps->fetchall_arrayref();
my @speakers;

$ccs->execute($talkid);
my $trackmgrlist = $ccs->fetchall_arrayref();
my @trackmgrs;

foreach my $speaker(@$recplist) {
	if(defined($speaker->[1])) {
		push @speakers, $speaker->[0] . " <" . $speaker->[1] . ">";
	}
}

foreach my $trackmgr(@$trackmgrlist) {
	if(defined($trackmgr->[1])) {
		push @trackmgrs, "managers for devroom " . $trackmgr->[0] . " <" . $trackmgr->[1] . ">";
	}
}

if(scalar(@trackmgrs) == 0 && scalar(@speakers) == 0) {
	die "no addressees, can't send email!";
}

$talk->execute($talkid);
my $talkdata = $talk->fetchrow_hashref;
my $title = $talkdata->{title};
$title =~ s/[^[:ascii:]]//g;
my $save = $/;
$/ = '/';
chomp($config->{urlbase});
$/ = $save;
my $url = join('/', ($config->{urlbase}, "review", $talkdata->{nonce}));

my $body = <<EOF;
Hi,

This is to inform you that a preview of the recording of the talk titled

$title

is now available at

$url

for you to review.

You may already have received this email before; if so, that means you
submitted the talk with some corrections, and a new cut was made. I'm
sorry that the system doesn't have the ability to know whether that is
the case; at any rate, please check out the above URL and choose the
appropriate answer.

Please note that with the packed schedule at FOSDEM, and the requirement
of manual review for every video, it takes a lot of work for one person,
or even just one team, to handle the review workload. Yet, reviewing
just one video is often no more than a few minutes of work. If you were
to help us out, we could release your video much sooner than would
otherwise be the case.

If you do not find the time to help us out with this, we will eventually
get around to it ourselves, but this will take much longer.

In particular, you should pay attention to:
- Does the video start at the correct time? If not, enter new start and
  length values, or use the buttons below the video elements to help you
  calculate the correct values.
- Is the audio usable? If not, you may want to try to use the alternate
  audio channel.

Note: if you click on the above link, and you do not see the video,
there are two possibilities:
- Someone else already did a review of this video, but found some issues
  and entered the right values to fix that. A new cut is being
  generated. You'll get this email a second (or third, or fourth, ...)
  time when the video is ready for a new review.
- Someone else already did a review of this video, found no issues, and
  released the video. It's being transcoded to WebM now. Once that's
  finished, the video will appear on video.fosdem.org and (eventually)
  your event page on the FOSDEM schedule.

If you have any further questions, note that this email was sent from an
address that does not receive email. If you want to reach us, you can do
so via:

IRC: irc.freenode.net, #fosdem-video
email: video\@lists.fosdem.org
or just fill out the "comments" field in the URL above

Thank you for your cooperation,

The FOSDEM video team
EOF

my $email = Email::Simple->create(header =>
	       	[
			From => '<noreply@fosdem.org>',
	       		To => join(',',@speakers),
	       		Subject => "Video of your FOSDEM talk entitled \"$title\" ready for review",
	       		Cc => join(',',@trackmgrs)
	       	],
	       	body => $body
	);

sendmail($email);
