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 File::Temp qw(tempfile);
26 use BettIrssi 101 qw(_bcb _bcs);
33 my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.xml');
34 my $scriptdir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts');
35 my $plugindir = File::Spec->catfile($scriptdir, 'videosite');
36 my @outputstack = (undef);
47 # "message public", SERVER_REC, char *msg, char *nick, char *address, char *target
48 signal_add_last(_bcs("message public" => sub {check_for_link(@_)}));
49 # "message own_public", SERVER_REC, char *msg, char *target
50 signal_add_last(_bcs("message own_public" => sub {check_for_link(@_)}));
52 # "message private", SERVER_REC, char *msg, char *nick, char *address
53 signal_add_last(_bcs("message private" => sub {check_for_link(@_)}));
54 # "message own_private", SERVER_REC, char *msg, char *target, char *orig_target
55 signal_add_last(_bcs("message own_private" => sub {check_for_link(@_)}));
57 # "message irc action", SERVER_REC, char *msg, char *nick, char *address, char *target
58 signal_add_last(_bcs("message irc action" => sub {check_for_link(@_)}));
59 # "message irc own_action", SERVER_REC, char *msg, char *target
60 signal_add_last(_bcs("message irc own_action" => sub {check_for_link(@_)}));
63 # This does not use BettIrssi (yet)
64 signal_add_first('complete word', \&sig_complete);
67 unshift(@outputstack, shift);
73 @outputstack = (undef) unless (@outputstack);
76 my $videosite_commands = {
115 foreach (@grabbers, @getters) {
118 write_irssi('Enabled debugging');
123 foreach (@grabbers, @getters) {
126 write_irssi('Disabled debugging');
132 my $output = $outputstack[0];
134 my $format = "%%mvideosite: %%n" . shift(@text);
136 # escape % in parameters from irssi
137 s/%/%%/g foreach @text;
139 if (defined $output) {
140 $output->(sprintf($format, @text), MSGLEVEL_CLIENTCRAP);
142 Irssi::print(sprintf($format, @text));
153 sub expand_url_shortener {
157 'is\.gd/[[:alnum:]]+',
158 'otf\.me/[[:alnum:]]+',
159 'hel\.me/[[:alnum:]]+',
160 '7ax\.de/[[:alnum:]]+',
161 'ow\.ly/[[:alnum:]]+',
162 'j\.mp/[[:alnum:]]+',
163 'bit\.ly/[[:alnum:]]+',
164 'tinyurl\.com/[[:alnum:]]+',
165 'pop\.is/[[:alnum:]]+',
166 'post\.ly/[[:alnum:]]+',
167 '1\.ly/[[:alnum:]]+',
168 '2\.ly/[[:alnum:]]+',
169 't\.co/[[:alnum:]]+',
170 'shar\.es/[[:alnum:]]+',
171 'goo\.gl/[[:alnum:]]+',
173 my $ua = LWP::UserAgent->new(agent => 'Mozilla', max_redirect => 0);
176 OUTER: while (($os ne $s) and ($i > 0)) {
181 foreach my $pattern (@urlshortener) {
182 my $p = "https?:\/\/" . $pattern;
184 write_debug("Matching %s against %s", $p, $s);
189 write_debug("Found %s", $matched);
190 $res = $ua->head($matched);
191 if ($res->is_redirect()) {
192 my $new = $res->headers()->header("Location");
194 write_debug("Replacing %s with %s", $matched, $new);
195 $s =~ s/$matched/$new/;
198 write_debug("Error resolving %s", $matched);
205 write_debug("Loop terminated by counter");
208 write_debug("Final string: %s", $s);
216 my $message = $event->message();
217 my $witem = $event->channel();
223 # Look if we should ignore this line
224 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
228 push_output($event->ewpf);
229 $message = expand_url_shortener($message);
233 # Offer the message to all Grabbers in turn
234 GRABBER: foreach $g (@grabbers) {
235 ($m, $p) = $g->get($message);
236 while (defined($m)) {
237 write_debug('Metadata: %s', Dumper($m));
238 if ('download' eq ($conf->{'videosite'}->{'mode'})) {
239 write_irssi('%%R>>> %%NSaving %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
240 unless($getter->get($m)) {
241 write_irssi('%%R>>> FAILED');
243 } elsif ('display' eq ($conf->{'videosite'}->{'mode'})) {
244 write_irssi('%%M>>> %%NSaw %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
246 write_irssi('%%R>>> Invalid operation mode');
249 # Remove the matched part from the message and try again (there may be
253 last GRABBER if ($message =~ /^\s*$/);
255 ($m, $p) = $g->get($message);
266 my ($tempfile, $tempfn) = tempfile("videosite.xml.XXXXXX", dir => Irssi::get_irssi_dir());
267 print $tempfile XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
269 rename($tempfn, $conffile);
272 write_irssi('Could not save config to %s: %s', ($conffile, $@));
274 write_irssi('configuration saved to %s', $conffile);
284 foreach $p (@getters, @grabbers) {
285 if ($p->{'NAME'} eq $target) {
286 $p->setval($key, $val);
290 write_irssi('No such module');
298 foreach $p (@grabbers) {
299 if ($p->{'NAME'} eq $target) {
304 write_irssi('No such module');
312 foreach $p (@grabbers) {
313 if ($p->{'NAME'} eq $target) {
318 write_irssi('No such module');
327 if (defined($target)) {
328 foreach $p (@getters, @grabbers) {
329 if ($p->{'NAME'} eq $target) {
330 write_irssi($p->getconfstr());
334 write_irssi('No such module');
336 write_irssi('Loaded grabbers (* denotes enabled modules):');
337 foreach $p (@grabbers) {
338 $e = $p->_getval('enabled');
339 write_irssi(' %s%s', $p->{'NAME'}, $e?'*':'');
342 write_irssi('Loaded getters:');
343 foreach $p (@getters) {
344 write_irssi(' %s', $p->{'NAME'});
353 if (defined($target)) {
354 foreach $p (@getters, @grabbers) {
355 if ($p->{'NAME'} eq $target) {
356 write_irssi($p->gethelpstr());
360 write_irssi('No such module');
362 write_irssi(<<'EOT');
364 save: save the current configuration
365 help [modulename]: display this help, or module specific help
366 show [modulename]: show loaded modules, or the current parameters of a module
367 set modulename parameter value: set a module parameter to a new value
368 getter [modulename]: display or set the getter to use
369 enable [modulename]: enable the usage of this module (grabbers only)
370 disable [modulename]: disable the usage of this module (grabbers only)
371 reload: reload all modules (this is somewhat experimental)
372 mode [modename]: display or set the operation mode (download/display)
373 debug: enable debugging messages
374 nodebug: disable debugging messages
383 if (defined($target)) {
384 foreach $p (@getters) {
385 if ($p->{'NAME'} eq $target) {
387 $conf->{'videosite'}->{'getter'} = $target;
388 write_irssi("Getter changed to %s", $target);
392 write_irssi('No such getter');
394 write_irssi('Current getter: %s', $conf->{'videosite'}->{'getter'});
401 if (defined($mode)) {
403 if (('download' eq $mode) or ('display' eq $mode)) {
404 $conf->{'videosite'}->{'mode'} = $mode;
405 write_irssi('Now using %s mode', $mode);
407 write_irssi('Invalid mode: %s', $mode);
410 write_irssi('Current mode: %s', $conf->{'videosite'}->{'mode'});
416 sub sig_command_script_unload {
418 if ($script =~ /(.*\/)?videosite(\.pl)?$/) {
433 opendir(D, $dir) || return ();
434 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
438 write_debug("Trying to load $p:");
440 eval qq{ require videosite::$p; };
442 write_irssi("Failed to load plugin: $@");
446 $g = eval qq{ videosite::$p->new(); };
448 write_irssi("Failed to instanciate: $@");
453 write_debug("found $g->{'TYPE'} $g->{'NAME'}");
454 if ($type eq $g->{'TYPE'}) {
456 $g->setio(\&write_irssi);
458 write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
463 write_debug("Loaded %d plugins", $#g+1);
468 sub _load_modules($) {
472 foreach (keys(%INC)) {
473 if ($INC{$_} =~ m|^$path|) {
474 write_debug("Removing %s from \$INC", $_);
478 @grabbers = ploader($path, '.*Grabber\.pm$', 'grabber');
479 @getters = ploader($path, '.*Getter\.pm$', 'getter');
485 my $bindings = shift;
488 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
489 # No config, start with an empty one
490 write_debug('No config found, using defaults');
491 $conf = { 'videosite' => { }};
493 foreach (keys(%{$PARAMS})) {
494 unless (exists($conf->{'videosite'}->{$_})) {
495 $conf->{'videosite'}->{$_} = $PARAMS->{$_};
499 _load_modules($plugindir);
501 unless (defined(@grabbers) && defined(@getters)) {
502 write_irssi('No grabbers or no getters found, can not proceed.');
506 $getter = $getters[0];
507 foreach $p (@getters) {
508 if ($conf->{'videosite'}->{'getter'} eq $p->{'NAME'}) {
512 write_debug('Selected %s as getter', $getter->{'NAME'});
513 $conf->{'videosite'}->{'getter'} = $getter->{'NAME'};
515 # Loop through all plugins and load the config
516 foreach $p (@grabbers, @getters) {
517 $conf->{'videosite'}->{'config'}->{$p->{'NAME'}} = $p->mergeconfig($conf->{'videosite'}->{'config'}->{$p->{'NAME'}});
522 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
523 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
524 Irssi::signal_add('setup saved', 'cmd_save');
527 Irssi::command_bind(_bcb('videosite' => \&cmdhandler));
530 write_irssi('initialized successfully');
534 my ($complist, $window, $word, $linestart, $want_space) = @_;
537 if ($linestart !~ m|^/videosite\b|) {
541 if ('/videosite' eq $linestart) {
542 # No command enterd so far. Produce a list of possible follow-ups
543 @matches = grep {/^$word/} keys (%{$videosite_commands});
544 } elsif ('/videosite set' eq $linestart) {
545 # 'set' command entered. Produce a list of modules
546 foreach (@grabbers, @getters) {
547 push(@matches, $_->{'NAME'}) if $_->{'NAME'} =~ m|^$word|;
549 } elsif ($linestart =~ m|^/videosite set (\w+)$|) {
552 foreach my $p (@getters, @grabbers) {
553 if ($p->{'NAME'} eq $module) {
554 @matches = $p->getparamlist($word);
558 } elsif ($linestart =~ m|/videosite set (\w+) (\w+)$|) {
562 foreach my $p (@getters, @grabbers) {
563 if ($p->{'NAME'} eq $module) {
564 @matches = $p->getparamvalues($param, $word);
571 push(@{$complist}, sort @matches);
574 Irssi::signal_stop();
579 my ($cmd, @params) = split(/\s+/, $event->message());
581 push_output($event->ewpf);
583 if (exists($videosite_commands->{$cmd})) {
584 $videosite_commands->{$cmd}->(@params);
590 unshift(@INC, $scriptdir);