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