1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
4 package videosite::Base;
13 my $self = {'_DEBUG' => 0,
14 '_OUT' => sub {printf(@_)},
15 '_CONNECTORS' => sub { return ({ 'name' => 'direct',
17 '_CONNECTOR' => undef,
19 io => sub { printf(@_) },
20 connectors => sub { return ({ 'name' => 'direct',
28 $self->_prepare_parameters();
37 $data[0] = "(" . ref($self) . ") " . $data[0];
39 $self->{'_OUT'}(@data);
46 $data[0] = "DEBUG: " . $data[0];
47 if ($self->{'_DEBUG'} != 0) {$self->error(@data)};
55 return $self->{'_CONFIG'} unless defined($c);
57 foreach $o (keys(%{$c->{'option'}})) {
58 if (exists($self->{'_CONFIG'}->{'option'}->{$o})) {
59 $self->{'_CONFIG'}->{'option'}->{$o}->{'content'} = $c->{'option'}->{$o}->{'content'};
63 return $self->{'_CONFIG'};
66 sub _prepare_parameters {
70 $self->{'_CONFIG'} = {'option' => {'enabled' => {'content' => '1'}}};
72 foreach $p (keys(%{$self->{'_PARAMS'}})) {
73 $self->{'_CONFIG'}->{'option'}->{$p}->{'content'} = $self->{'_PARAMS'}->{$p}->[0];
82 $val = $self->{'_CONFIG'}->{'option'}->{$key}->{'content'};
83 $self->debug('Returning %s=%s', $key, $val);
93 if (exists($self->{'_CONFIG'}->{'option'}->{$key})) {
94 $self->{'_CONFIG'}->{'option'}->{$key}->{'content'} = $val;
96 $self->error('Module %s does not have a parameter named %s', $self->{'NAME'}, $key);
104 $self->{'_OUT'} = $io;
109 my $s = 'Options for ' . $self->{'NAME'} . ":\n";
113 foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
114 $p = $self->{'_CONFIG'}->{'option'}->{$k}->{'content'};
116 $s .= sprintf(" %s: %s", $k, $p);
117 if ($self->{'_CONFIG'}->{'option'}->{$k}->{'content'} eq $self->{'_PARAMS'}->{$k}->[0]) {
118 $s .= " (default)\n";
127 # Return a list of the parameters supported by the module
128 # Does not return the 'enabled' parameter
133 return grep {$_ ne 'enabled' && /^$word/} keys %{$self->{'_PARAMS'}};
136 # Return a list of valid parameter values, if the parameter has
143 unless(exists($self->{'_PARAMS'}->{$param}->[2])) {
146 return grep {/^$word/} keys %{$self->{'_PARAMS'}->{$param}->[2]};
153 my $s = 'Help for ' . $self->{'NAME'} . ":\n";
157 if (exists($self->{'DESC'})) {
158 $s .= "Description:\n " . $self->{'DESC'};
162 foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
163 $p = $self->{'_PARAMS'}->{$k}->[0];
165 if (exists($self->{'_PARAMS'}->{$k}->[2])) {
166 # The parameter has a list of allowed values. Add the keys and their help
167 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
168 foreach (sort keys(%{$self->{'_PARAMS'}->{$k}->[2]})) {
169 $s .= sprintf(" %s: %s\n", $_, $self->{'_PARAMS'}->{$k}->[2]->{$_});
172 # The parameter just has a default value and a help text
173 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
183 $self->{'_DEBUG'} = shift;
190 $ua = LWP::UserAgent->new(
191 'agent' => 'Mozilla/5.0',
192 'cookie_jar' => HTTP::Cookies->new,
196 # Remove a currently defined HTTPS proxy. See below for a longer explanation.
197 delete($ENV{'HTTPS_PROXY'});
199 if (defined($self->{'_CONNECTOR'})) {
200 my $schemas = $self->{'_CONNECTOR'}->{'schemas'};
201 foreach (keys(%{$schemas})) {
202 $self->debug("Adding schema %s with proxy %s", $_, $schemas->{$_});
204 # OK, so here's the gist.
206 # The usual way of reqesting an HTTPS URL through a proxy is
207 # to connect to the proxy server, issue a CONNECT request to
208 # create a channel to the web server and start an SSL session over
209 # this channel, so there is an end-to-end connection between
210 # the client and the server.
212 # Setting a proxy for the https schema in LWP WILL NOT ACCOMPLISH
215 # LWP will connect to the proxy server, and issue a standard GET
216 # request for the target URL, which most proxy servers will refuse
219 # The way to use a proxy server is to set some environment variables
220 # and let the underlying Crypt::SSLeay module do the rest.
222 # This is positively appaling.
223 $ENV{'HTTPS_PROXY'} = $schemas->{$_};
225 $ua->proxy($_, $schemas->{$_});
230 $self->{_CACHED_UA} = $ua;
238 return $self->{_CACHED_UA};
244 my $ua = shift || $self->ua();
248 return $r->decoded_content() if $r->is_success();
256 $d =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge;
261 sub decode_querystring {
264 return { map { split /=/, $_, 2; } split /&/, shift };
270 return $self->{'_CONNECTORS'}->();
276 $self->{'_CONNECTOR'} = shift;
280 # Register a callbacks into the core API to the plugin.
281 # Example of those are config getter/setters and IO functions
282 # The API is a hash reference containing subroutine references.
284 # After the API is registered an attempt is made to load the config
285 # (or set defaults if config values are not found)
291 $self->{_API} = $api;