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 'otf\.me/[[:alnum:]]+',
157 'hel\.me/[[:alnum:]]+',
158 '7ax\.de/[[:alnum:]]+',
159 'ow\.ly/[[:alnum:]]+',
160 'j\.mp/[[:alnum:]]+',
161 'bit\.ly/[[:alnum:]]+',
162 'tinyurl\.com/[[:alnum:]]+',
164 my $ua = LWP::UserAgent->new(agent => 'Mozilla', max_redirect => 0);
167 OUTER: while (($os ne $s) and ($i > 0)) {
171 foreach my $pattern (@urlshortener) {
172 my $p = "https?:\/\/" . $pattern;
174 write_debug("Matching %s against %s", $p, $s);
179 write_debug("Found %s", $matched);
180 $res = $ua->head($matched);
181 if ($res->is_redirect()) {
182 my $new = $res->headers()->header("Location");
184 write_debug("Replacing %s with %s", $matched, $new);
185 $s =~ s/$matched/$new/;
188 write_debug("Error resolving %s", $matched);
195 write_debug("Loop terminated by counter");
198 write_debug("Final string: %s", $s);
206 my $message = $event->message();
207 my $witem = $event->channel();
213 # Look if we should ignore this line
214 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
218 push_output($event->ewpf);
219 $message = expand_url_shortener($message);
223 # Offer the message to all Grabbers in turn
224 GRABBER: foreach $g (@grabbers) {
225 ($m, $p) = $g->get($message);
226 while (defined($m)) {
227 write_debug('Metadata: %s', Dumper($m));
228 if ('download' eq ($conf->{'videosite'}->{'mode'})) {
229 write_irssi('%%R>>> %%NSaving %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
230 unless($getter->get($m)) {
231 write_irssi('%%R>>> FAILED');
233 } elsif ('display' eq ($conf->{'videosite'}->{'mode'})) {
234 write_irssi('%%M>>> %%NSaw %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
236 write_irssi('%%R>>> Invalid operation mode');
239 # Remove the matched part from the message and try again (there may be
243 last GRABBER if ($message =~ /^\s*$/);
245 ($m, $p) = $g->get($message);
255 open(CONF, '>'.$conffile) or die 'Could not open config file';
256 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
260 write_irssi('Could not save config to %s: %s', ($conffile, $@));
262 write_irssi('configuration saved to %s', $conffile);
272 foreach $p (@getters, @grabbers) {
273 if ($p->{'NAME'} eq $target) {
274 $p->setval($key, $val);
278 write_irssi('No such module');
286 foreach $p (@grabbers) {
287 if ($p->{'NAME'} eq $target) {
292 write_irssi('No such module');
300 foreach $p (@grabbers) {
301 if ($p->{'NAME'} eq $target) {
306 write_irssi('No such module');
315 if (defined($target)) {
316 foreach $p (@getters, @grabbers) {
317 if ($p->{'NAME'} eq $target) {
318 write_irssi($p->getconfstr());
322 write_irssi('No such module');
324 write_irssi('Loaded grabbers (* denotes enabled modules):');
325 foreach $p (@grabbers) {
326 $e = $p->_getval('enabled');
327 write_irssi(' %s%s', $p->{'NAME'}, $e?'*':'');
330 write_irssi('Loaded getters:');
331 foreach $p (@getters) {
332 write_irssi(' %s', $p->{'NAME'});
341 if (defined($target)) {
342 foreach $p (@getters, @grabbers) {
343 if ($p->{'NAME'} eq $target) {
344 write_irssi($p->gethelpstr());
348 write_irssi('No such module');
350 write_irssi(<<'EOT');
352 save: save the current configuration
353 help [modulename]: display this help, or module specific help
354 show [modulename]: show loaded modules, or the current parameters of a module
355 set modulename parameter value: set a module parameter to a new value
356 getter [modulename]: display or set the getter to use
357 enable [modulename]: enable the usage of this module (grabbers only)
358 disable [modulename]: disable the usage of this module (grabbers only)
359 reload: reload all modules (this is somewhat experimental)
360 mode [modename]: display or set the operation mode (download/display)
361 debug: enable debugging messages
362 nodebug: disable debugging messages
371 if (defined($target)) {
372 foreach $p (@getters) {
373 if ($p->{'NAME'} eq $target) {
375 $conf->{'videosite'}->{'getter'} = $target;
376 write_irssi("Getter changed to %s", $target);
380 write_irssi('No such getter');
382 write_irssi('Current getter: %s', $conf->{'videosite'}->{'getter'});
389 if (defined($mode)) {
391 if (('download' eq $mode) or ('display' eq $mode)) {
392 $conf->{'videosite'}->{'mode'} = $mode;
393 write_irssi('Now using %s mode', $mode);
395 write_irssi('Invalid mode: %s', $mode);
398 write_irssi('Current mode: %s', $conf->{'videosite'}->{'mode'});
404 sub sig_command_script_unload {
406 if ($script =~ /(.*\/)?videosite(\.pl)?$/) {
421 opendir(D, $dir) || return ();
422 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
426 write_debug("Trying to load $p:");
428 eval qq{ require videosite::$p; };
430 write_irssi("Failed to load plugin: $@");
434 $g = eval qq{ videosite::$p->new(); };
436 write_irssi("Failed to instanciate: $@");
441 write_debug("found $g->{'TYPE'} $g->{'NAME'}");
442 if ($type eq $g->{'TYPE'}) {
444 $g->setio(\&write_irssi);
446 write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
451 write_debug("Loaded %d plugins", $#g+1);
456 sub _load_modules($) {
460 foreach (keys(%INC)) {
461 if ($INC{$_} =~ m|^$path|) {
462 write_debug("Removing %s from \$INC", $_);
466 @grabbers = ploader($path, '.*Grabber\.pm$', 'grabber');
467 @getters = ploader($path, '.*Getter\.pm$', 'getter');
473 my $bindings = shift;
476 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
477 # No config, start with an empty one
478 write_debug('No config found, using defaults');
479 $conf = { 'videosite' => { }};
481 foreach (keys(%{$PARAMS})) {
482 unless (exists($conf->{'videosite'}->{$_})) {
483 $conf->{'videosite'}->{$_} = $PARAMS->{$_};
487 _load_modules($plugindir);
489 unless (defined(@grabbers) && defined(@getters)) {
490 write_irssi('No grabbers or no getters found, can not proceed.');
494 $getter = $getters[0];
495 foreach $p (@getters) {
496 if ($conf->{'videosite'}->{'getter'} eq $p->{'NAME'}) {
500 write_debug('Selected %s as getter', $getter->{'NAME'});
501 $conf->{'videosite'}->{'getter'} = $getter->{'NAME'};
503 # Loop through all plugins and load the config
504 foreach $p (@grabbers, @getters) {
505 $conf->{'videosite'}->{'config'}->{$p->{'NAME'}} = $p->mergeconfig($conf->{'videosite'}->{'config'}->{$p->{'NAME'}});
510 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
511 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
512 Irssi::signal_add('setup saved', 'cmd_save');
515 Irssi::command_bind(_bcb('videosite' => \&cmdhandler));
518 write_irssi('initialized successfully');
522 my ($complist, $window, $word, $linestart, $want_space) = @_;
525 if ($linestart !~ m|^/videosite\b|) {
529 if ('/videosite' eq $linestart) {
530 # No command enterd so far. Produce a list of possible follow-ups
531 @matches = grep {/^$word/} keys (%{$videosite_commands});
532 } elsif ('/videosite set' eq $linestart) {
533 # 'set' command entered. Produce a list of modules
534 foreach (@grabbers, @getters) {
535 push(@matches, $_->{'NAME'}) if $_->{'NAME'} =~ m|^$word|;
537 } elsif ($linestart =~ m|^/videosite set (\w+)$|) {
540 foreach my $p (@getters, @grabbers) {
541 if ($p->{'NAME'} eq $module) {
542 @matches = $p->getparamlist($word);
546 } elsif ($linestart =~ m|/videosite set (\w+) (\w+)$|) {
550 foreach my $p (@getters, @grabbers) {
551 if ($p->{'NAME'} eq $module) {
552 @matches = $p->getparamvalues($param, $word);
559 push(@{$complist}, sort @matches);
562 Irssi::signal_stop();
567 my ($cmd, @params) = split(/\s+/, $event->message());
569 push_output($event->ewpf);
571 if (exists($videosite_commands->{$cmd})) {
572 $videosite_commands->{$cmd}->(@params);
578 unshift(@INC, $scriptdir);