Start converting videosite-irssi.pl to libvideosite
authorRalf Ertzinger <ralf@skytale.net>
Tue, 30 Apr 2013 13:48:21 +0000 (15:48 +0200)
committerRalf Ertzinger <ralf@skytale.net>
Tue, 30 Apr 2013 13:48:21 +0000 (15:48 +0200)
videosite-irssi.pl

index 737ba82..f20e3a1 100644 (file)
@@ -7,25 +7,12 @@
 # which in turn is
 # based on trigger.pl by Wouter Coekaerts <wouter@coekaerts.be>
 
-
-BEGIN {
-    # Get rid of a (possibly old) version of BettIrssi
-    # This is a hack to prevent having to reload irssi just
-    # because BettIrssi.pm changed
-
-    delete($INC{'BettIrssi.pm'});
-}
-
 use strict;
 use Irssi 20020324 qw (command_bind command_runsub signal_add_first signal_add_last);
 use vars qw($VERSION %IRSSI);
-use XML::Simple;
 use Data::Dumper;
 use File::Spec;
 use File::Temp qw(tempfile);
-use BettIrssi 101 qw(_bcb _bcs);
-use LWP::UserAgent;
-use JSON -support_by_pp;
 
 my @grabbers;
 my @getters;
@@ -37,17 +24,6 @@ my $scriptdir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts');
 my $plugindir = File::Spec->catfile($scriptdir, 'videosite');
 my @outputstack = (undef);
 
-my $PARAMS = {
-    'getter' => '',
-    'mode' => 'download',
-    'connectorlist' => ['direct'],
-    'connectors' => {},
-};
-
-
-# activate debug here
-my $debug = 0;
-
 # "message public", SERVER_REC, char *msg, char *nick, char *address, char *target
 signal_add_last(_bcs("message public" => sub {check_for_link(@_)}));
 # "message own_public", SERVER_REC, char *msg, char *target
@@ -780,18 +756,237 @@ sub sig_complete {
     Irssi::signal_stop();
 }
 
-sub cmdhandler {
-    my $event = shift;
-    my ($cmd, @params) = split(/\s+/, $event->message());
+# =================================
+# Reworked code below this line
+# =================================
 
-    push_output($event->ewpf);
+#
+# Initialize the config subsystem. Called by the core.
+#
+# Due to historic reasons this has to deal with a number of possible config sources:
+# * irssi internal config
+# * JSON config, old format
+# * XML config, old format
+#
+# JSON and XML configs are parsed, converted and moved to the irssi internal
+# format. This happens only once, as the config search stops with the first
+# format found
+#
+sub config_init {
+    my $xmlconffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.xml');
+    my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.json');
+    my $conf;
+
+    # Check for irssi internal config. If not found...
 
-    if (exists($videosite_commands->{$cmd})) {
-        $videosite_commands->{$cmd}->(@params);
+    if (-r $conffile) {
+        write_debug("Attempting JSON config load from %s", $conffile);
+        eval {
+            local $/;
+            open(CONF, '<', $conffile);
+            $conf = JSON->new->utf8->decode(<CONF>);
+            close(CONF);
+        };
+    } elsif (-r $xmlconffile) {
+        write_debug("Attempting XML config load from %s", $xmlconffile);
+        $conf = XML::Simple::XMLin($xmlconffile, ForceArray => ['config', 'option', 'connectorlist'], KeepRoot => 1, KeyAttr => {'connector' => '+name', 'config' => 'module', 'option' => 'key'});
     }
 
-    pop_output();
+    #
+    # Configuration conversion:
+    # Replace this structure:
+    #
+    # key => {
+    #   content => value
+    # }
+    #
+    # by this structure
+    #
+    # key => value
+    #
+    Irssi::print("Converting configuration, stage 1");
+
+    # Only the getter/grabbers have this, so just check that part of the config
+    foreach my $g (keys(%{$conf->{videosite}->{config}})) {
+        foreach (keys(%{$conf->{videosite}->{config}->{$g}->{option}})) {
+            if (exists($conf->{videosite}->{config}->{$g}->{option}->{$_}->{content})) {
+                $conf->{videosite}->{config}->{$g}->{option}->{$_} = $conf->{videosite}->{config}->{$g}->{option}->{$_}->{content};
+            }
+        }
+    }
+
+    #
+    # Walk the configuration hash, creating irssi config entries for
+    # each leaf node.
+    #
+    # Some config values changed, so not the entire config is copied over.
+    # There is a helper function for this in libvideosite that we're using.
+    #
+    Irssi::print("Converting configuration, stage 2");
+
+    # Copy the "basic" settings.
+    foreach (qw(getter mode)) {
+        config_set(['getter'], $conf->{videosite}->{$_});
+    }
+
+    # Copy the per-getter/setter settings
+    foreach my $g (keys(%{$conf->{videosite}->{config}})) {
+        foreach (keys(%{$conf->{videosite}->{config}->{$g}->{option}})) {
+            config_set(['plugin', $g, $_], $conf->{videosite}->{config}->{$g}->{option}->{$_});
+        }
+    }
+
+    # Copy the connectors. The connectors themselves are copied as-is,
+    # the list of active connectors is copied under a different name,
+    # and a list of all existing connectors is created
+    my @connectors;
+
+    foreach my $c (keys(%{$conf->{videosite}->{connectors}})) {
+        push(@connectors, $c);
+        config_set(['connectors', $c, 'name'], $conf->{videosite}->{connectors}->{$c}->{name});
+        if (exists($conf->{videosite}->{connectors}->{$c}->{_immutable})) {
+            config_set(['connectors', $c, '_immutable'], $conf->{videosite}->{connectors}->{$c}->{_immutable});
+        }
+        foreach (qw(http https)) {
+            if (exists($conf->{videosite}->{connectors}->{$c}->{schemas}->{http})) {
+                config_set(['connectors', $c, 'schemas', $_], $conf->{videosite}->{connectors}->{$c}->{schemas_}->{$_});
+            }
+        }
+    }
+    config_set(['active-connectors'], join(",", @{$conf->{connectorlist}}));
+    config_set(['defined-connectors'], join(",", @connectors));
+}
+
+#
+# Reading a configuration value. Called by the core
+#
+sub config_get {
+    my $path = shift;
+    my $item = join('.', @{$path});
+    my $val;
+
+
+    Irssi::settings_add_str('videosite', $item, "\0");
+    $val = Irssi::settigs_get_str($item);
+
+    return ($val ne "\0")?$val:undef;
+}
+
+#
+# Returns a true value if the config item exists
+#
+sub config_has {
+    my $path = shift;
+    my $item = join('.', @{$path});
+
+    Irssi::settings_add_str('videosite', $item, "\0");
+    return Irssi::settings_get_str ne "\0";
+}
+
+#
+# Setting a configuration value. Called by the core
+#
+sub config_set {
+    my $path = shift;
+    my $value = shift;
+    my $item = join('.', @{$path});
+
+    Irssi::settings_add_str('videosite', $item, "\0");
+    Irssi::settings_set_str($item, $value);
+}
+
+#
+# Delete a configuration value. Called by the core.
+#
+sub config_del {
+    my $path = shift;
+    my $item = join('.', @{$path});
+
+    Irssi::settings_remove($item);
+}
+
+#
+# Return a color code. Called by the core
+#
+sub colorpair {
+    my ($fg, $bg) = @_;
+
+    Irssi::print(sprintf("Asked to convert (%s,%s) into irssi color codes", $fg, $bg));o
+
+    return '';
+}
+
+#
+# Handle commands (/videosite ...)
+#
+sub videosite_hook {
+    my ($cmdline, $server, $witem) = @_;
+    my %event = (
+        message => $cmdline,
+        ewpf => sub { defined($evitem)?$evitem->print(@_):Irssi::print(@_) },
+    );
+
+    libvideosite::handle_command(\%event);
+}
+
+#
+# Handle a received message
+# Create an event structure and hand it off to libvideosite
+#
+sub message_hook {
+    my ($server, $msg, $nick, $userhost, $channel) = @_;
+    my $evitem = $server->window_item_find($channel);
+    my %event = (
+        message => $msg,
+        ewpf => sub { defined($evitem)?$evitem->print(@_):Irssi::print(@_) },
+    );
+
+    libvideosite::check_for_link(\%event);
+}
+
+sub videosite_reset {
+    unless(libvideosite::register_api({
+        io => sub { Irssi::print(@_) },
+        config_init => \&config_init,
+        config_get =>  \&config_get,
+        config_set => \&config_set,
+        config_has => \&config_has,
+        config_save => \&config_save,
+        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 },
+    })) {
+        Irssi::print(sprintf("videosite API register failed: %s", $libvideosite::error));
+        return 0;
+    }
+
+    unless(libvideosite::init()) {
+        Irssi::print(sprintf("videosite init failed: %s", $libvideosite::error));
+        return 0;
+    }
+
+    return 1;
+}
+
+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';
+
+    unless (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 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]) });
+
+        Irssi::command_bind('videosite', sub { videosite_hook(@_) });
+    }
 }
 
-unshift(@INC, $scriptdir);
-init_videosite(1);
+videosite_init();