3d72728623fa91d787d6d972f944297f547ba9f4
[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         eval {
54             local $/;
55             open(CONF, '<', $conffile);
56             $conf = JSON->new->utf8->decode(<CONF>);
57             close(CONF);
58         };
59     } elsif (-r $xmlconffile) {
60         $conf = XML::Simple::XMLin($xmlconffile, ForceArray => ['config', 'option', 'connectorlist'], KeepRoot => 1, KeyAttr => {'connector' => '+name', 'config' => 'module', 'option' => 'key'});
61     }
62
63     #
64     # Configuration conversion:
65     # Replace this structure:
66     #
67     # key => {
68     #   content => value
69     # }
70     #
71     # with this structure
72     #
73     # key => value
74     #
75     Irssi::print("Converting configuration, stage 1");
76
77     # Only the getter/grabbers have this, so just check that part of the config
78     foreach my $g (keys(%{$conf->{videosite}->{config}})) {
79         foreach (keys(%{$conf->{videosite}->{config}->{$g}->{option}})) {
80             if (exists($conf->{videosite}->{config}->{$g}->{option}->{$_}->{content})) {
81                 $conf->{videosite}->{config}->{$g}->{option}->{$_} = $conf->{videosite}->{config}->{$g}->{option}->{$_}->{content};
82             }
83         }
84     }
85
86     #
87     # Walk the configuration hash, creating irssi config entries for
88     # each leaf node.
89     #
90     # Some config values changed, so not the entire config is copied over.
91     # There is a helper function for this in libvideosite that we're using.
92     #
93     Irssi::print("Converting configuration, stage 2");
94
95     # Copy the "basic" settings.
96     foreach (qw(getter mode)) {
97         config_set(['getter'], $conf->{videosite}->{$_});
98     }
99
100     # Copy the per-getter/setter settings
101     foreach my $g (keys(%{$conf->{videosite}->{config}})) {
102         foreach (keys(%{$conf->{videosite}->{config}->{$g}->{option}})) {
103             config_set(['plugin', $g, $_], $conf->{videosite}->{config}->{$g}->{option}->{$_});
104         }
105     }
106
107     # Copy the connectors. The connectors themselves are copied as-is,
108     # the list of active connectors is copied under a different name,
109     # and a list of all existing connectors is created
110     my @connectors;
111
112     foreach my $c (keys(%{$conf->{videosite}->{connectors}})) {
113         push(@connectors, $c);
114         config_set(['connectors', $c, 'name'], $conf->{videosite}->{connectors}->{$c}->{name});
115         if (exists($conf->{videosite}->{connectors}->{$c}->{_immutable})) {
116             config_set(['connectors', $c, '_immutable'], $conf->{videosite}->{connectors}->{$c}->{_immutable});
117         }
118         foreach (qw(http https)) {
119             if (exists($conf->{videosite}->{connectors}->{$c}->{schemas}->{http})) {
120                 config_set(['connectors', $c, 'schemas', $_], $conf->{videosite}->{connectors}->{$c}->{schemas_}->{$_});
121             }
122         }
123     }
124     config_set(['active-connectors'], join(",", @{$conf->{connectorlist}}));
125     config_set(['defined-connectors'], join(",", @connectors));
126     config_set(['config-version'], '2');
127 }
128
129 #
130 # Reading a configuration value. Called by the core
131 #
132 sub config_get {
133     my $path = shift;
134     my $item = join('.', 'videosite', @{$path});
135     my $val;
136
137
138     Irssi::settings_add_str('videosite', $item, "\0");
139     $val = Irssi::settings_get_str($item);
140
141     return ($val ne "\0")?$val:undef;
142 }
143
144 #
145 # Returns a true value if the config item exists
146 #
147 sub config_has {
148     my $path = shift;
149     my $item = join('.', 'videosite', @{$path});
150
151     Irssi::settings_add_str('videosite', $item, "\0");
152     return Irssi::settings_get_str($item) ne "\0";
153 }
154
155 #
156 # Setting a configuration value. Called by the core
157 #
158 sub config_set {
159     my $path = shift;
160     my $value = shift;
161     my $item = join('.', 'videosite', @{$path});
162
163     Irssi::settings_add_str('videosite', $item, "\0");
164     Irssi::settings_set_str($item, $value);
165 }
166
167 #
168 # Delete a configuration value. Called by the core.
169 #
170 sub config_del {
171     my $path = shift;
172     my $item = join('.', 'videosite', @{$path});
173
174     Irssi::settings_remove($item);
175 }
176
177 #
178 # Return a color code. Called by the core
179 #
180 # Does not handle background colors yet.
181 #
182 sub colorpair {
183     my ($fg, $bg) = @_;
184
185     $fg = exists($foreground_colors{$fg})?$foreground_colors{$fg}:'';
186     $bg = '';
187
188     return $fg . $bg;
189 }
190
191 #
192 # Handle commands (/videosite ...)
193 #
194 sub videosite_hook {
195     my ($cmdline, $server, $witem) = @_;
196     my %event = (
197         message => $cmdline,
198         ewpf => sub { defined($witem)?$witem->print($_[0]):Irssi::print($_[0]) },
199     );
200
201     libvideosite::handle_command(\%event);
202 }
203
204 #
205 # Handle a received message
206 # Create an event structure and hand it off to libvideosite
207 #
208 sub message_hook {
209     my ($server, $msg, $nick, $userhost, $channel) = @_;
210     my $evitem = $server->window_item_find($channel);
211     my %event = (
212         message => $msg,
213         ewpf => sub { defined($evitem)?$evitem->print($_[0]):Irssi::print($_[0]) },
214     );
215
216     libvideosite::check_for_link(\%event);
217 }
218
219 sub videosite_reset {
220     unless(libvideosite::register_api({
221         io => sub { Irssi::print($_[0]) },
222         config_init => \&config_init,
223         config_get =>  \&config_get,
224         config_set => \&config_set,
225         config_has => \&config_has,
226         config_save => \&config_save,
227         config_del => \&config_del,
228         color => \&colorpair,
229         module_path => sub { return File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts') },
230         quote => sub { s/%/%%/g; return $_ },
231         _debug => sub { 1 },
232     })) {
233         Irssi::print(sprintf("videosite API register failed: %s", $libvideosite::error));
234         return 0;
235     }
236
237     unless(libvideosite::init()) {
238         Irssi::print(sprintf("videosite init failed: %s", $libvideosite::error));
239         return 0;
240     }
241
242     return 1;
243 }
244
245 sub videosite_init {
246     # Find out the script directory, and add it to @INC.
247     # This is necessary to find libvideosite.pm
248
249     push(@INC, File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts'));
250     load 'libvideosite';
251
252     if (videosite_reset()) {
253         signal_add_last("message public", sub { message_hook(@_) });
254         signal_add_last("message own_public", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
255         signal_add_last("message private", sub { message_hooK($_[0], $_[1], $_[2], $_[3], $_[2]) });
256         signal_add_last("message own_private", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
257         signal_add_last("message irc action", sub { message_hook(@_) });
258         signal_add_last("message irc own_action", sub { message_hook($_[0], $_[1], undef, undef, $_[2]) });
259
260         Irssi::command_bind('videosite', sub { videosite_hook(@_) });
261     }
262 }
263
264 videosite_init();