videosite-irssi: remove unneeded code
[videosite.git] / videosite-irssi.pl
1 # autodownload flash videos
2 #
3 # (c) 2007-2008 by Ralf Ertzinger <ralf@camperquake.de>
4 # licensed under GNU GPL v2
5 #
6 # Based on youtube.pl by Christian Garbs <mitch@cgarbs.de>
7 # which in turn is
8 # based on trigger.pl by Wouter Coekaerts <wouter@coekaerts.be>
9
10 use strict;
11 use Irssi 20020324 qw (command_bind command_runsub signal_add_first signal_add_last);
12 use vars qw($VERSION %IRSSI);
13 use File::Spec;
14
15 #
16 # Initialize the config subsystem. Called by the core.
17 #
18 # Due to historic reasons this has to deal with a number of possible config sources:
19 # * irssi internal config
20 # * JSON config, old format
21 # * XML config, old format
22 #
23 # JSON and XML configs are parsed, converted and moved to the irssi internal
24 # format. This happens only once, as the config search stops with the first
25 # format found
26 #
27 sub config_init {
28     my $xmlconffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.xml');
29     my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.json');
30     my $conf;
31
32     # Check for irssi internal config. If not found...
33     if (config_has(['config-version'])) {
34         # Configuration in irssi config file. We're done.
35         return;
36     }
37
38     # Try to find old config files and load them.
39     if (-r $conffile) {
40         eval {
41             local $/;
42             open(CONF, '<', $conffile);
43             $conf = JSON->new->utf8->decode(<CONF>);
44             close(CONF);
45         };
46     } elsif (-r $xmlconffile) {
47         $conf = XML::Simple::XMLin($xmlconffile, ForceArray => ['config', 'option', 'connectorlist'], KeepRoot => 1, KeyAttr => {'connector' => '+name', 'config' => 'module', 'option' => 'key'});
48     }
49
50     #
51     # Configuration conversion:
52     # Replace this structure:
53     #
54     # key => {
55     #   content => value
56     # }
57     #
58     # with this structure
59     #
60     # key => value
61     #
62     Irssi::print("Converting configuration, stage 1");
63
64     # Only the getter/grabbers have this, so just check that part of the config
65     foreach my $g (keys(%{$conf->{videosite}->{config}})) {
66         foreach (keys(%{$conf->{videosite}->{config}->{$g}->{option}})) {
67             if (exists($conf->{videosite}->{config}->{$g}->{option}->{$_}->{content})) {
68                 $conf->{videosite}->{config}->{$g}->{option}->{$_} = $conf->{videosite}->{config}->{$g}->{option}->{$_}->{content};
69             }
70         }
71     }
72
73     #
74     # Walk the configuration hash, creating irssi config entries for
75     # each leaf node.
76     #
77     # Some config values changed, so not the entire config is copied over.
78     # There is a helper function for this in libvideosite that we're using.
79     #
80     Irssi::print("Converting configuration, stage 2");
81
82     # Copy the "basic" settings.
83     foreach (qw(getter mode)) {
84         config_set(['getter'], $conf->{videosite}->{$_});
85     }
86
87     # Copy the per-getter/setter settings
88     foreach my $g (keys(%{$conf->{videosite}->{config}})) {
89         foreach (keys(%{$conf->{videosite}->{config}->{$g}->{option}})) {
90             config_set(['plugin', $g, $_], $conf->{videosite}->{config}->{$g}->{option}->{$_});
91         }
92     }
93
94     # Copy the connectors. The connectors themselves are copied as-is,
95     # the list of active connectors is copied under a different name,
96     # and a list of all existing connectors is created
97     my @connectors;
98
99     foreach my $c (keys(%{$conf->{videosite}->{connectors}})) {
100         push(@connectors, $c);
101         config_set(['connectors', $c, 'name'], $conf->{videosite}->{connectors}->{$c}->{name});
102         if (exists($conf->{videosite}->{connectors}->{$c}->{_immutable})) {
103             config_set(['connectors', $c, '_immutable'], $conf->{videosite}->{connectors}->{$c}->{_immutable});
104         }
105         foreach (qw(http https)) {
106             if (exists($conf->{videosite}->{connectors}->{$c}->{schemas}->{http})) {
107                 config_set(['connectors', $c, 'schemas', $_], $conf->{videosite}->{connectors}->{$c}->{schemas_}->{$_});
108             }
109         }
110     }
111     config_set(['active-connectors'], join(",", @{$conf->{connectorlist}}));
112     config_set(['defined-connectors'], join(",", @connectors));
113     config_set(['config-version'], '2');
114 }
115
116 #
117 # Reading a configuration value. Called by the core
118 #
119 sub config_get {
120     my $path = shift;
121     my $item = join('.', 'videosite', @{$path});
122     my $val;
123
124
125     Irssi::settings_add_str('videosite', $item, "\0");
126     $val = Irssi::settigs_get_str($item);
127
128     return ($val ne "\0")?$val:undef;
129 }
130
131 #
132 # Returns a true value if the config item exists
133 #
134 sub config_has {
135     my $path = shift;
136     my $item = join('.', 'videosite', @{$path});
137
138     Irssi::settings_add_str('videosite', $item, "\0");
139     return Irssi::settings_get_str ne "\0";
140 }
141
142 #
143 # Setting a configuration value. Called by the core
144 #
145 sub config_set {
146     my $path = shift;
147     my $value = shift;
148     my $item = join('.', 'videosite', @{$path});
149
150     Irssi::settings_add_str('videosite', $item, "\0");
151     Irssi::settings_set_str($item, $value);
152 }
153
154 #
155 # Delete a configuration value. Called by the core.
156 #
157 sub config_del {
158     my $path = shift;
159     my $item = join('.', 'videosite', @{$path});
160
161     Irssi::settings_remove($item);
162 }
163
164 #
165 # Return a color code. Called by the core
166 #
167 sub colorpair {
168     my ($fg, $bg) = @_;
169
170     Irssi::print(sprintf("Asked to convert (%s,%s) into irssi color codes", $fg, $bg));o
171
172     return '';
173 }
174
175 #
176 # Handle commands (/videosite ...)
177 #
178 sub videosite_hook {
179     my ($cmdline, $server, $witem) = @_;
180     my %event = (
181         message => $cmdline,
182         ewpf => sub { defined($evitem)?$evitem->print(@_):Irssi::print(@_) },
183     );
184
185     libvideosite::handle_command(\%event);
186 }
187
188 #
189 # Handle a received message
190 # Create an event structure and hand it off to libvideosite
191 #
192 sub message_hook {
193     my ($server, $msg, $nick, $userhost, $channel) = @_;
194     my $evitem = $server->window_item_find($channel);
195     my %event = (
196         message => $msg,
197         ewpf => sub { defined($evitem)?$evitem->print(@_):Irssi::print(@_) },
198     );
199
200     libvideosite::check_for_link(\%event);
201 }
202
203 sub videosite_reset {
204     unless(libvideosite::register_api({
205         io => sub { Irssi::print(@_) },
206         config_init => \&config_init,
207         config_get =>  \&config_get,
208         config_set => \&config_set,
209         config_has => \&config_has,
210         config_save => \&config_save,
211         config_del => \&config_del,
212         color => \&colorpair,
213         module_path => sub { return File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts') },
214         quote => sub { s/%/%%/g; return $_ },
215         _debug => sub { 1 },
216     })) {
217         Irssi::print(sprintf("videosite API register failed: %s", $libvideosite::error));
218         return 0;
219     }
220
221     unless(libvideosite::init()) {
222         Irssi::print(sprintf("videosite init failed: %s", $libvideosite::error));
223         return 0;
224     }
225
226     return 1;
227 }
228
229 sub videosite_init {
230     # Find out the script directory, and add it to @INC.
231     # This is necessary to find libvideosite.pm
232
233     push(@INC, File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts'));
234     load 'libvideosite';
235
236     if (videosite_reset()) {
237         signal_add_last("message public", sub { message_hook(@_) });
238         signal_add_last("message own_public", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
239         signal_add_last("message private", sub { message_hooK($_[0], $_[1], $_[2], $_[3], $_[2]) });
240         signal_add_last("message own_private", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
241         signal_add_last("message irc action", sub { message_hook(@_) });
242         signal_add_last("message irc own_action", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
243
244         Irssi::command_bind('videosite', sub { videosite_hook(@_) });
245     }
246 }
247
248 videosite_init();