Base: Add simple_get() function
[videosite.git] / videosite / Base.pm
index 5aa8eae..b31fcb6 100644 (file)
@@ -1,15 +1,22 @@
 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
 # licensed under GNU GPL v2
 
-package Base;
+package videosite::Base;
 
 use strict;
-use Irssi;
+use LWP::UserAgent;
+use HTTP::Cookies;
+use Data::Dumper;
 
 sub new {
     my $class = shift;
-    my $self = {'_DEBUG' => 0};
-
+    my $self = {'_DEBUG' => 0,
+                '_OUT' => sub {printf(@_)},
+                '_CONNECTORS' => sub { return ({ 'name' => 'direct',
+                                                 'schemas' => {} }) },
+                '_CONNECTOR' => undef,
+               };
+    
     bless($self, $class);
 
     $self->_prepare_parameters();
@@ -21,7 +28,9 @@ sub error {
     my $self = shift;
     my @data = @_;
 
-    Irssi::print(sprintf(shift(@_), @_));
+    $data[0] = "(" . ref($self) . ") " . $data[0];
+
+    $self->{'_OUT'}(@data);
 }
 
 sub debug {
@@ -37,6 +46,8 @@ sub mergeconfig {
     my $c = shift;
     my $o;
 
+    return $self->{'_CONFIG'} unless defined($c);
+
     foreach $o (keys(%{$c->{'option'}})) {
         if (exists($self->{'_CONFIG'}->{'option'}->{$o})) {
             $self->{'_CONFIG'}->{'option'}->{$o}->{'content'} = $c->{'option'}->{$o}->{'content'};
@@ -50,8 +61,7 @@ sub _prepare_parameters {
     my $self = shift;
     my $p;
 
-    $self->{'_CONFIG'} = { 'module' => $self->{'NAME'},
-                           'option' => {}};
+    $self->{'_CONFIG'} = {'option' => {'enabled' => {'content' => '1'}}};
 
     foreach $p (keys(%{$self->{'_PARAMS'}})) {
         $self->{'_CONFIG'}->{'option'}->{$p}->{'content'} = $self->{'_PARAMS'}->{$p}->[0];
@@ -61,8 +71,12 @@ sub _prepare_parameters {
 sub _getval {
     my $self = shift;
     my $key = shift;
+    my $val;
+
+    $val = $self->{'_CONFIG'}->{'option'}->{$key}->{'content'};
+    $self->debug('Returning %s=%s', $key, $val);
 
-    return $self->{'_CONFIG'}->{'option'}->{$key}->{'content'}
+    return $val;
 }
 
 sub setval {
@@ -77,6 +91,13 @@ sub setval {
     }
 }
 
+sub setio {
+    my $self = shift;
+    my $io = shift;
+
+    $self->{'_OUT'} = $io;
+}
+
 sub getconfstr {
     my $self = shift;
     my $s = 'Options for ' . $self->{'NAME'} . ":\n";
@@ -87,7 +108,7 @@ sub getconfstr {
         $p = $self->{'_CONFIG'}->{'option'}->{$k}->{'content'};
         $p =~ s/%/%%/g;
         $s .= sprintf("  %s: %s", $k, $p);
-        if ($self->{'_CONFIG'}->{'option'}->{$k}->{'content'} == $self->{'_PARAMS'}->{$k}->[0]) {
+        if ($self->{'_CONFIG'}->{'option'}->{$k}->{'content'} eq $self->{'_PARAMS'}->{$k}->[0]) {
             $s .= " (default)\n";
         } else {
             $s .= "\n";
@@ -97,6 +118,30 @@ sub getconfstr {
     return $s;
 }
 
+# Return a list of the parameters supported by the module
+# Does not return the 'enabled' parameter
+sub getparamlist {
+    my $self = shift;
+    my $word = shift;
+
+    return grep {$_ ne 'enabled' && /^$word/} keys %{$self->{'_PARAMS'}};
+}
+
+# Return a list of valid parameter values, if the parameter has
+# such a list.
+sub getparamvalues {
+    my $self = shift;
+    my $param = shift;
+    my $word = shift;
+
+    unless(exists($self->{'_PARAMS'}->{$param}->[2])) {
+        return ();
+    } else {
+        return grep {/^$word/} keys %{$self->{'_PARAMS'}->{$param}->[2]};
+    }
+}
+
+
 sub gethelpstr {
     my $self = shift;
     my $s = 'Help for ' . $self->{'NAME'} . ":\n";
@@ -111,7 +156,16 @@ sub gethelpstr {
     foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
         $p = $self->{'_PARAMS'}->{$k}->[0];
         $p =~ s/%/%%/g;
-        $s .= sprintf("  %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
+        if (exists($self->{'_PARAMS'}->{$k}->[2])) {
+            # The parameter has a list of allowed values. Add the keys and their help
+            $s .= sprintf("  %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
+            foreach (sort keys(%{$self->{'_PARAMS'}->{$k}->[2]})) {
+                $s .= sprintf("     %s: %s\n", $_, $self->{'_PARAMS'}->{$k}->[2]->{$_});
+            }
+        } else {
+            # The parameter just has a default value and a help text
+            $s .= sprintf("  %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
+        }
     }
 
     return $s;
@@ -123,4 +177,91 @@ sub setdebug {
     $self->{'_DEBUG'} = shift;
 }
 
+sub ua {
+    my $self = shift;
+    my $ua;
+
+    $ua = LWP::UserAgent->new('agent' => 'Mozilla/5.0', 'cookie_jar' => HTTP::Cookies->new);
+
+    # Remove a currently defined HTTPS proxy. See below for a longer explanation.
+    delete($ENV{'HTTPS_PROXY'});
+
+    if (defined($self->{'_CONNECTOR'})) {
+        my $schemas = $self->{'_CONNECTOR'}->{'schemas'};
+        foreach (keys(%{$schemas})) {
+            $self->debug("Adding schema %s with proxy %s", $_, $schemas->{$_});
+            if ($_ eq 'https') {
+                # OK, so here's the gist.
+                #
+                # The usual way of reqesting an HTTPS URL through a proxy is
+                # to connect to the proxy server, issue a CONNECT request to 
+                # create a channel to the web server and start an SSL session over
+                # this channel, so there is an end-to-end connection between
+                # the client and the server.
+                #
+                # Setting a proxy for the https schema in LWP WILL NOT ACCOMPLISH
+                # THIS.
+                #
+                # LWP will connect to the proxy server, and issue a standard GET
+                # request for the target URL, which most proxy servers will refuse
+                # to get.
+                #
+                # The way to use a proxy server is to set some environment variables
+                # and let the underlying Crypt::SSLeay module do the rest.
+                #
+                # This is positively appaling.
+                $ENV{'HTTPS_PROXY'} = $schemas->{$_};
+            } else {
+                $ua->proxy($_, $schemas->{$_});
+            }
+        }
+    }
+
+    return $ua;
+}
+
+sub simple_get {
+    my $self = shift;
+    my $url = shift;
+    my $ua = shift || $self->ua();
+    my $r;
+
+    $r = $ua->get($url);
+    return $r->decoded_content() if $r->is_success();
+    return undef;
+}
+
+sub decode_hexurl {
+    my $self = shift;
+    my $d = shift;
+
+    $d =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge;
+
+    return $d;
+}
+
+sub decode_querystring {
+    my $self = shift;
+
+    return { map { split /=/, $_, 2; } split /&/, shift };
+}
+
+sub connectors {
+    my $self = shift;
+    
+    return $self->{'_CONNECTORS'}->();
+}
+
+sub selectconn {
+    my $self = shift;
+
+    $self->{'_CONNECTOR'} = shift;
+}
+
+sub setconn {
+    my $self = shift;
+
+    $self->{'_CONNECTORS'} = shift;
+}
+
 1;