aboutsummaryrefslogtreecommitdiffstats
path: root/mkadvisories
blob: 0f767cb943c464b0a39e49c23d8aa1da66465eda (plain)
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
#!/usr/bin/perl -w

use strict;

use YAML qw(LoadFile DumpFile);
use Template;
use DateTime;
use Email::Sender::Simple qw(try_to_sendmail);
use Email::Simple;
use Email::Simple::Creator;
#use Data::Dump qw(dd);

my $config_file = '/usr/share/mga-advisories/config';
my $config = LoadFile($ENV{MGAADV_CONF} || $config_file);
my $etc_config_file = '/etc/mga-advisories.conf';
my $etc_config = LoadFile($etc_config_file);

my @report_logs;
sub report_log {
    push @report_logs, @_;
}

sub report_exit {
    report_log($_[0]);
    send_report_mail({ error => $_[0] });
    exit 1;
}

foreach my $k (keys %$etc_config) {
    $config->{$k} = $etc_config->{$k};
}

sub status_file {
    $config->{status_dir} . '/' . $_[0];
}

sub save_status {
    my ($advdb, $adv) = @_;
    my $statusfile = status_file($adv);
    DumpFile($statusfile, $advdb->{advisories}{$adv}{status});
}

sub get_advisories {
    my %advisories;
    foreach my $advfile (glob "$config->{advisories_dir}/*.adv") {
        my $adv = LoadFile($advfile);
        next unless $adv->{ID};
        report_exit("Duplicate advisory $adv->{ID}") if $advisories{$adv->{ID}};
        $advisories{$adv->{ID}} = $adv;
        my $statusfile = status_file($adv->{ID});
        $adv->{status} = -f $statusfile ? LoadFile($statusfile) : {};
    }
    return \%advisories;
}

sub process_template {
    my ($template, $src, $vars, $dest, $ext) = @_;
    foreach my $extension ($ext ? $ext : @{$config->{output_format}}) {
        next unless -f "$config->{tmpl_dir}/$src.$extension";
        $template->process("$src.$extension", $vars, ref $dest ? $dest : "$dest.$extension")
                || die $template->error, "\n";
    }
}

sub output_pages {
    my ($advdb) = @_;
    my $template = Template->new(
        INCLUDE_PATH => $config->{tmpl_dir},
        OUTPUT_PATH  => $config->{out_dir},
    );
    foreach my $adv (keys %{$advdb->{advisories}}) {
        my $vars = {
            config   => $config,
            advisory => $adv,
            advdb    => $advdb,
        };
        process_template($template, 'advisory', $vars, $adv);
    }
    my $vars = {
        config   => $config,
        advdb    => $advdb,
    };
    process_template($template, 'index', $vars, 'index');
}

sub send_adv_mail {
    my ($advdb) = @_;
    return unless $config->{send_adv_mail} eq 'yes';
    my $template = Template->new(
        INCLUDE_PATH => $config->{tmpl_dir},
    );
    foreach my $adv (keys %{$advdb->{advisories}}) {
        next if $advdb->{advisories}{$adv}{status}{mail_sent};
        my $mailcontent;
        my $vars = {
            config   => $config,
            advisory => $adv,
            advdb    => $advdb,
        };
        process_template($template, 'advisory', $vars, \$mailcontent, 'txt');
        my $email = Email::Simple->create(
            header => [
                To   => $config->{adv_mail_to},
                From => $config->{adv_mail_from},
                Subject => "$adv: " . $advdb->{advisories}{$adv}{subject},
            ],
            body   => $mailcontent
        );
        if (try_to_sendmail($email)) {
            report_log("Advisory mail for $adv sent");
            $advdb->{advisories}{$adv}{status}{mail_sent} = time();
            save_status($advdb, $adv);
        } else {
            report_log("Error sending advisory mail $adv");
        }
    }
}

sub send_report_mail {
    my ($advdb) = @_;
    return unless $config->{send_report_mail} eq 'yes';
    return unless @report_logs;
    my $template = Template->new(
        INCLUDE_PATH => $config->{tmpl_dir},
    );
    my $mailcontent;
    my $vars = {
        config      => $config,
        advdb       => $advdb,
        report_logs => \@report_logs,
    };
    process_template($template, 'report', $vars, \$mailcontent, 'txt');
    my $email = Email::Simple->create(
        header => [
            To   => $config->{report_mail_to},
            From => $config->{report_mail_from},
            Subject => $advdb->{error} ? 'Advisories Error' : 'Advisories Update',
        ],
        body   => $mailcontent
    );
    try_to_sendmail($email);
}

sub dumpdb {
    my ($advdb) = @_;
    DumpFile($config->{out_dir} . '/advisories.yaml', $advdb->{advisories});
}

my %advdb;
$advdb{advisories} = get_advisories();
output_pages(\%advdb);
dumpdb(\%advdb);
send_adv_mail(\%advdb);
send_report_mail(\%advdb);