/
a8bot.pm
151 lines (136 loc) · 2.74 KB
/
a8bot.pm
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
package a8bot;
use feature ':5.10';
use a8bot::Plugin;
use threads;
use Moose;
use MooseX::NonMoose;
use Module::Pluggable sub_name => 'pluggable', require => 1;
use AnyEvent;
use AnyEvent::IRC::Client;
# temporary?
use Carp::Always::Color;
use Data::Dumper;
extends 'AnyEvent::IRC::Client';
has 'channel' => (
is => 'ro',
isa => 'Str',
required => 1,
documentation => 'A channel on which bot resides on',
);
has 'server' => (
is => 'ro',
isa => 'Str',
required => 1,
documentation => 'A server bot connects to',
);
has 'nick' => (
is => 'ro',
isa => 'Str',
default => 'a8bot',
documentation => 'An IRC nickname bot is using',
);
has 'passwd' => (
is => 'ro',
isa => 'Str',
documentation => 'An IRC password',
);
has 'plugins' => (
is => 'rw',
isa => 'ArrayRef[a8bot::Plugin]',
default => sub { [] },
traits => [ 'Array' ],
handles => {
add_plugin => 'push',
list_plugins => 'elements',
},
documentation => 'Array of plugins we use',
);
has 'port' => (
is => 'ro',
isa => 'Int',
default => '6667',
documentation => 'A port of a server bot is connecting to',
);
has 'verbose' => (
is => 'rw',
isa => 'Bool',
default => 0,
documentation => 'Whether to enable verbosity',
);
has 'wantconnection' => (
is => 'rw',
isa => 'Bool',
default => 1,
documentation => 'Whether to reconnect on disconnect event',
);
sub BUILD {
my $self = shift;
foreach my $plugin ($self->pluggable) {
my $plug = a8bot::Plugin->new(
bot => $self,
plugin => $plugin,
);
$self->add_plugin($plug);
}
$self->reg_cb(
disconnect => sub {
if ($self->wantconnection) {
$self->log("We still want connection, reconnecting");
$self->connect(
$self->server,
$self->port,
{ nick => $self->nick,
password => $self->passwd },
);
}
},
publicmsg => sub {
my ($client, $channel, $params) = @_;
foreach my $plugin ($self->list_plugins) {
my $thr = threads->create(
sub { $plugin->publicmsg(@_) },
$channel,
$params,
);
$thr->detach();
}
},
registered => sub {
my $client = shift;
$client->send_srv(JOIN => $self->channel);
# TODO: Some callback maybe? Rather for
# logging purposes
$client->enable_ping(60);
foreach my $plugin ($self->list_plugins) {
$plugin->registered;
}
},
);
}
#sub cleanup {
# my $self = shift;
# say "Exiting";
# $self->wantconnection(0);
# $self->disconnect;
# # TODO: Let the plugins know
# exit 0;
#}
sub log {
my ($self, @args) = @_;
if ($self->verbose) {
say @args;
}
}
# Tu się zaczyna ten cały burdel
sub run {
my $self = shift;
my $j = AnyEvent->condvar;
$self->connect(
$self->server,
$self->port,
{ nick => $self->nick, password => $self->passwd }
);
$j->wait;
}
__PACKAGE__->meta->make_immutable;
1;