fix quoting in AsyncWgetFileGetter again
[videosite.git] / videosite-xchat2.pl
1 # shim to connect libvideosite to xchat2
2 #
3 # (c) 2007-2013 by Ralf Ertzinger <ralf@camperquake.de>
4 # licensed under GNU GPL v2
5 use strict;
6 use File::Spec;
7 use Module::Load;
8 use Data::Dumper;
9 use Xchat;
10
11 #
12 # List of foreground colors. This list is not complete, it just
13 # contains the colors needed by videosite.
14 #
15 my %foreground_colors = (
16     'magenta'   => "\x0313",
17     '*magenta'  => "\x0313\x02",
18     '*yellow'   => "\x038\x02",
19     '*green'    => "\x039\x02",
20     '*red'      => "\x035\x02",
21     'default'   => "\x0f",
22 );
23
24 #
25 # A hash to hold the config
26 #
27 my %config = ();
28
29 Xchat::register(
30     "videosite",
31     "0.1",
32     "videosite Video URL grabber script (usage: /videosite)");
33
34 #
35 # Return a color code. Called by the core
36 #
37 # Does not handle background colors yet.
38 #
39 sub colorpair {
40     my ($fg, $bg) = @_;
41
42     $fg = exists($foreground_colors{$fg})?$foreground_colors{$fg}:'';
43     $bg = '';
44
45     return $fg . $bg;
46 }
47
48 #
49 # Handle commands (/videosite ...)
50 #
51 sub videosite_hook {
52     my (undef, $msg) = @_;
53     my %event;
54     my $context = Xchat::get_context();
55
56     %event = (
57         message => $msg->[1],
58         io => sub {
59             my $oldcxt = Xchat::get_context();
60             Xchat::set_context($context);
61             Xchat::print(@_);
62             Xchat::set_context($oldcxt);
63         },
64         window => sprintf("%s/%s", Xchat::get_info('server'), Xchat::get_info('channel')),
65     );
66
67     libvideosite::handle_command(\%event);
68
69     return Xchat::EAT_XCHAT;
70 }
71
72 #
73 # Handle a received message.
74 # Create an event structure and hand it off to libvideosite
75 #
76 sub message_hook {
77     my $msg = shift;
78     my %event;
79     my $context = Xchat::get_context();
80
81     %event = (
82         message => $msg->[1],
83         io => sub {
84             my $oldcxt = Xchat::get_context();
85             Xchat::set_context($context);
86             Xchat::print(@_);
87             Xchat::set_context($oldcxt);
88         },
89         window => sprintf("%s/%s", Xchat::get_info('server'), Xchat::get_info('channel')),
90     );
91
92     libvideosite::check_for_link(\%event);
93     return Xchat::EAT_NONE;
94 }
95
96 #
97 # Reset the plugin
98 #
99 sub videosite_reset {
100     unless(libvideosite::register_api({
101         io => sub { Xchat::print(@_) },
102         _config_path => sub { return File::Spec->catfile(Xchat::get_info("xchatdir")) },
103         color => \&colorpair,
104         module_path => sub { return File::Spec->catfile(Xchat::get_info("xchatdir"), 'perl') },
105         quote => sub { return $_ },
106         reload => \&videosite_reset,
107     })) {
108         Xchat::print("", sprintf("videosite API register failed: %s", $libvideosite::error));
109         return 0;
110     }
111
112     unless(libvideosite::init()) {
113         Xchat::print("", sprintf("videosite init failed: %s", $libvideosite::error));
114         return 0;
115     }
116
117     return 1;
118 }
119
120 sub videosite_init {
121     # Find out the script directory, and add it to @INC.
122     # This is necessary to find libvideosite.pm
123
124     push(@INC, File::Spec->catfile(Xchat::get_info("xchatdir"), 'perl'));
125     load 'libvideosite';
126
127     if (videosite_reset()) {
128         foreach ('Channel Message', 'Your Message') {
129             Xchat::hook_print($_, \&message_hook);
130         }
131         Xchat::hook_command('videosite', \&videosite_hook);
132     }
133 }
134
135 videosite_init();