Finish initial videosite-weechat.pl version
authorRalf Ertzinger <ralf@skytale.net>
Tue, 30 Apr 2013 13:48:57 +0000 (15:48 +0200)
committerRalf Ertzinger <ralf@skytale.net>
Tue, 30 Apr 2013 13:48:57 +0000 (15:48 +0200)
videosite-weechat.pl

index 1f26b32..389a1fb 100644 (file)
@@ -1,6 +1,8 @@
 #!/usr/bin/perl -w
 
 use strict;
+use File::Spec;
+use Module::Load;
 use Data::Dumper;
 
 weechat::register(
@@ -11,21 +13,136 @@ weechat::register(
     "videosite Video URL grabber script (usage: /videosite)",
     "",
     "");
-weechat::hook_command(
-    "videosite",
-    "videosite control functions",
-    "",
-    "",
-    "",
-    "videosite_hook",
-    "");
 
+#
+# Reading a configuration value. Called by the core
+#
+sub config_get {
+    my $path = shift;
+    my $item = join('.', @{$path});
+
+    if (weechat::config_is_set_plugin($item)) {
+        return weechat::config_get_plugin($item);
+    } else {
+        return undef;
+    }
+}
+
+#
+# Returns a true value if the config item exists
+#
+sub config_has {
+    my $path = shift;
+    my $item = join('.', @{$path});
+
+    return weechat::config_is_set_plugin($item);
+}
+
+#
+# Setting a configuration value. Called by the core
+#
+sub config_set {
+    my $path = shift;
+    my $value = shift;
+    my $item = join('.', @{$path});
+
+    weechat::config_set_plugin($item, $value);
+}
+
+#
+# Delete a configuration value. Called by the core.
+#
+sub config_del {
+    my $path = shift;
+    my $item = join('.', @{$path});
+
+    weechat::config_unset_plugin($item);
+}
+
+#
+# Return a color code. Called by the core
+#
+sub colorpair {
+    my ($fg, $bg) = @_;
+
+    $fg //= 'default';
+    $bg //= 'default';
+
+    return weechat::color($fg . ",", $bg);
+}
+
+#
+# Handle commands (/videosite ...)
+#
 sub videosite_hook {
     my ($data, $buffer, $args) = @_;
+    my %event = (
+        message => $args,
+        ewpf => sub { weechat::print($buffer, @_) },
+    );
 
-    weechat::print("", sprintf("Data: %s", Dumper($data)));
-    weechat::print("", sprintf("Buffer: %s", Dumper($buffer)));
-    weechat::print("", sprintf("Args: %s", Dumper($args)));
+    libvideosite::handle_command(\%event);
 
     return weechat::WEECHAT_RC_OK;
 }
+
+#
+# Handle a received message.
+# Create an event structure and hand it off to libvideosite
+#
+sub message_hook {
+    my ($data, $buffer, $date, $tags, $displayed, $highlight, $prefix, $message ) = @_;
+    my %event = (
+        message => $message,
+        ewpf => sub { weechat::print($buffer, @_) },
+    );
+
+    libvideosite::check_for_link(\%event);
+
+    return weechat::WEECHAT_RC_OK;
+}
+
+#
+# Reset the plugin
+#
+sub videosite_reset {
+    unless(libvideosite::register_api({
+        io => sub { weechat::print("", @_) },
+        config_init => sub {},
+        config_get =>  \&config_get,
+        config_set => \&config_set,
+        config_has => \&config_has,
+        config_save => sub {},
+        config_del => \&config_del,
+        color => \&colorpair,
+        module_path => sub { return File::Spec->catfile(weechat::info_get("weechat_dir", ""), 'perl') },
+        quote => sub { return $_ },
+        _debug => sub { 1 },
+    })) {
+        weechat::print("", sprintf("videosite API register failed: %s", $libvideosite::error));
+        return;
+    }
+
+    unless(libvideosite::init()) {
+        weechat::print("", sprintf("videosite init failed: %s", $libvideosite::error));
+        return;
+    }
+
+    weechat::hook_print("", "notify_message", "://", 1, "message_hook", "");
+    weechat::hook_print("", "notify_private", "://", 1, "message_hook", "");
+    weechat::hook_print("", "notify_highlight", "://", 1, "message_hook", "");
+    weechat::hook_print("", "notify_none", "://", 1, "message_hook", "");
+    weechat::hook_command( "videosite", "videosite control functions", "", "", "", "videosite_hook", "");
+}
+
+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(weechat::info_get("weechat_dir", ""), 'perl'));
+    load 'libvideosite';
+
+    videosite_reset();
+}
+
+videosite_init();