1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
4 package quotesite::Base;
13 my $self = {'_DEBUG' => 0, '_OUT' => sub {print shift}};
17 $self->_prepare_parameters();
26 $t = sprintf(shift(@_), @_);
35 $data[0] = "DEBUG: " . $data[0];
36 if ($self->{'_DEBUG'} != 0) {$self->error(@data)};
44 return $self->{'_CONFIG'} unless defined($c);
46 foreach $o (keys(%{$c->{'option'}})) {
47 if (exists($self->{'_CONFIG'}->{'option'}->{$o})) {
48 $self->{'_CONFIG'}->{'option'}->{$o}->{'content'} = $c->{'option'}->{$o}->{'content'};
52 return $self->{'_CONFIG'};
55 sub _prepare_parameters {
59 $self->{'_CONFIG'} = {'option' => {'enabled' => {'content' => '1'}}};
61 foreach $p (keys(%{$self->{'_PARAMS'}})) {
62 $self->{'_CONFIG'}->{'option'}->{$p}->{'content'} = $self->{'_PARAMS'}->{$p}->[0];
71 $val = $self->{'_CONFIG'}->{'option'}->{$key}->{'content'};
72 $self->debug('Returning %s=%s', $key, $val);
82 if (exists($self->{'_CONFIG'}->{'option'}->{$key})) {
83 $self->{'_CONFIG'}->{'option'}->{$key}->{'content'} = $val;
85 $self->error('Module %s does not have a parameter named %s', $self->{'NAME'}, $key);
93 $self->{'_OUT'} = $io;
98 my $s = 'Options for ' . $self->{'NAME'} . ":\n";
102 foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
103 $p = $self->{'_CONFIG'}->{'option'}->{$k}->{'content'};
105 $s .= sprintf(" %s: %s", $k, $p);
106 if ($self->{'_CONFIG'}->{'option'}->{$k}->{'content'} eq $self->{'_PARAMS'}->{$k}->[0]) {
107 $s .= " (default)\n";
116 # Return a list of the parameters supported by the module
117 # Does not return the 'enabled' parameter
122 return grep {$_ ne 'enabled' && /^$word/} keys %{$self->{'_PARAMS'}};
125 # Return a list of valid parameter values, if the parameter has
132 unless(exists($self->{'_PARAMS'}->{$param}->[2])) {
135 return grep {/^$word/} keys %{$self->{'_PARAMS'}->{$param}->[2]};
142 my $s = 'Help for ' . $self->{'NAME'} . ":\n";
146 if (exists($self->{'DESC'})) {
147 $s .= "Description:\n " . $self->{'DESC'};
151 foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
152 $p = $self->{'_PARAMS'}->{$k}->[0];
154 if (exists($self->{'_PARAMS'}->{$k}->[2])) {
155 # The parameter has a list of allowed values. Add the keys and their help
156 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
157 foreach (sort keys(%{$self->{'_PARAMS'}->{$k}->[2]})) {
158 $s .= sprintf(" %s: %s\n", $_, $self->{'_PARAMS'}->{$k}->[2]->{$_});
161 # The parameter just has a default value and a help text
162 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
172 $self->{'_DEBUG'} = shift;
179 $ua = LWP::UserAgent->new(
180 'agent' => 'Mozilla/5.0',
181 'cookie_jar' => HTTP::Cookies->new,
185 $self->{_CACHED_UA} = $ua;
193 my $ua = shift || $self->ua();
197 return $r->decoded_content() if $r->is_success();