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 'is\.gd/[[:alnum:]]+',
157 'otf\.me/[[:alnum:]]+',
158 'hel\.me/[[:alnum:]]+',
159 '7ax\.de/[[:alnum:]]+',
160 'ow\.ly/[[:alnum:]]+',
161 'j\.mp/[[:alnum:]]+',
162 'bit\.ly/[[:alnum:]]+',
163 'tinyurl\.com/[[:alnum:]]+',
164 'pop\.is/[[:alnum:]]+',
165 'post\.ly/[[:alnum:]]+',
166 '2\.ly/[[:alnum:]]+',
167 't\.co/[[:alnum:]]+',
168 'shar\.es/[[:alnum:]]+',
170 my $ua = LWP::UserAgent->new(agent => 'Mozilla', max_redirect => 0);
173 OUTER: while (($os ne $s) and ($i > 0)) {
178 foreach my $pattern (@urlshortener) {
179 my $p = "https?:\/\/" . $pattern;
181 write_debug("Matching %s against %s", $p, $s);
186 write_debug("Found %s", $matched);
187 $res = $ua->head($matched);
188 if ($res->is_redirect()) {
189 my $new = $res->headers()->header("Location");
191 write_debug("Replacing %s with %s", $matched, $new);
192 $s =~ s/$matched/$new/;
195 write_debug("Error resolving %s", $matched);
202 write_debug("Loop terminated by counter");
205 write_debug("Final string: %s", $s);
213 my $message = $event->message();
214 my $witem = $event->channel();
220 # Look if we should ignore this line
221 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
225 push_output($event->ewpf);
226 $message = expand_url_shortener($message);
230 # Offer the message to all Grabbers in turn
231 GRABBER: foreach $g (@grabbers) {
232 ($m, $p) = $g->get($message);
233 while (defined($m)) {
234 write_debug('Metadata: %s', Dumper($m));
235 if ('download' eq ($conf->{'videosite'}->{'mode'})) {
236 write_irssi('%%R>>> %%NSaving %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
237 unless($getter->get($m)) {
238 write_irssi('%%R>>> FAILED');
240 } elsif ('display' eq ($conf->{'videosite'}->{'mode'})) {
241 write_irssi('%%M>>> %%NSaw %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
243 write_irssi('%%R>>> Invalid operation mode');
246 # Remove the matched part from the message and try again (there may be
250 last GRABBER if ($message =~ /^\s*$/);
252 ($m, $p) = $g->get($message);
262 open(CONF, '>'.$conffile) or die 'Could not open config file';
263 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
267 write_irssi('Could not save config to %s: %s', ($conffile, $@));
269 write_irssi('configuration saved to %s', $conffile);
279 foreach $p (@getters, @grabbers) {
280 if ($p->{'NAME'} eq $target) {
281 $p->setval($key, $val);
285 write_irssi('No such module');
293 foreach $p (@grabbers) {
294 if ($p->{'NAME'} eq $target) {
299 write_irssi('No such module');
307 foreach $p (@grabbers) {
308 if ($p->{'NAME'} eq $target) {
313 write_irssi('No such module');
322 if (defined($target)) {
323 foreach $p (@getters, @grabbers) {
324 if ($p->{'NAME'} eq $target) {
325 write_irssi($p->getconfstr());
329 write_irssi('No such module');
331 write_irssi('Loaded grabbers (* denotes enabled modules):');
332 foreach $p (@grabbers) {
333 $e = $p->_getval('enabled');
334 write_irssi(' %s%s', $p->{'NAME'}, $e?'*':'');
337 write_irssi('Loaded getters:');
338 foreach $p (@getters) {
339 write_irssi(' %s', $p->{'NAME'});
348 if (defined($target)) {
349 foreach $p (@getters, @grabbers) {
350 if ($p->{'NAME'} eq $target) {
351 write_irssi($p->gethelpstr());
355 write_irssi('No such module');
357 write_irssi(<<'EOT');
359 save: save the current configuration
360 help [modulename]: display this help, or module specific help
361 show [modulename]: show loaded modules, or the current parameters of a module
362 set modulename parameter value: set a module parameter to a new value
363 getter [modulename]: display or set the getter to use
364 enable [modulename]: enable the usage of this module (grabbers only)
365 disable [modulename]: disable the usage of this module (grabbers only)
366 reload: reload all modules (this is somewhat experimental)
367 mode [modename]: display or set the operation mode (download/display)
368 debug: enable debugging messages
369 nodebug: disable debugging messages
378 if (defined($target)) {
379 foreach $p (@getters) {
380 if ($p->{'NAME'} eq $target) {
382 $conf->{'videosite'}->{'getter'} = $target;
383 write_irssi("Getter changed to %s", $target);
387 write_irssi('No such getter');
389 write_irssi('Current getter: %s', $conf->{'videosite'}->{'getter'});
396 if (defined($mode)) {
398 if (('download' eq $mode) or ('display' eq $mode)) {
399 $conf->{'videosite'}->{'mode'} = $mode;
400 write_irssi('Now using %s mode', $mode);
402 write_irssi('Invalid mode: %s', $mode);
405 write_irssi('Current mode: %s', $conf->{'videosite'}->{'mode'});
411 sub sig_command_script_unload {
413 if ($script =~ /(.*\/)?videosite(\.pl)?$/) {
428 opendir(D, $dir) || return ();
429 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
433 write_debug("Trying to load $p:");
435 eval qq{ require videosite::$p; };
437 write_irssi("Failed to load plugin: $@");
441 $g = eval qq{ videosite::$p->new(); };
443 write_irssi("Failed to instanciate: $@");
448 write_debug("found $g->{'TYPE'} $g->{'NAME'}");
449 if ($type eq $g->{'TYPE'}) {
451 $g->setio(\&write_irssi);
453 write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
458 write_debug("Loaded %d plugins", $#g+1);
463 sub _load_modules($) {
467 foreach (keys(%INC)) {
468 if ($INC{$_} =~ m|^$path|) {
469 write_debug("Removing %s from \$INC", $_);
473 @grabbers = ploader($path, '.*Grabber\.pm$', 'grabber');
474 @getters = ploader($path, '.*Getter\.pm$', 'getter');
480 my $bindings = shift;
483 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
484 # No config, start with an empty one
485 write_debug('No config found, using defaults');
486 $conf = { 'videosite' => { }};
488 foreach (keys(%{$PARAMS})) {
489 unless (exists($conf->{'videosite'}->{$_})) {
490 $conf->{'videosite'}->{$_} = $PARAMS->{$_};
494 _load_modules($plugindir);
496 unless (defined(@grabbers) && defined(@getters)) {
497 write_irssi('No grabbers or no getters found, can not proceed.');
501 $getter = $getters[0];
502 foreach $p (@getters) {
503 if ($conf->{'videosite'}->{'getter'} eq $p->{'NAME'}) {
507 write_debug('Selected %s as getter', $getter->{'NAME'});
508 $conf->{'videosite'}->{'getter'} = $getter->{'NAME'};
510 # Loop through all plugins and load the config
511 foreach $p (@grabbers, @getters) {
512 $conf->{'videosite'}->{'config'}->{$p->{'NAME'}} = $p->mergeconfig($conf->{'videosite'}->{'config'}->{$p->{'NAME'}});
517 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
518 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
519 Irssi::signal_add('setup saved', 'cmd_save');
522 Irssi::command_bind(_bcb('videosite' => \&cmdhandler));
525 write_irssi('initialized successfully');
529 my ($complist, $window, $word, $linestart, $want_space) = @_;
532 if ($linestart !~ m|^/videosite\b|) {
536 if ('/videosite' eq $linestart) {
537 # No command enterd so far. Produce a list of possible follow-ups
538 @matches = grep {/^$word/} keys (%{$videosite_commands});
539 } elsif ('/videosite set' eq $linestart) {
540 # 'set' command entered. Produce a list of modules
541 foreach (@grabbers, @getters) {
542 push(@matches, $_->{'NAME'}) if $_->{'NAME'} =~ m|^$word|;
544 } elsif ($linestart =~ m|^/videosite set (\w+)$|) {
547 foreach my $p (@getters, @grabbers) {
548 if ($p->{'NAME'} eq $module) {
549 @matches = $p->getparamlist($word);
553 } elsif ($linestart =~ m|/videosite set (\w+) (\w+)$|) {
557 foreach my $p (@getters, @grabbers) {
558 if ($p->{'NAME'} eq $module) {
559 @matches = $p->getparamvalues($param, $word);
566 push(@{$complist}, sort @matches);
569 Irssi::signal_stop();
574 my ($cmd, @params) = split(/\s+/, $event->message());
576 push_output($event->ewpf);
578 if (exists($videosite_commands->{$cmd})) {
579 $videosite_commands->{$cmd}->(@params);
585 unshift(@INC, $scriptdir);