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 'hel\.me/[[:alnum:]]+',
157 '7ax\.de/[[:alnum:]]+',
158 'ow\.ly/[[:alnum:]]+',
159 'j\.mp/[[:alnum:]]+',
160 'bit\.ly/[[:alnum:]]+',
161 'tinyurl\.com/[[:alnum:]]+',
163 my $ua = LWP::UserAgent->new(agent => 'Mozilla', max_redirect => 0);
166 OUTER: while (($os ne $s) and ($i > 0)) {
170 foreach my $pattern (@urlshortener) {
171 my $p = "https?:\/\/" . $pattern;
173 write_debug("Matching %s against %s", $p, $s);
178 write_debug("Found %s", $matched);
179 $res = $ua->head($matched);
180 if ($res->is_redirect()) {
181 my $new = $res->headers()->header("Location");
183 write_debug("Replacing %s with %s", $matched, $new);
184 $s =~ s/$matched/$new/;
187 write_debug("Error resolving %s", $matched);
194 write_debug("Loop terminated by counter");
197 write_debug("Final string: %s", $s);
205 my $message = $event->message();
206 my $witem = $event->channel();
212 # Look if we should ignore this line
213 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
217 push_output($event->ewpf);
218 $message = expand_url_shortener($message);
222 # Offer the message to all Grabbers in turn
223 GRABBER: foreach $g (@grabbers) {
224 ($m, $p) = $g->get($message);
225 while (defined($m)) {
226 write_debug('Metadata: %s', Dumper($m));
227 if ('download' eq ($conf->{'videosite'}->{'mode'})) {
228 write_irssi('%%R>>> %%NSaving %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
229 unless($getter->get($m)) {
230 write_irssi('%%R>>> FAILED');
232 } elsif ('display' eq ($conf->{'videosite'}->{'mode'})) {
233 write_irssi('%%M>>> %%NSaw %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
235 write_irssi('%%R>>> Invalid operation mode');
238 # Remove the matched part from the message and try again (there may be
242 last GRABBER if ($message =~ /^\s*$/);
244 ($m, $p) = $g->get($message);
254 open(CONF, '>'.$conffile) or die 'Could not open config file';
255 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
259 write_irssi('Could not save config to %s: %s', ($conffile, $@));
261 write_irssi('configuration saved to %s', $conffile);
271 foreach $p (@getters, @grabbers) {
272 if ($p->{'NAME'} eq $target) {
273 $p->setval($key, $val);
277 write_irssi('No such module');
285 foreach $p (@grabbers) {
286 if ($p->{'NAME'} eq $target) {
291 write_irssi('No such module');
299 foreach $p (@grabbers) {
300 if ($p->{'NAME'} eq $target) {
305 write_irssi('No such module');
314 if (defined($target)) {
315 foreach $p (@getters, @grabbers) {
316 if ($p->{'NAME'} eq $target) {
317 write_irssi($p->getconfstr());
321 write_irssi('No such module');
323 write_irssi('Loaded grabbers (* denotes enabled modules):');
324 foreach $p (@grabbers) {
325 $e = $p->_getval('enabled');
326 write_irssi(' %s%s', $p->{'NAME'}, $e?'*':'');
329 write_irssi('Loaded getters:');
330 foreach $p (@getters) {
331 write_irssi(' %s', $p->{'NAME'});
340 if (defined($target)) {
341 foreach $p (@getters, @grabbers) {
342 if ($p->{'NAME'} eq $target) {
343 write_irssi($p->gethelpstr());
347 write_irssi('No such module');
349 write_irssi(<<'EOT');
351 save: save the current configuration
352 help [modulename]: display this help, or module specific help
353 show [modulename]: show loaded modules, or the current parameters of a module
354 set modulename parameter value: set a module parameter to a new value
355 getter [modulename]: display or set the getter to use
356 enable [modulename]: enable the usage of this module (grabbers only)
357 disable [modulename]: disable the usage of this module (grabbers only)
358 reload: reload all modules (this is somewhat experimental)
359 mode [modename]: display or set the operation mode (download/display)
360 debug: enable debugging messages
361 nodebug: disable debugging messages
370 if (defined($target)) {
371 foreach $p (@getters) {
372 if ($p->{'NAME'} eq $target) {
374 $conf->{'videosite'}->{'getter'} = $target;
375 write_irssi("Getter changed to %s", $target);
379 write_irssi('No such getter');
381 write_irssi('Current getter: %s', $conf->{'videosite'}->{'getter'});
388 if (defined($mode)) {
390 if (('download' eq $mode) or ('display' eq $mode)) {
391 $conf->{'videosite'}->{'mode'} = $mode;
392 write_irssi('Now using %s mode', $mode);
394 write_irssi('Invalid mode: %s', $mode);
397 write_irssi('Current mode: %s', $conf->{'videosite'}->{'mode'});
403 sub sig_command_script_unload {
405 if ($script =~ /(.*\/)?videosite(\.pl)?$/) {
420 opendir(D, $dir) || return ();
421 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
425 write_debug("Trying to load $p:");
427 eval qq{ require videosite::$p; };
429 write_irssi("Failed to load plugin: $@");
433 $g = eval qq{ videosite::$p->new(); };
435 write_irssi("Failed to instanciate: $@");
440 write_debug("found $g->{'TYPE'} $g->{'NAME'}");
441 if ($type eq $g->{'TYPE'}) {
443 $g->setio(\&write_irssi);
445 write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
450 write_debug("Loaded %d plugins", $#g+1);
455 sub _load_modules($) {
459 foreach (keys(%INC)) {
460 if ($INC{$_} =~ m|^$path|) {
461 write_debug("Removing %s from \$INC", $_);
465 @grabbers = ploader($path, '.*Grabber\.pm$', 'grabber');
466 @getters = ploader($path, '.*Getter\.pm$', 'getter');
472 my $bindings = shift;
475 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
476 # No config, start with an empty one
477 write_debug('No config found, using defaults');
478 $conf = { 'videosite' => { }};
480 foreach (keys(%{$PARAMS})) {
481 unless (exists($conf->{'videosite'}->{$_})) {
482 $conf->{'videosite'}->{$_} = $PARAMS->{$_};
486 _load_modules($plugindir);
488 unless (defined(@grabbers) && defined(@getters)) {
489 write_irssi('No grabbers or no getters found, can not proceed.');
493 $getter = $getters[0];
494 foreach $p (@getters) {
495 if ($conf->{'videosite'}->{'getter'} eq $p->{'NAME'}) {
499 write_debug('Selected %s as getter', $getter->{'NAME'});
500 $conf->{'videosite'}->{'getter'} = $getter->{'NAME'};
502 # Loop through all plugins and load the config
503 foreach $p (@grabbers, @getters) {
504 $conf->{'videosite'}->{'config'}->{$p->{'NAME'}} = $p->mergeconfig($conf->{'videosite'}->{'config'}->{$p->{'NAME'}});
509 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
510 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
511 Irssi::signal_add('setup saved', 'cmd_save');
514 Irssi::command_bind(_bcb('videosite' => \&cmdhandler));
517 write_irssi('initialized successfully');
521 my ($complist, $window, $word, $linestart, $want_space) = @_;
524 if ($linestart !~ m|^/videosite\b|) {
528 if ('/videosite' eq $linestart) {
529 # No command enterd so far. Produce a list of possible follow-ups
530 @matches = grep {/^$word/} keys (%{$videosite_commands});
531 } elsif ('/videosite set' eq $linestart) {
532 # 'set' command entered. Produce a list of modules
533 foreach (@grabbers, @getters) {
534 push(@matches, $_->{'NAME'}) if $_->{'NAME'} =~ m|^$word|;
536 } elsif ($linestart =~ m|^/videosite set (\w+)$|) {
539 foreach my $p (@getters, @grabbers) {
540 if ($p->{'NAME'} eq $module) {
541 @matches = $p->getparamlist($word);
545 } elsif ($linestart =~ m|/videosite set (\w+) (\w+)$|) {
549 foreach my $p (@getters, @grabbers) {
550 if ($p->{'NAME'} eq $module) {
551 @matches = $p->getparamvalues($param, $word);
558 push(@{$complist}, sort @matches);
561 Irssi::signal_stop();
566 my ($cmd, @params) = split(/\s+/, $event->message());
568 push_output($event->ewpf);
570 if (exists($videosite_commands->{$cmd})) {
571 $videosite_commands->{$cmd}->(@params);
577 unshift(@INC, $scriptdir);