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

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

Now Plagger::UserAgent? and $plugin->cookie_jar can share cookies data with your browser. No need to set username and password to CustomFeed?::* plugins, etc.

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