summaryrefslogtreecommitdiffstats
path: root/scripts/openurl.pl
blob: 066a5f43e0a820ed2a8e1b2c39d1cf00d6422e4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# OpenURL by Stefan'tommie' Tomanek
#
# 05.06.2002
# * complete rewrite

use strict;

use vars qw($VERSION %IRSSI);
$VERSION = "20030208";
%IRSSI = (
    authors     => "Stefan 'tommie' Tomanek",
    contact     => "stefan\@pico.ruhr.de",
    name        => "OpenURL",
    description => "Stores URLs in a list and launches mail, web or ftp software",
    license     => "GPLv2",
    url         => "http://scripts.irssi.org",
    changed     => "$VERSION",
    commands	=> "openurl"
);

use Irssi 20020324;
use Irssi::TextUI;
use Irssi::UI;

use vars qw(@urls %urltypes $recent);
$recent = 1;

# RegExp & defaultcommands
%urltypes = ( http => { regexp => qr#((?:https?://[^\s<>"]+|www\.[-a-z0-9.]+)[^\s.,;<">\):])#, cmd => 'w3m "$1"' },
              ftp  => { regexp => qr#((?:ftp://[^\s<>"]+|ftp\.[-a-z0-9.]+)[^\s.,;<">\):])#, cmd => 'ncftp "$1"' },
	      mail => { regexp => qr#([-_a-z0-9.]+\@[-a-z0-9.]+\.[-a-z0-9.]+)#, cmd => 'mutt "$1" -s "$2"' },
	    );

sub draw_box ($$$$) {
    my ($title, $text, $footer, $colour) = @_;
    my $box = '';
    $box .= '%R,--[%n%9%U'.$title.'%U%9%R]%n'."\n";
    foreach (split(/\n/, $text)) {
        $box .= '%R|%n '.$_."\n";
    }                                                                               $box .= '%R`--<%n'.$footer.'%R>->%n';
    $box =~ s/%.//g unless $colour;
    return $box;
}

sub show_help() {
    my $help=$IRSSI{name}." ".$VERSION."
/openurl
    List the saved URLs
/openurl <number> <number>...
    Load the corresponding URLs in your browser/mailer
/openurl paste <number> <number>...
    Paste the selected URLs to the current channel/query
/openurl topics
    Look for URLs in channel topics
/openurl clear
    Clear all URLs
/openurl help
    Display this help
";
    my $text = '';
    foreach (split(/\n/, $help)) {
        $_ =~ s/^\/(.*)$/%9\/$1%9/;
	$text .= $_."\n";
    }
    print CLIENTCRAP draw_box($IRSSI{name}." help", $text, "help", 1) ;
}

sub list_urls {
    my $string = '';
    my $i = 1;
    foreach (@urls) {
	my $text = $_->{url};
	my $url = $_->{url};
	$text = $_->{text} if Irssi::settings_get_bool('openurl_display_context');
	$url =~ s/%/%%/g;
	$text =~ s/%/%%/g;
	$text =~ s/\Q$url/%U$url%U/;
	if ($recent-1 == $i) {
	    $string .= '%B�%n';
	} else {
	    $string .= ' ';
	}
	$string .= '%r['.$i.']%n ';
	$string .= '<'.$_->{channel};
	$string .= '/'.$_->{nick} unless $_->{nick} eq "";
	$string .= '> ';
	$string .= $text." %n\n";
	$i++;
    }
    print CLIENTCRAP draw_box("OpenURL", $string, "URLs", 1);
}

sub event_private_message {
    my ($server, $text, $nick, $address) = @_;
    process_line($server, $nick, $nick, $text);
}
sub event_public_message {
    my ($server, $text, $nick, $address, $target) = @_;
    process_line($server, $target, $nick, $text);
}
sub event_topic_changed {
    my ($channel) = @_;
    process_line($channel->{server}, $channel->{name}, "", $channel->{topic});
}

sub process_line ($$$$) {
    my ($server, $target, $nick, $line) = @_;
    my $url = get_url($line);
    if ($url) {
	my $type = url_type($url);
	return unless Irssi::settings_get_bool('openurl_watch_'.$type);
	new_url($server, $target, $nick, $line, $url);
    }
}

sub get_url ($) {
    my ($text) = @_;
    foreach (keys %urltypes) {
	return $1 if ($text =~ /$urltypes{$_}->{regexp}/);
    }
}

sub url_type ($) {
    my ($url) = @_;
    foreach (keys %urltypes) {
	return $_ if ($url =~ /$urltypes{$_}->{regexp}/);
    }
}

sub launch_url ($) {
    my ($url) = @_;
    my $type = url_type($url);
    my $address = $url;
    my $suffix= "";
    if ($type eq "mail") {
	$address = $1 if $url =~ /(.*?@.*?)($|\?)/;
	$suffix = $2 if $url =~ /(.*?@.*?)\?subject=(.*)/;
    }
    my $command = Irssi::settings_get_str("openurl_app_".$type);
    $command =~ s/\$1/$address/;
    $command =~ s/\$2/$suffix/;
    system($command);
}

sub new_url ($$$$$) {
    my ($server, $channel, $nick, $text, $url) = @_;
    $recent = 1 if ($recent > Irssi::settings_get_int('openurl_max_urls'));
    # Check for existance of URL
    my $i = 1;
    foreach (@urls) {
	if ($text eq $_->{text} && $channel eq $_->{channel}) {
	    my $note_id = add_note($server, $channel, $i);
	    push @{$_->{notes}}, $note_id;
	    return();
	}
	$i++;
    }
    if (defined $urls[$recent-1]) {
	del_notes($recent);
    }
    $urls[$recent-1] = {channel => $channel,
                        text    => $text,
			nick    => $nick,
			url     => $url,
			notes   => [],
			};
    my $note_id = add_note($server, $channel, $recent);
    push @{$urls[$recent-1]{notes}}, $note_id;
    $recent++;
}


sub del_notes ($) {
    my ($num) = @_;
    my $view;
    my $witem = Irssi::window_item_find($urls[$num-1]->{channel});
    if (defined $witem) {
	$view =  $witem->window()->view();
    }
    if (defined $view) {
	foreach (@{$urls[$num-1]->{notes}}) {
	    my $line = $view->get_bookmark($_);
	    $view->remove_line($line) if defined $line;
	    $view->set_bookmark($_, undef);
	}
	@{$urls[$num-1]->{notes}} = ();
	$view->redraw();
    }
}

sub add_note ($$$) {
    my ($server, $target, $num) = @_;
    my $witem;
    if (defined $server) {
	$witem = $server->window_item_find($target);
    } else {
	$witem = Irssi::window_item_find($target);
    }
    if (defined $witem) {
	$witem->print("%R>>%n OpenURL ".$num, MSGLEVEL_CLIENTCRAP);
	# create a unique ID for the mark
	my $foo = time().'-'.int(rand()*1000);
	$witem->window()->view()->set_bookmark_bottom("openurl_".$num.'-'.$foo);
	return("openurl_".$num.'-'.$foo);
    }
    return(undef);
}

sub clear_urls {
    del_notes($_) foreach (0..scalar(@urls)-1);
    pop(@urls) foreach (1..scalar(@urls));
    $recent = 1;
    print CLIENTCRAP '%R>>%n URLs cleared';
}

sub cmd_openurl ($$$) {
    my ($arg, $server, $witem) = @_;
    my @args = split(/ /, $arg);
    if (scalar(@args) == 0) {
	list_urls;
    } elsif ($args[0] eq 'clear') {
	clear_urls;
    } elsif ($args[0] eq 'topics') {
    	event_topic_changed($_) foreach (Irssi::channels());
    } elsif ($args[0] eq 'help') {
	show_help();
    } elsif ($args[0] eq 'open') {
	launch_url($args[1]);
    } else {
	my $paste = 0;
	if ($args[0] eq 'paste') {
	    $paste = 1;
	    shift(@args);
	}
	foreach (@args) {
	    next unless /\d+/;
	    next unless defined $urls[$_-1];
	    my $url = $urls[$_-1]->{url};
	    if ($paste == 1) {
		if (ref $witem && ($witem->{type} eq "CHANNEL" || $witem->{type} eq "QUERY")) {
		    $witem->command("MSG ".$witem->{name}." ".$url);
		}
	    } else {
		launch_url($url);
	    }
	}
    }
}

foreach (keys %urltypes) {
    Irssi::settings_add_str($IRSSI{'name'}, 'openurl_app_'.$_, "screen ".$urltypes{$_}->{cmd});
    Irssi::settings_add_bool($IRSSI{'name'}, 'openurl_watch_'.$_, 1);
}
Irssi::settings_add_int($IRSSI{'name'}, 'openurl_max_urls', 20);
Irssi::settings_add_bool($IRSSI{'name'}, 'openurl_display_context', 1);

Irssi::signal_add_last("message private", "event_private_message");
Irssi::signal_add_last("message public", "event_public_message");
Irssi::signal_add_last("channel topic changed", "event_topic_changed");

#Irssi::signal_add('open url', \&launch_url);

foreach my $cmd ('topics', 'clear', 'paste', 'help') {
    Irssi::command_bind('openurl '.$cmd => sub {
	cmd_openurl("$cmd ".$_[0], $_[1], $_[2]); });
}
Irssi::command_bind('openurl', 'cmd_openurl');

print CLIENTCRAP '%B>>%n '.$IRSSI{name}.' '.$VERSION.' loaded: /openurl help for help';