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 # List of foreground colors. This list is not complete, it just
15 # contains the colors needed by videosite.
17 # The % are doubled because these are used in sprintf.
19 my %foreground_colors = (
29 # This is a canary value used in the config system as the default
30 # value. As irssi does not have a way to test if a setting exists
31 # this is used instead. A config value is never expected to be set
32 # to this value and be valid.
34 my $config_canary = "\1";
37 # Initialize the config subsystem. Called by the core.
39 # Due to historic reasons this has to deal with a number of possible config sources:
40 # * irssi internal config
41 # * JSON config, old format
42 # * XML config, old format
44 # JSON and XML configs are parsed, converted and moved to the irssi internal
45 # format. This happens only once, as the config search stops with the first
49 my $xmlconffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.xml');
50 my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.json');
53 # Check for irssi internal config. If not found...
54 if (config_has(['config-version'])) {
55 # Configuration in irssi config file. We're done.
59 # Try to find old config files and load them.
61 Irssi::print("Converting configuration from videosite.json. This will happen only once.");
64 open(CONF, '<', $conffile);
65 $conf = JSON->new->utf8->decode(<CONF>);
68 } elsif (-r $xmlconffile) {
69 Irssi::print("Converting configuration from videosite.xml. This will happen only once.");
70 $conf = XML::Simple::XMLin($xmlconffile, ForceArray => ['config', 'option', 'connectorlist'], KeepRoot => 1, KeyAttr => {'connector' => '+name', 'config' => 'module', 'option' => 'key'});
72 # No old config files around. Just exit.
77 # Configuration conversion:
78 # Replace this structure:
88 Irssi::print("Converting configuration, stage 1");
90 # Only the getter/grabbers have this, so just check that part of the config
91 foreach my $g (keys(%{$conf->{videosite}->{config}})) {
92 foreach (keys(%{$conf->{videosite}->{config}->{$g}->{option}})) {
93 if (exists($conf->{videosite}->{config}->{$g}->{option}->{$_}->{content})) {
94 $conf->{videosite}->{config}->{$g}->{option}->{$_} = $conf->{videosite}->{config}->{$g}->{option}->{$_}->{content};
100 # Walk the configuration hash, creating irssi config entries for
103 # Some config values changed, so not the entire config is copied over.
104 # There is a helper function for this in libvideosite that we're using.
106 Irssi::print("Converting configuration, stage 2");
108 # Copy the "basic" settings.
109 foreach (qw(getter mode)) {
110 config_set([$_], $conf->{videosite}->{$_});
113 # Copy the per-getter/setter settings
114 foreach my $g (keys(%{$conf->{videosite}->{config}})) {
115 foreach (keys(%{$conf->{videosite}->{config}->{$g}->{option}})) {
116 config_set(['plugin', $g, $_], $conf->{videosite}->{config}->{$g}->{option}->{$_});
120 # Copy the connectors. The connectors themselves are copied as-is,
121 # the list of active connectors is copied under a different name,
122 # and a list of all existing connectors is created
125 foreach my $c (keys(%{$conf->{videosite}->{connectors}})) {
126 push(@connectors, $c);
127 config_set(['connectors', $c, 'name'], $conf->{videosite}->{connectors}->{$c}->{name});
128 if (exists($conf->{videosite}->{connectors}->{$c}->{_immutable})) {
129 config_set(['connectors', $c, '_immutable'], $conf->{videosite}->{connectors}->{$c}->{_immutable});
131 foreach (qw(http https)) {
132 if (exists($conf->{videosite}->{connectors}->{$c}->{schemas}->{http})) {
133 config_set(['connectors', $c, 'schemas', $_], $conf->{videosite}->{connectors}->{$c}->{schemas_}->{$_});
137 config_set(['active-connectors'], join(",", @{$conf->{videosite}->{connectorlist}}));
138 config_set(['defined-connectors'], join(",", @connectors));
139 config_set(['config-version'], '2');
143 # Reading a configuration value. Called by the core
147 my $item = join('.', 'videosite', @{$path});
151 Irssi::settings_add_str('videosite', $item, $config_canary);
152 $val = Irssi::settings_get_str($item);
154 return ($val ne $config_canary)?$val:undef;
158 # Returns a true value if the config item exists
162 my $item = join('.', 'videosite', @{$path});
164 Irssi::settings_add_str('videosite', $item, $config_canary);
165 return Irssi::settings_get_str($item) ne $config_canary;
169 # Setting a configuration value. Called by the core
174 my $item = join('.', 'videosite', @{$path});
176 Irssi::settings_add_str('videosite', $item, $config_canary);
177 Irssi::settings_set_str($item, $value);
181 # Delete a configuration value. Called by the core.
183 # Now, according to the configuration Irssi::settings_remove() removes a
184 # config settings. This does not work in any irssi version available to me.
185 # So just set the key to the canary value.
190 config_set($path, $config_canary);
194 # Return a color code. Called by the core
196 # Does not handle background colors yet.
201 $fg = exists($foreground_colors{$fg})?$foreground_colors{$fg}:'';
208 # Handle commands (/videosite ...)
211 my ($cmdline, $server, $witem) = @_;
214 ewpf => sub { defined($witem)?$witem->print($_[0]):Irssi::print($_[0]) },
215 window => defined($witem)?$witem->{server}->{real_address} . "/" . $witem->{name}:"",
218 libvideosite::handle_command(\%event);
222 # Handle a received message
223 # Create an event structure and hand it off to libvideosite
226 my ($server, $msg, $nick, $userhost, $channel) = @_;
227 my $witem = $server->window_item_find($channel);
230 ewpf => sub { defined($witem)?$witem->print($_[0]):Irssi::print($_[0]) },
231 window => defined($witem)?$witem->{server}->{real_address} . "/" . $witem->{name}:"",
234 libvideosite::check_for_link(\%event);
237 sub videosite_reset {
240 # Find out the script directory, and add it to @INC.
241 # This is necessary to find libvideosite.pm
242 $libpath = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts');
244 # If the library is already loaded unload it
245 foreach (keys(%INC)) {
246 if ($INC{$_} eq File::Spec->catfile($libpath, 'libvideosite.pm')) {
251 push(@INC, $libpath);
254 unless(libvideosite::register_api({
255 io => sub { Irssi::print($_[0]) },
256 config_init => \&config_init,
257 config_get => \&config_get,
258 config_set => \&config_set,
259 config_has => \&config_has,
260 config_save => sub { 1 },
261 config_del => \&config_del,
262 color => \&colorpair,
263 module_path => sub { return File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts') },
264 quote => sub { s/%/%%/g; return $_ },
265 reload => \&videosite_reset,
267 Irssi::print(sprintf("videosite API register failed: %s", $libvideosite::error));
271 unless(libvideosite::init()) {
272 Irssi::print(sprintf("videosite init failed: %s", $libvideosite::error));
281 if (videosite_reset()) {
282 signal_add_last("message public", sub { message_hook(@_) });
283 signal_add_last("message own_public", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
284 signal_add_last("message private", sub { message_hook($_[0], $_[1], $_[2], $_[3], $_[2]) });
285 signal_add_last("message own_private", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
286 signal_add_last("message irc action", sub { message_hook(@_) });
287 signal_add_last("message irc own_action", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
289 Irssi::command_bind('videosite', sub { videosite_hook(@_) });