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,
163 # Remove a currently defined HTTPS proxy. See below for a longer explanation.
164 delete($ENV{'HTTPS_PROXY'});
166 if (defined($self->{'_CONNECTOR'})) {
167 my $schemas = $self->{'_CONNECTOR'}->{'schemas'};
168 foreach (keys(%{$schemas})) {
169 $self->debug("Adding schema %s with proxy %s", $_, $schemas->{$_});
171 # OK, so here's the gist.
173 # The usual way of reqesting an HTTPS URL through a proxy is
174 # to connect to the proxy server, issue a CONNECT request to
175 # create a channel to the web server and start an SSL session over
176 # this channel, so there is an end-to-end connection between
177 # the client and the server.
179 # Setting a proxy for the https schema in LWP WILL NOT ACCOMPLISH
182 # LWP will connect to the proxy server, and issue a standard GET
183 # request for the target URL, which most proxy servers will refuse
186 # The way to use a proxy server is to set some environment variables
187 # and let the underlying Crypt::SSLeay module do the rest.
189 # This is positively appaling.
190 $ENV{'HTTPS_PROXY'} = $schemas->{$_};
192 $ua->proxy($_, $schemas->{$_});
197 $self->{_CACHED_UA} = $ua;
205 return $self->{_CACHED_UA};
211 my $ua = shift || $self->ua();
214 $self->debug("Getting %s", $url);
216 $self->debug("Return code: %s", $r->status_line);
217 $self->debug("Content length: %d", length($r->decoded_content)) if $r->is_success();
218 return $r->decoded_content() if $r->is_success();
226 $d =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge;
231 sub decode_querystring {
234 return { map { split /=/, $_, 2; } split /&/, shift };
240 return $self->{_API}->{connectors}->();
246 $self->{'_CONNECTOR'} = shift;
250 # This function was used in previous versions of videosite. If it's called
251 # we are dealing with an old plugin which probably needs some minor modifications
254 # Generate a warning message.
256 sub _prepare_parameters {
259 $self->error("THIS MODULE IS CALLING _prepare_parameters(). THIS FUNCTION IS DEPRECATED. See readme.txt in the plugin directory.");
263 # Register a callbacks into the core API to the plugin.
264 # Example of those are config getter/setters and IO functions
265 # The API is a hash reference containing subroutine references.
271 $self->{_API} = $api;