aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Youri/Check/Output/Mail.pm
blob: eb9a1f2561d7a33b3c278d781d8dc3902c7f51b1 (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
156
# $Id: Mail.pm 1179 2006-08-05 08:30:57Z warly $
package Youri::Check::Output::Mail;

=head1 NAME

Youri::Check::Output::Mail - Report results by mail

=head1 DESCRIPTION

This plugin reports results by mail. Additional subplugins handle specific
formats.

=cut

use warnings;
use strict;
use Carp;
use MIME::Entity;
use Youri::Utils;
use base 'Youri::Check::Output';

sub _init {
    my $self = shift;
    my %options = (
        from     => '', # mail from header
        to       => '', # mail to header
        reply_to => '', # mail reply-to header
        mta      => '', # mta path
        noempty  => 1,  # don't generate empty reports
        formats  => {},
        @_
    );

    croak "no format defined" unless $options{formats};
    croak "formats should be an hashref" unless ref $options{formats} eq 'HASH';

    $self->{_from}       = $options{from};
    $self->{_to}         = $options{to};
    $self->{_reply_to}   = $options{reply_to};
    $self->{_mta}        = $options{mta};
    $self->{_noempty}    = $options{noempty};

    foreach my $id (keys %{$options{formats}}) {
        print "Creating format $id\n" if $options{verbose};
        eval {
            push(
                @{$self->{_formats}},
                create_instance(
                    'Youri::Check::Output::Mail::Format',
                    id        => $id,
                    test      => $options{test},
                    verbose   => $options{verbose},
                    %{$options{formats}->{$id}}
                )
            );
        };
        print STDERR "Failed to create format $id: $@\n" if $@;
    }

    croak "no formats created" unless @{$self->{_formats}};
}

sub _global_report {
    my ($self, $resultset, $type, $columns, $links) = @_;

    foreach my $format (@{$self->{_formats}}) {
        my $iterator = $resultset->get_iterator(
            $type,
            [ 'package' ]
        );

        return if $self->{_noempty} && ! $iterator->has_results();

        my $content = $format->get_report(
            $self->{_time},
            "$type global report",
            $iterator,
            $type,
            $columns,
            $links,
            undef
        );

        $self->_send_mail(
            $format->type(),
            $self->{_to},
            "$type global report",
            $content,
        );
    }
}

sub _individual_report {
    my ($self, $resultset, $type, $columns, $links, $maintainer) = @_;

    foreach my $format (@{$self->{_formats}}) {
        my $iterator = $resultset->get_iterator(
            $type,
            [ 'package' ],
            { maintainer => [ $maintainer ] }
        );

        return if $self->{_noempty} && ! $iterator->has_results();

        my $content = $format->get_report(
            $self->{_time},
            "$type individual report for $maintainer",
            $iterator,
            $type,
            $columns,
            $links,
            $maintainer
        );

        $self->_send_mail(
            $format->type(),
            $maintainer,
            "$type individual report for $maintainer",
            $content,
        );
    }

}

sub _send_mail {
    my ($self, $type, $to, $subject, $content) = @_;

    return unless $content;

    my $mail = MIME::Entity->build(
        'Type'     => $type,
        'From'     => $self->{_from},
        'Reply-To' => $self->{_reply_to},
        'To'       => $to,
        'Subject'  => $subject,
        'Data'     => $$content
    );

    if ($self->{_test}) {
        $mail->print(\*STDOUT);
    } else {
        open(MAIL, "| $self->{_mta} -t -oi -oem") or die "Can't open MTA program: $!";
        $mail->print(\*MAIL);
        close MAIL;
    }
}

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2002-2006, YOURI project

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=cut

1;