root/trunk/plagger/lib/Plagger/Plugin.pm

Revision 807 (checked in by miyagawa, 14 years ago)

Now Plagger::UserAgent? and $plugin->cookie_jar can share cookies data with your browser. No need to set username and password to CustomFeed?::* plugins, etc.

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Plugin;
2 use strict;
3 use base qw( Class::Accessor::Fast );
4
5 __PACKAGE__->mk_accessors( qw(conf rule rule_hook cache) );
6
7 use Plagger::Cookies;
8 use Plagger::Crypt;
9 use Plagger::Rule;
10 use Plagger::Rules;
11
12 use FindBin;
13 use File::Spec;
14
15 sub new {
16     my($class, $opt) = @_;
17     my $self = bless {
18         conf => $opt->{config} || {},
19         rule => $opt->{rule},
20         rule_op => $opt->{rule_op} || 'AND',
21         rule_hook => '',
22         meta => {},
23     }, $class;
24     $self->init();
25     $self;
26 }
27
28 sub init {
29     my $self = shift;
30
31     if (my $rule = $self->{rule}) {
32         $rule = [ $rule ] if ref($rule) eq 'HASH';
33         my $op = $self->{rule_op};
34         $self->{rule} = Plagger::Rules->new($op, @$rule);
35     } else {
36         $self->{rule} = Plagger::Rule->new({ module => 'Always' });
37     }
38
39     $self->walk_config_encryption();
40 }
41
42 sub walk_config_encryption {
43     my $self = shift;
44     my $conf = $self->conf;
45
46     $self->do_walk($conf);
47 }
48
49 sub do_walk {
50     my($self, $data) = @_;
51     return unless defined($data) && ref $data;
52
53     if (ref($data) eq 'HASH') {
54         for my $key (keys %$data) {
55             if ($key =~ /password/) {
56                 $self->decrypt_config($data, $key);
57             }
58             $self->do_walk($data->{$key});
59         }
60     } elsif (ref($data) eq 'ARRAY') {
61         for my $value (@$data) {
62             $self->do_walk($value);
63         }
64     }
65 }
66
67 sub decrypt_config {
68     my($self, $data, $key) = @_;
69
70     my $decrypted = Plagger::Crypt->decrypt($data->{$key});
71     if ($decrypted eq $data->{$key}) {
72         Plagger->context->add_rewrite_task($key, $decrypted, Plagger::Crypt->encrypt($decrypted, 'base64'));
73     } else {
74         $data->{$key} = $decrypted;
75     }
76 }
77
78 sub conf { $_[0]->{conf} }
79 sub rule { $_[0]->{rule} }
80
81 sub dispatch_rule_on {
82     my($self, $hook) = @_;
83     $self->rule_hook && $self->rule_hook eq $hook;
84 }
85
86 sub class_id {
87     my $self = shift;
88
89     my $pkg = ref($self) || $self;
90        $pkg =~ s/Plagger::Plugin:://;
91     my @pkg = split /::/, $pkg;
92
93     return join '-', @pkg;
94 }
95
96 # subclasses may overload to avoid cache sharing
97 sub plugin_id {
98     my $self = shift;
99     $self->class_id;
100 }
101
102 sub assets_dir {
103     my $self = shift;
104
105     my $context = Plagger->context;
106     my $assets_base = $context->conf->{assets_path} || File::Spec->catfile($FindBin::Bin, "assets");
107     return File::Spec->catfile(
108         $assets_base, "plugins", $self->class_id,
109     );
110 }
111
112 sub log {
113     my $self = shift;
114     Plagger->context->log(@_, caller => ref($self));
115 }
116
117 sub cookie_jar {
118     my $self = shift;
119
120     my $agent_conf = Plagger->context->conf->{user_agent} || {};
121     if ($agent_conf->{cookies}) {
122         return Plagger::Cookies->create($agent_conf->{cookies});
123     }
124
125     return $self->cache->cookie_jar;
126 }
127
128 1;
Note: See TracBrowser for help on using the browser.