1 # shim to connect libvideosite to irssi
3 # (c) 2007-2008 by Ralf Ertzinger <ralf@camperquake.de>
4 # licensed under GNU GPL v2
6 use Irssi 20020324 qw (command_bind command_runsub signal_add_first signal_add_last);
7 use vars qw($VERSION %IRSSI);
11 use JSON -support_by_pp;
14 # Initialize the config subsystem. Called by the core.
16 # Due to historic reasons this has to deal with a number of possible config sources:
17 # * irssi internal config
18 # * JSON config, old format
19 # * XML config, old format
21 # JSON and XML configs are parsed, converted and moved to the irssi internal
22 # format. This happens only once, as the config search stops with the first
26 my $xmlconffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.xml');
27 my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.json');
30 # Check for irssi internal config. If not found...
31 if (config_has(['config-version'])) {
32 # Configuration in irssi config file. We're done.
36 # Try to find old config files and load them.
40 open(CONF, '<', $conffile);
41 $conf = JSON->new->utf8->decode(<CONF>);
44 } elsif (-r $xmlconffile) {
45 $conf = XML::Simple::XMLin($xmlconffile, ForceArray => ['config', 'option', 'connectorlist'], KeepRoot => 1, KeyAttr => {'connector' => '+name', 'config' => 'module', 'option' => 'key'});
49 # Configuration conversion:
50 # Replace this structure:
60 Irssi::print("Converting configuration, stage 1");
62 # Only the getter/grabbers have this, so just check that part of the config
63 foreach my $g (keys(%{$conf->{videosite}->{config}})) {
64 foreach (keys(%{$conf->{videosite}->{config}->{$g}->{option}})) {
65 if (exists($conf->{videosite}->{config}->{$g}->{option}->{$_}->{content})) {
66 $conf->{videosite}->{config}->{$g}->{option}->{$_} = $conf->{videosite}->{config}->{$g}->{option}->{$_}->{content};
72 # Walk the configuration hash, creating irssi config entries for
75 # Some config values changed, so not the entire config is copied over.
76 # There is a helper function for this in libvideosite that we're using.
78 Irssi::print("Converting configuration, stage 2");
80 # Copy the "basic" settings.
81 foreach (qw(getter mode)) {
82 config_set(['getter'], $conf->{videosite}->{$_});
85 # Copy the per-getter/setter settings
86 foreach my $g (keys(%{$conf->{videosite}->{config}})) {
87 foreach (keys(%{$conf->{videosite}->{config}->{$g}->{option}})) {
88 config_set(['plugin', $g, $_], $conf->{videosite}->{config}->{$g}->{option}->{$_});
92 # Copy the connectors. The connectors themselves are copied as-is,
93 # the list of active connectors is copied under a different name,
94 # and a list of all existing connectors is created
97 foreach my $c (keys(%{$conf->{videosite}->{connectors}})) {
98 push(@connectors, $c);
99 config_set(['connectors', $c, 'name'], $conf->{videosite}->{connectors}->{$c}->{name});
100 if (exists($conf->{videosite}->{connectors}->{$c}->{_immutable})) {
101 config_set(['connectors', $c, '_immutable'], $conf->{videosite}->{connectors}->{$c}->{_immutable});
103 foreach (qw(http https)) {
104 if (exists($conf->{videosite}->{connectors}->{$c}->{schemas}->{http})) {
105 config_set(['connectors', $c, 'schemas', $_], $conf->{videosite}->{connectors}->{$c}->{schemas_}->{$_});
109 config_set(['active-connectors'], join(",", @{$conf->{connectorlist}}));
110 config_set(['defined-connectors'], join(",", @connectors));
111 config_set(['config-version'], '2');
115 # Reading a configuration value. Called by the core
119 my $item = join('.', 'videosite', @{$path});
123 Irssi::settings_add_str('videosite', $item, "\0");
124 $val = Irssi::settings_get_str($item);
126 return ($val ne "\0")?$val:undef;
130 # Returns a true value if the config item exists
134 my $item = join('.', 'videosite', @{$path});
136 Irssi::settings_add_str('videosite', $item, "\0");
137 return Irssi::settings_get_str($item) ne "\0";
141 # Setting a configuration value. Called by the core
146 my $item = join('.', 'videosite', @{$path});
148 Irssi::settings_add_str('videosite', $item, "\0");
149 Irssi::settings_set_str($item, $value);
153 # Delete a configuration value. Called by the core.
157 my $item = join('.', 'videosite', @{$path});
159 Irssi::settings_remove($item);
163 # Return a color code. Called by the core
168 Irssi::print(sprintf("Asked to convert (%s,%s) into irssi color codes", $fg, $bg));
174 # Handle commands (/videosite ...)
177 my ($cmdline, $server, $witem) = @_;
180 ewpf => sub { defined($witem)?$witem->print($_[0]):Irssi::print($_[0]) },
183 libvideosite::handle_command(\%event);
187 # Handle a received message
188 # Create an event structure and hand it off to libvideosite
191 my ($server, $msg, $nick, $userhost, $channel) = @_;
192 my $evitem = $server->window_item_find($channel);
195 ewpf => sub { defined($evitem)?$evitem->print($_[0]):Irssi::print($_[0]) },
198 libvideosite::check_for_link(\%event);
201 sub videosite_reset {
202 unless(libvideosite::register_api({
203 io => sub { Irssi::print($_[0]) },
204 config_init => \&config_init,
205 config_get => \&config_get,
206 config_set => \&config_set,
207 config_has => \&config_has,
208 config_save => \&config_save,
209 config_del => \&config_del,
210 color => \&colorpair,
211 module_path => sub { return File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts') },
212 quote => sub { s/%/%%/g; return $_ },
215 Irssi::print(sprintf("videosite API register failed: %s", $libvideosite::error));
219 unless(libvideosite::init()) {
220 Irssi::print(sprintf("videosite init failed: %s", $libvideosite::error));
228 # Find out the script directory, and add it to @INC.
229 # This is necessary to find libvideosite.pm
231 push(@INC, File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts'));
234 if (videosite_reset()) {
235 signal_add_last("message public", sub { message_hook(@_) });
236 signal_add_last("message own_public", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
237 signal_add_last("message private", sub { message_hooK($_[0], $_[1], $_[2], $_[3], $_[2]) });
238 signal_add_last("message own_private", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
239 signal_add_last("message irc action", sub { message_hook(@_) });
240 signal_add_last("message irc own_action", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
242 Irssi::command_bind('videosite', sub { videosite_hook(@_) });