Merge branch 'devel'
[videosite.git] / videosite-weechat.pl
diff --git a/videosite-weechat.pl b/videosite-weechat.pl
new file mode 100644 (file)
index 0000000..5bddd1f
--- /dev/null
@@ -0,0 +1,154 @@
+# shim to connect libvideosite to weechat
+#
+# (c) 2007-2008 by Ralf Ertzinger <ralf@camperquake.de>
+# licensed under GNU GPL v2
+use strict;
+use File::Spec;
+use Module::Load;
+use Data::Dumper;
+use Carp;
+
+$SIG{ __DIE__ } = sub { Carp::confess( @_ ) };
+
+weechat::register(
+    "videosite",
+    "Ralf Ertzinger (ralf\@skytale.net)",
+    "0.1",
+    "GPL",
+    "videosite Video URL grabber script (usage: /videosite)",
+    "",
+    "");
+
+#
+# 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) = @_;
+
+    return weechat::color($fg . ",", $bg);
+}
+
+#
+# Handle commands (/videosite ...)
+#
+sub videosite_hook {
+    my ($data, $buffer, $args) = @_;
+    my %event = (
+        message => $args,
+        io => sub { weechat::print($buffer, @_) },
+        window => $buffer,
+    );
+
+    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,
+        io => sub { weechat::print($buffer, @_) },
+        window => $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 { 1 },
+        config_del => \&config_del,
+        color => \&colorpair,
+        module_path => sub { return File::Spec->catfile(weechat::info_get("weechat_dir", ""), 'perl') },
+        quote => sub { return $_ },
+        reload => sub { weechat::print("", "Please use \"/script reload ...\" to reload") },
+    })) {
+        weechat::print("", sprintf("videosite API register failed: %s", $libvideosite::error));
+        return 0;
+    }
+
+    unless(libvideosite::init()) {
+        weechat::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(weechat::info_get("weechat_dir", ""), 'perl'));
+    load 'libvideosite';
+
+    if (videosite_reset()) {
+        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", "");
+    }
+}
+
+videosite_init();