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'];
34 $data[0] = "(" . ref($self) . ") " . $data[0];
36 $self->{_API}->{io}->(@data);
43 $data[0] = "DEBUG: " . $data[0];
44 if ($self->{'_DEBUG'} != 0) {$self->error(@data)};
50 my $path = ['plugin', $self->{NAME}, $key];
53 # Try to read from the global config
54 # Fall back to default
55 if ($self->{_API}->{config_has}->($path)) {
56 $val = $self->{_API}->{config_get}->($path);
57 } elsif (exists($self->{_PARAMS}->{$key})) {
58 $val = $self->{_PARAMS}->{$key}->[0];
60 $self->error('Requested unknown config key %s', $key);
63 $self->debug('Returning %s=%s', $key, $val);
71 my $path = ['plugin', $self->{NAME}, $key];
73 if (exists($self->{'_PARAMS'}->{$key})) {
74 $self->{_API}->{config_set}->($path, $val);
76 $self->error('Module does not have a parameter named %s', $self->$key);
82 my $s = 'Options for ' . $self->{'NAME'} . ":\n";
86 foreach $k (keys(%{$self->{'_PARAMS'}})) {
87 $p = $self->{_API}->{config_get}->{$k};
88 $s .= sprintf(" %s: %s", $k, $p);
89 if ($p eq $self->{'_PARAMS'}->{$k}->[0]) {
99 # Return a list of the parameters supported by the module
100 # Does not return the 'enabled' parameter
105 return grep {$_ ne 'enabled' && /^$word/} keys %{$self->{'_PARAMS'}};
108 # Return a list of valid parameter values, if the parameter has
115 unless(exists($self->{'_PARAMS'}->{$param}->[2])) {
118 return grep {/^$word/} keys %{$self->{'_PARAMS'}->{$param}->[2]};
125 my $s = 'Help for ' . $self->{'NAME'} . ":\n";
129 if (exists($self->{'DESC'})) {
130 $s .= "Description:\n " . $self->{'DESC'};
134 foreach $k (keys(%{$self->{'_PARAMS'}})) {
135 $p = $self->{'_PARAMS'}->{$k}->[0];
136 if (exists($self->{'_PARAMS'}->{$k}->[2])) {
137 # The parameter has a list of allowed values. Add the keys and their help
138 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
139 foreach (sort keys(%{$self->{'_PARAMS'}->{$k}->[2]})) {
140 $s .= sprintf(" %s: %s\n", $_, $self->{'_PARAMS'}->{$k}->[2]->{$_});
143 # The parameter just has a default value and a help text
144 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
154 $self->{'_DEBUG'} = shift;
161 $ua = LWP::UserAgent->new(
162 'agent' => 'Mozilla/5.0',
163 'cookie_jar' => HTTP::Cookies->new,
167 # Remove a currently defined HTTPS proxy. See below for a longer explanation.
168 delete($ENV{'HTTPS_PROXY'});
170 if (defined($self->{'_CONNECTOR'})) {
171 my $schemas = $self->{'_CONNECTOR'}->{'schemas'};
172 foreach (keys(%{$schemas})) {
173 $self->debug("Adding schema %s with proxy %s", $_, $schemas->{$_});
175 # OK, so here's the gist.
177 # The usual way of reqesting an HTTPS URL through a proxy is
178 # to connect to the proxy server, issue a CONNECT request to
179 # create a channel to the web server and start an SSL session over
180 # this channel, so there is an end-to-end connection between
181 # the client and the server.
183 # Setting a proxy for the https schema in LWP WILL NOT ACCOMPLISH
186 # LWP will connect to the proxy server, and issue a standard GET
187 # request for the target URL, which most proxy servers will refuse
190 # The way to use a proxy server is to set some environment variables
191 # and let the underlying Crypt::SSLeay module do the rest.
193 # This is positively appaling.
194 $ENV{'HTTPS_PROXY'} = $schemas->{$_};
196 $ua->proxy($_, $schemas->{$_});
201 $self->{_CACHED_UA} = $ua;
209 return $self->{_CACHED_UA};
215 my $ua = shift || $self->ua();
219 return $r->decoded_content() if $r->is_success();
227 $d =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge;
232 sub decode_querystring {
235 return { map { split /=/, $_, 2; } split /&/, shift };
241 return $self->{_API}->{connectors}->();
247 $self->{'_CONNECTOR'} = shift;
251 # Register a callbacks into the core API to the plugin.
252 # Example of those are config getter/setters and IO functions
253 # The API is a hash reference containing subroutine references.
255 # After the API is registered an attempt is made to load the config
256 # (or set defaults if config values are not found)
262 $self->{_API} = $api;