diff options
Diffstat (limited to 'modules')
| -rw-r--r-- | modules/classes.pl | 75 | ||||
| -rw-r--r-- | modules/command.pl | 1 | ||||
| -rw-r--r-- | modules/event.pl | 244 | ||||
| -rw-r--r-- | modules/inspect.pl | 8 | ||||
| -rw-r--r-- | modules/log.pl | 47 | ||||
| -rw-r--r-- | modules/mysql.pl | 121 | ||||
| -rw-r--r-- | modules/util.pl | 41 |
7 files changed, 469 insertions, 68 deletions
diff --git a/modules/classes.pl b/modules/classes.pl index f943976..fc8e9e7 100644 --- a/modules/classes.pl +++ b/modules/classes.pl @@ -26,13 +26,53 @@ sub new "nuhg" => \&nuhg, "levenflood" => \&levenflood, "proxy" => \&proxy, - "nickbl" => \&nickbl + "nickbl" => \&nickbl, + "nickfuzzy" => \&nickfuzzy, + "asciiflood" => \&asciiflood, + "joinmsgquit" => \&joinmsgquit, + "garbagemeter" => \&garbagemeter, + "cyclebotnet" => \&cyclebotnet }; $self->{ftbl} = $tbl; bless($self); return $self; } +sub garbagemeter { + my ($chk, $id, $event, $chan, $rev) = @_; + my @cut = split(/:/, $chk->{content}); + my $limit = int($cut[0]); + my $timeout = int($cut[1]); + my $threshold = int($cut[2]); + my $threshold2 = int($cut[3]); + my $wordcount = 0; + my $line = $event->{args}->[0]; + return 0 unless ($line =~ /^[A-Za-z: ]+$/); + my @words = split(/ /, $line); + return 0 unless ((scalar @words) >= $threshold2); + foreach my $word (@words) { + if (defined($::wordlist{lc $word})) { + $wordcount += 1; + } + return 0 if ($wordcount >= $threshold); + } + return 1 if ( flood_add( $chan, $id, 0, $timeout ) == $limit ); + return 0; +} + +sub joinmsgquit +{ + my ($chk, $id, $event, $chan, $rev) = @_; + my $time = $chk->{content}; +##STATE + $chan = lc $chan; #don't know if this is necessary but I'm trying to track down some mysterious state tracking corruption + return 0 unless defined($::sc{$chan}{users}{lc $event->{nick}}{jointime}); + return 0 unless defined($::sc{$chan}{users}{lc $event->{nick}}{msgtime}); + return 0 if ((time - $::sc{$chan}{users}{lc $event->{nick}}{jointime}) > $time); + return 0 if ((time - $::sc{$chan}{users}{lc $event->{nick}}{msgtime}) > $time); + return 1; +} + sub check { my $self = shift; @@ -40,6 +80,17 @@ sub check return $self->{ftbl}->{$item}->(@_); } +sub nickbl +{ + my ($chk, $id, $event, $chan, $rev) = @_; + my $match = lc $event->{nick}; + foreach my $line (@::nick_blacklist) { + if ($line eq $match) { + return 1; + } + } + return 0; +} sub proxy { my ($chk, $id, $event, $chan, $rev) = @_; @@ -85,7 +136,7 @@ sub levenflood return $ret; } -sub nickbl +sub nickfuzzy { my ($chk, $id, $event, $chan) = @_; my $nick = $event->{nick}; @@ -136,6 +187,26 @@ sub floodqueue { return 0; } +sub asciiflood { + my ($chk, $id, $event, $chan, $rev) = @_; + my @cut = split(/:/, $chk->{content}); + return 0 if (length($event->{args}->[0]) < $cut[0]); + return 0 if ($event->{args}->[0] =~ /[A-Za-z0-9]/); + return 1 if ( flood_add( $chan, $id, $event->{host}, int($cut[2]) ) == int($cut[1]) ); + return 0; +} + +sub cyclebotnet +{ + my ($chk, $id, $event, $chan, $rev) = @_; + my ($cycletime, $queueamt, $queuetime) = split(/:/, $chk->{content}); + $chan = lc $chan; #don't know if this is necessary but I'm trying to track down some mysterious state tracking corruption + return 0 unless defined($::sc{$chan}{users}{lc $event->{nick}}{jointime}); + return 0 if ((time - $::sc{$chan}{users}{lc $event->{nick}}{jointime}) > int($cycletime)); + return 1 if ( flood_add( $chan, $id, "cycle", int($queuetime)) == int($queueamt) ); + return 0; +} + sub nickspam { my ($chk, $id, $event, $chan) = @_; my @cut = split(/:/, $chk->{content}); diff --git a/modules/command.pl b/modules/command.pl index 3acaedd..7576850 100644 --- a/modules/command.pl +++ b/modules/command.pl @@ -5,6 +5,7 @@ use strict; use IO::All; use POSIX qw(strftime); use Data::Dumper; +use URI::Escape; sub new { diff --git a/modules/event.pl b/modules/event.pl index 3ed711c..bd76fb1 100644 --- a/modules/event.pl +++ b/modules/event.pl @@ -6,6 +6,7 @@ use Data::Dumper; use Text::LevenshteinXS qw(distance); use IO::All; use POSIX qw(strftime); +use Regexp::Wildcards; sub cs { my ($chan) = @_; @@ -21,12 +22,6 @@ sub maxlen { return $lb; } -sub alarmdeath -{ - die "SIG ALARM!!!\n"; -} -$SIG{ALRM} = \&alarmdeath; - sub new { my $module = shift; @@ -82,22 +77,24 @@ sub new my $clearstatsp = 1; my %statsp = (); +my %oldstatsp = (); sub on_statsdebug { my ($conn, $event) = @_; my ($char, $line) = ($event->{args}->[1], $event->{args}->[2]); if ($char eq 'p') { + if ($clearstatsp) { + $clearstatsp = 0; + %oldstatsp = %statsp; + %statsp = (); + } if ($line =~ /^(\d+) staff members$/) { #this is the end of the report } else { my ($nick, $userhost) = split(" ", $line); $userhost =~ s/\((.*)\)/$1/; my ($user, $host) = split("@", $userhost); - if ($clearstatsp) { - $clearstatsp = 0; - %statsp = (); - } $statsp{$nick}= [$user, $host]; } } @@ -109,24 +106,55 @@ sub on_endofstats if ($event->{args}->[1] eq 'p') { $clearstatsp=1; my $tmp = Dumper(\%statsp); chomp $tmp; - ASM::Util->dprint($tmp, 'statsp'); + if ( join(",", sort(keys %oldstatsp)) ne join(",", sort(keys %statsp)) ) { + open(FH, '>>', 'statsplog.txt'); + print FH strftime("%F %T ", gmtime) . join(",", sort(keys %statsp)) . "\n"; + close(FH); + ASM::Util->dprint(join(",", keys %statsp), 'statsp'); + } # $event->{args}->[2] == "End of /STATS report" #end of /stats p } } +my $lagcycles = 0; +my $pongcount = 0; + sub on_pong { my ($conn, $event) = @_; - alarm 90; - $conn->schedule( 60, sub { $conn->sl("PING :" . time); } ); + alarm 120; + $conn->schedule( 30, sub { $conn->sl("PING :" . time); } ); ASM::Util->dprint('Pong? ... Ping!', 'pingpong'); -# ASM::Util->dprint(Dumper($event), 'pingpong'); - $conn->sl("STATS p"); my $lag = time - $event->{args}->[0]; if ($lag > 1) { ASM::Util->dprint("Latency: $lag", 'latency'); } + if (($pongcount % 3) == 0) { #easiest way to do something roughly every 90 seconds + $conn->sl('STATS p'); + } + if ((time - $::starttime) < 60 ) { + return; #we don't worry about lag if we've just started up and are still syncing etc. + } + if (($lag > 2) && ($lag < 5)) { + $conn->privmsg( $::settings->{masterchan}, "Warning: I'm currently lagging by $lag seconds."); + } + if ($lag >= 5) { + $lagcycles++; + if ($lagcycles >= 3) { + $conn->quit("Automatic restart triggered due to persistent lag. Freenode staff: If this is happening too frequently, please " . + "set a nickserv freeze on my account, and once my connection is stable, unfreeze the account and /kill me to tri" . + "gger a reconnect."); + } elsif ($lagcycles >=2) { + $conn->privmsg( $::settings->{masterchan}, "Warning: I'm currently lagging by $lag seconds. This marks heavy lag cycle " . + "$lagcycles - automatic restart will be triggered after 3 lag cycles." ); + } + } + if (($lag <= 2) && ($lagcycles > 0)) { + $lagcycles--; +# $conn->privmsg( $::settings->{masterchan}, "Warning: Heavy lag cycle count has been reduced to $lagcycles" ); + ASM::Util->dprint('$lag = ' . $lag . '; $lagcycles = ' . $lagcycles, 'latency'); + } } sub on_dchat @@ -202,6 +230,8 @@ sub on_join { $::sc{$chan}{users}{$nick}{hostmask} = $event->{userhost}; $::sc{$chan}{users}{$nick}{op} = 0; $::sc{$chan}{users}{$nick}{voice} = 0; + $::sc{$chan}{users}{$nick}{jointime} = time; + $::sc{$chan}{users}{$nick}{msgtime} = 0; if (defined($::sn{$nick})) { my @mship = (); if (defined($::sn{$nick}->{mship})) { @@ -219,35 +249,43 @@ sub on_join { $::sn{$nick}->{user} = $event->{user}; $::sn{$nick}->{host} = $event->{host}; $::sn{$nick}->{account} = lc $event->{args}->[0]; - $::inspector->inspect( $conn, $event ) unless $::netsplit; $::db->logg($event); $::log->logg( $event ); + $::inspector->inspect( $conn, $event ) unless $::netsplit; } sub on_part { my ($conn, $event) = @_; - $::inspector->inspect( $conn, $event ); my $nick = lc $event->{nick}; + my $chan = lc $event->{to}->[0]; $::log->logg( $event ); $::db->logg( $event ); + if ($event->{args}->[0] =~ /^requested by/) { + my $idx = $::db->actionlog( $event); + $::log->sqlIncident($chan, $idx) if $idx; + } +# "to" => [ "#antispammeta" ], +# "args" => [ "requested by ow (test)" ], +# "nick" => "aoregcdu", + $::inspector->inspect( $conn, $event ); if (defined($::sn{$nick}) && defined($::sn{$nick}->{mship})) { my @mship = @{$::sn{$nick}->{mship}}; - @mship = grep { lc $_ ne lc $event->{to}->[0] } @mship; + @mship = grep { lc $_ ne $chan } @mship; if ( @mship ) { $::sn{$nick}->{mship} = \@mship; } else { delete($::sn{$nick}); } } - if ( lc $conn->{_nick} eq lc $nick ) + if ( lc $conn->{_nick} eq $nick ) { - delete( $::sc{lc $event->{to}->[0]} ); - on_byechan(lc $event->{to}->[0]); + delete( $::sc{$chan} ); + on_byechan($chan); } else { - delete( $::sc{lc $event->{to}->[0]}{users}{$nick} ); + delete( $::sc{$chan}{users}{$nick} ); } } @@ -265,19 +303,20 @@ sub on_public { my ($conn, $event) = @_; # alarm 200; - $::inspector->inspect( $conn, $event ); + $::sc{lc $event->{to}->[0]}{users}{lc $event->{nick}}{msgtime} = time; $::log->logg( $event ); $::db->logg( $event ); + $::inspector->inspect( $conn, $event ); $::commander->command( $conn, $event ); } sub on_notice { my ($conn, $event) = @_; - return if ( $event->{to}->[0] eq '$*' ); - $::inspector->inspect( $conn, $event ); + return if ( $event->{to}->[0] eq '$*' ); # if this is a global notice FUCK THAT SHIT $::log->logg( $event ); $::db->logg( $event ); + $::inspector->inspect( $conn, $event ); $::services->doServices($conn, $event); } @@ -302,17 +341,24 @@ sub on_quit my ($conn, $event) = @_; my @channels=(); for ( keys %::sc ) { - push ( @channels, $_ ) if delete $::sc{lc $_}{users}{lc $event->{nick}}; + push ( @channels, lc $_ ) if delete $::sc{lc $_}{users}{lc $event->{nick}}; } $event->{to} = \@channels; + my $idx = $::db->actionlog($event); + $::log->sqlIncident( join(',', @channels), $idx ) if $idx; $::db->logg( $event ); + $::log->logg( $event ); + if (($::netsplit == 0) && ($event->{args}->[0] eq "*.net *.split") && (lc $event->{nick} ne 'chanserv')) { #special, netsplit situation $conn->privmsg($::settings->{masterchan}, "Entering netsplit mode - JOIN and QUIT inspection will be disabled for 60 minutes"); $::netsplit = 1; $conn->schedule(60*60, sub { $::netsplit = 0; $conn->privmsg($::settings->{masterchan}, 'Returning to regular operation'); }); } $::inspector->inspect( $conn, $event ) unless $::netsplit; - $::log->logg( $event ); + #ugh. Repurge some shit, hopefully this will fix some stuff where things are going wrong + foreach my $chan ( keys %::sc ) { + delete $::sc{$chan}{users}{lc $event->{nick}}; + } delete($::sn{lc $event->{nick}}); } @@ -335,6 +381,7 @@ sub irc_users $::sc{lc $channel}{users}{lc $_} = {}; $::sc{lc $channel}{users}{lc $_}{op} = $op; $::sc{lc $channel}{users}{lc $_}{voice} = $voice; + $::sc{lc $channel}{users}{lc $_}{jointime} = 0; } } @@ -345,7 +392,6 @@ sub on_names { sub irc_topic { my ($conn, $event) = @_; - $::inspector->inspect($conn, $event) if ($event->{format} ne 'server'); if ($event->{format} eq 'server') { my $chan = lc $event->{args}->[1]; @@ -363,34 +409,42 @@ sub irc_topic { { if ($event->{type} eq 'topic') { - my $chan = lc $event->{args}->[1]; + my $chan = lc $event->{to}->[0]; $::sc{$chan}{topic}{text} = $event->{args}->[0]; $::sc{$chan}{topic}{time} = time; $::sc{$chan}{topic}{by} = $event->{from}; } $::log->logg($event); $::db->logg( $event ); + $::inspector->inspect($conn, $event); } } sub on_nick { my ($conn, $event) = @_; my @channels=(); - for ( keys %::sc ) + my $oldnick = lc $event->{nick}; + my $newnick = lc $event->{args}->[0]; + foreach my $chan ( keys %::sc ) { - if ( defined $::sc{lc $_}{users}{lc $event->{nick}} ) + $chan = lc $chan; + if ( defined $::sc{$chan}{users}{$oldnick} ) { - $::sc{lc $_}{users}{lc $event->{args}->[0]} = $::sc{lc $_}{users}{lc $event->{nick}}; - delete( $::sc{lc $_}{users}{lc $event->{nick}} ); - push ( @channels, lc $_ ); + if ($oldnick ne $newnick) { #otherwise a nick change where they're only + #changing the case of their nick means that + #ASM forgets about them. + $::sc{$chan}{users}{$newnick} = $::sc{$chan}{users}{$oldnick}; + delete( $::sc{$chan}{users}{$oldnick} ); + } + push ( @channels, $chan ); } } - $::sn{lc $event->{args}->[0]} = $::sn{lc $event->{nick}}; + $::sn{$newnick} = $::sn{$oldnick} if ($oldnick ne $newnick); $::db->logg( $event ); - delete( $::sn{lc $event->{nick}}); + delete( $::sn{$oldnick}) if ($oldnick ne $newnick); $event->{to} = \@channels; - $::inspector->inspect($conn, $event); $::log->logg($event); + $::inspector->inspect($conn, $event); } sub on_kick { @@ -399,18 +453,21 @@ sub on_kick { $conn->join($event->{args}->[0]); } my $nick = lc $event->{to}->[0]; + my $chan = lc $event->{args}->[0]; $::log->logg( $event ); $::db->logg( $event ); + my $idx = $::db->actionlog($event); + $::log->sqlIncident($chan, $idx) if $idx; if (defined($::sn{$nick}) && defined($::sn{$nick}->{mship})) { my @mship = @{$::sn{$nick}->{mship}}; - @mship = grep { lc $_ ne lc $event->{to}->[0] } @mship; + @mship = grep { lc $_ ne $chan } @mship; if ( @mship ) { $::sn{$nick}->{mship} = \@mship; } else { delete($::sn{$nick}); } } - if ( lc $conn->{_nick} eq lc $nick ) + if ( lc $conn->{_nick} eq $nick ) { delete( $::sc{lc $event->{args}->[0]} ); on_byechan(lc $event->{to}->[0]); @@ -433,14 +490,26 @@ sub parse_modes $t=$c; } else { #eIbq,k,flj,CFLMPQcgimnprstz - if ( grep( /[eIbqkfljov]/,($c) ) ) { #modes that take args - push (@new_modes, [$t.$c, shift @args]); - } - elsif ( grep( /[CFLMPQcgimnprstz]/, ($c) ) ) { - push (@new_modes, [$t.$c]); - } - else { - die "Unknown mode $c !\n"; + if ($t eq '+') { + if ( grep( /[eIbqkfljov]/,($c) ) ) { #modes that take args WHEN BEING ADDED + push (@new_modes, [$t.$c, shift @args]); + } + elsif ( grep( /[CFLMPQcgimnprstz]/, ($c) ) ) { + push (@new_modes, [$t.$c]); + } + else { + die "Unknown mode $c !\n"; + } + } else { + if ( grep( /[eIbqov]/,($c) ) ) { #modes that take args WHEN BEING REMOVED + push (@new_modes, [$t.$c, shift @args]); + } + elsif ( grep( /[CFLMPQcgimnprstzkflj]/, ($c) ) ) { + push (@new_modes, [$t.$c]); + } + else { + die "Unknown mode $c !\n"; + } } } } @@ -470,12 +539,45 @@ sub on_channelmodeis } } +sub whoGotHit +{ + my ($chan, $mask) = @_; + my $cvt = Regexp::Wildcards->new(type => 'jokers'); + my @affected = (); + if ($mask !~ /^\$/) { + my @div = split(/\$/, $mask); + my $regex = $cvt->convert($div[0]); + foreach my $nick (keys %::sn) { + next unless defined($::sn{$nick}{user}); + if (lc ($nick.'!'.$::sn{$nick}{user}.'@'.$::sn{$nick}{host}) =~ lc $regex) { + push @affected, $nick if defined($::sc{$chan}{users}{$nick}); + } + } + } elsif ($mask =~ /^\$a:(.*)/) { + my @div = split(/\$/, $1); + my $regex = $cvt->convert($div[0]); + foreach my $nick (keys %::sn) { + next unless defined($::sn{$nick}{account}); + if (lc ($::sn{$nick}{account}) =~ lc $regex) { + push @affected, $nick if defined($::sc{$chan}{users}{$nick}); + } + } + } + return @affected; +} + sub on_mode { my ($conn, $event) = @_; my $chan = lc $event->{to}->[0]; +# holy shit, I feel so bad doing this +# I have no idea how or why Net::IRC fucks up modes if they've got a ':' in one of the args +# but you do what you must... + my @splitted = split(/ /, $::lastline); shift @splitted; shift @splitted; shift @splitted; + $event->{args}=\@splitted; if ($chan =~ /^#/) { my @modes = @{parse_modes($event->{args})}; + ASM::Util->dprint(Dumper(\@modes), 'misc'); foreach my $line ( @modes ) { my @ex = @{$line}; @@ -484,23 +586,39 @@ sub on_mode elsif ( $ex[0] eq '+v' ) { $::sc{$chan}{users}{lc $ex[1]}{voice} = 1; } elsif ( $ex[0] eq '-v' ) { $::sc{$chan}{users}{lc $ex[1]}{voice} = 0; } - elsif ( $ex[0] eq '+b') { + elsif ( $ex[0] eq '+b' ) { $::sc{$chan}{bans}{$ex[1]} = { bannedBy => $event->{from}, bannedOn => time }; + if (lc $event->{nick} !~ /^(floodbot)/) { #ignore the ubuntu floodbots 'cause they quiet people a lot + my @affected = whoGotHit($chan, $ex[1]); + if ( (@affected) && (scalar @affected <= 4) ) { + foreach my $victim (@affected) { + my $idx = $::db->actionlog($event, 'ban', $victim); + $::log->sqlIncident( $chan, $idx ) if $idx; + } + } + } } - elsif ( $ex[0] eq '-b') { - delete $::sc{$chan}{bans}{$ex[1]}; - } - elsif ( $ex[0] eq '+q') { + elsif ( $ex[0] eq '-b' ) { delete $::sc{$chan}{bans}{$ex[1]}; } + + elsif ( $ex[0] eq '+q' ) { $::sc{$chan}{quiets}{$ex[1]} = { bannedBy => $event->{from}, bannedOn => time }; + if (lc $event->{nick} !~ /^(floodbot)/) { + my @affected = whoGotHit($chan, $ex[1]); + if ( (@affected) && (scalar @affected <= 4) ) { + foreach my $victim (@affected) { + my $idx = $::db->actionlog($event, 'quiet', $victim); + $::log->sqlIncident( $chan, $idx ) if $idx; + } + } + } } - elsif ( $ex[0] eq '-q') { - delete $::sc{$chan}{quiets}{$ex[1]}; - } + elsif ( $ex[0] eq '-q' ) { delete $::sc{$chan}{quiets}{$ex[1]}; } + else { my ($what, $mode) = split (//, $ex[0]); if ($what eq '+') { if (defined($ex[1])) { push @{$::sc{$chan}{modes}}, $mode . ' ' . $ex[1]; } - else { push @{$::sc{$chan}{modes}}, $mode; } + else { push @{$::sc{$chan}{modes}}, $mode; } } else { my @modes = grep {!/^$mode/} @{$::sc{$chan}{modes}}; $::sc{$chan}{modes} = \@modes; @@ -518,7 +636,7 @@ sub on_mode sub checkRegged { my ($conn, $chan) = @_; - if (grep {/r/} @{$::sc{$chan}{modes}}) { + if (grep {/^r/} @{$::sc{$chan}{modes}}) { my $tgt = $chan; my $risk = "debug"; my $hilite=ASM::Util->commaAndify(ASM::Util->getAlert($tgt, $risk, 'hilights')); @@ -553,6 +671,7 @@ sub on_ctcp { my ($conn, $event) = @_; my $acct = lc $::sn{lc $event->{nick}}->{account}; + ASM::Util->dprint(Dumper($event), 'ctcp'); if (($event->{type} eq 'cdcc') && (defined($::users->{person}->{$acct})) && (defined($::users->{person}->{$acct}->{flags})) && @@ -576,9 +695,7 @@ sub dcc_open sub on_ctcp_source { my ($conn, $event) = @_; - if (lc $event->{args}->[0] eq lc $conn->{_nick}) { - $conn->ctcp_reply($event->{nick}, 'SOURCE http://svn.linuxrulz.org/repos/antispammeta/trunk/'); - } + $conn->ctcp_reply($event->{nick}, 'SOURCE http://svn.linuxrulz.org/repos/antispammeta/trunk/'); } sub on_whoxreply @@ -631,6 +748,13 @@ sub on_whofuckedup { my ($conn, $event) = @_; ASM::Util->dprint('on_whofuckedup called!', 'sync'); + if ($event->{args}->[1] eq "STATS") { +#most likely this is getting called because we did stats p too often. +#unfortunately the server doesn't let us know what exactly we called stats for. +#anyways, we don't need to do anything for this + } else { #dunno why it got called, print the data and I'll add a handler for it. + ASM::Util->dprint(Dumper($event), 'sync'); + } } sub on_bannedfromchan { diff --git a/modules/inspect.pl b/modules/inspect.pl index 24d93d8..4466d69 100644 --- a/modules/inspect.pl +++ b/modules/inspect.pl @@ -31,7 +31,7 @@ sub inspect { } } else { - $iaddr = gethostbyname($event->{host}); + $iaddr = gethostbyname($event->{host}) if ($event->{host} !~ /\//); $rev = join('.', reverse(unpack('C4', $iaddr))).'.' if (defined $iaddr); } ## NB: isn't there a better way to do this with grep, somehow? @@ -74,12 +74,16 @@ sub inspect { if ($id eq 'last_measure_regex') { #TODO: Note that this is another example of things that shouldn't be hardcoded, but are. } - unless (defined($::ignored{$chan}) && ($::ignored{$chan} >= $::RISKS{$dct{$id}{risk}})) { + if ( + (!(defined($::ignored{$chan}) && ($::ignored{$chan} >= $::RISKS{$dct{$id}{risk}}))) || + (($::pacealerts == 0) && ($dct{$id}{risk} eq 'info')) + ) { my @tgts = ASM::Util->getAlert($chan, $dct{$id}{risk}, 'msgs'); ASM::Util->sendLongMsg($conn, \@tgts, $txtz); $::ignored{$chan} = $::RISKS{$dct{$id}{risk}}; $conn->schedule(45, sub { delete($::ignored{$chan})}); } + $::log->incident($chan, "$chan: $dct{$id}{risk} risk: $event->{nick} - $nicereason\n"); delete $dct{$id}{xresult}; } } diff --git a/modules/log.pl b/modules/log.pl index 8b30eab..bdf4821 100644 --- a/modules/log.pl +++ b/modules/log.pl @@ -12,10 +12,43 @@ sub new my $config = shift; my $self = {}; $self->{CONFIG} = $config; + $self->{backlog} = {}; bless($self); return $self; } +sub incident +{ + my $self = shift; + my ($chan, $header) = @_; + $chan = lc $chan; + open(FH, '>>', 'dctlog.txt'); + print FH $header; + if (defined($self->{backlog}->{$chan})) { + print FH join('', @{$self->{backlog}->{$chan}}); + } + print FH "\n\n"; + close(FH); +} + +#writes out the backlog to a file which correlates to ASM's SQL actionlog table +sub sqlIncident +{ + my $self = shift; + my ($channel, $index) = @_; + $channel = lc $channel; + my @chans = split(/,/, $channel); + open(FH, '>', $self->{CONFIG}->{actiondir} . $index . '.txt'); + foreach my $chan (@chans) { + if (defined($self->{backlog}->{$chan})) { + print FH "$chan\n"; + print FH join('', @{$self->{backlog}->{$chan}}); + print FH "\n"; + } + } + close(FH); +} + sub logg { my $self = shift; @@ -35,10 +68,10 @@ sub logg $_ = ''; $_ = "<$event->{nick}> $event->{args}->[0]" if $event->{type} eq 'public'; $_ = "*** $event->{nick} has joined $chan" if $event->{type} eq 'join'; - $_ = "*** $event->{nick} has left $chan" if $event->{type} eq 'part'; + $_ = "*** $event->{nick} has left $chan ($event->{args}->[0])" if $event->{type} eq 'part'; $_ = "* $event->{nick} $event->{args}->[0]" if $event->{type} eq 'caction'; $_ = "*** $event->{nick} is now known as $event->{args}->[0]" if $event->{type} eq 'nick'; - $_ = "*** $event->{nick} has quit IRC" if $event->{type} eq 'quit'; + $_ = "*** $event->{nick} has quit ($event->{args}->[0])" if $event->{type} eq 'quit'; $_ = "*** $event->{to}->[0] was kicked by $event->{nick}" if $event->{type} eq 'kick'; $_ = "-$event->{nick}- $event->{args}->[0]" if $event->{type} eq 'notice'; $_ = "*** $event->{nick} sets mode: " . join(" ",@{$event->{args}}) if $event->{type} eq 'mode'; @@ -46,8 +79,18 @@ sub logg my $nostamp = $_; $_ = strftime($cfg->{timefmt}, @time) . $_ . "\n"; my $line = $_; + my @backlog = (); + if (defined($self->{backlog}->{$chan})) { + @backlog = @{$self->{backlog}->{$chan}}; + if (scalar @backlog >= 30) { + shift @backlog; + } + } + push @backlog, $line; + $self->{backlog}->{$chan} = \@backlog; if (open(FH, $path)) { # or die "Can't open $path: $!"; print FH $line; + ASM::Util->dprint($line, 'logger'); close(FH); } else { print "COULDN'T PRINT TO $path - $line"; diff --git a/modules/mysql.pl b/modules/mysql.pl index d0cee00..7484ca3 100644 --- a/modules/mysql.pl +++ b/modules/mysql.pl @@ -6,13 +6,14 @@ use DBI; sub new { my $module = shift; - my ($db, $host, $port, $user, $pass, $table, $dblog) = @_; + my ($db, $host, $port, $user, $pass, $table, $actiontable, $dblog) = @_; my $self = {}; $self->{DBH} = DBI->connect("DBI:mysql:database=$db;host=$host;port=$port", $user, $pass); $self->{DBH_LOG} = DBI->connect("DBI:mysql:database=$dblog;host=$host;port=$port", $user, $pass); $self->{DBH}->{mysql_auto_reconnect} = 1; $self->{DBH_LOG}->{mysql_auto_reconnect} = 1; $self->{TABLE} = $table; + $self->{ACTIONTABLE} = $actiontable; bless($self); return $self; } @@ -60,6 +61,122 @@ sub record $dbh->quote($id) . ", " . $dbh->quote($reason) . ");"); } +sub actionlog +{ + my ($self, $event, $modedata1, $modedata2) = @_; + my $dbh = $self->{DBH}; + my ($action, $reason, $channel, + $nick, $user, $host, $gecos, $account, $ip, + $bynick, $byuser, $byhost, $bygecos, $byaccount); + + if ($event->{type} eq 'mode') { + $action = $modedata1; + $nick = $modedata2; + $channel = lc $event->{to}->[0]; + $bynick = $event->{nick}; + $byuser = $event->{user}; + $byhost = $event->{host}; + } elsif ($event->{type} eq 'quit') { + my $quitmsg = $event->{args}->[0]; + if ($quitmsg =~ /^Killed \((\S+) \((.*)\)\)$/) { + $bynick = $1; + $reason = $2 unless ($2 eq '<No reason given>'); + return if ($reason =~ /Nickname regained by services/); + $action = 'kill'; + } elsif ($quitmsg =~ /^K-Lined$/) { + $action = 'k-line'; + } else { + return; #quit not forced/tracked + } + $nick = $event->{nick}; + $user = $event->{user}; + $host = $event->{host}; + } elsif (($event->{type} eq 'part') && ($event->{args}->[0] =~ /^requested by (\S+) \((.*)\)/)) { + $bynick = $1; + $reason = $2 unless (lc $reason eq lc $event->{nick}); + $action = 'remove'; + $nick = $event->{nick}; + $user = $event->{user}; + $host = $event->{host}; + $channel = $event->{to}->[0]; + } elsif ($event->{type} eq 'kick') { + $action = 'kick'; + $bynick = $event->{nick}; + $byuser = $event->{user}; + $byhost = $event->{host}; + $reason = $event->{args}->[1] unless ($event->{args}->[1] eq $event->{to}->[0]); + $nick = $event->{to}->[0]; + $channel = $event->{args}->[0]; + } + return unless defined($action); +# $bynick = lc $bynick if defined $bynick; #we will lowercase the NUHGA info later. + if ( (defined($bynick)) && (defined($::sn{lc $bynick})) ) { #we have the nick taking the action available, fill in missing NUHGA info + $byuser = $::sn{lc $bynick}{user} unless defined($byuser); + $byhost = $::sn{lc $bynick}{host} unless defined($byhost); + $bygecos = $::sn{lc $bynick}{gecos} unless defined($bygecos); + $byaccount = $::sn{lc $bynick}{account} unless defined($byaccount); + if (($byaccount eq '0') or ($byaccount eq '*')) { + $byaccount = undef; + } + } +# $nick = lc $nick if defined $nick; + if ( (defined($nick)) && (defined($::sn{lc $nick})) ) { #this should always be true, else something has gone FUBAR + $user = $::sn{lc $nick}{user} unless defined($user); + $host = $::sn{lc $nick}{host} unless defined($host); + $gecos = $::sn{lc $nick}{gecos} unless defined($gecos); + $account = $::sn{lc $nick}{account} unless defined($account); + if (($account eq '0') or ($account eq '*')) { + $account = undef; + } + $ip = ASM::Util->getNickIP(lc $nick); + } +# my ($action, $reason, $channel, +# $nick, $user, $host, $gecos, $account, $ip +# $bynick, $byuser, $byhost, $bygecos, $byaccount); +#Now, time to escape/NULLify everything + $action = $dbh->quote($action); + if (defined($reason)) { $reason = $dbh->quote($reason); } else { $reason = 'NULL'; } +## removed lc's from everything except IP + if (defined($channel)) { $channel = $dbh->quote($channel); } else { $channel = 'NULL'; } + + if (defined($nick)) { $nick = $dbh->quote($nick); } else { $nick = 'NULL'; } + if (defined($user)) { $user = $dbh->quote($user); } else { $user = 'NULL'; } + if (defined($host)) { $host = $dbh->quote($host); } else { $host = 'NULL'; } + if (defined($gecos)) { $gecos = $dbh->quote($gecos); } else { $gecos = 'NULL'; } + if (defined($account)) { $account = $dbh->quote($account); } else { $account = 'NULL'; } + if (defined($ip)) { $ip = $dbh->quote($ip); } else { $ip = 'NULL'; } + + if (defined($bynick)) { $bynick = $dbh->quote($bynick); } else { $bynick = 'NULL'; } + if (defined($byuser)) { $byuser = $dbh->quote($byuser); } else { $byuser = 'NULL'; } + if (defined($byhost)) { $byhost = $dbh->quote($byhost); } else { $byhost = 'NULL'; } + if (defined($bygecos)) { $bygecos = $dbh->quote($bygecos); } else { $bygecos = 'NULL'; } + if (defined($byaccount)) { $byaccount = $dbh->quote($byaccount); } else { $byaccount = 'NULL'; } + my $sqlstr = "INSERT INTO $self->{ACTIONTABLE} " . + "(action, reason, channel, " . + "nick, user, host, gecos, account, ip, " . + "bynick, byuser, byhost, bygecos, byaccount)" . + " VALUES " . + "($action, $reason, $channel, " . + "$nick, $user, $host, $gecos, $account, $ip, " . + "$bynick, $byuser, $byhost, $bygecos, $byaccount);"; + ASM::Util->dprint( $sqlstr, 'mysql' ); + $dbh->do( $sqlstr ); + return $dbh->last_insert_id(undef, undef, $self->{ACTIONTABLE}, undef); +# $::sn{ow} looks like: +#$VAR1 = { +# "account" => "afterdeath", +# "gecos" => "William Athanasius Heimbigner", +# "user" => "icxcnika", +# "mship" => [ +# "#baadf00d", +# "#antispammeta-debug", +# "#antispammeta" +# ], +# "host" => "freenode/weird-exception/network-troll/afterdeath" +# }; + +} + #FIXME: This function is shit. Also, it doesn't work like I want it to with mode. sub logg { @@ -110,7 +227,7 @@ sub logg $string = $string . ', ' . $dbh->quote($event->{args}->[1]); } $string = $string . ');'; - ASM::Util->dprint($string, "mysql"); +# ASM::Util->dprint($string, "mysql"); $dbh->do($string); } diff --git a/modules/util.pl b/modules/util.pl index dc3f285..54e25a6 100644 --- a/modules/util.pl +++ b/modules/util.pl @@ -183,6 +183,47 @@ sub dprint { print STDERR $text, "\n"; } +sub dottedQuadToInt +{ + my ($module, $dottedquad) = @_; + my $ip_number = 0; + my @octets = split(/\./, $dottedquad); + foreach my $octet (@octets) { + $ip_number <<= 8; + $ip_number |= $octet; + } + return $ip_number; +} + +sub getNickIP +{ + my ($module, $nick) = @_; + $nick = lc $nick; + return unless defined($::sn{$nick}); + if (defined($::sn{$nick}{ip})) { + return $::sn{$nick}{ip}; + } + my $host = $::sn{$nick}{host}; + if ( ($host =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) or + ($host =~ /^gateway\/web\/freenode\/ip\.(\d+)\.(\d+)\.(\d+)\.(\d+)$/) ) { + #yay, easy IP! + $::sn{$nick}{ip} = dottedQuadToInt(undef, "$1.$2.$3.$4"); + return $::sn{$nick}{ip}; + } elsif (index($host, '/') != -1) { + return; + } elsif ($host =~ /^2001:0:/) { + my @splitip = split(/:/, $host); + #I think I can just do (hex($splitip[6] . $splitip[7]) ^ hex('ffffffff')) here but meh + my $host = join('.', unpack('C4', pack('N', (hex($splitip[6] . $splitip[7])^hex('ffffffff'))))); + $::sn{$nick}{ip} = dottedQuadToInt(undef, $host); + return $::sn{$nick}{ip}; + } + my @resolve = gethostbyname($::sn{$nick}{host}); + return unless @resolve; + $::sn{$nick}{ip} = dottedQuadToInt(undef, join('.', unpack('C4', $resolve[4]))); + return $::sn{$nick}{ip}; +} + sub notRestricted { my ($module, $nick, $restriction) = @_; $nick = lc $nick; |
