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