File Coverage

blib/lib/App/Siesh.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package App::Siesh;
2              
3 2     2   27671 use strict;
  2         5  
  2         114  
4 2     2   11 use warnings;
  2         4  
  2         56  
5              
6 2     2   2378 use Term::ShellUI;
  2         29401  
  2         79  
7 2     2   908 use Net::ManageSieve::Siesh;
  0            
  0            
8             use App::Siesh::Batch;
9              
10             our $VERSION = '0.21';
11              
12             sub read_config {
13             my ($class,$file) = @_;
14              
15             if (!$file) {
16             require Config::Find;
17             $file = Config::Find->find( name => 'siesh' );
18             }
19             return {} if ! $file;
20             require Config::Tiny;
21              
22             my $config = Config::Tiny->read($file);
23             die $config->errstr() if ! $config;
24              
25             return $config->{_};
26             }
27              
28             sub run {
29             my ( $class, %config ) = @_;
30              
31             # Set defaults for Net::ManageSieve::Siesh construction
32             $config{user} ||= $ENV{USER};
33             $config{host} ||= 'imap';
34             $config{tls} ||= 'auto';
35              
36             my @params;
37              
38             foreach ( qw(debug port tls) ) {
39             push @params, ucfirst($_), $config{$_}
40             if defined $config{$_};
41             }
42              
43             my $sieve = Net::ManageSieve::Siesh->new(
44             $config{host},
45             on_fail => sub { die "$_[1]\n" },
46             @params
47             ) or die "Can't connect to $config{host}: $!\n";
48              
49             $sieve->auth( $config{user}, $config{password} ) or die "$@\n";
50              
51             my %shellui_params;
52             if ($config{file}) {
53             $shellui_params{term} = App::Siesh::Batch->new($config{file});
54             }
55              
56             my $term = new Term::ShellUI(
57             %shellui_params,
58             history_file => '~/.siesh_history',
59             prompt => 'siesh> ',
60             commands => {
61             "help" => {
62             desc => "Print this help page.",
63             args => sub { shift->help_args( undef, @_ ); },
64             method => sub { shift->help_call( undef, @_ ); },
65             },
66             "put" => {
67             desc => 'Upload a script onto the server.',
68             maxargs => 2,
69             minargs => 2,
70             proc => sub { $sieve->putfile(@_) },
71             args => sub { complete_file_and_script(@_, $sieve); },
72             },
73             "get" => {
74             desc => "Fetch a script from the server and store locally.",
75             maxargs => 2,
76             minargs => 2,
77             proc => sub { $sieve->getfile(@_) or die $sieve->error() . "\n" },
78             args => sub { complete_script_and_file(@_, $sieve); },
79             },
80             "quit" => {
81             desc => "Quit siesh.",
82             maxargs => 0,
83             method => sub { $sieve->logout; shift->exit_requested(1); }
84             },
85             "list" => {
86             desc => "List all scripts stored on the server.",
87             maxargs => 0,
88             proc => sub {
89             my $active = $sieve->get_active();
90             my @scripts = $sieve->listscripts(1);
91             print $active . " *\n" if $active;
92             print join("\n",sort @scripts) . "\n" if @scripts;
93             },
94             },
95             "activate" => {
96             desc => "Mark a script as active.",
97             maxargs => 1,
98             proc => sub { $sieve->setactive(shift) },
99             args => sub { complete_scripts( @_, $sieve ) },
100             },
101             "edit" => {
102             desc => 'Edit script using $EDITOR.',
103             maxargs => 1,
104             proc => sub { $sieve->edit_script(shift) },
105             args => sub { complete_scripts( @_, $sieve ) },
106             },
107             "view" => {
108             desc => 'Examine the contents of a script using $PAGER.',
109             maxargs => 1,
110             proc => sub { $sieve->view_script(shift) },
111             args => sub { complete_scripts( @_, $sieve ) },
112             },
113             "delete" => {
114             desc => "Delete a script from the server.",
115             minargs => 1,
116             proc => sub {
117             if ( $_[0] eq '*' ) {
118             $sieve->deactivate();
119             $sieve->deletescript($sieve->listscripts);
120             } else {
121             $sieve->deletescript(@_);
122             }
123             },
124             args => sub { complete_scripts( @_, $sieve ) },
125             },
126             "cat" => {
127             desc => "Show the contents of a script on stdout.",
128             maxargs => 1,
129             proc => sub { print $sieve->getscript(shift) },
130             args => sub { complete_scripts( @_, $sieve ) },
131             },
132             "copy" => {
133             desc => 'Make a copy of a script under another name.',
134             maxargs => 2,
135             minargs => 2,
136             proc =>
137             sub { $sieve->copyscript(@_) },
138             args => sub { complete_scripts( @_, $sieve ) },
139             },
140             "move" => {
141             desc => 'Rename a script on the server.',
142             maxargs => 2,
143             minargs => 2,
144             proc =>
145             sub { $sieve->movescript(@_) },
146             args => sub { complete_scripts( @_, $sieve ) },
147             },
148             "deactivate" => {
149             desc => 'Mark the currently activated script as inactive.',
150             maxargs => 0,
151             proc => sub { $sieve->deactivate() },
152             args => sub { complete_scripts( @_, $sieve ) },
153             },
154             "q" => { alias => 'quit', exclude_from_completion => 1 },
155             "logout" => { alias => 'quit', exclude_from_completion => 1 },
156             "h" => { alias => "help", exclude_from_completion => 1 },
157             "ls" => { alias => "list", exclude_from_completion => 1 },
158             "dir" => { alias => "list", exclude_from_completion => 1 },
159             "rm" => { alias => "delete", exclude_from_completion => 1 },
160             "vi" => { alias => "edit", exclude_from_completion => 1 },
161             "more" => { alias => "less", exclude_from_completion => 1 },
162             "type" => { alias => "cat", exclude_from_completion => 1 },
163             "cp" => { alias => "copy", exclude_from_completion => 1 },
164             "mv" => { alias => "move", exclude_from_completion => 1 },
165             "set" => { alias => "activate", exclude_from_completion => 1 },
166             "unset" => { alias => "deactivate", exclude_from_completion => 1 },
167             },
168             );
169            
170             #$term->{debug_complete}=5;
171             $term->{term}->ornaments(0);
172             return $term->run();
173             }
174              
175             sub complete_script_and_file {
176             my ( $term, $cmp, $sieve ) = @_;
177             if ($cmp->{argno} == 0 ) {
178             my $scripts = complete_scripts($term,$cmp,$sieve) ;
179             if ( @{ $scripts } ) {
180             return $scripts;
181             } else {
182             return "No scripts to complete found.\n";
183             }
184             } elsif ($cmp->{argno} == 1 ) {
185             return $term->complete_files($cmp)
186             }
187             }
188              
189             sub complete_file_and_script {
190             my ( $term, $cmp, $sieve ) = @_;
191             if ($cmp->{argno} == 0 ) {
192             return $term->complete_files($cmp)
193             } elsif ($cmp->{argno} == 1 ) {
194             return "No scripts to complete found.\n";
195             }
196             }
197              
198             sub complete_scripts {
199             my ( $term, $cmp, $sieve ) = @_;
200             return [ grep { index( $_, $cmp->{str} ) == 0 } $sieve->listscripts() ];
201             }
202              
203             1;
204              
205             __END__