-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.pl
218 lines (187 loc) · 5.23 KB
/
core.pl
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#use IRC::Message::Object;
use POE qw(Component::IRC Component::SSLify);
use YAML qw(LoadFile);
use Getopt::Long;
use feature 'switch';
use Data::Dumper;
# base functions.
use DemonReach::Base;
# Protocol Line
use DemonReach::Protocol::ShadowIRCd;
# Additional Modules.
use DemonReach::Logging::Plain;
use DemonReach::Logging::Std;
use DemonReach::Logging::IRC
# declerations
my $debug = '';
my $verbose = '';
my $testonly = '';
my $configFile = 'config.yml';
my %irc_public_hooks = ();
my %irc_private_hooks = ();
my @statistics_handlers;
my %logging_handlers = ();
GetOptions(
'verbose|v' => \$verbose,
'debug|d' => \$debug,
'c|config=s' => \$configFile,
't|testonly' => \$testonly
);
# console log stuff
sub verbprint {
my $stuff = shift;
print $stuff . "\n" if $verbose;
}
sub debugprint {
my $stuff = shift;
print "\033[36m" . $stuff . "\033[0m \n" if $debug;
}
# add a hook
sub addHook {
my $type = shift;
my $trigger = shift;
my $code = shift;
given ($type) {
when('private') {
$irc_private_hooks{$trigger} = \$code;
}
when ('public') {
$irc_public_hooks{$trigger} = \$code;
}
}
}
sub addHandler {
my $type = shift;
my $object = shift;
my $target = shift;
$target = "main" if !$target;
given ($type) {
when ('stats') {
push @statistics_handlers, $object;
}
when ('log') {
push @{ $logging_handlers{$target} } , $object;
}
}
}
sub dr_event_log {
my $data = shift;
my $target = shift;
$target = 'main' if not $target;
foreach (split(/,/,$target)) {
foreach (@{$logging_handlers{$_}}){
$_->logEvent($data);
}
}
}
# testonly implies verbose and debug
$verbose = 1 if $testonly;
$debug = 1 if $testonly;
debugprint ("CONFIG TEST MODE ACTIVATED, WILL NOT START AN IRC SESSION") if $testonly;
debugprint "Debug mode activated";
verbprint "Verbose mode activated";
verbprint "Using config file: $configFile";
verbprint "Loading Config File";
my ($configdata) = LoadFile($configFile);
# for debug, dump the YAML object.
debugprint Dumper $configdata;
#
my $hostname = $configdata->{hostname};
my $port = $configdata->{port};
my $nick = $configdata->{nick};
my $realname = $configdata->{realname};
my $username = $configdata->{username};
my $channels = ($configdata->{channels});
verbprint "Connecting to $hostname:$port as '$nick!~$username' with realname '$realname'";
# if SSL is 'on', set $SSL to 1, otherwise, 0
my $ssl = 0;
if ($configdata->{ssl} eq 'on') {
$ssl = 1;
}
verbprint ((sub {if ($ssl == 1) { return "SSL Enabled" } else { return "SSL Disabled" }})->());
my $ignoreMOTD = 0;
if ($configdata->{ignoreMOTD} eq 'on') {
$ignoreMOTD = 1;
}
verbprint ((sub {if ($ignoreMOTD == 1) { return "Ignoring MOTD" } else { return "Not Ignoring MOTD" }})->());
# get Oper creds.
my $operuser = $configdata->{oper}->{username};
my $operpass = $configdata->{oper}->{password};
# POE::IRC stuff
my $irc = POE::Component::IRC->spawn(
nick => $nick,
ircname => $realname,
port=> $port,
server => $hostname,
username => $username,
UseSSL => $ssl,
useipv6 => 1
) or die("Couldn't spawn POE component $!");
# start events
sub _start {
my $heap = $_[HEAP];
# retrieve our component's object from the heap where we stashed it
my $irc = $heap->{irc};
$irc->yield( register => 'all' );
$irc->yield( connect => { } );
return;
}
# default handler
sub _default {
my ($event, $args) = @_[ARG0 .. $#_];
my @output = ( "$event: " );
for my $arg (@$args) {
if ( ref $arg eq 'ARRAY' ) {
push( @output, '[' . join(', ', @$arg ) . ']' );
} else {
push ( @output, "'$arg'" );
}
}
dr_event_log join ' ', @output;
return;
}
sub irc_001 {
my $sender = $_[SENDER];
my $irc = $sender->get_heap();
verbprint "Connected to ", $irc->server_name();
$irc->yield( oper => $operuser => $operpass);
# we join our channels
debugprint Dumper (@{$channels});
foreach(@{$channels}) { debugprint Dumper $_; $irc->yield( join => $_ ) }
return;
}
sub irc_snotice {
my ($sender, $what,$who) = @_[SENDER, ARG0, ARG1 ];
debugprint Dumper handle_snotice($what);
dr_event_log("Got snotice $what", 'irc,main');
return;
}
sub irc_375 {
my ($sender, $what, $who) = @_[SENDER, ARG0, ARG1];
(verbprint ("$who: $what")) if !$ignoreMOTD;
return;
}
sub irc_372 {
my ($sender, $what, $who) = @_[SENDER, ARG0, ARG1];
(verbprint ("$who: $what")) if !$ignoreMOTD;
return;
}
sub irc_376 {
my ($sender, $what, $who) = @_[SENDER, ARG0, ARG1];
(verbprint ("$who: $what")) if !$ignoreMOTD;
return;
}
sub irc_public {
}
# POE Session
POE::Session->create(
package_states => [
main => [ qw(_default _start irc_001 irc_snotice irc_375 irc_376 irc_372) ],
],
heap => { irc => $irc },
);
# run the fucking bot.
addHandler('log',DemonReach::Logging::Plain->new(file => "test.log"));
addHandler('log',DemonReach::Logging::IRC->new(irc => $irc,channel=>"#anope"),"irc");
addHandler('log',DemonReach::Logging::Std->new());
$poe_kernel->run() if !$testonly;