summaryrefslogtreecommitdiffstats
path: root/lib/ASM/Statsp.pm
blob: a83f55c11fa742faf3450769c0d105dc1f70b05b (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
package ASM::Statsp;
no autovivification;
use warnings;
use strict;

use Data::Dumper;
use IO::All;
use POSIX qw(strftime);
use ASM::Util;

no if $] >= 5.017011, warnings => 'experimental::smartmatch';

sub new
{
	my $module = shift;
	my ($conn) = @_;
	my $self = {};
	$self->{CONN} = $conn;
	bless($self);
	$conn->add_handler('statsdebug', \&on_statsdebug, 'after');
	$conn->add_handler('endofstats', \&on_endofstats, 'after');
	$conn->add_handler('263', \&on_whofuckedup, 'after');
	$conn->schedule(180, sub { $conn->sl('STATS p'); });
	return $self;
}

my $clearstatsp = 1;
my $ratelimited = 0;
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);
			$statsp{$nick}= [$user, $host];
		}
	}
}

sub on_endofstats
{
	my ($conn, $event) = @_;
	if ($event->{args}->[1] eq 'p') {
		$clearstatsp=1;
		my $tmp = Dumper(\%statsp); chomp $tmp;
		if ( join(',', sort(keys %oldstatsp)) ne join(',', sort(keys %statsp)) ) {
			open(FH, '>>', 'statsplog.txt');
			say FH strftime('%F %T ', gmtime) . join(',', sort(keys %statsp));
			close(FH);
			ASM::Util->dprint(join(",", keys %statsp), 'statsp');
		}
		# $event->{args}->[2] == "End of /STATS report"
		#end of /stats p
		$conn->schedule( 90, sub { $conn->sl('STATS p') } ) unless $ratelimited;
		$ratelimited = 0;
	}
}

sub on_whofuckedup
{
	my ($conn, $event) = @_;
	if ($event->{args}->[1] eq "STATS") {
		$ratelimited = 1;
		$conn->schedule(30, sub { $conn->sl('STATS p') } );
	}
}

1;
# vim: ts=8:sts=8:sw=8:noexpandtab