File Coverage

blib/lib/Net/ManageSieve/Siesh.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package Net::ManageSieve::Siesh;
2              
3 3     3   69286 use warnings;
  3         8  
  3         123  
4 3     3   15 use strict;
  3         8  
  3         126  
5 3     3   3720 use autodie qw(:all);
  3         308679  
  3         20  
6 3     3   90785 use File::Temp qw/tempfile/;
  3         69760  
  3         194  
7 3     3   2610 use Net::ManageSieve;
  3         181010  
  3         113  
8 3     3   1468 use IO::Prompt;
  0            
  0            
9             use parent qw(Net::ManageSieve);
10              
11             sub starttls {
12             my ( $self, @args ) = @_;
13             if ( $self->debug() ) {
14             eval {
15             require IO::Socket::SSL;
16             IO::Socket::SSL->import('debug3');
17             1;
18             } or do {
19             die "Cannot load module IO::Socket::SSL\n";
20             }
21             }
22             return $self->SUPER::starttls(@args);
23             }
24              
25             sub movescript {
26             my ( $self, $source, $target ) = @_;
27             my $is_active = $self->is_active($source);
28              
29             ## We can't delete a active script, so we just deactivate it ...
30             $self->deactivate() if $is_active;
31              
32             $self->copyscript( $source, $target );
33             $self->deletescript($source);
34              
35             ## ... and activate the target later
36             $self->setactive($target) if $is_active;
37             return 1;
38             }
39              
40             sub copyscript {
41             my ( $self, $source, $target ) = @_;
42             my $content = $self->getscript($source);
43             return $self->putscript( $target, $content );
44             }
45              
46             sub temp_scriptfile {
47             my ( $self, $script, $create ) = @_;
48             my ( $fh, $filename );
49             eval { ( $fh, $filename ) = tempfile( UNLINK => 1 ); 1; } or do { die $@ };
50              
51             my $content = '';
52             if ( $self->script_exists($script) ) {
53             $content = $self->getscript($script);
54             }
55             elsif ( !$create ) {
56             die "Script $script does not exists.\n";
57             }
58              
59             print {$fh} $content;
60             seek $fh, 0, 0;
61             return $fh, $filename;
62             }
63              
64             sub putfile {
65             my ( $self, $file, $name ) = @_;
66             my $script;
67             open( my $fh, '<', $file );
68             { local $/ = undef, $script = <$fh> }
69             close $fh;
70             my $length = length $script;
71             $self->havespace( $name, $length );
72             return $self->putscript( $name, $script );
73             }
74              
75             sub getfile {
76             my ( $self, $name, $file ) = @_;
77             my $script = $self->getscript($name);
78             open( my $fh, '>', $file );
79             print {$fh} $script;
80             return close $fh;
81             }
82              
83             sub listscripts {
84             my ( $self, $unactive ) = @_;
85             my (@scripts);
86             @scripts = @{ $self->SUPER::listscripts() };
87             my $active = delete $scripts[-1];
88             if ($unactive) {
89             @scripts = grep { $_ ne $active } @scripts;
90             }
91             return @scripts;
92             }
93              
94             sub deletescript {
95             my ( $sieve, @scripts ) = @_;
96             for my $script (@scripts) {
97             $sieve->SUPER::deletescript($script);
98             }
99             return 1;
100             }
101              
102             sub view_script {
103             my ( $sieve, $script ) = @_;
104             my ( $fh, $filename ) = $sieve->temp_scriptfile($script);
105             unless ($fh) { die $sieve->error() . "\n" }
106             my $pager = $ENV{'PAGER'} || "less";
107             no warnings 'exec';
108             eval { system( $pager, $filename ); 1; } or do {
109             print
110             "Error calling your pager application: $!\nUsing cat as fallback.\n\n";
111             $sieve->cat($script);
112             };
113             return 1;
114             }
115              
116             sub edit_script {
117             my ( $sieve, $script ) = @_;
118             my ( $fh, $filename ) = $sieve->temp_scriptfile( $script, 1 );
119             my $editor = $ENV{'VISUAL'} || $ENV{'EDITOR'} || "vi";
120             while (1) {
121             system( $editor, $filename );
122             eval { $sieve->putfile( $filename, $script ); 1; } or do {
123             print "$@\n";
124             ## There was maybe a parse error, if the user enters yes
125             ## we reedit the file, otherwise we leave it by the next last
126             next if prompt( "Re-edit script? ", -yn );
127             };
128             ## There was either no error with putfile or the user entered no
129             last;
130             }
131             return close $fh;
132             }
133              
134             sub activate {
135             my ( $self, $script ) = @_;
136             return $self->setactive($script);
137             }
138              
139             sub deactivate {
140             my $self = shift;
141             return $self->setactive("");
142             }
143              
144             sub is_active {
145             my ( $self, $script ) = @_;
146             return $self->get_active() eq $script;
147             }
148              
149             sub get_active {
150             my ($self) = @_;
151             return $self->SUPER::listscripts()->[-1];
152             }
153              
154             sub script_exists {
155             my ( $self, $scriptname ) = @_;
156             my %script = map { $_ => 1 } $self->listscripts;
157             return defined( $script{$scriptname} );
158             }
159              
160             1; # End of Net::ManageSieve::Siesh
161              
162             __END__