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',
26 # Add the 'enabled' property to all modules
27 $self->{_PARAMS}->{enabled} = [1, 'Whether the module is enabled'];
30 $self->_prepare_parameters();
39 $data[0] = "(" . ref($self) . ") " . $data[0];
41 $self->{'_OUT'}(@data);
48 $data[0] = "DEBUG: " . $data[0];
49 if ($self->{'_DEBUG'} != 0) {$self->error(@data)};
57 return $self->{'_CONFIG'} unless defined($c);
59 foreach $o (keys(%{$c->{'option'}})) {
60 if (exists($self->{'_CONFIG'}->{'option'}->{$o})) {
61 $self->{'_CONFIG'}->{'option'}->{$o}->{'content'} = $c->{'option'}->{$o}->{'content'};
65 return $self->{'_CONFIG'};
68 sub _prepare_parameters {
72 $self->{'_CONFIG'} = {'option' => {'enabled' => {'content' => '1'}}};
74 foreach $p (keys(%{$self->{'_PARAMS'}})) {
75 $self->{'_CONFIG'}->{'option'}->{$p}->{'content'} = $self->{'_PARAMS'}->{$p}->[0];
84 $val = $self->{'_CONFIG'}->{'option'}->{$key}->{'content'};
85 $self->debug('Returning %s=%s', $key, $val);
95 if (exists($self->{'_CONFIG'}->{'option'}->{$key})) {
96 $self->{'_CONFIG'}->{'option'}->{$key}->{'content'} = $val;
98 $self->error('Module %s does not have a parameter named %s', $self->{'NAME'}, $key);
106 $self->{'_OUT'} = $io;
111 my $s = 'Options for ' . $self->{'NAME'} . ":\n";
115 foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
116 $p = $self->{'_CONFIG'}->{'option'}->{$k}->{'content'};
118 $s .= sprintf(" %s: %s", $k, $p);
119 if ($self->{'_CONFIG'}->{'option'}->{$k}->{'content'} eq $self->{'_PARAMS'}->{$k}->[0]) {
120 $s .= " (default)\n";
129 # Return a list of the parameters supported by the module
130 # Does not return the 'enabled' parameter
135 return grep {$_ ne 'enabled' && /^$word/} keys %{$self->{'_PARAMS'}};
138 # Return a list of valid parameter values, if the parameter has
145 unless(exists($self->{'_PARAMS'}->{$param}->[2])) {
148 return grep {/^$word/} keys %{$self->{'_PARAMS'}->{$param}->[2]};
155 my $s = 'Help for ' . $self->{'NAME'} . ":\n";
159 if (exists($self->{'DESC'})) {
160 $s .= "Description:\n " . $self->{'DESC'};
164 foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
165 $p = $self->{'_PARAMS'}->{$k}->[0];
167 if (exists($self->{'_PARAMS'}->{$k}->[2])) {
168 # The parameter has a list of allowed values. Add the keys and their help
169 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
170 foreach (sort keys(%{$self->{'_PARAMS'}->{$k}->[2]})) {
171 $s .= sprintf(" %s: %s\n", $_, $self->{'_PARAMS'}->{$k}->[2]->{$_});
174 # The parameter just has a default value and a help text
175 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
185 $self->{'_DEBUG'} = shift;
192 $ua = LWP::UserAgent->new(
193 'agent' => 'Mozilla/5.0',
194 'cookie_jar' => HTTP::Cookies->new,
198 # Remove a currently defined HTTPS proxy. See below for a longer explanation.
199 delete($ENV{'HTTPS_PROXY'});
201 if (defined($self->{'_CONNECTOR'})) {
202 my $schemas = $self->{'_CONNECTOR'}->{'schemas'};
203 foreach (keys(%{$schemas})) {
204 $self->debug("Adding schema %s with proxy %s", $_, $schemas->{$_});
206 # OK, so here's the gist.
208 # The usual way of reqesting an HTTPS URL through a proxy is
209 # to connect to the proxy server, issue a CONNECT request to
210 # create a channel to the web server and start an SSL session over
211 # this channel, so there is an end-to-end connection between
212 # the client and the server.
214 # Setting a proxy for the https schema in LWP WILL NOT ACCOMPLISH
217 # LWP will connect to the proxy server, and issue a standard GET
218 # request for the target URL, which most proxy servers will refuse
221 # The way to use a proxy server is to set some environment variables
222 # and let the underlying Crypt::SSLeay module do the rest.
224 # This is positively appaling.
225 $ENV{'HTTPS_PROXY'} = $schemas->{$_};
227 $ua->proxy($_, $schemas->{$_});
232 $self->{_CACHED_UA} = $ua;
240 return $self->{_CACHED_UA};
246 my $ua = shift || $self->ua();
250 return $r->decoded_content() if $r->is_success();
258 $d =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge;
263 sub decode_querystring {
266 return { map { split /=/, $_, 2; } split /&/, shift };
272 return $self->{'_CONNECTORS'}->();
278 $self->{'_CONNECTOR'} = shift;
282 # Register a callbacks into the core API to the plugin.
283 # Example of those are config getter/setters and IO functions
284 # The API is a hash reference containing subroutine references.
286 # After the API is registered an attempt is made to load the config
287 # (or set defaults if config values are not found)
293 $self->{_API} = $api;