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