Merge branch 'master' into plugin
authorChristian Garbs <mitch@cgarbs.de>
Fri, 26 Dec 2008 23:00:07 +0000 (00:00 +0100)
committerChristian Garbs <mitch@cgarbs.de>
Fri, 26 Dec 2008 23:00:07 +0000 (00:00 +0100)
Conflicts:

bugs/issue-fa406e503dacdd866dfa5b4e8d2a60b4bdebaaeb.yaml

bugs/issue-bb5a7128e13bf2bc9da92485fe3798caa070080a.yaml [new file with mode: 0644]
xmlrtorrent.pl
xmlrtorrent.pm
xmlrtorrent/Base.pm [new file with mode: 0644]
xmlrtorrent/HTTPTalker.pm [new file with mode: 0644]
xmlrtorrent/TalkerBase.pm [new file with mode: 0644]

diff --git a/bugs/issue-bb5a7128e13bf2bc9da92485fe3798caa070080a.yaml b/bugs/issue-bb5a7128e13bf2bc9da92485fe3798caa070080a.yaml
new file mode 100644 (file)
index 0000000..bf5c7ae
--- /dev/null
@@ -0,0 +1,22 @@
+--- !ditz.rubyforge.org,2008-03-06/issue 
+title: use unix domain sockets
+desc: for local communication no webserver should be needed
+type: :feature
+component: xmlrtorrent
+release: 
+reporter: Christian Garbs <mitch@cgarbs.de>
+status: :in_progress
+disposition: 
+creation_time: 2008-12-13 22:32:31.573289 Z
+references: []
+
+id: bb5a7128e13bf2bc9da92485fe3798caa070080a
+log_events: 
+- - 2008-12-13 22:32:33.548753 Z
+  - Christian Garbs <mitch@cgarbs.de>
+  - created
+  - ""
+- - 2008-12-26 20:26:02.568421 Z
+  - Christian Garbs <mitch@cgarbs.de>
+  - changed status from unstarted to in_progress
+  - ""
index d3e4f81..12367cb 100644 (file)
@@ -13,9 +13,12 @@ use File::Spec;
 use List::Util qw(max);
 use xmlrtorrent;
 
+my @talkers;
+my $talker;
 my $conf;
 my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'xmlrtorrent.xml');
 my $scriptdir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts');
+my $plugindir = File::Spec->catfile($scriptdir, 'xmlrtorrent');
 my %torrentlist = ();
 my $torrentindex = 1;
 my $rtorrent;
@@ -30,7 +33,7 @@ my $PARAMS = {
 };
 
 # activate debug here
-my $debug = 0;
+my $debug = 1;
 
 # "message public", SERVER_REC, char *msg, char *nick, char *address, char *target
 signal_add_last("message public" => sub {check_for_link(\@_,1,4,2,0);});
@@ -75,6 +78,10 @@ my $xmlrtorrent_commands = {
         cmd_remote(@_);
     },
 
+    'talker' => sub {
+       cmd_talker(@_);
+    },
+
     'debug' => sub {
         $debug = 1;
         write_irssi('Enabled debugging');
@@ -202,7 +209,7 @@ sub cmd_queue {
         $u = $torrentlist{$id}->{'URL'};
 
         write_debug('Sending %s to rtorrent', $u);
-        unless(defined($rtorrent->load_start($u))) {
+        unless(defined($rtorrent->load_start($talker, $u))) {
             write_irssi('%%RError sending URL %s: %s', $u, $rtorrent->errstr());
         } else {
             write_irssi('%s enqueued', $u);
@@ -237,7 +244,7 @@ sub cmd_remote {
     my $rqueue;
 
     if (('list' eq $subcmd) or !defined($subcmd)) {
-        unless(defined($rqueue = $rtorrent->download_list())) {
+        unless(defined($rqueue = $rtorrent->download_list($talker))) {
             write_irssi('Error getting list of downloads: %s', $rtorrent->errstr());
             return;
         }
@@ -286,26 +293,17 @@ sub cmd_set {
     my $target = shift;
     my $key = shift;
     my $val = shift;
+    my $p;
 
-    if ('global' eq $target) {
-        if(exists($PARAMS->{$key})) {
-            $conf->{'xmlrtorrent'}->{$key} = $val;
-            if ('XMLURL' eq $key) {
-                unless(defined($rtorrent = xmlrtorrent->new(
-                        'XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'},
-                        'USERNAME' => $conf->{'xmlrtorrent'}->{'USERNAME'},
-                        'USERNAME' => $conf->{'xmlrtorrent'}->{'PASSWORD'}))) {
-                    write_irssi('Could not initialize XMLRPC instance');
-                    return;
-                }
-            }
-        } else {
-            write_irssi('Key %s does not exist', $key);
+    foreach $p (@talkers) {
+        if ($p->{'NAME'} eq $target) {
+            $p->setval($key, $val);
+            return;
         }
     }
+    write_irssi(undef, 'No such module');
 }
 
-
 sub cmd_show {
     my $target = shift;
     my $p;
@@ -316,15 +314,46 @@ sub cmd_help {
     my $target = shift;
     my $p;
 
-    write_irssi(<<'EOT');
+    if (defined($target)) {
+        foreach $p (@talkers) {
+            if ($p->{'NAME'} eq $target) {
+                write_irssi($p->gethelpstr());
+                return;
+            }
+        }
+        write_irssi('No such module');
+    } else {
+       write_irssi(<<'EOT');
 Supported commands:
- save: Save the current configuration
- help: Display this help
+ save: save the current configuration
+ help [modulename]: display this help or module specific help
+ talker [modulename]: display or set the talker to use
  debug: enable debugging messages
  nodebug: disable debugging messages
 EOT
+;
+    }
 }
 
+sub cmd_talker {
+    my $target = shift;
+    my $p;
+
+    if (defined($target)) {
+        foreach $p (@talkers) {
+            if (($p->{'NAME'} eq $target) && ($p->{'TYPE'} eq 'talker')) {
+                $talker = $p;
+                $conf->{'videosite'}->{'talker'} = $target;
+                return;
+            }
+        }
+        write_irssi('No such talker');
+    } else {
+        write_irssi('Current talker: %s', $conf->{'videosite'}->{'talker'});
+    }
+}
+
+
 
 # save on unload
 sub sig_command_script_unload {
@@ -334,6 +363,64 @@ sub sig_command_script_unload {
     }
 }
 
+sub ploader {
+
+    my $dir = shift;
+    my $pattern = shift;
+    my $type = shift;
+    my @list;
+    my $p;
+    my $g;
+    my @g = ();
+
+    opendir(D, $dir) || return ();
+    @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
+    closedir(D);
+
+    foreach $p (@list) {
+        write_debug('Trying to load %s:', $p);
+        $p =~ s/\.pm$//;
+        eval qq{ require xmlrtorrent::$p; };
+        if ($@) {
+            write_irssi('Failed to load plugin: %s', "$@");
+            next;
+        }
+
+        $g = eval qq{ xmlrtorrent::$p->new(); };
+        if ($@) {
+            write_irssi('Failed to instanciate: %s', "$@");
+            delete($INC{$p});
+            next;
+        }
+
+        write_debug('found %s %s', $g->{'TYPE'}, $g->{'NAME'});
+        if ($type eq $g->{'TYPE'}) {
+            push(@g, $g);
+            $g->setio(sub {Irssi::print(shift)});
+        } else {
+            write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
+            delete($INC{$p});
+        }
+    }
+
+    write_debug('Loaded %d plugins', $#g+1);
+    
+    return @g;
+}
+
+sub _load_modules($) {
+
+    my $path = shift;
+
+    foreach (keys(%INC)) {
+        if ($INC{$_} =~ m|^$path|) {
+            write_debug('Removing %s from $INC', $_);
+            delete($INC{$_});
+        }
+    }
+    @talkers = ploader($path, '.*Talker\.pm$', 'talker');
+}
+
 sub init_xmlrtorrent {
 
     my $bindings = shift;
@@ -350,15 +437,29 @@ sub init_xmlrtorrent {
         }
     }
 
+    _load_modules($plugindir);
+
+    unless (defined(@talkers)) {
+        write_irssi('No talkers found, can not proceed.');
+        return;
+    }
+
+    $talker = $talkers[0];
+    foreach $p (@talkers) {
+        if ($conf->{'xmlrtorrent'}->{'talker'} eq $p->{'NAME'}) {
+            $talker = $p;
+        }
+    }
+    write_debug(undef, 'Selected %s as talker', $talker->{'NAME'});
+    $conf->{'videosite'}->{'talker'} = $talker->{'NAME'};
+
+
     # Restore the queue
     %torrentlist = %{$conf->{'xmlrtorrent'}->{'_QUEUE'}};
     %torrentlist = map { my $a = substr($_, 1); ("$a" => $torrentlist{$_}) } keys(%torrentlist);
     $torrentindex = max(keys(%torrentlist)) + 1;
 
-    unless(defined($rtorrent = xmlrtorrent->new(
-            'XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'},
-            'USERNAME' => $conf->{'xmlrtorrent'}->{'USERNAME'},
-            'USERNAME' => $conf->{'xmlrtorrent'}->{'PASSWORD'}))) {
+    unless(defined($rtorrent = xmlrtorrent->new())) {
         write_irssi('Could not initialize XMLRPC instance');
         return;
     }
index b1c41b9..d50d5ac 100644 (file)
@@ -1,33 +1,22 @@
 package xmlrtorrent;
 
 use strict;
-use RPC::XML;
-use RPC::XML::Client;
 use Data::Dumper;
 
 sub new {
     my $class = shift;
     my $self = {@_};
 
-    unless(exists($self->{'XMLURL'}) and defined($self->{'XMLURL'})) {
-        return undef;
-    }
-
-    $self->{'__RPCClient'} = RPC::XML::Client->new($self->{'XMLURL'});
-    if ((exists($self->{'USERNAME'}) and exists($self->{'PASSWORD'})) and
-        ($self->{'USERNAME'} ne '')) {
-        $self->{'__RPCClient'}->credentials('', $self->{'USERNAME'}, $self->{'PASSWORD'});
-    }
-
     return bless($self, $class);
 }
 
-sub load_start{
+sub load_start {
     my $self = shift;
+    my $talker = shift;
     my $URL = shift;
     my $res;
 
-    $res = $self->{'__RPCClient'}->send_request('load_start', $URL);
+    $res = $talker->send_request('load_start', $URL);
     unless(ref($res)) {
         $self->{'__ERROR'} = $res;
         return undef
@@ -43,9 +32,10 @@ sub load_start{
 
 sub download_list {
     my $self = shift;
+    my $talker = shift;
     my $res;
 
-    $res = $self->{'__RPCClient'}->send_request('d.multicall', '',
+    $res = $talker->send_request('d.multicall', '',
             'd.get_name=',
             'd.get_size_bytes=',
             'd.get_bytes_done=',
diff --git a/xmlrtorrent/Base.pm b/xmlrtorrent/Base.pm
new file mode 100644 (file)
index 0000000..e85b517
--- /dev/null
@@ -0,0 +1,171 @@
+# (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
+# licensed under GNU GPL v2
+
+package xmlrtorrent::Base;
+
+use strict;
+use Data::Dumper;
+
+sub new {
+    my $class = shift;
+    my $self = {'_DEBUG' => 0, '_OUT' => sub {print shift}};
+
+    bless($self, $class);
+
+    $self->_prepare_parameters();
+
+    return $self;
+}
+
+sub error {
+    my $self = shift;
+    my $t;
+
+    $t = sprintf(shift(@_), @_);
+    $t =~ s/%/%%/g;
+    $self->{'_OUT'}($t);
+}
+
+sub debug {
+    my $self = shift;
+    my @data = @_;
+
+    $data[0] = "DEBUG: " . $data[0];
+    if ($self->{'_DEBUG'} != 0) {$self->error(@data)};
+}
+
+sub mergeconfig {
+    my $self = shift;
+    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'};
+        }
+    }
+
+    return $self->{'_CONFIG'};
+}
+
+sub _prepare_parameters {
+    my $self = shift;
+    my $p;
+
+    foreach $p (keys(%{$self->{'_PARAMS'}})) {
+        $self->{'_CONFIG'}->{'option'}->{$p}->{'content'} = $self->{'_PARAMS'}->{$p}->[0];
+    }
+}
+
+sub _getval {
+    my $self = shift;
+    my $key = shift;
+    my $val;
+
+    $val = $self->{'_CONFIG'}->{'option'}->{$key}->{'content'};
+    $self->debug('Returning %s=%s', $key, $val);
+
+    return $val;
+}
+
+sub setval {
+    my $self = shift;
+    my $key = shift;
+    my $val = shift;
+
+    if (exists($self->{'_CONFIG'}->{'option'}->{$key})) {
+        $self->{'_CONFIG'}->{'option'}->{$key}->{'content'} = $val;
+    } else {
+        $self->error('Module %s does not have a parameter named %s', $self->{'NAME'}, $key);
+    }
+}
+
+sub setio {
+    my $self = shift;
+    my $io = shift;
+
+    $self->{'_OUT'} = $io;
+}
+
+sub getconfstr {
+    my $self = shift;
+    my $s = 'Options for ' . $self->{'NAME'} . ":\n";
+    my $k;
+    my $p;
+
+    foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
+        $p = $self->{'_CONFIG'}->{'option'}->{$k}->{'content'};
+        $p =~ s/%/%%/g;
+        $s .= sprintf("  %s: %s", $k, $p);
+        if ($self->{'_CONFIG'}->{'option'}->{$k}->{'content'} eq $self->{'_PARAMS'}->{$k}->[0]) {
+            $s .= " (default)\n";
+        } else {
+            $s .= "\n";
+        }
+    }
+
+    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";
+    my $k;
+    my $p;
+
+    if (exists($self->{'DESC'})) {
+        $s .= "Description:\n  " . $self->{'DESC'} . "\n";
+    }
+
+    $s .= "Options:\n";
+    foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
+        $p = $self->{'_PARAMS'}->{$k}->[0];
+        $p =~ s/%/%%/g;
+        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;
+}
+
+sub setdebug {
+    my $self = shift;
+
+    $self->{'_DEBUG'} = shift;
+}
+
+1;
diff --git a/xmlrtorrent/HTTPTalker.pm b/xmlrtorrent/HTTPTalker.pm
new file mode 100644 (file)
index 0000000..4eb6a8d
--- /dev/null
@@ -0,0 +1,56 @@
+# Talker using RPC over HTTP
+#
+# (c) 2008 by Ralf Ertzinger <ralf@camperquake.de>
+# licensed under GNU GPL v2
+
+package xmlrtorrent::HTTPTalker;
+use xmlrtorrent::TalkerBase;
+@ISA = qw(xmlrtorrent::TalkerBase);
+
+use Data::Dumper;
+use RPC::XML::Client;
+use strict;
+
+sub new {
+    my $class = shift;
+    my $self = $class->SUPER::new();
+
+    $self->{'NAME'} = 'http';
+    $self->{'DESC'} = 'talker using RPC over HTTP';
+    $self->{'_PARAMS'} = {
+       'XMLURL' => ['', 'URL of SCGI script'],
+       'USERNAME' => ['', 'username for RPC credentials (optional)'],
+       'PASSWORD' => ['', 'password for RPC credentials (optional)'],
+    };
+    $self->{'_LASTXMLURL'} = undef;
+
+    bless($self, $class);
+
+    $self->_prepare_parameters();
+
+    return $self;
+}
+
+sub send_request {
+    my $self = shift;
+    my @params = @_;
+
+    unless (exists($self->{'XMLURL'}) and defined($self->{'XMLURL'})) {
+       return 'http talker: XMLURL not set';
+    }
+
+    if ($self->{'XMLURL'} ne $self->{'_LASTXMLURL'}) {
+       $self->{'_LASTXMLURL'} = $self->{'XMLURL'};
+       $self->{'__RPCClient'} = RPC::XML::Client->new($self->{'XMLURL'});
+    }  
+    
+    if ((exists($self->{'USERNAME'}) and exists($self->{'PASSWORD'})) and
+       ($self->{'USERNAME'} ne '')) {
+       # mitch: let me guess, the realm must be configurable, too!
+       $self->{'__RPCClient'}->credentials('', $self->{'USERNAME'}, $self->{'PASSWORD'});
+    }
+
+    return $self->{'__RPCClient'}->send_request(@params);
+}
+
+1;
diff --git a/xmlrtorrent/TalkerBase.pm b/xmlrtorrent/TalkerBase.pm
new file mode 100644 (file)
index 0000000..d1fa7c8
--- /dev/null
@@ -0,0 +1,20 @@
+# (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
+# licensed under GNU GPL v2
+
+package xmlrtorrent::TalkerBase;
+use xmlrtorrent::Base;
+@ISA = qw(xmlrtorrent::Base);
+
+use strict;
+
+sub new {
+    my $class = shift;
+    my $self = $class->SUPER::new();
+
+    $self = {%{$self},
+            TYPE => 'talker',
+        };
+    return bless($self, $class);
+}
+
+1;