From 88a170249981894a67e80509c76d63811628accb Mon Sep 17 00:00:00 2001 From: Ralf Ertzinger Date: Tue, 30 Apr 2013 15:48:57 +0200 Subject: [PATCH] Finish initial videosite-weechat.pl version --- videosite-weechat.pl | 139 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 128 insertions(+), 11 deletions(-) diff --git a/videosite-weechat.pl b/videosite-weechat.pl index 1f26b32..389a1fb 100644 --- a/videosite-weechat.pl +++ b/videosite-weechat.pl @@ -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(); -- 1.8.3.1