Tools/CLI/Create.pm untested

filter: Statementstmt 0.0% 0 / 108 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 207 CC 14 SCAR 31.7
CC
14
Cov
0%
run
72.0
construct_date_filter
20.0
construct_list_filter
6.0
CRAP 23.9
1 #!/usr/bin/perl
2  
3 package Mail::Summary::Tools::CLI::Create;
4 use base qw/Mail::Summary::Tools::CLI::Command/;
5  
6 use strict;
7 use warnings;
8  
9 use DateTime::Format::DateManip;
10 use DateTime::Infinite;
11  
12 use Class::Autouse (<<'#\'END_USE' =~ m!(\w+::[\w:]+)!g);
13 #\
14  
15 use Mail::Box::Manager;
16  
17 use Mail::Summary::Tools::Summary;
18 use Mail::Summary::Tools::Summary::List;
19 use Mail::Summary::Tools::Summary::Thread;
20 use Mail::Summary::Tools::ThreadFilter;
21  
22 #'END_USE
23  
24 use Mail::Summary::Tools::ThreadFilter::Util qw/
25 get_root_message guess_mailing_list
26 thread_root last_in_thread any_in_thread all_in_thread
27 negate
28 mailing_list_is in_date_range
29 /; # subject_matches
30  
31 sub usage_desc {
32 "%c create %o summary.yaml mailbox [mailbox2]\n".
33 "%c create \%o -o summary.yaml -i mailbox -i mailbox2"
34 }
35  
36 use constant options => (
37 [ 'input|i=s@' => "Mailboxes to read from (can be used several times)" ],
38 [ 'output|o=s' => "Summary file to write" ],
39 [ 'update|u!' => "Update an existing summary" ],
40 [ 'from|f=s' => "From date (any string Date::Manip can parse)" ],
41 [ 'to|t=s' => "To date (any string Date::Manip can parse)" ],
42 [ 'list|l=s@' => "Only posts in this list (can be used several times)" ],
43 #[ 'subject|s=s' => "subject" ], # TODO
44 [ 'posters|p!' => "Collect information about posters (defaults to true)", { default => 1 } ],
45 [ 'dates|d!' => "Collect information about dates (defaults to true)", { default => 1 } ],
46 #[ 'm|match=s' => "match" ], # TODO
47 [ 'clean|c!' => "Scrub the thread subjects" ],
48 [ 'rt|r!' => "Collect information about RT tickets (defaults to true)", { default => 1 } ],
49 );
50  
51 sub construct_filters {
52 my $self = shift;
53  
54 return (
55 $self->construct_date_filter,
56 $self->construct_list_filter,
57 $self->construct_subject_filter,
58 );
59 }
60  
61 sub construct_date_filter {
62 my $self = shift;
63 my $opt = $self->{opt};
64  
65 my $from = DateTime::Format::DateManip->parse_datetime( $opt->{from} || return );
66 my $to = DateTime::Format::DateManip->parse_datetime( $opt->{to} || return );
67  
68 if ( defined($from) || defined($to) ) {
69 $from = DateTime::Infinite::Past->new unless defined($from);
70 $to = DateTime::Infinite::Future->new unless defined($to);
71  
72 return $self->comb_filter( in_date_range( $from, $to ) );
73 } else {
74 die "From or to date specification is invalid\n";
75 }
76 }
77  
78 sub construct_list_filter {
79 my $self = shift;
80  
81 if ( my $list = $self->{opt}{list} ) {
82 return $self->comb_filter( mailing_list_is($list) );
83 } else {
84 return;
85 }
86 }
87  
88 sub construct_subject_filter {
89 return;
90 }
91  
92 sub comb_filter {
93 my ( $self, $filter ) = @_;
94 any_in_thread( $filter );
95 }
96  
97 sub filter {
98 my ( $self, @params ) = @_;
99  
100 my $f = Mail::Summary::Tools::ThreadFilter->new(
101 filters => [ $self->construct_filters ],
102 );
103  
104 return $f->filter(@params);
105 }
106  
107 sub clean_subject {
108 my ( $self, $subject ) = @_;
109  
110 return $subject unless $self->{opt}{clean};
111  
112 $subject =~ s/^\s*(?:Re|Fwd):\s*//i;
113 $subject =~ s/^\s*\[[\w-]+\]\s*//; # remove [Listname] munging
114 $subject =~ s/^\s*|\s*$//g; # strip whitespace
115  
116 return $subject;
117 }
118  
119 sub validate {
120 my ( $self, $opt, $args ) = @_;
121 $self->{opt} = $opt;
122  
123 @$args and $opt->{$_} ||= shift @$args for qw/output/;
124 push @{ $opt->{input} ||= [] }, @$args; @$args = ();
125  
126 unless ( $opt->{output} and @{ $opt->{input} } ) {
127 $self->usage_error("Please specify a summary output file and at least one mail box for input.");
128 }
129 }
130  
131 sub run {
132 my ( $self, $opt, $args ) = @_;
133  
134 my @folders = @{ $opt->{input} };
135 my $summary_out = $opt->{output};
136  
137 if ( -f $summary_out and !$opt->{update} ) {
138 die "The output file '$summary_out' exists. Either remove it or specify the --update option\n";
139 }
140
141 my $summary = -f $summary_out
142 ? Mail::Summary::Tools::Summary->load( $summary_out )
143 : Mail::Summary::Tools::Summary->new;
144  
145 $self->diag("loading and threading mailboxes: @folders");
146  
147 my $mgr = Mail::Box::Manager->new;
148 my $threads = $mgr->threads(
149 folders => [ map { $mgr->open( folder => $_ ) } @folders ],
150 timespan => 'EVER',
151 window => 'ALL',
152 ( $self->app->global_options->{verbose} ? (trace => "PROGRESS") : ()),
153 );
154  
155 my %lists = map { $_->name => $_ } $summary->lists;
156 my %seen;
157  
158 $self->filter( threads => $threads, callback => sub {
159 my $thread = shift;
160
161 my $root = get_root_message($thread);
162 return if $seen{$root->messageId}++;
163  
164 my $list_name = eval { guess_mailing_list($root)->listname };
165 my $list_key = $list_name || "unknown";
166  
167 my $list = $lists{$list_key} ||= do {
168 my $list = Mail::Summary::Tools::Summary::List->new( $list_name ? (name => $list_name) : () );
169 $summary->add_lists( $list );
170 $list;
171 };
172  
173 my $summarized_thread = Mail::Summary::Tools::Summary::Thread->from_mailbox_thread( $thread,
174 collect_posters => $opt->{posters},
175 collect_dates => $opt->{dates},
176 collect_rt => $opt->{dates},
177 process_subject => sub { $self->clean_subject(shift) },
178 );
179  
180 #$summarized_thread->extra->{ .... } = { ... }
181  
182 if ( my $existing = $summary->get_thread_by_id( $summarized_thread->message_id ) ) {
183 my $was_out_of_date = $existing->extra->{out_of_date};
184 $existing->merge( $summarized_thread );
185 $self->diag($summarized_thread->message_id . " is now out of date") if !$was_out_of_date and $existing->extra->{out_of_date};
186 } else {
187 $self->diag($summarized_thread->message_id . " has been added to the summary");
188 $list->add_threads( $summarized_thread );
189 }
190 });
191  
192 $self->diag( "found threads in the mailing lists: @{[ map { $_->name || '<unknown>' } values %lists ]}" );
193  
194 $summary->save( $summary_out );
195 }
196  
197 __PACKAGE__;
198  
199 __END__