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

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

s/pluggins/plugins/g

  • 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::Find::Rule ();
14 use File::Spec;
15 use Scalar::Util qw(blessed);
16
17 sub new {
18     my($class, $opt) = @_;
19     my $self = bless {
20         conf => $opt->{config} || {},
21         rule => $opt->{rule},
22         rule_op => $opt->{rule_op} || 'AND',
23         rule_hook => '',
24         meta => {},
25     }, $class;
26     $self->init();
27     $self;
28 }
29
30 sub init {
31     my $self = shift;
32
33     if (my $rule = $self->{rule}) {
34         $rule = [ $rule ] if ref($rule) eq 'HASH';
35         my $op = $self->{rule_op};
36         $self->{rule} = Plagger::Rules->new($op, @$rule);
37     } else {
38         $self->{rule} = Plagger::Rule->new({ module => 'Always' });
39     }
40
41     $self->walk_config_encryption();
42 }
43
44 sub walk_config_encryption {
45     my $self = shift;
46     my $conf = $self->conf;
47
48     $self->do_walk($conf);
49 }
50
51 sub do_walk {
52     my($self, $data) = @_;
53     return unless defined($data) && ref $data;
54
55     if (ref($data) eq 'HASH') {
56         for my $key (keys %$data) {
57             if ($key =~ /password/) {
58                 $self->decrypt_config($data, $key);
59             }
60             $self->do_walk($data->{$key});
61         }
62     } elsif (ref($data) eq 'ARRAY') {
63         for my $value (@$data) {
64             $self->do_walk($value);
65         }
66     }
67 }
68
69 sub decrypt_config {
70     my($self, $data, $key) = @_;
71
72     my $decrypted = Plagger::Crypt->decrypt($data->{$key});
73     if ($decrypted eq $data->{$key}) {
74         Plagger->context->add_rewrite_task($key, $decrypted, Plagger::Crypt->encrypt($decrypted, 'base64'));
75     } else {
76         $data->{$key} = $decrypted;
77     }
78 }
79
80 sub conf { $_[0]->{conf} }
81 sub rule { $_[0]->{rule} }
82
83 sub dispatch_rule_on {
84     my($self, $hook) = @_;
85     $self->rule_hook && $self->rule_hook eq $hook;
86 }
87
88 sub class_id {
89     my $self = shift;
90
91     my $pkg = ref($self) || $self;
92        $pkg =~ s/Plagger::Plugin:://;
93     my @pkg = split /::/, $pkg;
94
95     return join '-', @pkg;
96 }
97
98 # subclasses may overload to avoid cache sharing
99 sub plugin_id {
100     my $self = shift;
101     $self->class_id;
102 }
103
104 sub assets_dir {
105     my $self = shift;
106     my $context = Plagger->context;
107
108     if ($self->conf->{assets_path}) {
109         return $self->conf->{assets_path}; # look at config:assets_path first
110     }
111
112     my $assets_base =
113         $context->conf->{assets_path} ||              # or global:assets_path
114         File::Spec->catfile($FindBin::Bin, "assets"); # or "assets" under plagger script
115
116     return File::Spec->catfile(
117         $assets_base, "plugins", $self->class_id,
118     );
119 }
120
121 sub log {
122     my $self = shift;
123     Plagger->context->log(@_, caller => ref($self));
124 }
125
126 sub cookie_jar {
127     my $self = shift;
128
129     my $agent_conf = Plagger->context->conf->{user_agent} || {};
130     if ($agent_conf->{cookies}) {
131         return Plagger::Cookies->create($agent_conf->{cookies});
132     }
133
134     return $self->cache->cookie_jar;
135 }
136
137 sub templatize {
138     my($self, $file, $vars) = @_;
139
140     my $context = Plagger->context;
141     $vars->{context} ||= $context;
142
143     my $template = Plagger::Template->new($context, $self);
144     $template->process($file, $vars, \my $out) or $context->error($template->error);
145
146     $out;
147 }
148
149 sub load_assets {
150     my($self, $rule, $callback) = @_;
151
152     unless (blessed($rule) && $rule->isa('File::Find::Rule')) {
153         $rule = File::Find::Rule->name($rule);
154     }
155
156     # $rule isa File::Find::Rule
157     for my $file ($rule->in($self->assets_dir)) {
158         my $base = File::Basename::basename($file);
159         $callback->($file, $base);
160     }
161 }
162
163 1;
164
165 __END__
166
167 =head1 NAME
168
169 Plagger::Plugin - Base class for Plagger Plugins
170
171 =head1 SYNOPSIS
172
173   package Plagger::Plugin::Something;
174   use base qw(Plagger::Plugin);
175
176   # register hooks
177   sub register {
178     my ($self, $context) = @_;
179     $context->register_hook( $self,
180        'thingy.wosit'  => $self->can('doodad'),
181     )
182   }
183
184   sub doodad { ... }
185
186 =head1 DESCRIPTION
187
188 This is the base class for plagger plugins.  Pretty much everything is done
189 by plugins in Plagger.
190
191 To write a new plugin, simply inherit from Plagger::Plugin:
192
193   package Plagger::Plugin;
194   use base qw(Plagger::Plugin);
195
196 Then register some hooks:
197
198   # register hooks
199   sub register {
200     my ($self, $context) = @_;
201     $context->register_hook( $self,
202        'thingy.wosit'  => $self->can('doodad'),
203     )
204   }
205
206
207 This means that the "doodad" method will be called at the
208 "thingy.wosit" stage.
209
210 There is a handy L<tools/plugin-start.pl> tool that creates the
211 template of I<.pm> file, dependency YAML file and test files for you.
212
213   > ./tools/plugin-start.pl Foo::Bar
214
215 =head2 Methods
216
217 =over
218
219 =item new
220
221 Standard constructor.  Calls init.
222
223 =item init
224
225 Initilises the plugin
226
227 =item walk_config_encryption
228
229 =item do_walk
230
231 =item decrypt_config
232
233 =item conf
234
235 =item rule
236
237 =item rule_hook
238
239 =item cache
240
241 =item dispatch_rule_on
242
243 =item class_id
244
245 =item assets_dir
246
247 =item log
248
249 =item cookie_jar
250
251 Access the Plagger::Cookies object.
252
253 =item templatize
254
255 =item load_assets
256
257 =back
258
259 =head1 AUTHOR
260
261 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
262
263 See I<AUTHORS> file for the name of all the contributors.
264
265 =head1 LICENSE
266
267 Except where otherwise noted, Plagger is free software; you can
268 redistribute it and/or modify it under the same terms as Perl itself.
269
270 =head1 SEE ALSO
271
272 L<http://plagger.org/>
273
274 =cut
Note: See TracBrowser for help on using the browser.