| 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 |
if ($conf->{file} && !-e $conf->{file}) { |
|---|
| 25 |
Plagger->context->log(warn => "$conf->{file}: $!"); |
|---|
| 26 |
} |
|---|
| 27 |
|
|---|
| 28 |
$impl->new(%$conf); |
|---|
| 29 |
}; |
|---|
| 30 |
} |
|---|
| 31 |
|
|---|
| 32 |
sub auto_guess { |
|---|
| 33 |
my($self, $filename) = @_; |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
if ($filename =~ /cookies\.txt$/i) { |
|---|
| 38 |
return { type => 'Mozilla', file => $filename }; |
|---|
| 39 |
} |
|---|
| 40 |
elsif ($filename =~ /index\.dat$/i) { |
|---|
| 41 |
return { type => 'Microsoft', file => $filename }; |
|---|
| 42 |
} |
|---|
| 43 |
elsif ($filename =~ /Cookies\.plist$/i) { |
|---|
| 44 |
return { type => 'Safari', file => $filename }; |
|---|
| 45 |
} |
|---|
| 46 |
elsif ($filename =~ m!\.w3m/cookie$!) { |
|---|
| 47 |
return { type => 'w3m', file => $filename }; |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
Plagger->context->log(warn => "Don't know type of $filename. Use it as LWP default"); |
|---|
| 51 |
return { file => $filename, autosave => 1 }; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
1; |
|---|
| 55 |
|
|---|
| 56 |
__END__ |
|---|
| 57 |
|
|---|
| 58 |
=head1 NAME |
|---|
| 59 |
|
|---|
| 60 |
Plagger::Cookies - cookie_jar factory class |
|---|
| 61 |
|
|---|
| 62 |
=head1 SYNOPSIS |
|---|
| 63 |
|
|---|
| 64 |
# config.yaml: Firefox's cookies.txt |
|---|
| 65 |
global: |
|---|
| 66 |
user_agent: |
|---|
| 67 |
cookies: /path/to/cookies.txt |
|---|
| 68 |
|
|---|
| 69 |
# or more verbosely |
|---|
| 70 |
global: |
|---|
| 71 |
user_agent: |
|---|
| 72 |
cookies: |
|---|
| 73 |
type: Safari |
|---|
| 74 |
file: /path/to/Cookies.plist |
|---|
| 75 |
autosave: 1 |
|---|
| 76 |
|
|---|
| 77 |
=head1 DESCRIPTION |
|---|
| 78 |
|
|---|
| 79 |
Plagger::Cookies is a factory class to create HTTP::Cookies subclass |
|---|
| 80 |
instances by detecting the proper subclass using its filename (and |
|---|
| 81 |
possibly magic, if the filename format is share amongst multiple |
|---|
| 82 |
subclasses, eventually). |
|---|
| 83 |
|
|---|
| 84 |
=head1 THANKS TO |
|---|
| 85 |
|
|---|
| 86 |
Thanks to brian d foy and Gisle Aas for creating HTTP::Cookies::* subclass modules. |
|---|
| 87 |
|
|---|
| 88 |
=head1 AUTHOR |
|---|
| 89 |
|
|---|
| 90 |
Tatsuhiko Miyagawa |
|---|
| 91 |
|
|---|
| 92 |
=head1 SEE ALSO |
|---|
| 93 |
|
|---|
| 94 |
L<Plagger>, L<HTTP::Cookies>, L<HTTP::Cookies::Mozilla>, L<HTTP::Cookies::Microsoft> |
|---|
| 95 |
|
|---|
| 96 |
=cut |
|---|