summaryrefslogtreecommitdiffstats
path: root/scripts/mkshorterlink.pl
blob: 6199608952826ab3b1916b7b3f5778c0de0402ed (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
## mkshorterlink.pl -- Irssi interface for makeashorterlink.com
## (C) 2002 Gergely Nagy <algernon@bonehunter.rulez.org>
##
## Released under the GPLv2.
##
## ChangeLog:
## 0.1 -- Initial version
## 0.2 -- Added support for ignoring URLs matching certain regexps.
##        (Thanks to Ganneff for the idea)
## 0.3 -- Added help messages.

use Irssi qw();
use LWP::UserAgent;
use strict;
use vars qw($VERSION %IRSSI);

%IRSSI = (
	  'authors'	=> 'Gergely Nagy',
	  'contact'	=> 'algernon\@bonehunter.rulez.org',
	  'name'	=> 'makeashorterlink.com interface',
	  'description'	=> 'Automatically filters all http:// links through makeashorterlink.com',
	  'license'	=> 'GPL',
	  'url'		=> 'ftp://bonehunter.rulez.org/pub/irssi/mkshorterlink.pl',
	  'changed'	=> '2002-12-20'
	 );

my %noshort;
my %help = (
	    "mkshorterlink" =>
	      "mkshorterlink is an Irssi script that filters all " .
	      "http:// links through makeshorterlink.com. " .
	      "Available commands are: mkshorter, mkunshor, " .
	      "mkununshort, and mkunshortlist.",

	    "mkshort" => "MKSHORT <text>\n" .
	      "Filters the URLs in <text> through makeashorterlink.com.",
	    
	    "mkunshort" => "MKUNSHORT <regexps>\n" .
	      "All URLs matching any of the listed <regexps> will be " .
	      "ignored, and not filtered through makeashorterlink.com.",

	    "mkununshort" => "MKUNUNSHORT <regexp>\n" .
	      "Reverses the effect of MKUNSHORT.",

	    "mkunshortlist" => "MKUNSHORTLIST lists all the enabled regexps."
	   );

sub cmd_help {
	my ($args, $server, $win) = @_;

	my $topic = $args;
	$topic =~s/^\s*(.*)\s+?$/$1/;
	if (defined ($help{$topic}))
	{
		Irssi::signal_stop ();
		Irssi::print ($help{$topic});
		return;
	}
}

sub makeshorter {
	my $msg = $_[0];
	my $ua = LWP::UserAgent->new (env_proxy => 1,
				      keep_alive => 0,
				      timeout => 10,
				      agent => '');
	my $response = $ua->post ("http://makeashorterlink.com/index.php",
				  ['url' => "$msg"]);
	if ($response->content =~ /Your shorter link is: <a href=\"([^\"]+)\"/) {
		return $1;
	} else {
		return $msg;
	}
}

sub mkshorter {
	my $msg = $_[0];
	my $short = 1;

	foreach (keys %noshort)
	{
		$short = 0 if ($noshort{$_} && $msg =~ /$_/);
	}
	
	if ($msg =~ /(https?:\/\/[^ ]+)/ && $short)
	{
		my $t = $1;
		
		if ($t =~ /([\.\?\!,] ?)$/)
		{
			$t=~s/$1//;
		}
		$msg =~ s/$t/&makeshorter($t)/e;
	}
	return $msg;
}

sub cmd_mkshorter {
	my ($msg, undef, $channel) = @_;
	my $public = 0;
	
	if ($msg =~ /^-p */)
	{
		$public = 1;
		$msg =~ s/^-p *//;
	}
	
	if (defined ($channel) && $channel && $public)
	{
		$channel->command("msg $channel->{'name'} " .
				  mkshorter($msg));
	} else {
		Irssi::active_win()->printformat(MSGLEVEL_CLIENTCRAP,
						 'mkshorterlink_crap',
						 mkshorter ($msg));
	}
}

sub sig_mkshorter {
	my ($server, $msg, $nick, $address, $target) = @_;
	$target = $nick if $target eq "";
	$nick = $server->{'nick'} if $address eq "";
	my $newmsg = mkshorter ($msg);

	$server->window_item_find ($target)->print ("[mkshort] <$nick> " .
						    $newmsg, MSGLEVEL_CRAP)
	if ($newmsg ne $msg);
}

sub cmd_mkunshort {
	my @params = split (" ", $_[0]);

	foreach (@params)
	{
		$noshort{$_} = 1;
	}
}

sub cmd_mkununshort {
	my @params = split (" ", $_[0]);

	foreach (@params)
	{
		$noshort{$_} = 0;
	}
}

sub cmd_mkunshortlist {
	Irssi::active_win()->printformat (MSGLEVEL_CLIENTCRAP,
					  'mkshorterlink_crap',
					  "URLs matching these are ignored: ");
	foreach (keys %noshort)
	{
		Irssi::active_win()->printformat (MSGLEVEL_CLIENTCRAP,
						  'mkshorterlink_crap',
						  $_)
		  if ($noshort{$_});
	}
}

sub load_unshortlist {
	my $file = Irssi::get_irssi_dir."/unshortlist";
	my $count = 0;
	local *CONF;
	
	open CONF, "<", $file;
	while (<CONF>)
	{
		$noshort{$_} = 1;
		$count++;
	}
	close CONF;
	
	Irssi::printformat (MSGLEVEL_CLIENTCRAP, 'mkshorterlink_crap',
			    "Loaded $count ignore-regexps from $file.");
}

sub save_unshortlist {
	my $file = Irssi::get_irssi_dir."/unshortlist";
	local *CONF;

	open CONF, ">", $file;
	foreach (keys %noshort)
	{
		print CONF $_ if ($noshort{$_});
	}
	close CONF;

	Irssi::printformat (MSGLEVEL_CLIENTCRAP, 'mkshorterlink_crap',
			    "Saved ignore-regexps to $file.");
}

sub sig_setup_rered {
	load_unshortlist ();
}

sub sig_setup_save {
	save_unshortlist ();
}

Irssi::command_bind ('mkshorter', 'cmd_mkshorter');
Irssi::command_bind ('mkunshort', 'cmd_mkunshort');
Irssi::command_bind ('mkununshort', 'cmd_mkununshort');
Irssi::command_bind ('mkunshortlist', 'cmd_mkunshortlist');
Irssi::command_bind ('help', 'cmd_help');
Irssi::signal_add_last ('message own_public', 'sig_mkshorter');
Irssi::signal_add_last ('message public', 'sig_mkshorter');
Irssi::signal_add_last ('message own_private', 'sig_mkshorter');
Irssi::signal_add_last ('message private', 'sig_mkshorter');
Irssi::signal_add ('setup reread', 'sig_setup_reread');
Irssi::signal_add ('setup saved', 'sig_setup_save');

Irssi::theme_register(
		      [
		        'mkshorterlink_crap',
		        '{line_start}{hilight mkshorterlink:} $0'
		      ]);

load_unshortlist ();