html/topicsed.pl


   1 #
   2 # Topicsed edits channel topics by perl regexps via the command /topicsed.
   3 #
   4 # Thanks to Mikael Magnusson for the idea and patch to implement a
   5 # preview functionality. ;]
   6 #
   7 
   8 use Irssi;
   9 
  10 use vars %IRSSI;
  11 %IRSSI = (
  12 	authors		=> "Gabor Nyeki",
  13 	contact		=> "bigmac\@vim.hu",
  14 	name		=> "topicsed",
  15 	description	=> "editing channel topics by regexps",
  16 	license		=> "public domain",
  17 	changed		=> "Fri Aug 13 19:27:38 CEST 2004"
  18 );
  19 
  20 
  21 sub topicsed {
  22 	my ($regexp, $server, $winit) = @_;
  23 
  24 	my $preview = 0;
  25 	if ($regexp =~ m/^-p(review|) ?/) {
  26 		$preview = 1;
  27 		$regexp =~ s/^-p\w* ?//;
  28 	}
  29 
  30 	unless ($regexp) {
  31 		Irssi::print("Usage: /topicsed [-p[review]] <regexp>");
  32 		return;
  33 	}
  34 	return if (!$server || !$server->{connected} ||
  35 		!$winit || $winit->{type} != 'CHANNEL');
  36 
  37 	my $topic = $winit->{topic};
  38 	my $x = $topic;
  39 
  40 	unless (eval "\$x =~ $regexp") {
  41 		Irssi::print("topicsed:error: An error occured with your regexp.");
  42 		return;
  43 	}
  44 
  45 	if ($x eq $topic) {
  46 		Irssi::print("topicsed:error: The topic wouldn't be changed.");
  47 		return;
  48 	} elsif ($x eq "") {
  49 		Irssi::print("topicsed:error: Edited topic is empty;  try '/topic -delete' instead.");
  50 		return;
  51 	}
  52 
  53 	if ($preview) {
  54 		Irssi::print("topicsed: Edited topic for $winit->{name}: $x");
  55 	} else {
  56 		$server->send_raw("TOPIC $winit->{name} :$x");
  57 	}
  58 }
  59 
  60 Irssi::command_bind('topicsed', 'topicsed');