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