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
|
package Crawl::Bot;
use Moose;
use MooseX::NonMoose;
extends 'Bot::BasicBot';
use File::Path;
use Module::Pluggable (
instantiate => 'new',
sub_name => 'create_plugins',
);
has [qw(username name)] => (
# don't need (or want) accessors, just want to initialize the hash slot
# for B::BB to use
is => 'bare',
isa => 'Str',
default => sub { shift->nick },
);
has data_dir => (
is => 'ro',
isa => 'Str',
lazy => 1,
default => sub {
return File::Spec->rel2abs('dat');
},
);
has update_time => (
is => 'ro',
isa => 'Int',
default => 5,
);
has force_update_frequency => (
is => 'ro',
isa => 'Int',
default => 60, # Measured in ticks
);
has plugins => (
is => 'ro',
isa => 'ArrayRef[Crawl::Bot::Plugin]',
lazy => 1,
default => sub { [__PACKAGE__->create_plugins(bot => shift)] },
);
has max_length => (
is => 'ro',
isa => 'Int',
default => 410
);
sub BUILD {
my $self = shift;
File::Path::mkpath($self->data_dir);
$self->plugins;
}
before say => sub {
my $self = shift;
my %params = @_;
warn "sending '$params{body}' to $params{channel}";
$_->sent({%params, who => $self->nick}) for @{ $self->plugins };
};
my $tickcount = 0;
sub tick {
my $self = shift;
my $pokefile = File::Spec->catfile($self->data_dir, "poke");
if (++$tickcount < $self->force_update_frequency and ! -f $pokefile) {
return $self->update_time;
} else {
$tickcount = 0;
-f $pokefile and unlink $pokefile;
}
print STDERR "Checking for updates at " . localtime() . ":\n";
for (@{ $self->plugins }) {
print STDERR " --- " . ref($_);
my $before = time;
$_->tick;
printf STDERR " [%d sec]\n", time - $before
}
print STDERR "Done\n";
return $self->update_time;
}
for my $meth (qw(said emoted chanjoin chanpart
nick_change kicked topic userquit)) {
__PACKAGE__->meta->add_method($meth => sub {
my $self = shift;
$_->$meth(@_) for @{ $self->plugins };
undef;
});
}
sub say_all {
my $self = shift;
my ($message) = @_;
$self->say(
channel => $_,
body => $message,
) for $self->channels;
}
1;
|