html/mkshorterlink.pl


   1 ## mkshorterlink.pl -- Irssi interface for makeashorterlink.com
   2 ## (C) 2002 Gergely Nagy <algernon@bonehunter.rulez.org>
   3 ##
   4 ## Released under the GPLv2.
   5 ##
   6 ## ChangeLog:
   7 ## 0.1 -- Initial version
   8 ## 0.2 -- Added support for ignoring URLs matching certain regexps.
   9 ##        (Thanks to Ganneff for the idea)
  10 ## 0.3 -- Added help messages.
  11 
  12 use Irssi qw();
  13 use LWP::UserAgent;
  14 use strict;
  15 use vars qw($VERSION %IRSSI);
  16 
  17 $VERSION = "0.3";
  18 
  19 %IRSSI = (
  20 	  'authors'	=> 'Gergely Nagy',
  21 	  'contact'	=> 'algernon\@bonehunter.rulez.org',
  22 	  'name'	=> 'makeashorterlink.com interface',
  23 	  'description'	=> 'Automatically filters all http:// links through makeashorterlink.com',
  24 	  'license'	=> 'GPL',
  25 	  'url'		=> 'ftp://bonehunter.rulez.org/pub/irssi/mkshorterlink.pl',
  26 	  'changed'	=> '2002-12-20'
  27 	 );
  28 
  29 my %noshort;
  30 my %help = (
  31 	    "mkshorterlink" =>
  32 	      "mkshorterlink is an Irssi script that filters all " .
  33 	      "http:// links through makeshorterlink.com. " .
  34 	      "Available commands are: mkshorter, mkunshor, " .
  35 	      "mkununshort, and mkunshortlist.",
  36 
  37 	    "mkshort" => "MKSHORT <text>\n" .
  38 	      "Filters the URLs in <text> through makeashorterlink.com.",
  39 	    
  40 	    "mkunshort" => "MKUNSHORT <regexps>\n" .
  41 	      "All URLs matching any of the listed <regexps> will be " .
  42 	      "ignored, and not filtered through makeashorterlink.com.",
  43 
  44 	    "mkununshort" => "MKUNUNSHORT <regexp>\n" .
  45 	      "Reverses the effect of MKUNSHORT.",
  46 
  47 	    "mkunshortlist" => "MKUNSHORTLIST lists all the enabled regexps."
  48 	   );
  49 
  50 sub cmd_help {
  51 	my ($args, $server, $win) = @_;
  52 
  53 	my $topic = $args;
  54 	$topic =~s/^\s*(.*)\s+?$/$1/;
  55 	if (defined ($help{$topic}))
  56 	{
  57 		Irssi::signal_stop ();
  58 		Irssi::print ($help{$topic});
  59 		return;
  60 	}
  61 }
  62 
  63 sub makeshorter {
  64 	my $msg = $_[0];
  65 	my $ua = LWP::UserAgent->new (env_proxy => 1,
  66 				      keep_alive => 0,
  67 				      timeout => 10,
  68 				      agent => '');
  69 	my $response = $ua->post ("http://makeashorterlink.com/index.php",
  70 				  ['url' => "$msg"]);
  71 	if ($response->content =~ /Your shorter link is: <a href=\"([^\"]+)\"/) {
  72 		return $1;
  73 	} else {
  74 		return $msg;
  75 	}
  76 }
  77 
  78 sub mkshorter {
  79 	my $msg = $_[0];
  80 	my $short = 1;
  81 
  82 	foreach (keys %noshort)
  83 	{
  84 		$short = 0 if ($noshort{$_} && $msg =~ /$_/);
  85 	}
  86 	
  87 	if ($msg =~ /(https?:\/\/[^ ]+)/ && $short)
  88 	{
  89 		my $t = $1;
  90 		
  91 		if ($t =~ /([\.\?\!,] ?)$/)
  92 		{
  93 			$t=~s/$1//;
  94 		}
  95 		$msg =~ s/$t/&makeshorter($t)/e;
  96 	}
  97 	return $msg;
  98 }
  99 
 100 sub cmd_mkshorter {
 101 	my ($msg, undef, $channel) = @_;
 102 	my $public = 0;
 103 	
 104 	if ($msg =~ /^-p */)
 105 	{
 106 		$public = 1;
 107 		$msg =~ s/^-p *//;
 108 	}
 109 	
 110 	if (defined ($channel) && $channel && $public)
 111 	{
 112 		$channel->command("msg $channel->{'name'} " .
 113 				  mkshorter($msg));
 114 	} else {
 115 		Irssi::active_win()->printformat(MSGLEVEL_CLIENTCRAP,
 116 						 'mkshorterlink_crap',
 117 						 mkshorter ($msg));
 118 	}
 119 }
 120 
 121 sub sig_mkshorter {
 122 	my ($server, $msg, $nick, $address, $target) = @_;
 123 	$target = $nick if $target eq "";
 124 	$nick = $server->{'nick'} if $address eq "";
 125 	my $newmsg = mkshorter ($msg);
 126 
 127 	$server->window_item_find ($target)->print ("[mkshort] <$nick> " .
 128 						    $newmsg, MSGLEVEL_CRAP)
 129 	if ($newmsg ne $msg);
 130 }
 131 
 132 sub cmd_mkunshort {
 133 	my @params = split (" ", $_[0]);
 134 
 135 	foreach (@params)
 136 	{
 137 		$noshort{$_} = 1;
 138 	}
 139 }
 140 
 141 sub cmd_mkununshort {
 142 	my @params = split (" ", $_[0]);
 143 
 144 	foreach (@params)
 145 	{
 146 		$noshort{$_} = 0;
 147 	}
 148 }
 149 
 150 sub cmd_mkunshortlist {
 151 	Irssi::active_win()->printformat (MSGLEVEL_CLIENTCRAP,
 152 					  'mkshorterlink_crap',
 153 					  "URLs matching these are ignored: ");
 154 	foreach (keys %noshort)
 155 	{
 156 		Irssi::active_win()->printformat (MSGLEVEL_CLIENTCRAP,
 157 						  'mkshorterlink_crap',
 158 						  $_)
 159 		  if ($noshort{$_});
 160 	}
 161 }
 162 
 163 sub load_unshortlist {
 164 	my $file = Irssi::get_irssi_dir."/unshortlist";
 165 	my $count = 0;
 166 	local *CONF;
 167 	
 168 	open CONF, "< $file";
 169 	while (<CONF>)
 170 	{
 171 		$noshort{$_} = 1;
 172 		$count++;
 173 	}
 174 	close CONF;
 175 	
 176 	Irssi::printformat (MSGLEVEL_CLIENTCRAP, 'mkshorterlink_crap',
 177 			    "Loaded $count ignore-regexps from $file.");
 178 }
 179 
 180 sub save_unshortlist {
 181 	my $file = Irssi::get_irssi_dir."/unshortlist";
 182 	local *CONF;
 183 
 184 	open CONF, "> $file";
 185 	foreach (keys %noshort)
 186 	{
 187 		print CONF $_ if ($noshort{$_});
 188 	}
 189 	close CONF;
 190 
 191 	Irssi::printformat (MSGLEVEL_CLIENTCRAP, 'mkshorterlink_crap',
 192 			    "Saved ignore-regexps to $file.");
 193 }
 194 
 195 sub sig_setup_rered {
 196 	load_unshortlist ();
 197 }
 198 
 199 sub sig_setup_save {
 200 	save_unshortlist ();
 201 }
 202 
 203 Irssi::command_bind ('mkshorter', 'cmd_mkshorter');
 204 Irssi::command_bind ('mkunshort', 'cmd_mkunshort');
 205 Irssi::command_bind ('mkununshort', 'cmd_mkununshort');
 206 Irssi::command_bind ('mkunshortlist', 'cmd_mkunshortlist');
 207 Irssi::command_bind ('help', 'cmd_help');
 208 Irssi::signal_add_last ('message own_public', 'sig_mkshorter');
 209 Irssi::signal_add_last ('message public', 'sig_mkshorter');
 210 Irssi::signal_add_last ('message own_private', 'sig_mkshorter');
 211 Irssi::signal_add_last ('message private', 'sig_mkshorter');
 212 Irssi::signal_add ('setup reread', 'sig_setup_reread');
 213 Irssi::signal_add ('setup saved', 'sig_setup_save');
 214 
 215 Irssi::theme_register(
 216 		      [
 217 		        'mkshorterlink_crap',
 218 		        '{line_start}{hilight mkshorterlink:} $0'
 219 		      ]);
 220 
 221 load_unshortlist ();