##
## script to handle URLs found in IRC
##
use strict;
use Irssi;
use vars qw($VERSION %IRSSI);
my $VERSION = "0.1";
my %IRSSI = (
authors => 'ardnew',
contact => 'fuck@you.biz',
name => 'url.pl',
description => 'handles URLs found in IRC',
license => 'DWTFYWWI LICENSE Version 1 ' .
'http://tools.wikimedia.de/~avar/COPYING',
);
Irssi::settings_add_str('url.pl', 'webbrowser_cmd', '/usr/bin/firefox');
Irssi::settings_add_str('url.pl', 'mediaplayer_cmd', '/usr/bin/mplayer');
Irssi::settings_add_str('url.pl', 'saved_urls_path', '/home/andrew/.url.pl.dat');
Irssi::settings_add_int('url.pl', 'stored_urls_limit', 100);
my @url;
# read all public messages, and strip everything but the URL from 'em
sub pub_msg_grab_url ($$$$) {
my ($server, $msg, $nick, $mask, $targ) = @_;
my $url = $1 if ($msg =~ /.*(https?\:\/\/\S*).*/i);
if (defined $url) {
my $lim = Irssi::settings_get_int("stored_urls_limit");
if (@url < $lim) {
\&url_add_to_array($url, $nick, $targ);
\&url_add_to_file($url, $nick, $targ);
} else {
Irssi::print("You have reached the maximum allowed number of stored URLs. Please use /url_clear to clear the list.\n".
"If you would like to increase this limit, please use /set stored_urls_limit [newlimit].\n".
"This URL ($url) will still be added to the save file.");
\&url_add_to_file($url, $nick, $targ);
}
}
}
# add URL to @url array
sub url_add_to_array ($$$) { # argument is the $url itself
my ($url, $nick, $targ) = @_;
push(@url, $url);
Irssi::print("Added URL: <$nick/$targ> $url");
}
# add URL to file
sub url_add_to_file ($$$) { # argument is the $url itself
my ($url, $nick, $targ) = @_;
my $file = Irssi::settings_get_str("saved_urls_path");
die($!) unless open FILE, '>>', $file;
print FILE "<$nick/$targ> $url\n";
close(FILE);
}
# reinitialize the @url array
sub url_clear_array () {
@url = ();
Irssi::print("Contents of the \@url array have been cleared.");
}
# print the contents of @url to irssi
sub url_list_array () {
if (@url == 0) {
Irssi::print("The \@url array is currently empty.");
} else {
for (my $i = 0; $i < @url; $i++) {
Irssi::print("[$i] $url[$i]");
}
}
}
# open the URL per webbrowser_cmd by @url array index
sub url_open_by_index ($) {
my ($idx) = @_;
my $cmd = Irssi::settings_get_str("webbrowser_cmd");
if (defined $url[$idx]) {
system($cmd." ".$url[$idx]);
} else {
Irssi::print("$url[$idx] is not a valid index. Please see /url_list for available indices.");
}
}
# open the URL per webbrowser_cmd by provided URL
sub url_open_by_url ($) {
my ($url) = @_;
my $cmd = Irssi::settings_get_str("webbrowser_cmd");
system($cmd." ".$url);
}
# open the last URL provided per webbrowser_cmd
sub url_open_last () {
my $url = $url[$#url];
my $cmd = Irssi::settings_get_str("webbrowser_cmd");
my $pid = fork;
if ($pid) {
Irssi::pidwait_add($pid);
} elsif (defined $pid) {
system($cmd." ".$url);
exit;
} else {
die "Error: couldn't fork: $!\n";
}
}
Irssi::signal_add_last("message public", "pub_msg_grab_url");
Irssi::command_bind("url_clear", \&url_clear_array);
Irssi::command_bind("url_list", \&url_list_array);
Irssi::command_bind("open_url", \&url_open_by_url);
Irssi::command_bind("open_index", \&url_open_by_index);
Irssi::command_bind("open_last", \&url_open_last);