summaryrefslogtreecommitdiffstats
path: root/scripts/topicsed.pl
blob: 58d014342f520a55099c8db15284985c324893a5 (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
#
# Topicsed edits channel topics by perl regexps via the command /topicsed.
#
# Thanks to Mikael Magnusson for the idea and patch to implement a
# preview functionality. ;]
#
use strict;
use Irssi;

use vars qw/%IRSSI $VERSION/;
$VERSION="0.1";
%IRSSI = (
	authors		=> "Gabor Nyeki",
	contact		=> "bigmac\@vim.hu",
	name		=> "topicsed",
	description	=> "editing channel topics by regexps",
	license		=> "public domain",
	changed		=> "2017-03-18"
);


sub topicsed {
	my ($regexp, $server, $winit) = @_;

	my $preview = 0;
	if ($regexp =~ m/^-p(review|) ?/) {
		$preview = 1;
		$regexp =~ s/^-p\w* ?//;
	}

	unless ($regexp) {
		Irssi::print("Usage: /topicsed [-p[review]] <regexp>");
		return;
	}
	return if (!$server || !$server->{connected} ||
		!$winit || $winit->{type} ne 'CHANNEL');

	my $topic = $winit->{topic};
	my $x = $topic;

	unless (eval "\$x =~ $regexp") {
		Irssi::print("topicsed:error: An error occured with your regexp.");
		return;
	}

	if ($x eq $topic) {
		Irssi::print("topicsed:error: The topic wouldn't be changed.");
		return;
	} elsif ($x eq "") {
		Irssi::print("topicsed:error: Edited topic is empty;  try '/topic -delete' instead.");
		return;
	}

	if ($preview) {
		Irssi::print("topicsed: Edited topic for $winit->{name}: $x");
	} else {
		$server->send_raw("TOPIC $winit->{name} :$x");
	}
}

Irssi::command_bind('topicsed', 'topicsed');