1 # autodownload flash videos
3 # (c) 2007-2008 by Ralf Ertzinger <ralf@camperquake.de>
4 # licensed under GNU GPL v2
6 # Based on youtube.pl by Christian Garbs <mitch@cgarbs.de>
8 # based on trigger.pl by Wouter Coekaerts <wouter@coekaerts.be>
11 use Irssi 20020324 qw (command_bind command_runsub signal_add_first signal_add_last);
12 use vars qw($VERSION %IRSSI);
16 # Initialize the config subsystem. Called by the core.
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
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
28 my $xmlconffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.xml');
29 my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.json');
32 # Check for irssi internal config. If not found...
33 if (config_has(['config-version'])) {
34 # Configuration in irssi config file. We're done.
38 # Try to find old config files and load them.
42 open(CONF, '<', $conffile);
43 $conf = JSON->new->utf8->decode(<CONF>);
46 } elsif (-r $xmlconffile) {
47 $conf = XML::Simple::XMLin($xmlconffile, ForceArray => ['config', 'option', 'connectorlist'], KeepRoot => 1, KeyAttr => {'connector' => '+name', 'config' => 'module', 'option' => 'key'});
51 # Configuration conversion:
52 # Replace this structure:
62 Irssi::print("Converting configuration, stage 1");
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};
74 # Walk the configuration hash, creating irssi config entries for
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.
80 Irssi::print("Converting configuration, stage 2");
82 # Copy the "basic" settings.
83 foreach (qw(getter mode)) {
84 config_set(['getter'], $conf->{videosite}->{$_});
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}->{$_});
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
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});
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_}->{$_});
111 config_set(['active-connectors'], join(",", @{$conf->{connectorlist}}));
112 config_set(['defined-connectors'], join(",", @connectors));
113 config_set(['config-version'], '2');
117 # Reading a configuration value. Called by the core
121 my $item = join('.', 'videosite', @{$path});
125 Irssi::settings_add_str('videosite', $item, "\0");
126 $val = Irssi::settigs_get_str($item);
128 return ($val ne "\0")?$val:undef;
132 # Returns a true value if the config item exists
136 my $item = join('.', 'videosite', @{$path});
138 Irssi::settings_add_str('videosite', $item, "\0");
139 return Irssi::settings_get_str ne "\0";
143 # Setting a configuration value. Called by the core
148 my $item = join('.', 'videosite', @{$path});
150 Irssi::settings_add_str('videosite', $item, "\0");
151 Irssi::settings_set_str($item, $value);
155 # Delete a configuration value. Called by the core.
159 my $item = join('.', 'videosite', @{$path});
161 Irssi::settings_remove($item);
165 # Return a color code. Called by the core
170 Irssi::print(sprintf("Asked to convert (%s,%s) into irssi color codes", $fg, $bg));o
176 # Handle commands (/videosite ...)
179 my ($cmdline, $server, $witem) = @_;
182 ewpf => sub { defined($evitem)?$evitem->print(@_):Irssi::print(@_) },
185 libvideosite::handle_command(\%event);
189 # Handle a received message
190 # Create an event structure and hand it off to libvideosite
193 my ($server, $msg, $nick, $userhost, $channel) = @_;
194 my $evitem = $server->window_item_find($channel);
197 ewpf => sub { defined($evitem)?$evitem->print(@_):Irssi::print(@_) },
200 libvideosite::check_for_link(\%event);
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 $_ },
217 Irssi::print(sprintf("videosite API register failed: %s", $libvideosite::error));
221 unless(libvideosite::init()) {
222 Irssi::print(sprintf("videosite init failed: %s", $libvideosite::error));
230 # Find out the script directory, and add it to @INC.
231 # This is necessary to find libvideosite.pm
233 push(@INC, File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts'));
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]) });
244 Irssi::command_bind('videosite', sub { videosite_hook(@_) });