Merge branch 'per-window-debug' into weechat
[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 # 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.
33 #
34 my $config_canary = "\1";
35
36 #
37 # Initialize the config subsystem. Called by the core.
38 #
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
43 #
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
46 # format found
47 #
48 sub config_init {
49     my $xmlconffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.xml');
50     my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.json');
51     my $conf;
52
53     # Check for irssi internal config. If not found...
54     if (config_has(['config-version'])) {
55         # Configuration in irssi config file. We're done.
56         return;
57     }
58
59     # Try to find old config files and load them.
60     if (-r $conffile) {
61         Irssi::print("Converting configuration from videosite.json. This will happen only once.");
62         eval {
63             local $/;
64             open(CONF, '<', $conffile);
65             $conf = JSON->new->utf8->decode(<CONF>);
66             close(CONF);
67         };
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'});
71     } else {
72         # No old config files around. Just exit.
73         return;
74     }
75
76     #
77     # Configuration conversion:
78     # Replace this structure:
79     #
80     # key => {
81     #   content => value
82     # }
83     #
84     # with this structure
85     #
86     # key => value
87     #
88     Irssi::print("Converting configuration, stage 1");
89
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};
95             }
96         }
97     }
98
99     #
100     # Walk the configuration hash, creating irssi config entries for
101     # each leaf node.
102     #
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.
105     #
106     Irssi::print("Converting configuration, stage 2");
107
108     # Copy the "basic" settings.
109     foreach (qw(getter mode)) {
110         config_set([$_], $conf->{videosite}->{$_});
111     }
112
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}->{$_});
117         }
118     }
119
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
123     my @connectors;
124
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});
130         }
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_}->{$_});
134             }
135         }
136     }
137     config_set(['active-connectors'], join(",", @{$conf->{videosite}->{connectorlist}}));
138     config_set(['defined-connectors'], join(",", @connectors));
139     config_set(['config-version'], '2');
140 }
141
142 #
143 # Reading a configuration value. Called by the core
144 #
145 sub config_get {
146     my $path = shift;
147     my $item = join('.', 'videosite', @{$path});
148     my $val;
149
150
151     Irssi::settings_add_str('videosite', $item, $config_canary);
152     $val = Irssi::settings_get_str($item);
153
154     return ($val ne $config_canary)?$val:undef;
155 }
156
157 #
158 # Returns a true value if the config item exists
159 #
160 sub config_has {
161     my $path = shift;
162     my $item = join('.', 'videosite', @{$path});
163
164     Irssi::settings_add_str('videosite', $item, $config_canary);
165     return Irssi::settings_get_str($item) ne $config_canary;
166 }
167
168 #
169 # Setting a configuration value. Called by the core
170 #
171 sub config_set {
172     my $path = shift;
173     my $value = shift;
174     my $item = join('.', 'videosite', @{$path});
175
176     Irssi::settings_add_str('videosite', $item, $config_canary);
177     Irssi::settings_set_str($item, $value);
178 }
179
180 #
181 # Delete a configuration value. Called by the core.
182 #
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.
186 #
187 sub config_del {
188     my $path = shift;
189
190     config_set($path, $config_canary);
191 }
192
193 #
194 # Return a color code. Called by the core
195 #
196 # Does not handle background colors yet.
197 #
198 sub colorpair {
199     my ($fg, $bg) = @_;
200
201     $fg = exists($foreground_colors{$fg})?$foreground_colors{$fg}:'';
202     $bg = '';
203
204     return $fg . $bg;
205 }
206
207 #
208 # Handle commands (/videosite ...)
209 #
210 sub videosite_hook {
211     my ($cmdline, $server, $witem) = @_;
212     my %event = (
213         message => $cmdline,
214         ewpf => sub { defined($witem)?$witem->print($_[0], MSGLEVEL_CLIENTCRAP):Irssi::print($_[0]) },
215         window => defined($witem)?$witem->{server}->{real_address} . "/" . $witem->{name}:"",
216     );
217
218     libvideosite::handle_command(\%event);
219 }
220
221 #
222 # Handle a received message
223 # Create an event structure and hand it off to libvideosite
224 #
225 sub message_hook {
226     my ($server, $msg, $nick, $userhost, $channel) = @_;
227     my $witem = $server->window_item_find($channel);
228     my %event = (
229         message => $msg,
230         ewpf => sub { defined($witem)?$witem->print($_[0], MSGLEVEL_CLIENTCRAP):Irssi::print($_[0]) },
231         window => defined($witem)?$witem->{server}->{real_address} . "/" . $witem->{name}:"",
232     );
233
234     libvideosite::check_for_link(\%event);
235 }
236
237 sub videosite_reset {
238     my $libpath;
239
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');
243
244     # If the library is already loaded unload it
245     foreach (keys(%INC)) {
246         if ($INC{$_} eq File::Spec->catfile($libpath, 'libvideosite.pm')) {
247             delete($INC{$_});
248         }
249     }
250
251     push(@INC, $libpath);
252     load 'libvideosite';
253
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,
266     })) {
267         Irssi::print(sprintf("videosite API register failed: %s", $libvideosite::error));
268         return 0;
269     }
270
271     unless(libvideosite::init()) {
272         Irssi::print(sprintf("videosite init failed: %s", $libvideosite::error));
273         return 0;
274     }
275
276     return 1;
277 }
278
279 sub videosite_init {
280
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]) });
288
289         Irssi::command_bind('videosite', sub { videosite_hook(@_) });
290     }
291 }
292
293 videosite_init();