Imported Upstream version 3.3.3
[debian/amanda] / perl / Amanda / Interactivity / email.pm
1 # Copyright (c) 2010-2012 Zmanda, Inc.  All Rights Reserved.
2 #
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11 # for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19
20 package Amanda::Interactivity::email;
21
22 use strict;
23 use warnings;
24 use POSIX qw( :errno_h );
25 use vars qw( @ISA );
26 use IPC::Open3;
27 @ISA = qw( Amanda::Interactivity );
28
29 use Amanda::Paths;
30 use Amanda::Util;
31 use Amanda::Debug qw( debug );
32 use Amanda::Config qw( :getconf );
33 use Amanda::Changer;
34 use Amanda::MainLoop qw( :GIOCondition );
35
36 =head1 NAME
37
38 Amanda::Interactivity::email -- Interactivity class to send user request by email
39
40 =head1 SYNOPSIS
41
42 Amanda::Interactivity class to write user request by email
43
44 =cut
45
46 sub new {
47     my $class = shift;
48     my $properties = shift;
49
50     my $self = {
51         send_email_src => undef,
52         check_file_src => undef,
53         properties     => $properties,
54     };
55
56     return bless ($self, $class);
57 }
58
59 sub abort {
60     my $self = shift;
61
62     if ($self->{'send_email_src'}) {
63         $self->{'send_email_src'}->remove();
64     }
65     if ($self->{'check_file_src'}) {
66         $self->{'check_file_src'}->remove();
67     }
68 }
69
70 sub user_request {
71     my $self = shift;
72     my %params = @_;
73     my $buffer = "";
74
75     my $message    = $params{'message'};
76     my $label      = $params{'label'};
77     my $new_volume = $params{'new_volume'};
78     my $err        = $params{'err'};
79     my $chg_name   = $params{'chg_name'};
80
81     my $resend_delay;
82     if (defined $self->{'properties'}->{'resend-delay'}) {
83         $resend_delay = 1000 * $self->{'properties'}->{'resend-delay'}->{'values'}->[0];
84     }
85     my $check_file;
86     if (defined $self->{'properties'}->{'check-file'}) {
87         $check_file = $self->{'properties'}->{'check-file'}->{'values'}->[0];
88     }
89
90     my $check_file_delay = 10000;
91     if (defined $self->{'properties'}->{'check-file-delay'}) {
92         $check_file_delay = 1000 * $self->{'properties'}->{'check_file-delay'}->{'values'}->[0];
93     }
94
95     my $mailer  = getconf($CNF_MAILER);
96     my $subject;
97     if ($label) {
98         $subject = "AMANDA VOLUME REQUEST: $label";
99     } else {
100         $subject = "AMANDA VOLUME REQUEST: new volume";
101     }
102
103     my $mailto;
104     if (defined $self->{'properties'}->{'mailto'}) {
105         $mailto = $self->{'properties'}->{'mailto'}->{'values'};
106     } else {
107         my $a = getconf($CNF_MAILTO);
108         my @mailto = split (/ /, getconf($CNF_MAILTO));
109         $mailto = \@mailto;
110     }
111     my @cmd = ("$mailer", "-s", $subject, @{$mailto});
112
113     my $send_email_cb;
114     $send_email_cb = sub {
115         $self->{'send_email_src'} = undef;
116         debug("cmd: " . join(" ", @cmd) . "\n");
117         my ($pid, $fh);
118         $pid = open3($fh, ">&2", ">&2", @cmd);
119         print {$fh} "$err\n";
120         if ($label && $new_volume) {
121             print {$fh} "Insert volume labeled '$label' or a new volume in $chg_name\n";
122         } elsif ($label) {
123             print {$fh} "Insert volume labeled '$label' in $chg_name\n";
124         } else {
125             print {$fh} "Insert a new volume in $chg_name\n";
126         }
127         if ($check_file) {
128             print {$fh} "or write the name of a new changer in '$check_file'\n";
129             print {$fh} "or write 'abort' in the file to abort the scan.\n";
130         }
131         close $fh;
132         unlink($check_file);
133
134         if ($resend_delay) {
135             $self->{'send_email_src'} = Amanda::MainLoop::call_after($resend_delay, $send_email_cb);
136         }
137     };
138
139     my $check_file_cb;
140     $check_file_cb = sub {
141         $self->{'check_file_src'} = undef;
142
143         if (-f $check_file) {
144             my $fh;
145             open ($fh, '<' , $check_file);
146             my $line = <$fh>;
147             chomp $line;
148             $self->abort();
149             if ($line =~ /^abort$/i) {
150                 return $params{'request_cb'}->(
151                         Amanda::Changer::Error->new('fatal',
152                                 message => "Aborted by user"));
153             } else {
154                 return $params{'request_cb'}->(undef, $line);
155             }
156         }
157         $self->{'check_file_src'} = Amanda::MainLoop::call_after($check_file_delay, $check_file_cb);
158     };
159
160     $send_email_cb->();
161     if ($check_file) {
162         unlink($check_file);
163         $check_file_cb->();
164     }
165 }
166
167 1;