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 '_CONNECTOR' => undef,
16 io => sub { printf(@_) },
17 connectors => sub { return ({ 'name' => 'direct',
23 # Add the 'enabled' property to all modules
24 $self->{_PARAMS}->{enabled} = [1, 'Whether the module is enabled'];
27 $self->_prepare_parameters();
36 $data[0] = "(" . ref($self) . ") " . $data[0];
38 $self->{_API}->{io}->(@data);
45 $data[0] = "DEBUG: " . $data[0];
46 if ($self->{'_DEBUG'} != 0) {$self->error(@data)};
54 return $self->{'_CONFIG'} unless defined($c);
56 foreach $o (keys(%{$c->{'option'}})) {
57 if (exists($self->{'_CONFIG'}->{'option'}->{$o})) {
58 $self->{'_CONFIG'}->{'option'}->{$o}->{'content'} = $c->{'option'}->{$o}->{'content'};
62 return $self->{'_CONFIG'};
65 sub _prepare_parameters {
69 $self->{'_CONFIG'} = {'option' => {'enabled' => {'content' => '1'}}};
71 foreach $p (keys(%{$self->{'_PARAMS'}})) {
72 $self->{'_CONFIG'}->{'option'}->{$p}->{'content'} = $self->{'_PARAMS'}->{$p}->[0];
81 $val = $self->{'_CONFIG'}->{'option'}->{$key}->{'content'};
82 $self->debug('Returning %s=%s', $key, $val);
92 if (exists($self->{'_CONFIG'}->{'option'}->{$key})) {
93 $self->{'_CONFIG'}->{'option'}->{$key}->{'content'} = $val;
95 $self->error('Module %s does not have a parameter named %s', $self->{'NAME'}, $key);
101 my $s = 'Options for ' . $self->{'NAME'} . ":\n";
105 foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
106 $p = $self->{'_CONFIG'}->{'option'}->{$k}->{'content'};
108 $s .= sprintf(" %s: %s", $k, $p);
109 if ($self->{'_CONFIG'}->{'option'}->{$k}->{'content'} eq $self->{'_PARAMS'}->{$k}->[0]) {
110 $s .= " (default)\n";
119 # Return a list of the parameters supported by the module
120 # Does not return the 'enabled' parameter
125 return grep {$_ ne 'enabled' && /^$word/} keys %{$self->{'_PARAMS'}};
128 # Return a list of valid parameter values, if the parameter has
135 unless(exists($self->{'_PARAMS'}->{$param}->[2])) {
138 return grep {/^$word/} keys %{$self->{'_PARAMS'}->{$param}->[2]};
145 my $s = 'Help for ' . $self->{'NAME'} . ":\n";
149 if (exists($self->{'DESC'})) {
150 $s .= "Description:\n " . $self->{'DESC'};
154 foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
155 $p = $self->{'_PARAMS'}->{$k}->[0];
157 if (exists($self->{'_PARAMS'}->{$k}->[2])) {
158 # The parameter has a list of allowed values. Add the keys and their help
159 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
160 foreach (sort keys(%{$self->{'_PARAMS'}->{$k}->[2]})) {
161 $s .= sprintf(" %s: %s\n", $_, $self->{'_PARAMS'}->{$k}->[2]->{$_});
164 # The parameter just has a default value and a help text
165 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
175 $self->{'_DEBUG'} = shift;
182 $ua = LWP::UserAgent->new(
183 'agent' => 'Mozilla/5.0',
184 'cookie_jar' => HTTP::Cookies->new,
188 # Remove a currently defined HTTPS proxy. See below for a longer explanation.
189 delete($ENV{'HTTPS_PROXY'});
191 if (defined($self->{'_CONNECTOR'})) {
192 my $schemas = $self->{'_CONNECTOR'}->{'schemas'};
193 foreach (keys(%{$schemas})) {
194 $self->debug("Adding schema %s with proxy %s", $_, $schemas->{$_});
196 # OK, so here's the gist.
198 # The usual way of reqesting an HTTPS URL through a proxy is
199 # to connect to the proxy server, issue a CONNECT request to
200 # create a channel to the web server and start an SSL session over
201 # this channel, so there is an end-to-end connection between
202 # the client and the server.
204 # Setting a proxy for the https schema in LWP WILL NOT ACCOMPLISH
207 # LWP will connect to the proxy server, and issue a standard GET
208 # request for the target URL, which most proxy servers will refuse
211 # The way to use a proxy server is to set some environment variables
212 # and let the underlying Crypt::SSLeay module do the rest.
214 # This is positively appaling.
215 $ENV{'HTTPS_PROXY'} = $schemas->{$_};
217 $ua->proxy($_, $schemas->{$_});
222 $self->{_CACHED_UA} = $ua;
230 return $self->{_CACHED_UA};
236 my $ua = shift || $self->ua();
240 return $r->decoded_content() if $r->is_success();
248 $d =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge;
253 sub decode_querystring {
256 return { map { split /=/, $_, 2; } split /&/, shift };
262 return $self->{_API}->{connectors}->();
268 $self->{'_CONNECTOR'} = shift;
272 # Register a callbacks into the core API to the plugin.
273 # Example of those are config getter/setters and IO functions
274 # The API is a hash reference containing subroutine references.
276 # After the API is registered an attempt is made to load the config
277 # (or set defaults if config values are not found)
283 $self->{_API} = $api;