# 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();
