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

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

Auto-rewrite config if encrypted fields are found. Fixes #91

  • 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::Crypt;
8 use Plagger::Rule;
9 use Plagger::Rules;
10
11 use FindBin;
12 use File::Spec;
13
14 sub new {
15     my($class, $opt) = @_;
16     my $self = bless {
17         conf => $opt->{config} || {},
18         rule => $opt->{rule},
19         rule_op => $opt->{rule_op} || 'AND',
20         rule_hook => '',
21         meta => {},
22     }, $class;
23     $self->init();
24     $self;
25 }
26
27 sub init {
28     my $self = shift;
29
30     if (my $rule = $self->{rule}) {
31         $rule = [ $rule ] if ref($rule) eq 'HASH';
32         my $op = $self->{rule_op};
33         $self->{rule} = Plagger::Rules->new($op, @$rule);
34     } else {
35         $self->{rule} = Plagger::Rule->new({ module => 'Always' });
36     }
37
38     if (my $params = $self->encrypt_config) {
39         $params = [ $params ] unless ref $params;
40
41         for my $key (@$params) {
42             my $config = $self->conf;
43             # support foo/bar/baz
44             while ($key =~ s!^(\w+)/!!) {
45                 $config = $config->{$1};
46             }
47             my $decrypted = Plagger::Crypt->decrypt($config->{$key});
48             if ($decrypted eq $config->{$key}) {
49                 Plagger->context->add_rewrite_task($key, $decrypted, Plagger::Crypt->encrypt($decrypted, 'base64'));
50             } else {
51                 $config->{$key} = $decrypted;
52             }
53         }
54     }
55 }
56
57 sub encrypt_config { }
58
59 sub conf { $_[0]->{conf} }
60 sub rule { $_[0]->{rule} }
61
62 sub dispatch_rule_on {
63     my($self, $hook) = @_;
64     $self->rule_hook && $self->rule_hook eq $hook;
65 }
66
67 sub class_id {
68     my $self = shift;
69
70     my $pkg = ref($self) || $self;
71        $pkg =~ s/Plagger::Plugin:://;
72     my @pkg = split /::/, $pkg;
73
74     return join '-', map lc, @pkg;
75 }
76
77 # subclasses may overload to avoid cache sharing
78 sub plugin_id {
79     my $self = shift;
80     $self->class_id;
81 }
82
83 sub assets_dir {
84     my $self = shift;
85
86     my $assets_dir = File::Spec->catfile(
87                             $self->conf->{assets_path}
88                          || ($FindBin::Bin, "assets/plugins", $self->class_id)
89                      );
90
91 }
92
93 sub log {
94     my $self = shift;
95     Plagger->context->log(@_, caller => ref($self));
96 }
97
98 1;
Note: See TracBrowser for help on using the browser.