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
·165 lines (123 loc) · 4.74 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
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
#!/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 %opstate;
my %config = read_config();
while( my ($server_addr,$server) = each %{$config{server}} ) {
my @username;
if( exists $server->{username} && defined $server->{username} ) {
@username = ( username => $server->{username} );
}
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},
@username,
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";
for my $channel ( keys %{ $server->{channel} } ) {
# flag that we don't have op yet for this channel
$opstate{$server_addr}{$channel}{state} = 0;
}
# 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/^#//;
# See if we have op privileges before we go gallivanting about
# attempting to give +op and ticking off the server
if( $opstate{$server_addr}{$channel}{state} ) {
# 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 );
}
}
},
irc_mode => sub {
my ( $who, $where, $what, $towhom ) = @_[ ARG0 .. ARG3 ];
my $nick = ( split /!/, $who )[0];
my $channel = ref $where ? $where->[0] : $where;
$channel =~ s/^#//;
if( defined $towhom && $towhom eq $server->{nick} ) {
if( $what eq '+o' ) {
# I just received op privileges
$opstate{$server_addr}{$channel}{state} = 1;
}
elsif( $what eq '-o' ) {
$opstate{$server_addr}{$channel}{state} = 0;
}
}
},
irc_notice => sub {
my ( $sender, $what, $message ) = @_[ ARG0 .. ARG2 ];
my $i = 0;
if( $sender =~ /NickServ/i && $message =~ /This nickname is registered/ ) {
# do we have a password for our nick for this server?
if( exists $config{server}{$server_addr}{password} ) {
# Register our nick
$irc->yield( privmsg => 'NickServ', "identify " . $config{server}{$server_addr}{password} );
return;
}
}
},
},
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;
}