| 1 |
package Plagger; |
|---|
| 2 |
use strict; |
|---|
| 3 |
our $VERSION = '0.7.3'; |
|---|
| 4 |
|
|---|
| 5 |
use 5.8.1; |
|---|
| 6 |
use Carp; |
|---|
| 7 |
use Data::Dumper; |
|---|
| 8 |
use File::Copy; |
|---|
| 9 |
use File::Basename; |
|---|
| 10 |
use File::Find::Rule; |
|---|
| 11 |
use YAML; |
|---|
| 12 |
use UNIVERSAL::require; |
|---|
| 13 |
|
|---|
| 14 |
use base qw( Class::Accessor::Fast ); |
|---|
| 15 |
__PACKAGE__->mk_accessors( qw(conf update subscription plugins_path cache) ); |
|---|
| 16 |
|
|---|
| 17 |
use Plagger::Cache; |
|---|
| 18 |
use Plagger::CacheProxy; |
|---|
| 19 |
use Plagger::Date; |
|---|
| 20 |
use Plagger::Entry; |
|---|
| 21 |
use Plagger::Feed; |
|---|
| 22 |
use Plagger::Subscription; |
|---|
| 23 |
use Plagger::Template; |
|---|
| 24 |
use Plagger::Update; |
|---|
| 25 |
|
|---|
| 26 |
sub context { undef } |
|---|
| 27 |
|
|---|
| 28 |
sub bootstrap { |
|---|
| 29 |
my($class, %opt) = @_; |
|---|
| 30 |
|
|---|
| 31 |
my $self = bless { |
|---|
| 32 |
conf => {}, |
|---|
| 33 |
update => Plagger::Update->new, |
|---|
| 34 |
subscription => Plagger::Subscription->new, |
|---|
| 35 |
plugins_path => {}, |
|---|
| 36 |
plugins => [], |
|---|
| 37 |
rewrite_tasks => [] |
|---|
| 38 |
}, $class; |
|---|
| 39 |
|
|---|
| 40 |
my $config; |
|---|
| 41 |
if (-e $opt{config} && -r _) { |
|---|
| 42 |
$config = YAML::LoadFile($opt{config}); |
|---|
| 43 |
$self->{config_path} = $opt{config}; |
|---|
| 44 |
} elsif (ref($opt{config}) && ref($opt{config}) eq 'SCALAR') { |
|---|
| 45 |
$config = YAML::Load(${$opt{config}}); |
|---|
| 46 |
} elsif (ref($opt{config}) && ref($opt{config}) eq 'HASH') { |
|---|
| 47 |
$config = $opt{config}; |
|---|
| 48 |
} else { |
|---|
| 49 |
croak "Plagger->bootstrap: $opt{config}: $!"; |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
$self->load_include($config); |
|---|
| 53 |
$self->{conf} = $config->{global}; |
|---|
| 54 |
$self->{conf}->{log} ||= { level => 'debug' }; |
|---|
| 55 |
|
|---|
| 56 |
no warnings 'redefine'; |
|---|
| 57 |
local *Plagger::context = sub { $self }; |
|---|
| 58 |
|
|---|
| 59 |
$self->load_recipes($config); |
|---|
| 60 |
$self->load_cache($opt{config}); |
|---|
| 61 |
$self->load_plugins(@{ $config->{plugins} || [] }); |
|---|
| 62 |
$self->rewrite_config if @{ $self->{rewrite_tasks} }; |
|---|
| 63 |
$self->run(); |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
sub add_rewrite_task { |
|---|
| 67 |
my($self, @stuff) = @_; |
|---|
| 68 |
push @{ $self->{rewrite_tasks} }, \@stuff; |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
sub rewrite_config { |
|---|
| 72 |
my $self = shift; |
|---|
| 73 |
|
|---|
| 74 |
unless ($self->{config_path}) { |
|---|
| 75 |
$self->log(warn => "config is not loaded from file. Ignoring rewrite tasks."); |
|---|
| 76 |
return; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
open my $fh, $self->{config_path} or $self->error("$self->{config_path}: $!"); |
|---|
| 80 |
my $data = join '', <$fh>; |
|---|
| 81 |
close $fh; |
|---|
| 82 |
|
|---|
| 83 |
my $old = $data; |
|---|
| 84 |
my $count; |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
for my $task (@{ $self->{rewrite_tasks} }) { |
|---|
| 88 |
my($key, $old_value, $new_value ) = @$task; |
|---|
| 89 |
if ($data =~ s/^(\s+$key:\s+)\Q$old_value\E[ \t]*$/$1$new_value/m) { |
|---|
| 90 |
$count++; |
|---|
| 91 |
} else { |
|---|
| 92 |
$self->log(error => "$key: $old_value not found in $self->{config_path}"); |
|---|
| 93 |
} |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
if ($count) { |
|---|
| 97 |
File::Copy::copy( $self->{config_path}, $self->{config_path} . ".bak" ); |
|---|
| 98 |
open my $fh, ">", $self->{config_path} or return $self->log(error => "$self->{config_path}: $!"); |
|---|
| 99 |
print $fh $data; |
|---|
| 100 |
close $fh; |
|---|
| 101 |
|
|---|
| 102 |
$self->log(info => "Rewrote $count password(s) and saved to $self->{config_path}"); |
|---|
| 103 |
} |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
sub load_include { |
|---|
| 107 |
my($self, $config) = @_; |
|---|
| 108 |
|
|---|
| 109 |
return unless $config->{include}; |
|---|
| 110 |
for (@{ $config->{include} }) { |
|---|
| 111 |
my $include = YAML::LoadFile($_); |
|---|
| 112 |
|
|---|
| 113 |
for my $key (keys %{ $include }) { |
|---|
| 114 |
my $add = $include->{$key}; |
|---|
| 115 |
unless ($config->{$key}) { |
|---|
| 116 |
$config->{$key} = $add; |
|---|
| 117 |
next; |
|---|
| 118 |
} |
|---|
| 119 |
if (ref($config->{$key}) eq 'HASH') { |
|---|
| 120 |
next unless ref($add) eq 'HASH'; |
|---|
| 121 |
for (keys %{ $include->{$key} }) { |
|---|
| 122 |
$config->{$key}->{$_} = $include->{$key}->{$_}; |
|---|
| 123 |
} |
|---|
| 124 |
} elsif (ref($include->{$key}) eq 'ARRAY') { |
|---|
| 125 |
$add = [ $add ] unless ref($add) eq 'ARRAY'; |
|---|
| 126 |
push(@{ $config->{$key} }, @{ $include->{$key} }); |
|---|
| 127 |
} elsif ($add) { |
|---|
| 128 |
$config->{$key} = $add; |
|---|
| 129 |
} |
|---|
| 130 |
} |
|---|
| 131 |
} |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
sub load_recipes { |
|---|
| 135 |
my($self, $config) = @_; |
|---|
| 136 |
|
|---|
| 137 |
for (@{ $config->{recipes} }) { |
|---|
| 138 |
$self->error("no such recipe to $_") unless $config->{define_recipes}->{$_}; |
|---|
| 139 |
my $plugin = $config->{define_recipes}->{$_}; |
|---|
| 140 |
$plugin = [ $plugin ] unless ref($plugin) eq 'ARRAY'; |
|---|
| 141 |
push(@{ $config->{plugins} }, @{ $plugin }); |
|---|
| 142 |
} |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
sub load_cache { |
|---|
| 146 |
my($self, $config) = @_; |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
my $base = ( basename($config) =~ /^(.*?)\.yaml$/ )[0] || 'config'; |
|---|
| 150 |
my $dir = $base eq 'config' ? ".plagger" : ".plagger-$base"; |
|---|
| 151 |
|
|---|
| 152 |
$self->{conf}->{cache} ||= { |
|---|
| 153 |
base => File::Spec->catfile($ENV{HOME}, $dir), |
|---|
| 154 |
}; |
|---|
| 155 |
|
|---|
| 156 |
$self->cache( Plagger::Cache->new($self->{conf}->{cache}) ); |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
sub load_plugins { |
|---|
| 160 |
my($self, @plugins) = @_; |
|---|
| 161 |
|
|---|
| 162 |
if ($self->conf->{plugin_path}) { |
|---|
| 163 |
for my $path (@{ $self->conf->{plugin_path} }) { |
|---|
| 164 |
opendir my $dir, $path or do { |
|---|
| 165 |
$self->log(warn => "$path: $!"); |
|---|
| 166 |
next; |
|---|
| 167 |
}; |
|---|
| 168 |
while (my $ent = readdir $dir) { |
|---|
| 169 |
next if $ent =~ /^\./; |
|---|
| 170 |
$ent = File::Spec->catfile($path, $ent); |
|---|
| 171 |
if (-f $ent && $ent =~ /\.pm$/) { |
|---|
| 172 |
$self->add_plugin_path($ent); |
|---|
| 173 |
} elsif (-d $ent) { |
|---|
| 174 |
my $lib = File::Spec->catfile($ent, "lib"); |
|---|
| 175 |
if (-e $lib && -d _) { |
|---|
| 176 |
$self->log(debug => "Add $lib to INC path"); |
|---|
| 177 |
unshift @INC, $lib; |
|---|
| 178 |
} else { |
|---|
| 179 |
my $rule = File::Find::Rule->new; |
|---|
| 180 |
$rule->file; |
|---|
| 181 |
$rule->name('*.pm'); |
|---|
| 182 |
my @modules = $rule->in($ent); |
|---|
| 183 |
for my $module (@modules) { |
|---|
| 184 |
$self->add_plugin_path($module); |
|---|
| 185 |
} |
|---|
| 186 |
} |
|---|
| 187 |
} |
|---|
| 188 |
} |
|---|
| 189 |
} |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
for my $plugin (@plugins) { |
|---|
| 193 |
$self->load_plugin($plugin) unless $plugin->{disable}; |
|---|
| 194 |
} |
|---|
| 195 |
} |
|---|
| 196 |
|
|---|
| 197 |
sub add_plugin_path { |
|---|
| 198 |
my($self, $file) = @_; |
|---|
| 199 |
|
|---|
| 200 |
my $pkg = $self->extract_package($file) |
|---|
| 201 |
or die "Can't find package from $file"; |
|---|
| 202 |
$self->plugins_path->{$pkg} = $file; |
|---|
| 203 |
$self->log(debug => "$file is added as a path to plugin $pkg"); |
|---|
| 204 |
} |
|---|
| 205 |
|
|---|
| 206 |
sub extract_package { |
|---|
| 207 |
my($self, $file) = @_; |
|---|
| 208 |
|
|---|
| 209 |
open my $fh, $file or die "$file: $!"; |
|---|
| 210 |
while (<$fh>) { |
|---|
| 211 |
/^package (Plagger::Plugin::.*?);/ and return $1; |
|---|
| 212 |
} |
|---|
| 213 |
|
|---|
| 214 |
return; |
|---|
| 215 |
} |
|---|
| 216 |
|
|---|
| 217 |
sub autoload_plugin { |
|---|
| 218 |
my($self, $plugin) = @_; |
|---|
| 219 |
unless ($self->is_loaded($plugin)) { |
|---|
| 220 |
$self->load_plugin({ module => $plugin }); |
|---|
| 221 |
} |
|---|
| 222 |
} |
|---|
| 223 |
|
|---|
| 224 |
sub is_loaded { |
|---|
| 225 |
my($self, $stuff) = @_; |
|---|
| 226 |
|
|---|
| 227 |
my $sub = ref $stuff && ref $stuff eq 'Regexp' |
|---|
| 228 |
? sub { $_[0] =~ $stuff } |
|---|
| 229 |
: sub { $_[0] eq $stuff }; |
|---|
| 230 |
|
|---|
| 231 |
for my $plugin (@{ $self->{plugins} }) { |
|---|
| 232 |
my $module = ref $plugin; |
|---|
| 233 |
$module =~ s/^Plagger::Plugin:://; |
|---|
| 234 |
return 1 if $sub->($module); |
|---|
| 235 |
} |
|---|
| 236 |
|
|---|
| 237 |
return; |
|---|
| 238 |
} |
|---|
| 239 |
|
|---|
| 240 |
sub load_plugin { |
|---|
| 241 |
my($self, $config) = @_; |
|---|
| 242 |
|
|---|
| 243 |
my $module = delete $config->{module}; |
|---|
| 244 |
$module =~ s/^Plagger::Plugin:://; |
|---|
| 245 |
$module = "Plagger::Plugin::$module"; |
|---|
| 246 |
|
|---|
| 247 |
if ($module->isa('Plagger::Plugin')) { |
|---|
| 248 |
$self->log(debug => "$module is loaded elsewhere ... maybe .t script?"); |
|---|
| 249 |
} elsif (my $path = $self->plugins_path->{$module}) { |
|---|
| 250 |
eval { require $path } or die $@; |
|---|
| 251 |
} else { |
|---|
| 252 |
$module->require or die $@; |
|---|
| 253 |
} |
|---|
| 254 |
|
|---|
| 255 |
$self->log(info => "plugin $module loaded."); |
|---|
| 256 |
|
|---|
| 257 |
my $plugin = $module->new($config); |
|---|
| 258 |
$plugin->cache( Plagger::CacheProxy->new($plugin, $self->cache) ); |
|---|
| 259 |
$plugin->register($self); |
|---|
| 260 |
|
|---|
| 261 |
push @{$self->{plugins}}, $plugin; |
|---|
| 262 |
} |
|---|
| 263 |
|
|---|
| 264 |
sub register_hook { |
|---|
| 265 |
my($self, $plugin, @hooks) = @_; |
|---|
| 266 |
while (my($hook, $callback) = splice @hooks, 0, 2) { |
|---|
| 267 |
|
|---|
| 268 |
$plugin->rule_hook($hook) unless $plugin->rule_hook; |
|---|
| 269 |
|
|---|
| 270 |
push @{ $self->{hooks}->{$hook} }, +{ |
|---|
| 271 |
callback => $callback, |
|---|
| 272 |
plugin => $plugin, |
|---|
| 273 |
}; |
|---|
| 274 |
} |
|---|
| 275 |
} |
|---|
| 276 |
|
|---|
| 277 |
sub run_hook { |
|---|
| 278 |
my($self, $hook, $args, $once) = @_; |
|---|
| 279 |
for my $action (@{ $self->{hooks}->{$hook} }) { |
|---|
| 280 |
my $plugin = $action->{plugin}; |
|---|
| 281 |
if ( $plugin->rule->dispatch($plugin, $hook, $args) ) { |
|---|
| 282 |
my $done = $action->{callback}->($plugin, $self, $args); |
|---|
| 283 |
return 1 if $once && $done; |
|---|
| 284 |
} |
|---|
| 285 |
} |
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
return if $once; |
|---|
| 289 |
} |
|---|
| 290 |
|
|---|
| 291 |
sub run_hook_once { |
|---|
| 292 |
my($self, $hook, $args) = @_; |
|---|
| 293 |
$self->run_hook($hook, $args, 1); |
|---|
| 294 |
} |
|---|
| 295 |
|
|---|
| 296 |
sub run { |
|---|
| 297 |
my $self = shift; |
|---|
| 298 |
|
|---|
| 299 |
$self->run_hook('plugin.init'); |
|---|
| 300 |
$self->run_hook('subscription.load'); |
|---|
| 301 |
|
|---|
| 302 |
unless ( $self->is_loaded(qr/^Aggregator::/) ) { |
|---|
| 303 |
$self->load_plugin({ module => 'Aggregator::Simple' }); |
|---|
| 304 |
} |
|---|
| 305 |
|
|---|
| 306 |
for my $feed ($self->subscription->feeds) { |
|---|
| 307 |
if (my $sub = $feed->aggregator) { |
|---|
| 308 |
$sub->($self, { feed => $feed }); |
|---|
| 309 |
} else { |
|---|
| 310 |
my $ok = $self->run_hook_once('customfeed.handle', { feed => $feed }); |
|---|
| 311 |
if (!$ok) { |
|---|
| 312 |
Plagger->context->log(error => $feed->url . " is not aggregated by any aggregator"); |
|---|
| 313 |
Plagger->context->subscription->delete_feed($feed); |
|---|
| 314 |
} |
|---|
| 315 |
} |
|---|
| 316 |
} |
|---|
| 317 |
|
|---|
| 318 |
$self->run_hook('aggregator.finalize'); |
|---|
| 319 |
|
|---|
| 320 |
for my $feed ($self->update->feeds) { |
|---|
| 321 |
for my $entry ($feed->entries) { |
|---|
| 322 |
$self->run_hook('update.entry.fixup', { feed => $feed, entry => $entry }); |
|---|
| 323 |
} |
|---|
| 324 |
$self->run_hook('update.feed.fixup', { feed => $feed }); |
|---|
| 325 |
} |
|---|
| 326 |
|
|---|
| 327 |
$self->run_hook('update.fixup'); |
|---|
| 328 |
|
|---|
| 329 |
$self->run_hook('smartfeed.init'); |
|---|
| 330 |
for my $feed ($self->update->feeds) { |
|---|
| 331 |
for my $entry ($feed->entries) { |
|---|
| 332 |
$self->run_hook('smartfeed.entry', { feed => $feed, entry => $entry }); |
|---|
| 333 |
} |
|---|
| 334 |
$self->run_hook('smartfeed.feed', { feed => $feed }); |
|---|
| 335 |
} |
|---|
| 336 |
$self->run_hook('smartfeed.finalize'); |
|---|
| 337 |
|
|---|
| 338 |
$self->run_hook('publish.init'); |
|---|
| 339 |
for my $feed ($self->update->feeds) { |
|---|
| 340 |
for my $entry ($feed->entries) { |
|---|
| 341 |
$self->run_hook('publish.entry.fixup', { feed => $feed, entry => $entry }); |
|---|
| 342 |
} |
|---|
| 343 |
|
|---|
| 344 |
$self->run_hook('publish.feed', { feed => $feed }); |
|---|
| 345 |
|
|---|
| 346 |
for my $entry ($feed->entries) { |
|---|
| 347 |
$self->run_hook('publish.entry', { feed => $feed, entry => $entry }); |
|---|
| 348 |
} |
|---|
| 349 |
} |
|---|
| 350 |
|
|---|
| 351 |
$self->run_hook('publish.finalize'); |
|---|
| 352 |
} |
|---|
| 353 |
|
|---|
| 354 |
sub log { |
|---|
| 355 |
my($self, $level, $msg, %opt) = @_; |
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
my $caller = $opt{caller}; |
|---|
| 359 |
unless ($caller) { |
|---|
| 360 |
my $i = 0; |
|---|
| 361 |
while (my $c = caller($i++)) { |
|---|
| 362 |
last if $c !~ /Plugin|Rule/; |
|---|
| 363 |
$caller = $c; |
|---|
| 364 |
} |
|---|
| 365 |
$caller ||= caller(0); |
|---|
| 366 |
} |
|---|
| 367 |
|
|---|
| 368 |
chomp($msg); |
|---|
| 369 |
if ($self->should_log($level)) { |
|---|
| 370 |
warn "$caller [$level] $msg\n"; |
|---|
| 371 |
} |
|---|
| 372 |
} |
|---|
| 373 |
|
|---|
| 374 |
my %levels = ( |
|---|
| 375 |
debug => 0, |
|---|
| 376 |
warn => 1, |
|---|
| 377 |
info => 2, |
|---|
| 378 |
error => 3, |
|---|
| 379 |
); |
|---|
| 380 |
|
|---|
| 381 |
sub should_log { |
|---|
| 382 |
my($self, $level) = @_; |
|---|
| 383 |
$levels{$level} >= $levels{$self->conf->{log}->{level}}; |
|---|
| 384 |
} |
|---|
| 385 |
|
|---|
| 386 |
sub error { |
|---|
| 387 |
my($self, $msg) = @_; |
|---|
| 388 |
my($caller, $filename, $line) = caller(0); |
|---|
| 389 |
chomp($msg); |
|---|
| 390 |
die "$caller [fatal] $msg at line $line\n"; |
|---|
| 391 |
} |
|---|
| 392 |
|
|---|
| 393 |
sub dumper { |
|---|
| 394 |
my($self, $stuff) = @_; |
|---|
| 395 |
local $Data::Dumper::Indent = 1; |
|---|
| 396 |
$self->log(debug => Dumper($stuff)); |
|---|
| 397 |
} |
|---|
| 398 |
|
|---|
| 399 |
sub template { |
|---|
| 400 |
my $self = shift; |
|---|
| 401 |
my $plugin = shift || (caller)[0]; |
|---|
| 402 |
Plagger::Template->new($self, $plugin->class_id); |
|---|
| 403 |
} |
|---|
| 404 |
|
|---|
| 405 |
sub templatize { |
|---|
| 406 |
my($self, $plugin, $file, $vars) = @_; |
|---|
| 407 |
my $tt = $self->template($plugin); |
|---|
| 408 |
$tt->process($file, $vars, \my $out) or $self->error($tt->error); |
|---|
| 409 |
$out; |
|---|
| 410 |
} |
|---|
| 411 |
|
|---|
| 412 |
|
|---|
| 413 |
1; |
|---|
| 414 |
__END__ |
|---|
| 415 |
|
|---|
| 416 |
=head1 NAME |
|---|
| 417 |
|
|---|
| 418 |
Plagger - Pluggable RSS/Atom Aggregator |
|---|
| 419 |
|
|---|
| 420 |
=head1 SYNOPSIS |
|---|
| 421 |
|
|---|
| 422 |
% plagger -c config.yaml |
|---|
| 423 |
|
|---|
| 424 |
=head1 DESCRIPTION |
|---|
| 425 |
|
|---|
| 426 |
Plagger is a pluggable RSS/Atom feed aggregator and remixer platform. |
|---|
| 427 |
|
|---|
| 428 |
Everything is implemented as a small plugin just like qpsmtpd, blosxom |
|---|
| 429 |
and perlbal. All you have to do is write a flow of aggregation, |
|---|
| 430 |
filters, syndication, publishing and notification plugins in config |
|---|
| 431 |
YAML file. |
|---|
| 432 |
|
|---|
| 433 |
See L<http://plagger.org/> for cookbook examples, quickstart document, |
|---|
| 434 |
development community (Mailing List and IRC), subversion repository |
|---|
| 435 |
and bug tracking. |
|---|
| 436 |
|
|---|
| 437 |
=head1 BUGS / DEVELOPMENT |
|---|
| 438 |
|
|---|
| 439 |
If you find any bug, or you have an idea of nice plugin and want help |
|---|
| 440 |
on it, drop us a line to our mailing list |
|---|
| 441 |
L<http://groups.google.com/group/plagger-dev> or stop by the IRC |
|---|
| 442 |
channel C<#plagger> at irc.freenode.net. |
|---|
| 443 |
|
|---|
| 444 |
=head1 AUTHOR |
|---|
| 445 |
|
|---|
| 446 |
Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt> |
|---|
| 447 |
|
|---|
| 448 |
See I<AUTHORS> file for the name of all the contributors. |
|---|
| 449 |
|
|---|
| 450 |
=head1 LICENSE |
|---|
| 451 |
|
|---|
| 452 |
Except where otherwise noted, Plagger is free software; you can |
|---|
| 453 |
redistribute it and/or modify it under the same terms as Perl itself. |
|---|
| 454 |
|
|---|
| 455 |
=head1 SEE ALSO |
|---|
| 456 |
|
|---|
| 457 |
L<http://plagger.org/> |
|---|
| 458 |
|
|---|
| 459 |
=cut |
|---|