fix quoting in AsyncWgetFileGetter again
[videosite.git] / videosite-weechat.pl
1 # shim to connect libvideosite to weechat
2 #
3 # (c) 2007-2008 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 Carp;
10
11 $SIG{ __DIE__ } = sub { Carp::confess( @_ ) };
12
13 weechat::register(
14     "videosite",
15     "Ralf Ertzinger (ralf\@skytale.net)",
16     "0.1",
17     "GPL",
18     "videosite Video URL grabber script (usage: /videosite)",
19     "",
20     "");
21
22 #
23 # Reading a configuration value. Called by the core
24 #
25 sub config_get {
26     my $path = shift;
27     my $item = join('.', @{$path});
28
29     if (weechat::config_is_set_plugin($item)) {
30         return weechat::config_get_plugin($item);
31     } else {
32         return undef;
33     }
34 }
35
36 #
37 # Returns a true value if the config item exists
38 #
39 sub config_has {
40     my $path = shift;
41     my $item = join('.', @{$path});
42
43     return weechat::config_is_set_plugin($item);
44 }
45
46 #
47 # Setting a configuration value. Called by the core
48 #
49 sub config_set {
50     my $path = shift;
51     my $value = shift;
52     my $item = join('.', @{$path});
53
54     weechat::config_set_plugin($item, $value);
55 }
56
57 #
58 # Delete a configuration value. Called by the core.
59 #
60 sub config_del {
61     my $path = shift;
62     my $item = join('.', @{$path});
63
64     weechat::config_unset_plugin($item);
65 }
66
67 #
68 # Return a color code. Called by the core
69 #
70 sub colorpair {
71     my ($fg, $bg) = @_;
72
73     return weechat::color($fg . ",", $bg);
74 }
75
76 #
77 # Handle commands (/videosite ...)
78 #
79 sub videosite_hook {
80     my ($data, $buffer, $args) = @_;
81     my %event = (
82         message => $args,
83         io => sub { weechat::print($buffer, @_) },
84         window => $buffer,
85     );
86
87     libvideosite::handle_command(\%event);
88
89     return weechat::WEECHAT_RC_OK;
90 }
91
92 #
93 # Handle a received message.
94 # Create an event structure and hand it off to libvideosite
95 #
96 sub message_hook {
97     my ($data, $buffer, $date, $tags, $displayed, $highlight, $prefix, $message ) = @_;
98     my %event = (
99         message => $message,
100         io => sub { weechat::print($buffer, @_) },
101         window => $buffer
102     );
103
104     libvideosite::check_for_link(\%event);
105
106     return weechat::WEECHAT_RC_OK;
107 }
108
109 #
110 # Reset the plugin
111 #
112 sub videosite_reset {
113     unless(libvideosite::register_api({
114         io => sub { weechat::print("", @_) },
115         config_init => sub {},
116         config_get =>  \&config_get,
117         config_set => \&config_set,
118         config_has => \&config_has,
119         config_save => sub { 1 },
120         config_del => \&config_del,
121         color => \&colorpair,
122         module_path => sub { return File::Spec->catfile(weechat::info_get("weechat_dir", ""), 'perl') },
123         quote => sub { return $_ },
124         reload => sub { weechat::print("", "Please use \"/script reload ...\" to reload") },
125     })) {
126         weechat::print("", sprintf("videosite API register failed: %s", $libvideosite::error));
127         return 0;
128     }
129
130     unless(libvideosite::init()) {
131         weechat::print("", sprintf("videosite init failed: %s", $libvideosite::error));
132         return 0;
133     }
134
135     return 1;
136 }
137
138 sub videosite_init {
139     # Find out the script directory, and add it to @INC.
140     # This is necessary to find libvideosite.pm
141
142     push(@INC, File::Spec->catfile(weechat::info_get("weechat_dir", ""), 'perl'));
143     load 'libvideosite';
144
145     if (videosite_reset()) {
146         weechat::hook_print("", "notify_message", "://", 1, "message_hook", "");
147         weechat::hook_print("", "notify_private", "://", 1, "message_hook", "");
148         weechat::hook_print("", "notify_highlight", "://", 1, "message_hook", "");
149         weechat::hook_print("", "notify_none", "://", 1, "message_hook", "");
150         weechat::hook_command( "videosite", "videosite control functions", "", "", "", "videosite_hook", "");
151     }
152 }
153
154 videosite_init();