Add shim for xchat2
authorRalf Ertzinger <ralf@skytale.net>
Wed, 8 May 2013 18:41:08 +0000 (20:41 +0200)
committerRalf Ertzinger <ralf@skytale.net>
Wed, 8 May 2013 18:41:08 +0000 (20:41 +0200)
README-xchat [new file with mode: 0644]
videosite-xchat2.pl [new file with mode: 0644]

diff --git a/README-xchat b/README-xchat
new file mode 100644 (file)
index 0000000..0e928eb
--- /dev/null
@@ -0,0 +1,14 @@
+Installation instructions
+=========================
+
+Copy/symlink the following files/directories into ~/.xchat2/perl (create
+the directory if it does not exist)
+
+* videosite-xchat2.pl
+* libvideosite.pm
+* videosite/
+
+In xchat, use XChat->Load Plugin or Script to load videosite-xchat2.pl from
+~/.xchat/perl.
+
+The configuration will be saved as a file in ~/.xchat2/videosite.json.
diff --git a/videosite-xchat2.pl b/videosite-xchat2.pl
new file mode 100644 (file)
index 0000000..d71106d
--- /dev/null
@@ -0,0 +1,135 @@
+# shim to connect libvideosite to xchat2
+#
+# (c) 2007-2013 by Ralf Ertzinger <ralf@camperquake.de>
+# licensed under GNU GPL v2
+use strict;
+use File::Spec;
+use Module::Load;
+use Data::Dumper;
+use Xchat;
+
+#
+# List of foreground colors. This list is not complete, it just
+# contains the colors needed by videosite.
+#
+my %foreground_colors = (
+    'magenta'   => "\x0313",
+    '*magenta'  => "\x0313\x02",
+    '*yellow'   => "\x038\x02",
+    '*green'    => "\x039\x02",
+    '*red'      => "\x035\x02",
+    'default'   => "\x0f",
+);
+
+#
+# A hash to hold the config
+#
+my %config = ();
+
+Xchat::register(
+    "videosite",
+    "0.1",
+    "videosite Video URL grabber script (usage: /videosite)");
+
+#
+# Return a color code. Called by the core
+#
+# Does not handle background colors yet.
+#
+sub colorpair {
+    my ($fg, $bg) = @_;
+
+    $fg = exists($foreground_colors{$fg})?$foreground_colors{$fg}:'';
+    $bg = '';
+
+    return $fg . $bg;
+}
+
+#
+# Handle commands (/videosite ...)
+#
+sub videosite_hook {
+    my (undef, $msg) = @_;
+    my %event;
+    my $context = Xchat::get_context();
+
+    %event = (
+        message => $msg->[1],
+        ewpf => sub {
+            my $oldcxt = Xchat::get_context();
+            Xchat::set_context($context);
+            Xchat::print(@_);
+            Xchat::set_context($oldcxt);
+        },
+        window => sprintf("%s/%s", Xchat::get_info('server'), Xchat::get_info('channel')),
+    );
+
+    libvideosite::handle_command(\%event);
+
+    return Xchat::EAT_XCHAT;
+}
+
+#
+# Handle a received message.
+# Create an event structure and hand it off to libvideosite
+#
+sub message_hook {
+    my $msg = shift;
+    my %event;
+    my $context = Xchat::get_context();
+
+    %event = (
+        message => $msg->[1],
+        ewpf => sub {
+            my $oldcxt = Xchat::get_context();
+            Xchat::set_context($context);
+            Xchat::print(@_);
+            Xchat::set_context($oldcxt);
+        },
+        window => sprintf("%s/%s", Xchat::get_info('server'), Xchat::get_info('channel')),
+    );
+
+    libvideosite::check_for_link(\%event);
+    return Xchat::EAT_NONE;
+}
+
+#
+# Reset the plugin
+#
+sub videosite_reset {
+    unless(libvideosite::register_api({
+        io => sub { Xchat::print(@_) },
+        _config_path => sub { return File::Spec->catfile(Xchat::get_info("xchatdir")) },
+        color => \&colorpair,
+        module_path => sub { return File::Spec->catfile(Xchat::get_info("xchatdir"), 'perl') },
+        quote => sub { return $_ },
+        reload => \&videosite_reset,
+    })) {
+        Xchat::print("", sprintf("videosite API register failed: %s", $libvideosite::error));
+        return 0;
+    }
+
+    unless(libvideosite::init()) {
+        Xchat::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(Xchat::get_info("xchatdir"), 'perl'));
+    load 'libvideosite';
+
+    if (videosite_reset()) {
+        foreach ('Channel Message', 'Your Message') {
+            Xchat::hook_print($_, \&message_hook);
+        }
+        Xchat::hook_command('videosite', \&videosite_hook);
+    }
+}
+
+videosite_init();