summaryrefslogtreecommitdiffstats
path: root/modules/classes.pl
blob: 3ccd1133edbede1b6006829f6be5f24d0a6a98c6 (plain) (blame)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package ASM::Classes;

use strict;
use warnings;
my %sf = ();

sub new
{
  my $module = shift;
  my $self = {};
  my $tbl = {
    "dnsbl" => \&dnsbl,
    "floodqueue" => \&floodqueue,
    "nickspam" => \&nickspam,
    "splitflood" => \&splitflood,
    "re" => \&re,
    "nick" => \&nick,
    "ident" => \&ident,
    "host" => \&host,
    "gecos" => \&gecos,
    "nuhg" => \&nuhg,
  };
  $self->{ftbl} = $tbl;
  bless($self);
  return $self;
}

sub check {
  my $self = shift;
  my $item = shift;
  return $self->{ftbl}->{$item}->(@_);
}

sub dnsbl {
  my ($xchk, $id, $event, $chan, $rev) = @_;
  my %chk = %{$xchk};
  return unless index($event->{host}, '/') == -1;
  if (defined $rev) {
    my $iaddr = gethostbyname( "$rev$chk{content}" );
    my @dnsbl = unpack( 'C4', $iaddr ) if defined $iaddr;
    return 1 if (@dnsbl);
  }
  return 0;
}

sub floodqueue {
  my ($xchk, $id, $event, $chan, $rev) = @_;
  my %chk = %{$xchk};
  my @cut = split(/:/, $chk{content});
  return 1 if ( flood_add( $chan, $id, $event->{host}, int($cut[1]) ) == int($cut[0]) );
  return 0;
}

sub nickspam {
  my ($chk, $id, $event, $chan) = @_;
  my @cut = split(/:/, $chk->{content});
  if ( length $event->{args}->[0] >= int($cut[0]) ) {
    %_ = map { $_=>$_ } lc keys %{$::sc{lc $chan}{users}};
    my @uniq = grep( $_{$_}, split( / / , lc $event->{args}->[0]) );
    return 1 if ( $#{ @uniq } >= int($cut[1]) );
  }
  return 0;
}

my %cf=();
my %bs=();

sub splitflood {
  my ($chk, $id, $event, $chan) = @_;
  my $text;
  my @cut = split(/:/, $chk->{content});
  $cf{$id}{timeout}=int($cut[1]);
  if ($event->{type} =~ /^(public|notice|part|caction)$/) {
    $text=$event->{args}->[0];
  }
  return unless defined($text);
  return unless length($text) >= 10;
  if (defined($bs{$id}{$text}) && (time <= $bs{$id}{$text} + 600)) {
    return 1;
  }
  push( @{$cf{$id}{$chan}{$text}}, time );
  foreach my $nid ( keys %cf ) {
    foreach my $xchan ( keys %{$cf{$nid}} ) {
      next if $xchan eq 'timeout';
      foreach my $host ( keys %{$cf{$nid}{$xchan}} ) {
        next unless defined $cf{$nid}{$xchan}{$host}[0];
        while ( time >= $cf{$nid}{$xchan}{$host}[0] + $cf{$nid}{'timeout'} ) {
          last if ( $#{ $cf{$nid}{$xchan}{$host} } == 0 );
          shift ( @{$cf{$nid}{$xchan}{$host}} );
        }
      }
    }
  }
  if ( $#{ @{$cf{$id}{$chan}{$text}}}+1 == int($cut[0]) ) {
    $bs{$id}{$text} = time;
    return 1;
  }
  return 0;
} 

sub re {
  my ($chk, $id, $event, $chan) = @_;
  my $match = $event->{args}->[0];
  $match = $event->{nick} if ($event->{type} eq 'join');
  if ( defined($chk->{nocase}) ) {
    return 1 if ($match =~ /$chk->{content}/i);
  } else {
    return 1 if ($match =~ /$chk->{content}/);
  }
  return 0;
}

sub nick {
  my ($chk, $id, $event, $chan) = @_;
  if ( lc $event->{nick} eq lc $chk->{content} ) {
    return 1;
  }
  return 0;
}

sub ident {
  my ( $chk, $id, $event, $chan) = @_;
  if ( lc $event->{user} eq lc $chk->{content} ) {
    return 1;
  }
  return 0;
}

sub host {
  my ( $chk, $id, $event, $chan) = @_;
  if ( lc $event->{host} eq lc $chk->{content} ) {
    return 1;
  }
  return 0;
}

sub gecos {
  my ( $chk, $id, $event, $chan) = @_;
  if ( lc $::sn{lc $event->{nick}}->{gecos} eq lc $chk->{content} ) {
    return 1;
  }
  return 0;
}

sub nuhg {
  my ( $chk, $id, $event, $chan) = @_;
  my $match = $event->{from} . '!' . $::sn{lc $event->{nick}}->{gecos};
  if ( defined($chk->{nocase}) ) {
    return 1 if ($match =~ /$chk->{content}/i);
  } else {
    return 1 if ($match =~ /$chk->{content}/);
  }
  return 0;
}

sub flood_add {
    my ( $chan, $id, $host, $to ) = @_;
    push( @{$sf{$id}{$chan}{$host}}, time );
    while ( time >= $sf{$id}{$chan}{$host}[0] + $to ) {
      last if ( $#{ $sf{$id}{$chan}{$host} } == 0 );
      shift( @{$sf{$id}{$chan}{$host}} );
    }
    return $#{ @{$sf{$id}{$chan}{$host}}}+1;
}

sub flood_process {
  for my $id ( keys %sf ) {
    for my $chan ( keys %{$sf{$id}} ) {
      for my $host ( keys %{$sf{$id}{$chan}} ) {
        next unless defined $sf{$id}{$chan}{$host}[0];
        while ( time >= $sf{$id}{$chan}{$host}[0] + $sf{$id}{'timeout'} ) {
          last if ( $#{ $sf{$id}{$chan}{$host} } == 0 );
          shift ( @{$sf{$id}{$chan}{$host}} );
        }
      }
    }
  }
}

return 1;