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 # Get rid of a (possibly old) version of BettIrssi
13 # This is a hack to prevent having to reload irssi just
14 # because BettIrssi.pm changed
16 delete($INC{'BettIrssi.pm'});
20 use Irssi 20020324 qw (command_bind command_runsub signal_add_first signal_add_last);
21 use vars qw($VERSION %IRSSI);
25 use BettIrssi 101 qw(_bcb _bcs);
32 my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.xml');
33 my $scriptdir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts');
34 my $plugindir = File::Spec->catfile($scriptdir, 'videosite');
35 my @outputstack = (undef);
46 # "message public", SERVER_REC, char *msg, char *nick, char *address, char *target
47 signal_add_last(_bcs("message public" => sub {check_for_link(@_)}));
48 # "message own_public", SERVER_REC, char *msg, char *target
49 signal_add_last(_bcs("message own_public" => sub {check_for_link(@_)}));
51 # "message private", SERVER_REC, char *msg, char *nick, char *address
52 signal_add_last(_bcs("message private" => sub {check_for_link(@_)}));
53 # "message own_private", SERVER_REC, char *msg, char *target, char *orig_target
54 signal_add_last(_bcs("message own_private" => sub {check_for_link(@_)}));
56 # "message irc action", SERVER_REC, char *msg, char *nick, char *address, char *target
57 signal_add_last(_bcs("message irc action" => sub {check_for_link(@_)}));
58 # "message irc own_action", SERVER_REC, char *msg, char *target
59 signal_add_last(_bcs("message irc own_action" => sub {check_for_link(@_)}));
62 # This does not use BettIrssi (yet)
63 signal_add_first('complete word', \&sig_complete);
66 unshift(@outputstack, shift);
72 @outputstack = (undef) unless (@outputstack);
75 my $videosite_commands = {
114 foreach (@grabbers, @getters) {
117 write_irssi('Enabled debugging');
122 foreach (@grabbers, @getters) {
125 write_irssi('Disabled debugging');
131 my $output = $outputstack[0];
133 my $format = "%%mvideosite: %%n" . shift(@text);
135 # escape % in parameters from irssi
136 s/%/%%/g foreach @text;
138 if (defined $output) {
139 $output->(sprintf($format, @text), MSGLEVEL_CLIENTCRAP);
141 Irssi::print(sprintf($format, @text));
152 sub expand_url_shortener {
156 'j\.mp/[[:alnum:]]+',
157 'bit\.ly/[[:alnum:]]+',
158 'tinyurl\.com/[[:alnum:]]+',
160 my $ua = LWP::UserAgent->new(agent => 'Mozilla', max_redirect => 0);
163 OUTER: while (($os ne $s) and ($i > 0)) {
167 foreach my $pattern (@urlshortener) {
168 my $p = "https?:\/\/" . $pattern;
170 write_debug("Matching %s against %s", $p, $s);
175 write_debug("Found %s", $matched);
176 $res = $ua->head($matched);
177 if ($res->is_redirect()) {
178 my $new = $res->headers()->header("Location");
180 write_debug("Replacing %s with %s", $matched, $new);
181 $s =~ s/$matched/$new/;
184 write_debug("Error resolving %s", $matched);
191 write_debug("Loop terminated by counter");
194 write_debug("Final string: %s", $s);
202 my $message = $event->message();
203 my $witem = $event->channel();
209 # Look if we should ignore this line
210 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
214 push_output($event->ewpf);
215 $message = expand_url_shortener($message);
219 # Offer the message to all Grabbers in turn
220 GRABBER: foreach $g (@grabbers) {
221 ($m, $p) = $g->get($message);
222 while (defined($m)) {
223 write_debug('Metadata: %s', Dumper($m));
224 if ('download' eq ($conf->{'videosite'}->{'mode'})) {
225 write_irssi('%%R>>> %%NSaving %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
226 unless($getter->get($m)) {
227 write_irssi('%%R>>> FAILED');
229 } elsif ('display' eq ($conf->{'videosite'}->{'mode'})) {
230 write_irssi('%%M>>> %%NSaw %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
232 write_irssi('%%R>>> Invalid operation mode');
235 # Remove the matched part from the message and try again (there may be
239 last GRABBER if ($message =~ /^\s*$/);
241 ($m, $p) = $g->get($message);
251 open(CONF, '>'.$conffile) or die 'Could not open config file';
252 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
256 write_irssi('Could not save config to %s: %s', ($conffile, $@));
258 write_irssi('configuration saved to %s', $conffile);
268 foreach $p (@getters, @grabbers) {
269 if ($p->{'NAME'} eq $target) {
270 $p->setval($key, $val);
274 write_irssi('No such module');
282 foreach $p (@grabbers) {
283 if ($p->{'NAME'} eq $target) {
288 write_irssi('No such module');
296 foreach $p (@grabbers) {
297 if ($p->{'NAME'} eq $target) {
302 write_irssi('No such module');
311 if (defined($target)) {
312 foreach $p (@getters, @grabbers) {
313 if ($p->{'NAME'} eq $target) {
314 write_irssi($p->getconfstr());
318 write_irssi('No such module');
320 write_irssi('Loaded grabbers (* denotes enabled modules):');
321 foreach $p (@grabbers) {
322 $e = $p->_getval('enabled');
323 write_irssi(' %s%s', $p->{'NAME'}, $e?'*':'');
326 write_irssi('Loaded getters:');
327 foreach $p (@getters) {
328 write_irssi(' %s', $p->{'NAME'});
337 if (defined($target)) {
338 foreach $p (@getters, @grabbers) {
339 if ($p->{'NAME'} eq $target) {
340 write_irssi($p->gethelpstr());
344 write_irssi('No such module');
346 write_irssi(<<'EOT');
348 save: save the current configuration
349 help [modulename]: display this help, or module specific help
350 show [modulename]: show loaded modules, or the current parameters of a module
351 set modulename parameter value: set a module parameter to a new value
352 getter [modulename]: display or set the getter to use
353 enable [modulename]: enable the usage of this module (grabbers only)
354 disable [modulename]: disable the usage of this module (grabbers only)
355 reload: reload all modules (this is somewhat experimental)
356 mode [modename]: display or set the operation mode (download/display)
357 debug: enable debugging messages
358 nodebug: disable debugging messages
367 if (defined($target)) {
368 foreach $p (@getters) {
369 if ($p->{'NAME'} eq $target) {
371 $conf->{'videosite'}->{'getter'} = $target;
372 write_irssi("Getter changed to %s", $target);
376 write_irssi('No such getter');
378 write_irssi('Current getter: %s', $conf->{'videosite'}->{'getter'});
385 if (defined($mode)) {
387 if (('download' eq $mode) or ('display' eq $mode)) {
388 $conf->{'videosite'}->{'mode'} = $mode;
389 write_irssi('Now using %s mode', $mode);
391 write_irssi('Invalid mode: %s', $mode);
394 write_irssi('Current mode: %s', $conf->{'videosite'}->{'mode'});
400 sub sig_command_script_unload {
402 if ($script =~ /(.*\/)?videosite(\.pl)?$/) {
417 opendir(D, $dir) || return ();
418 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
422 write_debug("Trying to load $p:");
424 eval qq{ require videosite::$p; };
426 write_irssi("Failed to load plugin: $@");
430 $g = eval qq{ videosite::$p->new(); };
432 write_irssi("Failed to instanciate: $@");
437 write_debug("found $g->{'TYPE'} $g->{'NAME'}");
438 if ($type eq $g->{'TYPE'}) {
440 $g->setio(\&write_irssi);
442 write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
447 write_debug("Loaded %d plugins", $#g+1);
452 sub _load_modules($) {
456 foreach (keys(%INC)) {
457 if ($INC{$_} =~ m|^$path|) {
458 write_debug("Removing %s from \$INC", $_);
462 @grabbers = ploader($path, '.*Grabber\.pm$', 'grabber');
463 @getters = ploader($path, '.*Getter\.pm$', 'getter');
469 my $bindings = shift;
472 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
473 # No config, start with an empty one
474 write_debug('No config found, using defaults');
475 $conf = { 'videosite' => { }};
477 foreach (keys(%{$PARAMS})) {
478 unless (exists($conf->{'videosite'}->{$_})) {
479 $conf->{'videosite'}->{$_} = $PARAMS->{$_};
483 _load_modules($plugindir);
485 unless (defined(@grabbers) && defined(@getters)) {
486 write_irssi('No grabbers or no getters found, can not proceed.');
490 $getter = $getters[0];
491 foreach $p (@getters) {
492 if ($conf->{'videosite'}->{'getter'} eq $p->{'NAME'}) {
496 write_debug('Selected %s as getter', $getter->{'NAME'});
497 $conf->{'videosite'}->{'getter'} = $getter->{'NAME'};
499 # Loop through all plugins and load the config
500 foreach $p (@grabbers, @getters) {
501 $conf->{'videosite'}->{'config'}->{$p->{'NAME'}} = $p->mergeconfig($conf->{'videosite'}->{'config'}->{$p->{'NAME'}});
506 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
507 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
508 Irssi::signal_add('setup saved', 'cmd_save');
511 Irssi::command_bind(_bcb('videosite' => \&cmdhandler));
514 write_irssi('initialized successfully');
518 my ($complist, $window, $word, $linestart, $want_space) = @_;
521 if ($linestart !~ m|^/videosite\b|) {
525 if ('/videosite' eq $linestart) {
526 # No command enterd so far. Produce a list of possible follow-ups
527 @matches = grep {/^$word/} keys (%{$videosite_commands});
528 } elsif ('/videosite set' eq $linestart) {
529 # 'set' command entered. Produce a list of modules
530 foreach (@grabbers, @getters) {
531 push(@matches, $_->{'NAME'}) if $_->{'NAME'} =~ m|^$word|;
533 } elsif ($linestart =~ m|^/videosite set (\w+)$|) {
536 foreach my $p (@getters, @grabbers) {
537 if ($p->{'NAME'} eq $module) {
538 @matches = $p->getparamlist($word);
542 } elsif ($linestart =~ m|/videosite set (\w+) (\w+)$|) {
546 foreach my $p (@getters, @grabbers) {
547 if ($p->{'NAME'} eq $module) {
548 @matches = $p->getparamvalues($param, $word);
555 push(@{$complist}, sort @matches);
558 Irssi::signal_stop();
563 my ($cmd, @params) = split(/\s+/, $event->message());
565 push_output($event->ewpf);
567 if (exists($videosite_commands->{$cmd})) {
568 $videosite_commands->{$cmd}->(@params);
574 unshift(@INC, $scriptdir);