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 $SIG{ __DIE__ } = sub { Carp::confess( @_ ) };
17 # List of foreground colors. This list is not complete, it just
18 # contains the colors needed by videosite.
20 # The % are doubled because these are used in sprintf.
22 my %foreground_colors = (
32 # This is a canary value used in the config system as the default
33 # value. As irssi does not have a way to test if a setting exists
34 # this is used instead. A config value is never expected to be set
35 # to this value and be valid.
37 my $config_canary = "\1";
40 # Initialize the config subsystem. Called by the core.
42 # Due to historic reasons this has to deal with a number of possible config sources:
43 # * irssi internal config
44 # * JSON config, old format
45 # * XML config, old format
47 # JSON and XML configs are parsed, converted and moved to the irssi internal
48 # format. This happens only once, as the config search stops with the first
52 my $xmlconffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.xml');
53 my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.json');
56 # Check for irssi internal config. If not found...
57 if (config_has(['config-version'])) {
58 # Configuration in irssi config file. We're done.
62 # Try to find old config files and load them.
64 Irssi::print("Converting configuration from videosite.json. This will happen only once.");
67 open(CONF, '<', $conffile);
68 $conf = JSON->new->utf8->decode(<CONF>);
71 } elsif (-r $xmlconffile) {
72 Irssi::print("Converting configuration from videosite.xml. This will happen only once.");
73 $conf = XML::Simple::XMLin($xmlconffile, ForceArray => ['config', 'option', 'connectorlist'], KeepRoot => 1, KeyAttr => {'connector' => '+name', 'config' => 'module', 'option' => 'key'});
75 # No old config files around. Just exit.
80 # Configuration conversion:
81 # Replace this structure:
91 Irssi::print("Converting configuration, stage 1");
93 # Only the getter/grabbers have this, so just check that part of the config
94 foreach my $g (keys(%{$conf->{videosite}->{config}})) {
95 foreach (keys(%{$conf->{videosite}->{config}->{$g}->{option}})) {
96 if (exists($conf->{videosite}->{config}->{$g}->{option}->{$_}->{content})) {
97 $conf->{videosite}->{config}->{$g}->{option}->{$_} = $conf->{videosite}->{config}->{$g}->{option}->{$_}->{content};
103 # Walk the configuration hash, creating irssi config entries for
106 # Some config values changed, so not the entire config is copied over.
107 # There is a helper function for this in libvideosite that we're using.
109 Irssi::print("Converting configuration, stage 2");
111 # Copy the "basic" settings.
112 foreach (qw(getter mode)) {
113 config_set([$_], $conf->{videosite}->{$_});
116 # Copy the per-getter/setter settings
117 foreach my $g (keys(%{$conf->{videosite}->{config}})) {
118 foreach (keys(%{$conf->{videosite}->{config}->{$g}->{option}})) {
119 config_set(['plugin', $g, $_], $conf->{videosite}->{config}->{$g}->{option}->{$_});
123 # Copy the connectors. The connectors themselves are copied as-is,
124 # the list of active connectors is copied under a different name,
125 # and a list of all existing connectors is created
128 foreach my $c (keys(%{$conf->{videosite}->{connectors}})) {
129 push(@connectors, $c);
130 config_set(['connectors', $c, 'name'], $conf->{videosite}->{connectors}->{$c}->{name});
131 if (exists($conf->{videosite}->{connectors}->{$c}->{_immutable})) {
132 config_set(['connectors', $c, '_immutable'], $conf->{videosite}->{connectors}->{$c}->{_immutable});
134 foreach (qw(http https)) {
135 if (exists($conf->{videosite}->{connectors}->{$c}->{schemas}->{http})) {
136 config_set(['connectors', $c, 'schemas', $_], $conf->{videosite}->{connectors}->{$c}->{schemas_}->{$_});
140 config_set(['active-connectors'], join(",", @{$conf->{videosite}->{connectorlist}}));
141 config_set(['defined-connectors'], join(",", @connectors));
142 config_set(['config-version'], '2');
146 # Reading a configuration value. Called by the core
150 my $item = join('.', 'videosite', @{$path});
154 Irssi::settings_add_str('videosite', $item, $config_canary);
155 $val = Irssi::settings_get_str($item);
157 return ($val ne $config_canary)?$val:undef;
161 # Returns a true value if the config item exists
165 my $item = join('.', 'videosite', @{$path});
167 Irssi::settings_add_str('videosite', $item, $config_canary);
168 return Irssi::settings_get_str($item) ne $config_canary;
172 # Setting a configuration value. Called by the core
177 my $item = join('.', 'videosite', @{$path});
179 Irssi::settings_add_str('videosite', $item, $config_canary);
180 Irssi::settings_set_str($item, $value);
184 # Delete a configuration value. Called by the core.
186 # Now, according to the configuration Irssi::settings_remove() removes a
187 # config settings. This does not work in any irssi version available to me.
188 # So just set the key to the canary value.
193 config_set($path, $config_canary);
197 # Return a color code. Called by the core
199 # Does not handle background colors yet.
204 $fg = exists($foreground_colors{$fg})?$foreground_colors{$fg}:'';
211 # Handle commands (/videosite ...)
214 my ($cmdline, $server, $witem) = @_;
217 ewpf => sub { defined($witem)?$witem->print($_[0], MSGLEVEL_CLIENTCRAP):Irssi::print($_[0]) },
218 window => defined($witem)?$witem->{server}->{real_address} . "/" . $witem->{name}:"",
221 libvideosite::handle_command(\%event);
225 # Handle a received message
226 # Create an event structure and hand it off to libvideosite
229 my ($server, $msg, $nick, $userhost, $channel) = @_;
230 my $witem = $server->window_item_find($channel);
233 ewpf => sub { defined($witem)?$witem->print($_[0], MSGLEVEL_CLIENTCRAP):Irssi::print($_[0]) },
234 window => defined($witem)?$witem->{server}->{real_address} . "/" . $witem->{name}:"",
237 libvideosite::check_for_link(\%event);
240 sub videosite_reset {
243 # Find out the script directory, and add it to @INC.
244 # This is necessary to find libvideosite.pm
245 $libpath = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts');
247 # If the library is already loaded unload it
248 foreach (keys(%INC)) {
249 if ($INC{$_} eq File::Spec->catfile($libpath, 'libvideosite.pm')) {
254 push(@INC, $libpath);
257 unless(libvideosite::register_api({
258 io => sub { Irssi::print($_[0]) },
259 config_init => \&config_init,
260 config_get => \&config_get,
261 config_set => \&config_set,
262 config_has => \&config_has,
263 config_save => sub { 1 },
264 config_del => \&config_del,
265 color => \&colorpair,
266 module_path => sub { return File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts') },
267 quote => sub { s/%/%%/g; return $_ },
268 reload => \&videosite_reset,
270 Irssi::print(sprintf("videosite API register failed: %s", $libvideosite::error));
274 unless(libvideosite::init()) {
275 Irssi::print(sprintf("videosite init failed: %s", $libvideosite::error));
284 if (videosite_reset()) {
285 signal_add_last("message public", sub { message_hook(@_) });
286 signal_add_last("message own_public", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
287 signal_add_last("message private", sub { message_hook($_[0], $_[1], $_[2], $_[3], $_[2]) });
288 signal_add_last("message own_private", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
289 signal_add_last("message irc action", sub { message_hook(@_) });
290 signal_add_last("message irc own_action", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
292 Irssi::command_bind('videosite', sub { videosite_hook(@_) });