This repository has been archived by the owner on Feb 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
radicalweed.pl
executable file
·101 lines (82 loc) · 2.8 KB
/
radicalweed.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
#!/usr/bin/env perl
use strict;
use warnings;
chdir $ARGV[0] if @ARGV;
use POE qw(Component::IRC);
use Config::General;
use Hash::Util qw/ lock_hash /;
#use DB_File::Lock;
my %config = read_config();
while( my ($server_addr,$server) = each %{$config{server}} ) {
print "setting up $server_addr...\n";
# We create a new PoCo-IRC object
my $irc = POE::Component::IRC->spawn(
nick => $server->{nick},
ircname => $server->{description},
server => $server_addr,
) or die "Oh noooo! $!";
POE::Session->create(
package_states => [main => [qw[ _start _default ]]],
inline_states => {
# on server welcome message
irc_001 => sub {
my $sender = $_[SENDER];
# Since this is an irc_* event, we can get the component's object by
# accessing the heap of the sender. Then we register and connect to the
# specified server.
my $irc = $sender->get_heap();
print "$server_addr: connected to ", $irc->server_name, "\n";
# we join our channels
$irc->yield( join => "#$_" ) for keys %{ $server->{channel} };
},
# when someone joins a channel
irc_join => sub {
my ( $sender, $who, $where, $what ) = @_[ SENDER, ARG0 .. ARG2 ];
# parse out the nick and channel
my $nick = ( split /!/, $who )[0];
my $channel = ref $where ? $where->[0] : $where;
$channel =~ s/^#//;
# give them op if they are in the (reread) config
%config = read_config();
if( exists $config{server}{$server_addr}{channel}{$channel}{ops}{$nick} ) {
print "$server_addr: Gave op to $nick in #$channel.\n";
$irc->yield( mode => "#$channel" => '+o' => $nick );
}
},
},
heap => { irc => $irc },
);
}
$poe_kernel->run();
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;
}
# We registered for all events, this will produce some debug info.
sub _default {
#_print_debug(@_)
}
############## HELPER SUBS ######################
sub read_config {
my %h = Config::General->new('./radicalweed.conf')->getall;
lock_hash %h;
return %h;
}
sub _print_debug {
my ( $event, $args ) = @_[ ARG0 .. $#_ ];
my @output = ("$event: ");
for my $arg (@$args) {
if ( ref $arg eq 'ARRAY' ) {
push( @output, '[' . join( ', ', @$arg ) . ']' );
}
else {
push( @output, "'$arg'" );
}
}
print join ' ', @output, "\n";
return 0;
}