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:]]+',
167 my $ua = LWP::UserAgent->new(agent => 'Mozilla', max_redirect => 0);
170 OUTER: while (($os ne $s) and ($i > 0)) {
174 foreach my $pattern (@urlshortener) {
175 my $p = "https?:\/\/" . $pattern;
177 write_debug("Matching %s against %s", $p, $s);
182 write_debug("Found %s", $matched);
183 $res = $ua->head($matched);
184 if ($res->is_redirect()) {
185 my $new = $res->headers()->header("Location");
187 write_debug("Replacing %s with %s", $matched, $new);
188 $s =~ s/$matched/$new/;
191 write_debug("Error resolving %s", $matched);
198 write_debug("Loop terminated by counter");
201 write_debug("Final string: %s", $s);
209 my $message = $event->message();
210 my $witem = $event->channel();
216 # Look if we should ignore this line
217 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
221 push_output($event->ewpf);
222 $message = expand_url_shortener($message);
226 # Offer the message to all Grabbers in turn
227 GRABBER: foreach $g (@grabbers) {
228 ($m, $p) = $g->get($message);
229 while (defined($m)) {
230 write_debug('Metadata: %s', Dumper($m));
231 if ('download' eq ($conf->{'videosite'}->{'mode'})) {
232 write_irssi('%%R>>> %%NSaving %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
233 unless($getter->get($m)) {
234 write_irssi('%%R>>> FAILED');
236 } elsif ('display' eq ($conf->{'videosite'}->{'mode'})) {
237 write_irssi('%%M>>> %%NSaw %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'TITLE'});
239 write_irssi('%%R>>> Invalid operation mode');
242 # Remove the matched part from the message and try again (there may be
246 last GRABBER if ($message =~ /^\s*$/);
248 ($m, $p) = $g->get($message);
258 open(CONF, '>'.$conffile) or die 'Could not open config file';
259 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
263 write_irssi('Could not save config to %s: %s', ($conffile, $@));
265 write_irssi('configuration saved to %s', $conffile);
275 foreach $p (@getters, @grabbers) {
276 if ($p->{'NAME'} eq $target) {
277 $p->setval($key, $val);
281 write_irssi('No such module');
289 foreach $p (@grabbers) {
290 if ($p->{'NAME'} eq $target) {
295 write_irssi('No such module');
303 foreach $p (@grabbers) {
304 if ($p->{'NAME'} eq $target) {
309 write_irssi('No such module');
318 if (defined($target)) {
319 foreach $p (@getters, @grabbers) {
320 if ($p->{'NAME'} eq $target) {
321 write_irssi($p->getconfstr());
325 write_irssi('No such module');
327 write_irssi('Loaded grabbers (* denotes enabled modules):');
328 foreach $p (@grabbers) {
329 $e = $p->_getval('enabled');
330 write_irssi(' %s%s', $p->{'NAME'}, $e?'*':'');
333 write_irssi('Loaded getters:');
334 foreach $p (@getters) {
335 write_irssi(' %s', $p->{'NAME'});
344 if (defined($target)) {
345 foreach $p (@getters, @grabbers) {
346 if ($p->{'NAME'} eq $target) {
347 write_irssi($p->gethelpstr());
351 write_irssi('No such module');
353 write_irssi(<<'EOT');
355 save: save the current configuration
356 help [modulename]: display this help, or module specific help
357 show [modulename]: show loaded modules, or the current parameters of a module
358 set modulename parameter value: set a module parameter to a new value
359 getter [modulename]: display or set the getter to use
360 enable [modulename]: enable the usage of this module (grabbers only)
361 disable [modulename]: disable the usage of this module (grabbers only)
362 reload: reload all modules (this is somewhat experimental)
363 mode [modename]: display or set the operation mode (download/display)
364 debug: enable debugging messages
365 nodebug: disable debugging messages
374 if (defined($target)) {
375 foreach $p (@getters) {
376 if ($p->{'NAME'} eq $target) {
378 $conf->{'videosite'}->{'getter'} = $target;
379 write_irssi("Getter changed to %s", $target);
383 write_irssi('No such getter');
385 write_irssi('Current getter: %s', $conf->{'videosite'}->{'getter'});
392 if (defined($mode)) {
394 if (('download' eq $mode) or ('display' eq $mode)) {
395 $conf->{'videosite'}->{'mode'} = $mode;
396 write_irssi('Now using %s mode', $mode);
398 write_irssi('Invalid mode: %s', $mode);
401 write_irssi('Current mode: %s', $conf->{'videosite'}->{'mode'});
407 sub sig_command_script_unload {
409 if ($script =~ /(.*\/)?videosite(\.pl)?$/) {
424 opendir(D, $dir) || return ();
425 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
429 write_debug("Trying to load $p:");
431 eval qq{ require videosite::$p; };
433 write_irssi("Failed to load plugin: $@");
437 $g = eval qq{ videosite::$p->new(); };
439 write_irssi("Failed to instanciate: $@");
444 write_debug("found $g->{'TYPE'} $g->{'NAME'}");
445 if ($type eq $g->{'TYPE'}) {
447 $g->setio(\&write_irssi);
449 write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
454 write_debug("Loaded %d plugins", $#g+1);
459 sub _load_modules($) {
463 foreach (keys(%INC)) {
464 if ($INC{$_} =~ m|^$path|) {
465 write_debug("Removing %s from \$INC", $_);
469 @grabbers = ploader($path, '.*Grabber\.pm$', 'grabber');
470 @getters = ploader($path, '.*Getter\.pm$', 'getter');
476 my $bindings = shift;
479 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
480 # No config, start with an empty one
481 write_debug('No config found, using defaults');
482 $conf = { 'videosite' => { }};
484 foreach (keys(%{$PARAMS})) {
485 unless (exists($conf->{'videosite'}->{$_})) {
486 $conf->{'videosite'}->{$_} = $PARAMS->{$_};
490 _load_modules($plugindir);
492 unless (defined(@grabbers) && defined(@getters)) {
493 write_irssi('No grabbers or no getters found, can not proceed.');
497 $getter = $getters[0];
498 foreach $p (@getters) {
499 if ($conf->{'videosite'}->{'getter'} eq $p->{'NAME'}) {
503 write_debug('Selected %s as getter', $getter->{'NAME'});
504 $conf->{'videosite'}->{'getter'} = $getter->{'NAME'};
506 # Loop through all plugins and load the config
507 foreach $p (@grabbers, @getters) {
508 $conf->{'videosite'}->{'config'}->{$p->{'NAME'}} = $p->mergeconfig($conf->{'videosite'}->{'config'}->{$p->{'NAME'}});
513 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
514 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
515 Irssi::signal_add('setup saved', 'cmd_save');
518 Irssi::command_bind(_bcb('videosite' => \&cmdhandler));
521 write_irssi('initialized successfully');
525 my ($complist, $window, $word, $linestart, $want_space) = @_;
528 if ($linestart !~ m|^/videosite\b|) {
532 if ('/videosite' eq $linestart) {
533 # No command enterd so far. Produce a list of possible follow-ups
534 @matches = grep {/^$word/} keys (%{$videosite_commands});
535 } elsif ('/videosite set' eq $linestart) {
536 # 'set' command entered. Produce a list of modules
537 foreach (@grabbers, @getters) {
538 push(@matches, $_->{'NAME'}) if $_->{'NAME'} =~ m|^$word|;
540 } elsif ($linestart =~ m|^/videosite set (\w+)$|) {
543 foreach my $p (@getters, @grabbers) {
544 if ($p->{'NAME'} eq $module) {
545 @matches = $p->getparamlist($word);
549 } elsif ($linestart =~ m|/videosite set (\w+) (\w+)$|) {
553 foreach my $p (@getters, @grabbers) {
554 if ($p->{'NAME'} eq $module) {
555 @matches = $p->getparamvalues($param, $word);
562 push(@{$complist}, sort @matches);
565 Irssi::signal_stop();
570 my ($cmd, @params) = split(/\s+/, $event->message());
572 push_output($event->ewpf);
574 if (exists($videosite_commands->{$cmd})) {
575 $videosite_commands->{$cmd}->(@params);
581 unshift(@INC, $scriptdir);