1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
4 package videosite::Base;
13 my $self = {'_CONNECTOR' => undef,
15 io => sub { print(@_) },
16 io_debug => sub { print(@_) },
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] = "(" . ref($self) . ") " . $data[0];
45 $self->{_API}->{io_debug}->(@data);
51 my $path = ['plugin', $self->{NAME}, $key];
54 # Try to read from the global config
55 # Fall back to default
56 if ($self->{_API}->{config_has}->($path)) {
57 $val = $self->{_API}->{config_get}->($path);
58 } elsif (exists($self->{_PARAMS}->{$key})) {
59 $val = $self->{_PARAMS}->{$key}->[0];
61 $self->error('Requested unknown config key %s', $key);
64 $self->debug('Returning %s=%s', $key, $val);
72 my $path = ['plugin', $self->{NAME}, $key];
74 if (exists($self->{'_PARAMS'}->{$key})) {
75 $self->{_API}->{config_set}->($path, $val);
77 $self->error('Module does not have a parameter named %s', $self->$key);
83 my $s = 'Options for ' . $self->{'NAME'} . ":\n";
87 foreach $k (keys(%{$self->{'_PARAMS'}})) {
88 $p = $self->_getval($k);
89 $s .= sprintf(" %s: %s", $k, $p);
90 if ($p eq $self->{'_PARAMS'}->{$k}->[0]) {
100 # Return a list of the parameters supported by the module
101 # Does not return the 'enabled' parameter
106 return grep {$_ ne 'enabled' && /^$word/} keys %{$self->{'_PARAMS'}};
109 # Return a list of valid parameter values, if the parameter has
116 unless(exists($self->{'_PARAMS'}->{$param}->[2])) {
119 return grep {/^$word/} keys %{$self->{'_PARAMS'}->{$param}->[2]};
126 my $s = 'Help for ' . $self->{'NAME'} . ":\n";
130 if (exists($self->{'DESC'})) {
131 $s .= "Description:\n " . $self->{'DESC'};
135 foreach $k (keys(%{$self->{'_PARAMS'}})) {
136 $p = $self->{'_PARAMS'}->{$k}->[0];
137 if (exists($self->{'_PARAMS'}->{$k}->[2])) {
138 # The parameter has a list of allowed values. Add the keys and their help
139 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
140 foreach (sort keys(%{$self->{'_PARAMS'}->{$k}->[2]})) {
141 $s .= sprintf(" %s: %s\n", $_, $self->{'_PARAMS'}->{$k}->[2]->{$_});
144 # The parameter just has a default value and a help text
145 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
156 $ua = LWP::UserAgent->new(
157 'agent' => 'Mozilla/5.0',
158 'cookie_jar' => HTTP::Cookies->new,
162 # Remove a currently defined HTTPS proxy. See below for a longer explanation.
163 delete($ENV{'HTTPS_PROXY'});
165 if (defined($self->{'_CONNECTOR'})) {
166 my $schemas = $self->{'_CONNECTOR'}->{'schemas'};
167 foreach (keys(%{$schemas})) {
168 $self->debug("Adding schema %s with proxy %s", $_, $schemas->{$_});
170 # OK, so here's the gist.
172 # The usual way of reqesting an HTTPS URL through a proxy is
173 # to connect to the proxy server, issue a CONNECT request to
174 # create a channel to the web server and start an SSL session over
175 # this channel, so there is an end-to-end connection between
176 # the client and the server.
178 # Setting a proxy for the https schema in LWP WILL NOT ACCOMPLISH
181 # LWP will connect to the proxy server, and issue a standard GET
182 # request for the target URL, which most proxy servers will refuse
185 # The way to use a proxy server is to set some environment variables
186 # and let the underlying Crypt::SSLeay module do the rest.
188 # This is positively appaling.
189 $ENV{'HTTPS_PROXY'} = $schemas->{$_};
191 $ua->proxy($_, $schemas->{$_});
196 $self->{_CACHED_UA} = $ua;
204 return $self->{_CACHED_UA};
210 my $ua = shift || $self->ua();
214 return $r->decoded_content() if $r->is_success();
222 $d =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge;
227 sub decode_querystring {
230 return { map { split /=/, $_, 2; } split /&/, shift };
236 return $self->{_API}->{connectors}->();
242 $self->{'_CONNECTOR'} = shift;
246 # This function was used in previous versions of videosite. If it's called
247 # we are dealing with an old plugin which probably needs some minor modifications
250 # Generate a warning message.
252 sub _prepare_parameters {
255 $self->error("THIS MODULE IS CALLING _prepare_parameters(). THIS FUNCTION IS DEPRECATED. See readme.txt in the plugin directory.");
259 # Register a callbacks into the core API to the plugin.
260 # Example of those are config getter/setters and IO functions
261 # The API is a hash reference containing subroutine references.
267 $self->{_API} = $api;