1#!/usr/local/bin/perl -w
2#
3# Settings:
4#   /SET urlgrab_command <command>
5#     Command to execute when opening URLs
6#     Default: xdg-open '%s' > /dev/null 2>&1
7#
8
9use strict;
10use Irssi 20010120.0250 ();
11use vars qw($VERSION %IRSSI);
12$VERSION = "0.4";
13%IRSSI = (
14    authors     => 'David Leadbeater',
15    contact     => 'dgl@dgl.cx',
16    name        => 'urlgrab',
17    description => 'Captures urls said in channel and private messages and saves them to a file, also adds a /url command which loads the last said url into a browser.',
18    license     => 'GNU GPLv2 or later',
19    url         => 'http://irssi.dgl.cx/',
20);
21
22my $lasturl;
23
24# Change the file path below if needed
25my $file = "$ENV{HOME}/.urllog";
26
27sub url_public{
28   my($server,$text,$nick,$hostmask,$channel)=@_;
29   my $url = find_url($text);
30   url_log($nick, $channel, $url) if defined $url;
31}
32
33sub url_private{
34   my($server,$text,$nick,$hostmask)=@_;
35   my $url = find_url($text);
36   url_log($nick, $server->{nick}, $url) if defined $url;
37}
38
39sub url_cmd{
40   if(!$lasturl){
41	  Irssi::print("No url captured yet");
42	  return;
43   }
44   system(sprintf(Irssi::settings_get_str("urlgrab_command"), $lasturl));
45}
46
47sub find_url {
48   my $text = shift;
49   if($text =~ /((ftp|http|https):\/\/[a-zA-Z0-9\/\\\:\?\%\.\&\;=#\-\_\!\+\~]*)/i){
50	  return $1;
51   }elsif($text =~ /(www\.[a-zA-Z0-9\/\\\:\?\%\.\&\;=#\-\_\!\+\~]*)/i){
52	  return "http://".$1;
53   }
54   return undef;
55}
56
57sub url_log{
58   my($where,$channel,$url) = @_;
59   return if lc $url eq lc $lasturl; # a tiny bit of protection from spam/flood
60   $lasturl = $url;
61   open(URLLOG, ">>", $file) or return;
62   print URLLOG time." $where $channel $lasturl\n";
63   close(URLLOG);
64}
65
66Irssi::settings_add_str("misc", "urlgrab_command", "xdg-open '%s' > /dev/null 2>&1");
67Irssi::signal_add_last("message public", "url_public");
68Irssi::signal_add_last("message private", "url_private");
69Irssi::command_bind("url", "url_cmd");
70