File Coverage

blib/lib/Mail/Ezmlm/Archive.pm
Criterion Covered Total %
statement 6 81 7.4
branch 0 16 0.0
condition 0 8 0.0
subroutine 2 13 15.3
pod 1 9 11.1
total 9 127 7.0


line stmt bran cond sub pod time code
1             # ===========================================================================
2             # Mail::Ezmlm::Archive
3             #
4             # Object methods for ezmlm-idx archives
5             #
6             # Copyright (C) 2003-2005, Alessandro Ranellucci, All Rights Reserved.
7             # Please send bug reports and comments to
8             #
9             # Redistribution and use in source and binary forms, with or without
10             # modification, are permitted provided that the following conditions are
11             # met:
12             #
13             # Redistributions of source code must retain the above copyright notice,
14             # this list of conditions and the following disclaimer.
15             #
16             # Redistributions in binary form must reproduce the above copyright notice,
17             # this list of conditions and the following disclaimer in the documentation
18             # and/or other materials provided with the distribution.
19             #
20             # Neither name Alessandro Ranellucci nor the names of any contributors
21             # may be used to endorse or promote products derived from this software
22             # without specific prior written permission.
23             #
24             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
25             # IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26             # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27             # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
28             # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29             # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30             # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31             # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32             # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33             # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34             # POSSIBILITY OF SUCH DAMAGE.
35             #
36             # ==========================================================================
37             # POD is at the end of this file. Search for '=head' to find it
38              
39             package Mail::Ezmlm::Archive;
40              
41 1     1   6825 use strict;
  1         2  
  1         39  
42 1     1   5 use vars qw($VERSION *MONTHS);
  1         2  
  1         1395  
43             require 5.002;
44              
45             $VERSION = '0.16';
46              
47             %MONTHS = ( Jan => 1, Feb => 2, Mar => 3, Apr => 4, May => 5, Jun => 6,
48             Jul => 7, Aug => 8, Sep => 9, Oct => 10, Nov => 11, Dec => 12 );
49              
50             sub new {
51 0     0 1   my ($class, $list) = @_;
52 0           my $self = {};
53 0   0       bless $self, ref $class || $class || 'Mail::Ezmlm::Archive';
54 0           $self->{CACHE} = 1;
55 0           $self->{CACHED} = {};
56 0 0         $self->setlist($list) || return undef;
57 0           return $self;
58             }
59              
60             sub setlist {
61 0     0 0   my ($self, $list) = @_;
62 0 0 0       return undef if (!-e "$list/lock" || !-e "$list/archived" || !-e "$list/indexed");
      0        
63 0           return ($self->{LIST_PATH} = $list);
64             }
65              
66             sub cache {
67 0     0 0   my $self = shift;
68 0           $self->{CACHE} = 0;
69             }
70              
71             sub nocache {
72 0     0 0   my $self = shift;
73 0           $self->{CACHE} = 1;
74             }
75              
76             sub getmonths {
77 0     0 0   my $self = shift;
78 0           opendir(THREADS, $self->{LIST_PATH} . '/archive/threads');
79 0           my @months = grep /^\d{6}$/, readdir(THREADS);
80 0           closedir(THREADS);
81 0           return sort(@months);
82             }
83              
84             sub getthreads {
85 0     0 0   my ($self, $month) = @_;
86 0           my @threadlist = $self->_get_file($self->{LIST_PATH} . "/archive/threads/$month");
87 0           my $threads = [];
88 0           foreach my $thread (@threadlist) {
89 0           $thread =~ m/^(\d+):(\w+) \[(\d+)\] (.*)$/;
90 0           push (@{$threads}, {
  0            
91             subject => $4,
92             count => $3,
93             offset => $1,
94             id => $2,
95             date => $self->_get_date($1)
96             });
97             }
98 0           return $threads;
99             }
100              
101             sub getthread {
102 0     0 0   my ($self, $thread) = @_;
103 0           my ($a, $b) = (substr($thread,0,2), substr($thread,2));
104 0           my @messages = $self->_get_file($self->{LIST_PATH} . "/archive/subjects/$a/$b");
105 0           my $subject = shift(@messages);
106 0           chop($subject);
107 0           $subject =~ s/^\w+ //;
108 0           my $messages = [];
109 0           foreach my $message (@messages) {
110 0 0         $message =~ m/^(\d+):(\d+):(\w+) (.*)$/ || next;
111 0           push (@{$messages}, {
  0            
112             id => $1,
113             month => $2,
114             authorid => $3,
115             author => $4
116             });
117             }
118             return {
119 0           subject => $subject,
120             messages => $messages
121             };
122             }
123              
124             sub getmessage {
125 0     0 0   my ($self, $message) = @_;
126 0           $message = sprintf("%03u", $message);
127 0           $message =~ m/^(\d+)(\d{2})$/;
128 0           my ($a, $b) = ($1, $2);
129 0 0         return undef unless (-e $self->{LIST_PATH} . "/archive/$a/$b");
130 0           my @lines = $self->_get_file($self->{LIST_PATH} . "/archive/$a/$b");
131 0           my $date = $self->_get_date(1*$message);
132 0           $date =~ m/\s([A-Z][a-z]{2})\s(\d{4})/;
133             return {
134 0           month => $2 . sprintf("%02u", $MONTHS{$1}),
135             text => join("", @lines)
136             };
137             }
138              
139             sub getcount {
140 0     0 0   my $self = shift;
141 0           open(FILE, $self->{LIST_PATH} . "/num");
142 0           =~ m/^(\d+):/;
143 0           my $count = $1;
144 0           close(FILE);
145 0           return $count;
146             }
147              
148             sub _get_file {
149 0     0     my ($self, $file) = @_;
150 0 0         if ($self->{CACHED}->{$file}) {
151 0           return @{$self->{CACHED}->{$file}};
  0            
152             }
153 0           open(FH, "<$file");
154 0           my @lines = ;
155 0           close(FH);
156 0 0         if ($self->{CACHE} == 1) {
157 0           $self->{CACHED}->{$file} = [ @lines ];
158             }
159 0           return @lines;
160             }
161              
162             sub _get_date {
163 0     0     my ($self, $message) = @_;
164 0           my $msg = sprintf("%03u", $message);
165 0           $msg =~ m/^(\d+)(\d{2})$/;
166 0           my ($a, $b) = ($1, $2);
167 0           my @index = $self->_get_file($self->{LIST_PATH} . "/archive/$a/index");
168 0           my $found;
169 0           foreach my $line (@index) {
170 0 0         if ($found) {
171 0           $line =~ m/^\s([^;]+);/;
172 0           return $1;
173             }
174 0 0         $found = 1 if ($line =~ /^$message:/);
175             }
176             }
177              
178             1;
179             __END__