- Check for existance of hash key before using
[videosite.git] / videosite.pl
index fabc23a..3ae1e83 100644 (file)
@@ -21,6 +21,12 @@ my $conf;
 my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.xml');
 my $plugindir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts', 'videosite');
 
+my $PARAMS = {
+    'getter' => '',
+    'mode' => 'download'
+};
+
+
 # activate debug here
 my $debug = 0;
 
@@ -66,6 +72,7 @@ sub check_for_link {
     my $message = ($parammessage == -1) ? '' : $signal->[$parammessage];
     my $g;
     my $m;
+    my $p;
 
 
     my $witem;
@@ -82,12 +89,25 @@ sub check_for_link {
 
     # Offer the message to all Grabbers in turn
     foreach $g (@grabbers) {
-        if (defined($m = $g->get($message))) {
+        ($m, $p) = $g->get($message);
+        while (defined($m)) {
             write_debug($witem, 'Metadata: %s', Dumper($m));
-            write_irssi($witem, '%%R>>> %%NSaving %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
-            unless($getter->get($m)) {
-                write_irssi($witem, '%%R>>> FAILED');
+            if ('download' eq ($conf->{'videosite'}->{'mode'})) {
+                write_irssi($witem, '%%R>>> %%NSaving %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
+                unless($getter->get($m)) {
+                    write_irssi($witem, '%%R>>> FAILED');
+                }
+            } elsif ('display' eq ($conf->{'videosite'}->{'mode'})) {
+                write_irssi($witem, '%%M>>> %%NSaw %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
+            } else {
+                write_irssi($witem, '%%R>>> Invalid operation mode');
             }
+
+            # Remove the matched part from the message and try again (there may be
+            # more!)
+            $message =~ s/$p//;
+
+            ($m, $p) = $g->get($message);
         }
     }
 }
@@ -200,6 +220,7 @@ Supported commands:
  enable [modulename]: enable the usage of this module (grabbers only)
  disable [modulename]: disable the usage of this module (grabbers only)
  reload: reload all modules (this is somewhat experimental)
+ mode [modename]: display or set the operation mode (download/display)
  debug: enable debugging messages
  nodebug: disable debugging messages
 EOT
@@ -224,6 +245,21 @@ sub cmd_getter {
     }
 }
 
+sub cmd_mode {
+    my $mode = shift;
+
+    if (defined($mode)) {
+        $mode = lc($mode);
+        if (('download' eq $mode) or ('display' eq $mode)) {
+            $conf->{'videosite'}->{'mode'} = $mode;
+        } else {
+            write_irssi(undef, 'Invalid mode: %s', $mode);
+        }
+    } else {
+        write_irssi(undef, 'Current mode: %s', $conf->{'videosite'}->{'mode'});
+    }
+}
+
 
 # save on unload
 sub sig_command_script_unload {
@@ -266,6 +302,7 @@ sub ploader {
         write_debug(undef, "found $g->{'TYPE'} $g->{'NAME'}");
         if ($type eq $g->{'TYPE'}) {
             push(@g, $g);
+            $g->setio(sub {Irssi::print(shift)});
         } else {
             write_irssi(undef, '%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
             delete($INC{$p});
@@ -300,7 +337,12 @@ sub init_videosite {
     unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
         # No config, start with an empty one
         write_debug(undef, 'No config found, using defaults');
-        $conf = { 'videosite' => { 'getter' => '' }};
+        $conf = { 'videosite' => { }};
+    }
+    foreach (keys(%{$PARAMS})) {
+        unless (exists($conf->{'videosite'}->{$_})) {
+            $conf->{'videosite'}->{$_} = $PARAMS->{$_};
+        }
     }
 
     _load_modules($plugindir);
@@ -363,6 +405,9 @@ sub cmdhandler {
         cmd_disable(@params);
     } elsif ($params[0] eq 'reload') {
         init_videosite(0);
+    } elsif ($params[0] eq 'mode') {
+        shift(@params);
+        cmd_mode(@params);
     } elsif ($params[0] eq 'debug') {
         $debug = 1;
         foreach (@grabbers, @getters) {