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