X-Git-Url: https://git.camperquake.de/gitweb.cgi?p=quotesite.git;a=blobdiff_plain;f=quotesite%2FBase.pm;h=ebd65c3ede537a7e805480af79dfc9f95dcf506e;hp=d8a4e988b50ec500f23719752ea38ab7af063f84;hb=a5457541076766804405416fb653592932c688ec;hpb=7a2a97ead43f07fbeb25bca43f40c7f994cc77ab diff --git a/quotesite/Base.pm b/quotesite/Base.pm index d8a4e98..ebd65c3 100644 --- a/quotesite/Base.pm +++ b/quotesite/Base.pm @@ -1,9 +1,11 @@ # (c) 2007 by Ralf Ertzinger # licensed under GNU GPL v2 -package Base; +package quotesite::Base; use strict; +use LWP::UserAgent; +use HTTP::Cookies; use Data::Dumper; sub new { @@ -111,6 +113,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"; @@ -125,7 +151,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; @@ -137,4 +172,30 @@ 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, + 'timeout' => 15, + ); + + $self->{_CACHED_UA} = $ua; + + 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; +} + 1;