| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | require 5.006; | 
| 2 |  |  |  |  |  |  | package Mail::Box::MH::Resource; | 
| 3 | 1 |  |  | 1 |  | 706 | use Cwd; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 51 |  | 
| 4 | 1 |  |  | 1 |  | 5 | use File::Spec; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 28 |  | 
| 5 | 1 |  |  | 1 |  | 1013 | use Mail::Reporter; | 
|  | 1 |  |  |  |  | 2316 |  | 
|  | 1 |  |  |  |  | 45 |  | 
| 6 | 1 |  |  | 1 |  | 7 | use vars ('$VERSION', @ISA); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 880 |  | 
| 7 |  |  |  |  |  |  | $VERSION = 0.06; | 
| 8 |  |  |  |  |  |  | @ISA = 'Mail::Reporter'; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | local($_, $/, %ENV); | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | my $curdir = File::Spec->catfile(File::Spec->curdir(), 'foo'); | 
| 13 |  |  |  |  |  |  | $curdir =~s/\bfoo$//; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | sub new{ | 
| 16 | 5 |  |  | 5 | 1 | 27693 | shift; | 
| 17 | 5 |  |  |  |  | 24 | my $self = bless {}; | 
| 18 | 5 |  |  |  |  | 52 | $self->SUPER::init(); | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 5 | 100 |  |  |  | 265 | unless( $self->{_file} = shift ){ | 
| 21 | 1 | 50 |  |  |  | 28 | if( exists($ENV{MH}) ){ | 
| 22 | 1 | 50 |  |  |  | 5782 | $self->{_file} = File::Spec->file_name_is_absolute($ENV{MH}) ? | 
| 23 |  |  |  |  |  |  | $ENV{MH} : File::Spec->catfile(cwd(), $ENV{MH}); | 
| 24 |  |  |  |  |  |  | } | 
| 25 | 1 |  | 33 |  |  | 26 | $self->{_file} ||= File::Spec->catfile($ENV{HOME}, '.mh_profile'); | 
| 26 |  |  |  |  |  |  | } | 
| 27 | 5 | 100 | 100 |  |  | 210 | unless( File::Spec->file_name_is_absolute($self->{_file}) || | 
| 28 |  |  |  |  |  |  | $self->{_file} =~ m%^\Q$curdir\E% ){ | 
| 29 | 1 |  |  |  |  | 46 | my $profile = Mail::Box::MH::Resource->new(); | 
| 30 | 1 |  |  |  |  | 23 | my $path = $profile->get('Path'); | 
| 31 | 1 | 50 |  |  |  | 28 | $path = File::Spec->file_name_is_absolute($path) ? $path : | 
| 32 |  |  |  |  |  |  | File::Spec->catdir($ENV{HOME}, $path); | 
| 33 | 1 |  |  |  |  | 25 | $self->{_file} = File::Spec->catfile($path, $self->{_file}); | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 5 | 100 |  |  |  | 174 | if( -e $self->{_file} ){ | 
| 37 | 4 | 50 |  |  |  | 252 | if( open(my $profile, $self->{_file}) ){ | 
| 38 | 4 |  |  |  |  | 89 | while( <$profile> ){ | 
| 39 | 6 |  |  |  |  | 11 | chomp; | 
| 40 | 6 | 50 |  |  |  | 16 | next unless defined($_); | 
| 41 |  |  |  |  |  |  | #MH doesn't strip out leading whitespace, so this is okay | 
| 42 | 6 |  |  |  |  | 114 | my @F = split(/:\s*/, $_ ,2); | 
| 43 | 6 |  |  |  |  | 81 | $self->{_profile}->{$F[0]} = $F[1]; | 
| 44 |  |  |  |  |  |  | } | 
| 45 | 4 |  |  |  |  | 42 | close($profile); | 
| 46 | 4 |  |  |  |  | 59 | $self->log(PROGRESS=>"Resource file F<$self->{_file}> opened for read."); | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  | else{ | 
| 49 | 0 |  |  |  |  | 0 | $self->log(ERROR=>"Resource file F<$self->{_file}> could not be opened for read: $!"); | 
| 50 | 0 |  |  |  |  | 0 | return; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  | else{ | 
| 54 | 1 |  |  |  |  | 8 | $self->log(NOTICE=>"Resource file F<$self->{_file}> does not exist, it will be created on close()"); | 
| 55 |  |  |  |  |  |  | } | 
| 56 | 5 |  |  |  |  | 117 | return $self; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | sub get{ | 
| 60 | 4 |  |  | 4 | 1 | 17 | return @{shift->{_profile}}{@_}; | 
|  | 4 |  |  |  |  | 21 |  | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | sub set{ | 
| 64 | 1 |  |  | 1 | 1 | 6 | my $self = shift; | 
| 65 | 1 |  |  |  |  | 5 | my %hash = @_; | 
| 66 | 1 |  |  |  |  | 8 | $self->{_profile}->{$_} = $hash{$_} for keys %hash; | 
| 67 |  |  |  |  |  |  | #XXX Should this actually only get touched if any keys are *modified*? | 
| 68 | 1 |  |  |  |  | 5 | $self->{_modified} = 1; | 
| 69 |  |  |  |  |  |  | }; | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | sub close{ | 
| 72 | 2 |  |  | 2 | 1 | 9 | my $self = shift; | 
| 73 | 2 | 50 |  |  |  | 180 | if( open(my $profile, '>', $self->{_file}) ){ | 
| 74 | 2 |  |  |  |  | 3 | print $profile "$_: $self->{_profile}->{$_}$/" for keys %{$self->{_profile}}; | 
|  | 2 |  |  |  |  | 21 |  | 
| 75 | 2 | 50 |  |  |  | 81 | close($profile) && ($self->{_modified} = 0); | 
| 76 | 2 |  |  |  |  | 9 | $self->log(PROGRESS=>"Resource file F<$self->{_file}> synced."); | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  | else{ | 
| 79 | 0 |  |  |  |  | 0 | $self->log(ERROR=>"Resource file F<$self->{_file}> could not be open for write: $!"); | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  | }; | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | sub enum{ | 
| 84 | 1 |  |  | 1 | 1 | 200 | keys %{shift->{_profile}}; | 
|  | 1 |  |  |  |  | 13 |  | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | sub DESTROY{ | 
| 88 | 5 |  |  | 5 |  | 578 | my $self = shift; | 
| 89 | 5 | 50 |  |  |  | 207 | return unless $self->{_modified}; | 
| 90 | 0 |  |  |  |  |  | require Data::Dumper; | 
| 91 | 0 |  |  |  |  |  | $self->log( | 
| 92 |  |  |  |  |  |  | WARNING=>"Resource file F<$self->{_file}> modifications | 
| 93 |  |  |  |  |  |  | were destroyed, save changes by calling close() first:\n". | 
| 94 |  |  |  |  |  |  | Data::Dumper->Dump([$self->{_profile}])); | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | 1; | 
| 98 |  |  |  |  |  |  | __END__ |