videosite-test: Allow selective tests by using grabber names on the command line
[videosite.git] / videosite-irssi.pl
index c121257..f1c9b1d 100644 (file)
@@ -1,16 +1,29 @@
-# autodownload flash videos
+# shim to connect libvideosite to irssi
 #
 # (c) 2007-2008 by Ralf Ertzinger <ralf@camperquake.de>
 # licensed under GNU GPL v2
-#
-# Based on youtube.pl by Christian Garbs <mitch@cgarbs.de>
-# which in turn is
-# based on trigger.pl by Wouter Coekaerts <wouter@coekaerts.be>
-
 use strict;
 use Irssi 20020324 qw (command_bind command_runsub signal_add_first signal_add_last);
 use vars qw($VERSION %IRSSI);
 use File::Spec;
+use Module::Load;
+use XML::Simple;
+use JSON -support_by_pp;
+
+#
+# List of foreground colors. This list is not complete, it just
+# contains the colors needed by videosite.
+#
+# The % are doubled because these are used in sprintf.
+#
+my %foreground_colors = (
+    'magenta'   => '%%m',
+    '*magenta'  => '%%M',
+    '*yellow'   => '%%Y',
+    '*green'    => '%%G',
+    '*red'      => '%%R',
+    'default'   => '%%n',
+);
 
 #
 # Initialize the config subsystem. Called by the core.
@@ -37,6 +50,7 @@ sub config_init {
 
     # Try to find old config files and load them.
     if (-r $conffile) {
+        Irssi::print("Converting configuration from videosite.json. This will happen only once.");
         eval {
             local $/;
             open(CONF, '<', $conffile);
@@ -44,7 +58,11 @@ sub config_init {
             close(CONF);
         };
     } elsif (-r $xmlconffile) {
+        Irssi::print("Converting configuration from videosite.xml. This will happen only once.");
         $conf = XML::Simple::XMLin($xmlconffile, ForceArray => ['config', 'option', 'connectorlist'], KeepRoot => 1, KeyAttr => {'connector' => '+name', 'config' => 'module', 'option' => 'key'});
+    } else {
+        # No old config files around. Just exit.
+        return;
     }
 
     #
@@ -81,7 +99,7 @@ sub config_init {
 
     # Copy the "basic" settings.
     foreach (qw(getter mode)) {
-        config_set(['getter'], $conf->{videosite}->{$_});
+        config_set([$_], $conf->{videosite}->{$_});
     }
 
     # Copy the per-getter/setter settings
@@ -108,7 +126,7 @@ sub config_init {
             }
         }
     }
-    config_set(['active-connectors'], join(",", @{$conf->{connectorlist}}));
+    config_set(['active-connectors'], join(",", @{$conf->{videosite}->{connectorlist}}));
     config_set(['defined-connectors'], join(",", @connectors));
     config_set(['config-version'], '2');
 }
@@ -122,10 +140,10 @@ sub config_get {
     my $val;
 
 
-    Irssi::settings_add_str('videosite', $item, "\0");
-    $val = Irssi::settigs_get_str($item);
+    Irssi::settings_add_str('videosite', $item, "\1");
+    $val = Irssi::settings_get_str($item);
 
-    return ($val ne "\0")?$val:undef;
+    return ($val ne "\1")?$val:undef;
 }
 
 #
@@ -135,8 +153,8 @@ sub config_has {
     my $path = shift;
     my $item = join('.', 'videosite', @{$path});
 
-    Irssi::settings_add_str('videosite', $item, "\0");
-    return Irssi::settings_get_str ne "\0";
+    Irssi::settings_add_str('videosite', $item, "\1");
+    return Irssi::settings_get_str($item) ne "\1";
 }
 
 #
@@ -147,7 +165,7 @@ sub config_set {
     my $value = shift;
     my $item = join('.', 'videosite', @{$path});
 
-    Irssi::settings_add_str('videosite', $item, "\0");
+    Irssi::settings_add_str('videosite', $item, "\1");
     Irssi::settings_set_str($item, $value);
 }
 
@@ -164,12 +182,15 @@ sub config_del {
 #
 # Return a color code. Called by the core
 #
+# Does not handle background colors yet.
+#
 sub colorpair {
     my ($fg, $bg) = @_;
 
-    Irssi::print(sprintf("Asked to convert (%s,%s) into irssi color codes", $fg, $bg));o
+    $fg = exists($foreground_colors{$fg})?$foreground_colors{$fg}:'';
+    $bg = '';
 
-    return '';
+    return $fg . $bg;
 }
 
 #
@@ -179,7 +200,7 @@ sub videosite_hook {
     my ($cmdline, $server, $witem) = @_;
     my %event = (
         message => $cmdline,
-        ewpf => sub { defined($evitem)?$evitem->print(@_):Irssi::print(@_) },
+        ewpf => sub { defined($witem)?$witem->print($_[0]):Irssi::print($_[0]) },
     );
 
     libvideosite::handle_command(\%event);
@@ -194,7 +215,7 @@ sub message_hook {
     my $evitem = $server->window_item_find($channel);
     my %event = (
         message => $msg,
-        ewpf => sub { defined($evitem)?$evitem->print(@_):Irssi::print(@_) },
+        ewpf => sub { defined($evitem)?$evitem->print($_[0]):Irssi::print($_[0]) },
     );
 
     libvideosite::check_for_link(\%event);
@@ -202,7 +223,7 @@ sub message_hook {
 
 sub videosite_reset {
     unless(libvideosite::register_api({
-        io => sub { Irssi::print(@_) },
+        io => sub { Irssi::print($_[0]) },
         config_init => \&config_init,
         config_get =>  \&config_get,
         config_set => \&config_set,