| 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 | # autosave is off by default for foreign cookies files | ||
| 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__ |