File: sreview-notify

package info (click to toggle)
sreview 0.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,504 kB
  • sloc: perl: 12,360; javascript: 509; sh: 72; makefile: 8
file content (165 lines) | stat: -rw-r--r-- 4,842 bytes parent folder | download
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
#!/usr/bin/perl -w

use v5.28;
use strict;
use warnings;

use DBI;
use Mojo::Template;
use Mojo::URL;
use Mojo::UserAgent;
use File::Temp qw/tempfile/;
use SReview::Config::Common;
use SReview::Talk;
use Email::Stuffer;
use Email::Address;
use Email::Sender::Simple qw/sendmail/;

my $config = SReview::Config::Common::setup;

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

my $talkid = shift;

die "need talk ID!" unless defined($talkid);

my $configprefix = shift;
$configprefix = 'notify' unless defined($configprefix);

say "Sending out notification for talk with ID $talkid";

$dbh->prepare("UPDATE talks SET progress='running' WHERE id=?")->execute($talkid);

my $mt = Mojo::Template->new;
$mt->vars(1);
my $title;
my $url;
my $overview;
my $talk;

sub notify_email() {
	my $to_query = $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 $cc_query = $dbh->prepare("SELECT tracks.name, tracks.email FROM tracks JOIN talks ON (talks.track = tracks.id) WHERE talks.id = ?");

	$to_query->execute($talkid);
	my $to = $to_query->fetchall_arrayref();
	my @to_recips;

	foreach my $to_recip(@$to) {
		if(defined($to_recip->[1])) {
			my $rcp = $to_recip->[0];
			$rcp =~ s/,//g;
			push @to_recips, $rcp . " <" . $to_recip->[1] . ">";
		}
	}

	$cc_query->execute($talkid);
	my $cc = $cc_query->fetchall_arrayref();
	my @cc_recips;

	foreach my $cc_recip(@$cc) {
		if(defined($cc_recip->[1])) {
			my $rcp = $cc_recip->[0];
			$rcp =~ s/,//g;
			push @cc_recips, "responsible for track " . $rcp . " <" . $cc_recip->[1] . ">";
		}
	}

	if(scalar(@to_recips) == 0 && scalar(@cc_recips) == 0) {
		print "no addressees, can't send email for talk with id $talkid";
		return;
	} elsif(scalar(@to_recips) == 0 && scalar(@cc_recips) > 0) {
                @to_recips = @cc_recips;
                @cc_recips = ();
        }

	my $file = $config->get("${configprefix}_email_template");
	$file = $config->get("email_template") unless defined($file);
	my $template_url = Mojo::URL->new($file);
	my $template;
	if(defined($template_url->scheme)) {
		my $res = Mojo::UserAgent->new->get($template_url)->result;
		die "could not download $template_url" unless $res->is_success;
		(undef, $file) = tempfile();
		$res->save_to($file);
	}
	my $body = $mt->render_file($file, {title => $title, url => $url, overview => $overview, talk => $talk});
	my $subject = $config->get("${configprefix}_email_subject");
	$subject = $config->get("email_subject") unless defined($subject);
	my @references;
	my $nonce = $talk->nonce;
	my $refid = $talk->corrections->{serial};
	my $action = 'ntf';
	if($configprefix eq 'announce') {
		$refid--;
		$action = 'ann';
	} elsif ($configprefix eq 'notify_final') {
		$refid--;
		$action = 'fnt';
	}
	my @addrs = Email::Address->parse($config->get('email_from'));
	my $host = $addrs[0]->host;
	for (; $refid >= 0; $refid--) {
		push @references, "<sreview-$nonce-$refid-ntf\@$host>";
	}
	$refid = $talk->corrections->{serial};
	my $email = Email::Stuffer->from($config->get('email_from'))
		->to(@to_recips)
		->subject($mt->render($subject, {title => $title}))
		->cc(@cc_recips)
		->header(References => join(",\n  ", @references))
		->header("Message-ID" => "<sreview-$nonce-$refid-$action\@$host>")
		->text_body($body);
	say "Sending out " . $email->as_string;
	sendmail($email->as_string);
}

sub notify_command() {
	my $data = $dbh->prepare("SELECT title, nonce FROM talks WHERE id = ?");
	$data->execute($talkid);
	my $row = $data->fetchrow_hashref;

	foreach my $command(@{$config->get("${configprefix}_commands")}) {
		my @run;
		foreach my $component(@$command) {
			my $rendered = $mt->render($component, {title => $title, url => $url, overview => $overview, talk => $talk});
			chomp($rendered);
			push @run, $rendered;
		}
		say "Running '" . join("', '", @run) . "'";
		system(@run);
	}
}

my $actions = $config->get("${configprefix}_actions");

$talk = SReview::Talk->new(talkid => $talkid);
$title = $talk->title;
my $save = $/;
my $urlbase = $config->get('urlbase');
$/ = '/';
chomp($urlbase);
$/ = $save;
if($configprefix eq 'notify_final') {
	$url = join('/', ($urlbase, "f", $talk->nonce));
} else {
	$url = join('/', ($urlbase, "r", $talk->nonce));
}
$overview = join('/', ($urlbase, "overview"));

say "Performing " . scalar(@$actions) . " actions for notification:";
foreach my $action(@$actions) {
	if($action eq "email") {
		say "sending email";
		notify_email();
	} elsif($action eq "command") {
		say "running command";
		notify_command();
	} else {
		die "Unknown notification action $action!";
	}
}

$dbh->prepare("UPDATE talks SET progress='done' WHERE id = ?")->execute($talkid) or die $!;

say "finished";