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');
27 my @outputstack = (undef);
38 # "message public", SERVER_REC, char *msg, char *nick, char *address, char *target
39 signal_add_last(_bcs("message public" => sub {check_for_link(@_)}));
40 # "message own_public", SERVER_REC, char *msg, char *target
41 signal_add_last(_bcs("message own_public" => sub {check_for_link(@_)}));
43 # "message private", SERVER_REC, char *msg, char *nick, char *address
44 signal_add_last(_bcs("message private" => sub {check_for_link(@_)}));
45 # "message own_private", SERVER_REC, char *msg, char *target, char *orig_target
46 signal_add_last(_bcs("message own_private" => sub {check_for_link(@_)}));
48 # "message irc action", SERVER_REC, char *msg, char *nick, char *address, char *target
49 signal_add_last(_bcs("message irc action" => sub {check_for_link(@_)}));
50 # "message irc own_action", SERVER_REC, char *msg, char *target
51 signal_add_last(_bcs("message irc own_action" => sub {check_for_link(@_)}));
54 signal_add_first('complete word', \&sig_complete);
57 unshift(@outputstack, shift);
63 @outputstack = (undef) unless (@outputstack);
66 my $videosite_commands = {
105 foreach (@grabbers, @getters) {
108 write_irssi('Enabled debugging');
113 foreach (@grabbers, @getters) {
116 write_irssi('Disabled debugging');
122 my $output = $outputstack[0];
124 my $format = "%%mvideosite: %%n" . shift(@text);
126 # escape % in parameters from irssi
127 s/%/%%/g foreach @text;
129 if (defined $output) {
130 $output->(sprintf($format, @text), MSGLEVEL_CLIENTCRAP);
132 Irssi::print(sprintf($format, @text));
145 my $message = $event->message();
146 my $witem = $event->channel();
152 # Look if we should ignore this line
153 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
159 # Offer the message to all Grabbers in turn
160 foreach $g (@grabbers) {
161 ($m, $p) = $g->get($message);
162 while (defined($m)) {
163 write_debug('Metadata: %s', Dumper($m));
164 if ('download' eq ($conf->{'videosite'}->{'mode'})) {
165 write_irssi('%%R>>> %%NSaving %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
166 unless($getter->get($m)) {
167 write_irssi('%%R>>> FAILED');
169 } elsif ('display' eq ($conf->{'videosite'}->{'mode'})) {
170 write_irssi('%%M>>> %%NSaw %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
172 write_irssi('%%R>>> Invalid operation mode');
175 # Remove the matched part from the message and try again (there may be
180 ($m, $p) = $g->get($message);
188 open(CONF, '>'.$conffile) or die 'Could not open config file';
189 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
193 write_irssi('Could not save config to %s: %s', ($conffile, $@));
195 write_irssi('configuration saved to %s', $conffile);
205 foreach $p (@getters, @grabbers) {
206 if ($p->{'NAME'} eq $target) {
207 $p->setval($key, $val);
211 write_irssi('No such module');
219 foreach $p (@grabbers) {
220 if ($p->{'NAME'} eq $target) {
225 write_irssi('No such module');
233 foreach $p (@grabbers) {
234 if ($p->{'NAME'} eq $target) {
239 write_irssi('No such module');
248 if (defined($target)) {
249 foreach $p (@getters, @grabbers) {
250 if ($p->{'NAME'} eq $target) {
251 write_irssi($p->getconfstr());
255 write_irssi('No such module');
257 write_irssi('Loaded grabbers (* denotes enabled modules):');
258 foreach $p (@grabbers) {
259 $e = $p->_getval('enabled');
260 write_irssi(' %s%s', $p->{'NAME'}, $e?'*':'');
263 write_irssi('Loaded getters:');
264 foreach $p (@getters) {
265 write_irssi(' %s', $p->{'NAME'});
274 if (defined($target)) {
275 foreach $p (@getters, @grabbers) {
276 if ($p->{'NAME'} eq $target) {
277 write_irssi($p->gethelpstr());
281 write_irssi('No such module');
283 write_irssi(<<'EOT');
285 save: save the current configuration
286 help [modulename]: display this help, or module specific help
287 show [modulename]: show loaded modules, or the current parameters of a module
288 set modulename parameter value: set a module parameter to a new value
289 getter [modulename]: display or set the getter to use
290 enable [modulename]: enable the usage of this module (grabbers only)
291 disable [modulename]: disable the usage of this module (grabbers only)
292 reload: reload all modules (this is somewhat experimental)
293 mode [modename]: display or set the operation mode (download/display)
294 debug: enable debugging messages
295 nodebug: disable debugging messages
304 if (defined($target)) {
305 foreach $p (@getters) {
306 if ($p->{'NAME'} eq $target) {
308 $conf->{'videosite'}->{'getter'} = $target;
312 write_irssi('No such getter');
314 write_irssi('Current getter: %s', $conf->{'videosite'}->{'getter'});
321 if (defined($mode)) {
323 if (('download' eq $mode) or ('display' eq $mode)) {
324 $conf->{'videosite'}->{'mode'} = $mode;
326 write_irssi('Invalid mode: %s', $mode);
329 write_irssi('Current mode: %s', $conf->{'videosite'}->{'mode'});
335 sub sig_command_script_unload {
337 if ($script =~ /(.*\/)?videosite(\.pl)?$/) {
352 opendir(D, $dir) || return ();
353 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
357 write_debug("Trying to load $p:");
359 eval qq{ require videosite::$p; };
361 write_irssi("Failed to load plugin: $@");
365 $g = eval qq{ videosite::$p->new(); };
367 write_irssi("Failed to instanciate: $@");
372 write_debug("found $g->{'TYPE'} $g->{'NAME'}");
373 if ($type eq $g->{'TYPE'}) {
375 $g->setio(\&write_irssi);
377 write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
382 write_debug("Loaded %d plugins", $#g+1);
387 sub _load_modules($) {
391 foreach (keys(%INC)) {
392 if ($INC{$_} =~ m|^$path|) {
393 write_debug("Removing %s from \$INC", $_);
397 @grabbers = ploader($path, '.*Grabber\.pm$', 'grabber');
398 @getters = ploader($path, '.*Getter\.pm$', 'getter');
404 my $bindings = shift;
407 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
408 # No config, start with an empty one
409 write_debug('No config found, using defaults');
410 $conf = { 'videosite' => { }};
412 foreach (keys(%{$PARAMS})) {
413 unless (exists($conf->{'videosite'}->{$_})) {
414 $conf->{'videosite'}->{$_} = $PARAMS->{$_};
418 _load_modules($plugindir);
420 unless (defined(@grabbers) && defined(@getters)) {
421 write_irssi('No grabbers or no getters found, can not proceed.');
425 $getter = $getters[0];
426 foreach $p (@getters) {
427 if ($conf->{'videosite'}->{'getter'} eq $p->{'NAME'}) {
431 write_debug('Selected %s as getter', $getter->{'NAME'});
432 $conf->{'videosite'}->{'getter'} = $getter->{'NAME'};
434 # Loop through all plugins and load the config
435 foreach $p (@grabbers, @getters) {
436 $conf->{'videosite'}->{'config'}->{$p->{'NAME'}} = $p->mergeconfig($conf->{'videosite'}->{'config'}->{$p->{'NAME'}});
441 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
442 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
443 Irssi::signal_add('setup saved', 'cmd_save');
446 Irssi::command_bind(_bcb('videosite' => \&cmdhandler));
449 write_irssi('initialized successfully');
453 my ($complist, $window, $word, $linestart, $want_space) = @_;
456 if ($linestart !~ m|^/videosite\b|) {
460 if ('/videosite' eq $linestart) {
461 # No command enterd so far. Produce a list of possible follow-ups
462 @matches = grep {/^$word/} keys (%{$videosite_commands});
463 } elsif ('/videosite set' eq $linestart) {
464 # 'set' command entered. Produce a list of modules
465 foreach (@grabbers, @getters) {
466 push(@matches, $_->{'NAME'}) if $_->{'NAME'} =~ m|^$word|;
468 } elsif ($linestart =~ m|^/videosite set (\w+)$|) {
471 foreach my $p (@getters, @grabbers) {
472 if ($p->{'NAME'} eq $module) {
473 @matches = $p->getparamlist($word);
477 } elsif ($linestart =~ m|/videosite set (\w+) (\w+)$|) {
481 foreach my $p (@getters, @grabbers) {
482 if ($p->{'NAME'} eq $module) {
483 @matches = $p->getparamvalues($param, $word);
490 push(@{$complist}, sort @matches);
493 Irssi::signal_stop();
498 my ($cmd, @params) = split(/\s+/, $event->message());
500 if (exists($videosite_commands->{$cmd})) {
501 $videosite_commands->{$cmd}->(@params);
505 unshift(@INC, $scriptdir);