root/branches/feature-server/plagger/lib/Plagger/Cookies.pm

Revision 856 (checked in by miyagawa, 2 years ago)

merge from trunk to plagger-server for Enclosures support and such. Sorry for the big commit

Line 
1 package Plagger::Cookies;
2 use strict;
3
4 use UNIVERSAL::require;
5 use Storable;
6
7 our %Instances;
8
9 sub create {
10     my($class, $conf) = @_;
11
12     unless (ref $conf && $conf->{type}) {
13         my $file = ref $conf ? $conf->{file} : $conf;
14         $conf = $class->auto_guess($file);
15     }
16
17     $Instances{$conf->{file}} ||= do {
18         $conf = Storable::dclone($conf);
19         my $type = delete $conf->{type};
20         my $impl = $type ? "HTTP::Cookies::$type" : "HTTP::Cookies";
21         Plagger->context->log(debug => "$conf->{file} => $impl");
22         $impl->require or Plagger->context->error("Error loading $impl: $@");
23
24         $impl->new(%$conf);
25     };
26 }
27
28 sub auto_guess {
29     my($self, $filename) = @_;
30
31     # autosave is off by default for foreign cookies files
32
33     if ($filename =~ /cookies\.txt$/i) {
34         return { type => 'Mozilla', file => $filename };
35     }
36     elsif ($filename =~ /index\.dat$/i) {
37         return { type => 'Microsoft', file => $filename };
38     }
39     elsif ($filename =~ /Cookies\.plist$/i) {
40         return { type => 'Safari', file => $filename };
41     }
42     elsif ($filename =~ m!\.w3m/cookie$!) {
43         return { type => 'w3m', file => $filename };
44     }
45
46     Plagger->context->log(warn => "Don't know type of $filename. Use it as LWP default");
47     return { file => $filename, autosave => 1 };
48 }
49
50 1;
51
52 __END__
53
54 =head1 NAME
55
56 Plagger::Cookies - cookie_jar factory class
57
58 =head1 SYNOPSIS
59
60   # config.yaml: Firefox's cookies.txt
61   global:
62     user_agent:
63       cookies: /path/to/cookies.txt
64
65   # or more verbosely
66   global:
67     user_agent:
68       cookies:
69         type: Safari
70         file: /path/to/Cookies.plist
71         autosave: 1
72
73 =head1 DESCRIPTION
74
75 Plagger::Cookies is a factory class to create HTTP::Cookies subclass
76 instances by detecting the proper subclass using its filename (and
77 possibly magic, if the filename format is share amongst multiple
78 subclasses, eventually).
79
80 =head1 THANKS TO
81
82 Thanks to brian d foy and Gisle Aas for creating HTTP::Cookies::* subclass modules.
83
84 =head1 AUTHOR
85
86 Tatsuhiko Miyagawa
87
88 =head1 SEE ALSO
89
90 L<Plagger>, L<HTTP::Cookies>, L<HTTP::Cookies::Mozilla>, L<HTTP::Cookies::Microsoft>
91
92 =cut
Note: See TracBrowser for help on using the browser.