summaryrefslogtreecommitdiffstats
path: root/scripts/norepeat.pl
blob: 02aec568e431a4b18d086a879ea44a0d9cafb6d2 (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
use strict;
use Digest::MD5 'md5_hex';

use Irssi qw(settings_add_bool settings_get_bool signal_add signal_add_first signal_stop );

use vars qw($VERSION %IRSSI);
$VERSION = '0.5';
%IRSSI = (
   authors => 'Marcin Rozycki',
   contact => 'derwan@irssi.pl',
   url => 'http://derwan.irssi.pl',
   name => 'norepeat',
   description => 'stops public repeating',
   license => 'GNU GPL v2',
   modules => 'Digest::MD5',
   changed => 'Tue Sep  9 16:34:44 CEST 2003',
);

our $norepeat_enabled = 1;
settings_add_bool('misc', 'norepeat_enabled', $norepeat_enabled);

signal_add('setup changed' => sub {
   $norepeat_enabled = settings_get_bool('norepeat_enabled');
} );

our %last_message = ();
our $last_timeout = 300;

sub check_last_message ($$$$$) {
  my ($server, $data, $nick, $address, $target) = @_;
  my ($time, $nick, $target, $md5) = (time, lc $nick, lc $target, md5_hex($data));
  if ( $norepeat_enabled and my $ref = $last_message{$server->{tag}}{$target}{$nick} ) {
    signal_stop(), return if ( $ref->[0] eq $md5 and $time - $ref->[1] <= $last_timeout  );
  }
  remove_last_message($server, $target, $nick); 
  $last_message{$server->{tag}}{$target}{$nick} = [ $md5, $time ];
}

sub remove_last_message ($$$) {
  my ($server, $target, $nick) = @_;  
  if ( my $ref = delete $last_message{$server->{tag}}{$target}{$nick} ) {
    @{$ref} = (); 
  }
}

sub last_message_clear ($;$) {
  my $chanrec = shift;
  my $target = lc $chanrec->{name};
  foreach my $nick ( keys %{$last_message{$chanrec->{server}->{tag}}{$target}} ) {
     remove_last_message($chanrec->{server}, $target, $nick);
  }
  %{$last_message{$chanrec->{server}->{tag}}{$target}} = ();
}

signal_add_first('message public', \&check_last_message); 
signal_add_first('message irc action', \&check_last_message);
signal_add_first('message irc notice', \&check_last_message);

signal_add('nicklist remove' => sub {
  my ($chanrec, $nickrec) = @_;
  remove_last_message($chanrec->{server}, lc $chanrec->{name}, lc $nickrec->{nick});
});

signal_add('nicklist new' => sub {
  my ($chanrec, $nickrec) = @_;
  remove_last_message($chanrec->{server}, lc $chanrec->{name}, lc $nickrec->{nick});
});

signal_add('nicklist changed' => sub {
  my ($chanrec, $nickrec, $oldnick) = @_;
  $last_message{$chanrec->{server}->{tag}}{lc $chanrec->{name}}{lc $nickrec->{nick}} =
     delete $last_message{$chanrec->{server}->{tag}}{lc $chanrec->{name}}{lc $oldnick};
} );

signal_add('channel created', \&last_message_clear);
signal_add('channel destroyed', \&last_message_clear);