1 # autodownload flash videos
3 # (c) 2007-2008 by Ralf Ertzinger <ralf@camperquake.de>
4 # licensed under GNU GPL v2
6 # Based on youtube.pl by Christian Garbs <mitch@cgarbs.de>
8 # based on trigger.pl by Wouter Coekaerts <wouter@coekaerts.be>
12 use Irssi 20020324 qw (command_bind command_runsub signal_add_first signal_add_last);
13 use vars qw($VERSION %IRSSI);
17 delete($INC{'BettIrssi.pm'});
18 use BettIrssi qw(_bcb _bcs);
24 my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.xml');
25 my $scriptdir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts');
26 my $plugindir = File::Spec->catfile($scriptdir, 'videosite');
37 # "message public", SERVER_REC, char *msg, char *nick, char *address, char *target
38 signal_add_last(_bcs("message public" => sub {check_for_link(@_)}));
39 # "message own_public", SERVER_REC, char *msg, char *target
40 signal_add_last(_bcs("message own_public" => sub {check_for_link(@_)}));
42 # "message private", SERVER_REC, char *msg, char *nick, char *address
43 signal_add_last(_bcs("message private" => sub {check_for_link(@_)}));
44 # "message own_private", SERVER_REC, char *msg, char *target, char *orig_target
45 signal_add_last(_bcs("message own_private" => sub {check_for_link(@_)}));
47 # "message irc action", SERVER_REC, char *msg, char *nick, char *address, char *target
48 signal_add_last(_bcs("message irc action" => sub {check_for_link(@_)}));
49 # "message irc own_action", SERVER_REC, char *msg, char *target
50 signal_add_last(_bcs("message irc own_action" => sub {check_for_link(@_)}));
53 signal_add_first('complete word', \&sig_complete);
55 my $videosite_commands = {
94 foreach (@grabbers, @getters) {
97 write_irssi(undef, 'Enabled debugging');
102 foreach (@grabbers, @getters) {
105 write_irssi(undef, 'Disabled debugging');
113 $text[0] = 'videosite: ' . $text[0];
115 if (defined $witem) {
116 $witem->print(sprintf(shift(@text), @text), MSGLEVEL_CLIENTCRAP);
118 Irssi::print(sprintf(shift(@text), @text));
125 write_irssi(shift, @_);
131 my $message = $event->message();
132 my $witem = $event->channel();
138 # Look if we should ignore this line
139 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
145 # Offer the message to all Grabbers in turn
146 foreach $g (@grabbers) {
147 ($m, $p) = $g->get($message);
148 while (defined($m)) {
149 write_debug($witem, 'Metadata: %s', Dumper($m));
150 if ('download' eq ($conf->{'videosite'}->{'mode'})) {
151 write_irssi($witem, '%%R>>> %%NSaving %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
152 unless($getter->get($m)) {
153 write_irssi($witem, '%%R>>> FAILED');
155 } elsif ('display' eq ($conf->{'videosite'}->{'mode'})) {
156 write_irssi($witem, '%%M>>> %%NSaw %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
158 write_irssi($witem, '%%R>>> Invalid operation mode');
161 # Remove the matched part from the message and try again (there may be
166 ($m, $p) = $g->get($message);
174 open(CONF, '>'.$conffile) or die 'Could not open config file';
175 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
179 write_irssi(undef, 'Could not save config to %s: %s', ($conffile, $@));
181 write_irssi(undef, 'configuration saved to %s', $conffile);
191 foreach $p (@getters, @grabbers) {
192 if ($p->{'NAME'} eq $target) {
193 $p->setval($key, $val);
197 write_irssi(undef, 'No such module');
205 foreach $p (@grabbers) {
206 if ($p->{'NAME'} eq $target) {
211 write_irssi(undef, 'No such module');
219 foreach $p (@grabbers) {
220 if ($p->{'NAME'} eq $target) {
225 write_irssi(undef, 'No such module');
234 if (defined($target)) {
235 foreach $p (@getters, @grabbers) {
236 if ($p->{'NAME'} eq $target) {
237 write_irssi(undef, $p->getconfstr());
241 write_irssi(undef, 'No such module');
243 write_irssi(undef, 'Loaded grabbers (* denotes enabled modules):');
244 foreach $p (@grabbers) {
245 $e = $p->_getval('enabled');
246 write_irssi(undef, ' %s%s', $p->{'NAME'}, $e?'*':'');
249 write_irssi(undef, 'Loaded getters:');
250 foreach $p (@getters) {
251 write_irssi(undef, ' %s', $p->{'NAME'});
260 if (defined($target)) {
261 foreach $p (@getters, @grabbers) {
262 if ($p->{'NAME'} eq $target) {
263 write_irssi(undef, $p->gethelpstr());
267 write_irssi(undef, 'No such module');
269 write_irssi(undef, <<'EOT');
271 save: save the current configuration
272 help [modulename]: display this help, or module specific help
273 show [modulename]: show loaded modules, or the current parameters of a module
274 set modulename parameter value: set a module parameter to a new value
275 getter [modulename]: display or set the getter to use
276 enable [modulename]: enable the usage of this module (grabbers only)
277 disable [modulename]: disable the usage of this module (grabbers only)
278 reload: reload all modules (this is somewhat experimental)
279 mode [modename]: display or set the operation mode (download/display)
280 debug: enable debugging messages
281 nodebug: disable debugging messages
290 if (defined($target)) {
291 foreach $p (@getters) {
292 if ($p->{'NAME'} eq $target) {
294 $conf->{'videosite'}->{'getter'} = $target;
298 write_irssi(undef, 'No such getter');
300 write_irssi(undef, 'Current getter: %s', $conf->{'videosite'}->{'getter'});
307 if (defined($mode)) {
309 if (('download' eq $mode) or ('display' eq $mode)) {
310 $conf->{'videosite'}->{'mode'} = $mode;
312 write_irssi(undef, 'Invalid mode: %s', $mode);
315 write_irssi(undef, 'Current mode: %s', $conf->{'videosite'}->{'mode'});
321 sub sig_command_script_unload {
323 if ($script =~ /(.*\/)?videosite(\.pl)?$/) {
338 opendir(D, $dir) || return ();
339 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
343 write_debug(undef, "Trying to load $p:");
345 eval qq{ require videosite::$p; };
347 write_irssi(undef, "Failed to load plugin: $@");
351 $g = eval qq{ videosite::$p->new(); };
353 write_irssi(undef, "Failed to instanciate: $@");
358 write_debug(undef, "found $g->{'TYPE'} $g->{'NAME'}");
359 if ($type eq $g->{'TYPE'}) {
361 $g->setio(sub {Irssi::print(shift)});
363 write_irssi(undef, '%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
368 write_debug(undef, "Loaded %d plugins", $#g+1);
373 sub _load_modules($) {
377 foreach (keys(%INC)) {
378 if ($INC{$_} =~ m|^$path|) {
379 write_debug(undef, "Removing %s from \$INC", $_);
383 @grabbers = ploader($path, '.*Grabber\.pm$', 'grabber');
384 @getters = ploader($path, '.*Getter\.pm$', 'getter');
390 my $bindings = shift;
393 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
394 # No config, start with an empty one
395 write_debug(undef, 'No config found, using defaults');
396 $conf = { 'videosite' => { }};
398 foreach (keys(%{$PARAMS})) {
399 unless (exists($conf->{'videosite'}->{$_})) {
400 $conf->{'videosite'}->{$_} = $PARAMS->{$_};
404 _load_modules($plugindir);
406 unless (defined(@grabbers) && defined(@getters)) {
407 write_irssi(undef, 'No grabbers or no getters found, can not proceed.');
411 $getter = $getters[0];
412 foreach $p (@getters) {
413 if ($conf->{'videosite'}->{'getter'} eq $p->{'NAME'}) {
417 write_debug(undef, 'Selected %s as getter', $getter->{'NAME'});
418 $conf->{'videosite'}->{'getter'} = $getter->{'NAME'};
420 # Loop through all plugins and load the config
421 foreach $p (@grabbers, @getters) {
422 $conf->{'videosite'}->{'config'}->{$p->{'NAME'}} = $p->mergeconfig($conf->{'videosite'}->{'config'}->{$p->{'NAME'}});
427 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
428 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
429 Irssi::signal_add('setup saved', 'cmd_save');
432 Irssi::command_bind('videosite' => \&cmdhandler);
435 write_irssi(undef, 'videosite initialized');
439 my ($complist, $window, $word, $linestart, $want_space) = @_;
442 if ($linestart !~ m|^/videosite\b|) {
446 if ('/videosite' eq $linestart) {
447 # No command enterd so far. Produce a list of possible follow-ups
448 @matches = grep {/^$word/} keys (%{$videosite_commands});
449 } elsif ('/videosite set' eq $linestart) {
450 # 'set' command entered. Produce a list of modules
451 foreach (@grabbers, @getters) {
452 push(@matches, $_->{'NAME'}) if $_->{'NAME'} =~ m|^$word|;
454 } elsif ($linestart =~ m|^/videosite set (\w+)$|) {
457 foreach my $p (@getters, @grabbers) {
458 if ($p->{'NAME'} eq $module) {
459 @matches = $p->getparamlist($word);
463 } elsif ($linestart =~ m|/videosite set (\w+) (\w+)$|) {
467 foreach my $p (@getters, @grabbers) {
468 if ($p->{'NAME'} eq $module) {
469 @matches = $p->getparamvalues($param, $word);
476 push(@{$complist}, sort @matches);
479 Irssi::signal_stop();
483 my ($data, $server, $item) = @_;
484 my ($cmd, @params) = split(/\s+/, $data);
486 if (exists($videosite_commands->{$cmd})) {
487 $videosite_commands->{$cmd}->(@params);
491 unshift(@INC, $scriptdir);