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