# Based on youtube.pl by Christian Garbs <mitch@cgarbs.de>
# which in turn is
# based on trigger.pl by Wouter Coekaerts <wouter@coekaerts.be>
-# This is helena
+
+
+BEGIN {
+ # Get rid of a (possibly old) version of BettIrssi
+ # This is a hack to prevent having to reload irssi just
+ # because BettIrssi.pm changed
+
+ delete($INC{'BettIrssi.pm'});
+}
use strict;
use Irssi 20020324 qw (command_bind command_runsub signal_add_first signal_add_last);
use XML::Simple;
use Data::Dumper;
use File::Spec;
-delete($INC{'BettIrssi.pm'});
-use BettIrssi qw(_bcb _bcs);
+use File::Temp qw(tempfile);
+use BettIrssi 101 qw(_bcb _bcs);
+use LWP::UserAgent;
my @grabbers;
my @getters;
signal_add_last(_bcs("message irc own_action" => sub {check_for_link(@_)}));
# For tab completion
+# This does not use BettIrssi (yet)
signal_add_first('complete word', \&sig_complete);
sub push_output {
}
}
+sub expand_url_shortener {
+ my $s = shift;
+ my $os = '';
+ my @urlshortener = (
+ 'is\.gd/[[:alnum:]]+',
+ 'otf\.me/[[:alnum:]]+',
+ 'hel\.me/[[:alnum:]]+',
+ '7ax\.de/[[:alnum:]]+',
+ 'ow\.ly/[[:alnum:]]+',
+ 'j\.mp/[[:alnum:]]+',
+ 'bit\.ly/[[:alnum:]]+',
+ 'tinyurl\.com/[[:alnum:]]+',
+ 'pop\.is/[[:alnum:]]+',
+ 'post\.ly/[[:alnum:]]+',
+ '1\.ly/[[:alnum:]]+',
+ '2\.ly/[[:alnum:]]+',
+ 't\.co/[[:alnum:]]+',
+ 'shar\.es/[[:alnum:]]+',
+ 'goo\.gl/[[:alnum:]]+',
+ );
+ my $ua = LWP::UserAgent->new(agent => 'Mozilla', max_redirect => 0);
+ my $i = 10;
+
+ OUTER: while (($os ne $s) and ($i > 0)) {
+ study($s);
+ $os = $s;
+ $i--;
+
+ foreach my $pattern (@urlshortener) {
+ my $p = "https?:\/\/" . $pattern;
+
+ write_debug("Matching %s against %s", $p, $s);
+ if ($s =~ m|($p)|) {
+ my $matched = $1;
+ my $res;
+
+ write_debug("Found %s", $matched);
+ $res = $ua->head($matched);
+ if ($res->is_redirect()) {
+ my $new = $res->headers()->header("Location");
+
+ write_debug("Replacing %s with %s", $matched, $new);
+ $s =~ s/$matched/$new/;
+ next OUTER;
+ } else {
+ write_debug("Error resolving %s", $matched);
+ }
+ }
+ }
+ }
+
+ if ($i == 0) {
+ write_debug("Loop terminated by counter");
+ }
+
+ write_debug("Final string: %s", $s);
+
+ return $s;
+}
+
+
sub check_for_link {
my $event = shift;
my $message = $event->message();
return;
}
+ push_output($event->ewpf);
+ $message = expand_url_shortener($message);
+
study($message);
# Offer the message to all Grabbers in turn
- foreach $g (@grabbers) {
+ GRABBER: foreach $g (@grabbers) {
($m, $p) = $g->get($message);
while (defined($m)) {
write_debug('Metadata: %s', Dumper($m));
# more!)
$message =~ s/$p//;
study($message);
+ last GRABBER if ($message =~ /^\s*$/);
($m, $p) = $g->get($message);
}
}
+
+ pop_output();
}
sub cmd_save {
+
eval {
- open(CONF, '>'.$conffile) or die 'Could not open config file';
- print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
- close(CONF);
+ my ($tempfile, $tempfn) = tempfile("videosite.xml.XXXXXX", dir => Irssi::get_irssi_dir());
+ print $tempfile XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
+ close($tempfile);
+ rename($tempfn, $conffile);
};
if ($@) {
write_irssi('Could not save config to %s: %s', ($conffile, $@));
if ($p->{'NAME'} eq $target) {
$getter = $p;
$conf->{'videosite'}->{'getter'} = $target;
+ write_irssi("Getter changed to %s", $target);
return;
}
}
$mode = lc($mode);
if (('download' eq $mode) or ('display' eq $mode)) {
$conf->{'videosite'}->{'mode'} = $mode;
+ write_irssi('Now using %s mode', $mode);
} else {
write_irssi('Invalid mode: %s', $mode);
}
my $event = shift;
my ($cmd, @params) = split(/\s+/, $event->message());
+ push_output($event->ewpf);
+
if (exists($videosite_commands->{$cmd})) {
$videosite_commands->{$cmd}->(@params);
}
+
+ pop_output();
}
unshift(@INC, $scriptdir);