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