summaryrefslogtreecommitdiffstats
path: root/scripts/repeat.pl
blob: 0d70b7225dfc8874fa2d0ea345542b0294fb52ff (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
use Irssi;
use strict;

use vars qw($VERSION %IRSSI);

$VERSION="0.2.0";
%IRSSI = (
	authors=> 'BC-bd',
	contact=> 'bd@bc-bd.org',
	name=> 'repeat',
	description=> 'Hide duplicate lines',
	license=> 'GPL v2',
	url=> 'http://bc-bd.org/blog/irssi/',
);

# repeal.pl: ignore repeated messages
# 
# for irssi 0.8.5 by bd@bc-bd.org
#
#########
# USAGE
###
# 
# This script hides repeated lines from:
#
#   dude> Plz Help me!!!
#   dude> Plz Help me!!!
#   dude> Plz Help me!!!
#   guy> foo
#
# Becomes:
#
#   dude> Plz Help me!!!
#   guy> foo
#
# Or with 'repeat_show' set to ON:
#
#   dude> Plz Help me!!!
# Irssi: Message repeated 3 times
#   guy> foo
#
#########
# OPTIONS
#########
#
# /set repeat_show <ON|OFF>
#   * ON  : show info line: 'Message repeated N times'
#   * OFF : don't show it.
#
# /set repeat_count <N>
#   N : Display a message N times, then ignore it.
#
###
################
###
# Changelog
#
# Version 0.2.0
# - addes support for /me
#
# Version master
# - updated url
#
# Version 0.1.3
# - fix: also check before own message (by Wouter Coekaerts)
#
# Version 0.1.2
# - removed stray debug message (duh!)
#
# Version 0.1.1
# - off by one fixed
# - fixed missing '$'
#
# Version 0.1.0
# - initial release
#
my %said;
my %count;

sub sig_public {
  my ($server, $msg, $nick, $address, $target) = @_;

	my $maxcount = Irssi::settings_get_int('repeat_count');

	my $window = $server->window_find_item($target);
	my $refnum = $window->{refnum};

	my $this = $refnum.$nick.$msg;

	my $last = $said{$refnum};
	my $i = $count{$refnum};

	if ($last eq $this and not $nick eq $server->{nick}) {
		$count{$refnum} = $i +1;

		if ($i >= $maxcount) {
			Irssi::signal_stop();
		}
	} else {
		if ($i > $maxcount && Irssi::settings_get_bool('repeat_show')) {
			$window->print("Message repeated ".($i-1)." times");
		}

		$count{$refnum} = 1;
		$said{$refnum} = $this;
	}
}

sub sig_own_public {
	my ($server, $msg, $target) = @_;
	sig_public ($server, $msg, $server->{nick}, "", $target);
}

sub remove_window {
	my ($num) = @_;

	delete($count{$num});
	delete($said{$num});
}

sub sig_refnum {
	my ($window,$old) = @_;
	my $refnum = $window->{refnum};

	$count{$refnum} = $count{old};
	$said{$refnum} = $count{old};

	remove_window($old);
}

sub sig_destroyed {
	my ($window) = @_;
	remove_window($window->{refnum});
}

Irssi::signal_add('message public', 'sig_public');
Irssi::signal_add('message own_public', 'sig_own_public');
Irssi::signal_add('message irc action', 'sig_public');
Irssi::signal_add_last('window refnum changed', 'sig_refnum');
Irssi::signal_add_last('window destroyed', 'sig_destroyed');

Irssi::settings_add_int('misc', 'repeat_count', 1);
Irssi::settings_add_bool('misc', 'repeat_show', 1);