fix quoting in AsyncWgetFileGetter again
[videosite.git] / videosite-irssi.pl
index c121257..aa838c0 100644 (file)
@@ -1,16 +1,40 @@
-# 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;
+use Carp;
+
+$SIG{ __DIE__ } = sub { Carp::confess( @_ ) };
+
+#
+# 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',
+);
+
+#
+# This is a canary value used in the config system as the default
+# value. As irssi does not have a way to test if a setting exists
+# this is used instead. A config value is never expected to be set
+# to this value and be valid.
+#
+my $config_canary = "\1";
 
 #
 # Initialize the config subsystem. Called by the core.
@@ -37,6 +61,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 +69,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 +110,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 +137,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 +151,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, $config_canary);
+    $val = Irssi::settings_get_str($item);
 
-    return ($val ne "\0")?$val:undef;
+    return ($val ne $config_canary)?$val:undef;
 }
 
 #
@@ -135,8 +164,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, $config_canary);
+    return Irssi::settings_get_str($item) ne $config_canary;
 }
 
 #
@@ -147,29 +176,35 @@ sub config_set {
     my $value = shift;
     my $item = join('.', 'videosite', @{$path});
 
-    Irssi::settings_add_str('videosite', $item, "\0");
+    Irssi::settings_add_str('videosite', $item, $config_canary);
     Irssi::settings_set_str($item, $value);
 }
 
 #
 # Delete a configuration value. Called by the core.
 #
+# Now, according to the configuration Irssi::settings_remove() removes a
+# config settings. This does not work in any irssi version available to me.
+# So just set the key to the canary value.
+#
 sub config_del {
     my $path = shift;
-    my $item = join('.', 'videosite', @{$path});
 
-    Irssi::settings_remove($item);
+    config_set($path, $config_canary);
 }
 
 #
 # 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 +214,8 @@ sub videosite_hook {
     my ($cmdline, $server, $witem) = @_;
     my %event = (
         message => $cmdline,
-        ewpf => sub { defined($evitem)?$evitem->print(@_):Irssi::print(@_) },
+        io => sub { defined($witem)?$witem->print($_[0], MSGLEVEL_CLIENTCRAP):Irssi::print($_[0]) },
+        window => defined($witem)?$witem->{server}->{real_address} . "/" . $witem->{name}:"",
     );
 
     libvideosite::handle_command(\%event);
@@ -191,28 +227,47 @@ sub videosite_hook {
 #
 sub message_hook {
     my ($server, $msg, $nick, $userhost, $channel) = @_;
-    my $evitem = $server->window_item_find($channel);
+    my $witem = $server->window_item_find($channel);
     my %event = (
         message => $msg,
-        ewpf => sub { defined($evitem)?$evitem->print(@_):Irssi::print(@_) },
+        io => sub { defined($witem)?$witem->print($_[0], MSGLEVEL_CLIENTCRAP):Irssi::print($_[0]) },
+        window => defined($witem)?$witem->{server}->{real_address} . "/" . $witem->{name}:"",
     );
 
     libvideosite::check_for_link(\%event);
 }
 
 sub videosite_reset {
+    my $libpath;
+
+    # Find out the script directory, and add it to @INC.
+    # This is necessary to find libvideosite.pm
+    $libpath = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts');
+
+    # If the library is already loaded unload it
+    foreach (keys(%INC)) {
+        if ($INC{$_} eq File::Spec->catfile($libpath, 'libvideosite.pm')) {
+            delete($INC{$_});
+        }
+    }
+
+    push(@INC, $libpath);
+    load 'libvideosite';
+
     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,
         config_has => \&config_has,
-        config_save => \&config_save,
+        config_save => sub { 1 },
         config_del => \&config_del,
         color => \&colorpair,
         module_path => sub { return File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts') },
         quote => sub { s/%/%%/g; return $_ },
-        _debug => sub { 1 },
+        reload => \&videosite_reset,
+       # irssi needs this to prevent fork()ed child processes becoming zombies:
+       wait_for_child => sub { Irssi::pidwait_add($_[0]) },
     })) {
         Irssi::print(sprintf("videosite API register failed: %s", $libvideosite::error));
         return 0;
@@ -227,16 +282,11 @@ sub videosite_reset {
 }
 
 sub videosite_init {
-    # Find out the script directory, and add it to @INC.
-    # This is necessary to find libvideosite.pm
-
-    push(@INC, File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts'));
-    load 'libvideosite';
 
     if (videosite_reset()) {
         signal_add_last("message public", sub { message_hook(@_) });
         signal_add_last("message own_public", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
-        signal_add_last("message private", sub { message_hooK($_[0], $_[1], $_[2], $_[3], $_[2]) });
+        signal_add_last("message private", sub { message_hook($_[0], $_[1], $_[2], $_[3], $_[2]) });
         signal_add_last("message own_private", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
         signal_add_last("message irc action", sub { message_hook(@_) });
         signal_add_last("message irc own_action", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });