/
peach_picker.pl
344 lines (231 loc) · 8.4 KB
/
peach_picker.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/perl
use strict;
use warnings;
use Storable;
#use Data::Dumper; $Data::Dumper::Useqq = 1;
=head1 NAME
peach_picker - An bot for small, project-based or group-run IRC channels.
=head1 VERSION
Version 0.11
=cut
our $VERSION = '0.11';
=begin comments
If anyone wants to play with this, just make up a channel name and a bot name for use while testing.
=end comments
=cut
#CHANNELS => [ '#atlanta.pm_test', '#atlanta.pm' ],
use constant {
BOT_NICK => 'opbot_Atlanta',
BOT_OWNER => 'Util <bruce.gray@acm.org>',
IRC_SERVER => 'irc.perl.org',
CHANNELS => [ '#atlanta.pm_test' ],
SEEN_FILE => 'opbot_seen.dat',
LOG_DIR => 'opbot_logs',
SAVE_INTERVAL => 20 * 60, # Save state every 20 mins
};
# XXX Make this an external config file! Check for it having been touched since last CHECK_INTERVAL, and reload if so.
my $nick_file = 'nicks.txt';
my %nick_to_auto_op;
if ( not -e $nick_file ) {
print "Warning: Nick config file '$nick_file' not found; skipping\n";
}
else {
open my $fh, '<', $nick_file
or die;
my @lines = <$fh>;
close $fh or warn;
chomp @lines;
# XXX Add code to supress comment lines.
%nick_to_auto_op = map { $_ => 1 } @lines;
}
use POE qw(
Component::IRC::State
Component::IRC::Plugin::Logger
Component::IRC::Plugin::AutoJoin
Component::IRC::Plugin::BotCommand
Component::IRC::Plugin::CycleEmpty
Component::IRC::Plugin::CTCP
);
use POE::Component::IRC::Common qw( parse_user l_irc );
my @irc_states = qw( join part public quit ctcp_action botcmd_seen );
my @poe_states = ( '_start', 'save', map { 'irc_' . $_ } @irc_states );
my $SEEN = ( -s SEEN_FILE ) ? retrieve(SEEN_FILE) : {};
{
my $irc = POE::Component::IRC::State->spawn(
Server => IRC_SERVER,
Nick => BOT_NICK,
Ircname => BOT_OWNER,
# XXX Play with this - it is from http://search.cpan.org/~tbr/POE-Component-IRC-Plugin-Trac-RSS-0.11/lib/POE/Component/IRC/Plugin/Trac/RSS.pm
# Debug => 0,
# Plugin_debug => 1,
# Options => { trace => 0 },
) or die;
# This is so we can see when a start-up is hung by network downtime.
print "IRC connection spawned\n";
POE::Session->create(
heap => { irc => $irc },
package_states => [ main => \@poe_states ],
# options => { trace => 1, debug => 1 },
);
$poe_kernel->run();
}
sub _start {
my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
my $irc = $heap->{irc};
$irc->plugin_add( CTCP => POE::Component::IRC::Plugin::CTCP->new( ) );
$irc->plugin_add( Logger => POE::Component::IRC::Plugin::Logger->new( Path => LOG_DIR ) );
$irc->plugin_add( AutoJoin => POE::Component::IRC::Plugin::AutoJoin->new( Channels => CHANNELS ) );
$irc->plugin_add( BotCommand => POE::Component::IRC::Plugin::BotCommand->new( Commands => { seen => 'Usage: seen <nick>' } ) );
$irc->plugin_add( CycleEmpty => POE::Component::IRC::Plugin::CycleEmpty->new( ) );
$irc->yield( register => @irc_states );
$irc->yield('connect');
$kernel->delay_set( 'save', SAVE_INTERVAL );
return;
}
sub save {
my $kernel = $_[KERNEL];
warn "storing\n";
store( $SEEN, SEEN_FILE )
or die "Can't save state";
$kernel->delay_set( 'save', SAVE_INTERVAL );
}
sub irc_ctcp_action {
my ( $nick, $channel_aref, $text ) = @_[ ARG0 .. $#_ ];
my $channel = $channel_aref->[0];
my $nick_parsed = parse_user($nick);
_add_nick( $nick, "on $channel doing: * $nick_parsed $text" );
}
sub irc_join {
my ( $kernel, $heap, $nick, $channel ) = @_[ KERNEL, HEAP, ARG0 .. $#_ ];
my $nick_parsed = parse_user($nick);
if ( $nick_parsed eq BOT_NICK ) {
warn "Ignore my own joining of a channel.\n";
return;
}
if ( !$nick_to_auto_op{$nick_parsed} ) {
warn "Not op-ing $nick_parsed - not in authorized list\n";
return;
}
my $irc = $heap->{irc};
my $bot_op = $irc->is_channel_operator( $channel, BOT_NICK );
my $user_op = $irc->is_channel_operator( $channel, $nick );
if ( !$bot_op ) {
warn "irc_join - can't op - Bot is not a channel op\n";
}
elsif ( $user_op ) {
warn "irc_join - won't op - User joined, and is somehow already a channel op (maybe due to another bot?)\n";
}
else {
# Definitely use $nick_parsed to change mode; don't use unparsed $nick.
$irc->yield( mode => $channel => '+o' => $nick_parsed );
warn "$channel => '+o' => $nick_parsed\n";
}
_add_nick( $nick, "joining $channel" );
}
sub irc_part {
my ( $nick, $channel, $text ) = @_[ ARG0 .. $#_ ];
my $msg = "parting $channel";
$msg .= " with message '$text'" if defined $text;
_add_nick( $nick, $msg );
}
sub irc_public {
my ( $nick, $channel_aref, $text ) = @_[ ARG0 .. $#_ ];
my $channel = $channel_aref->[0];
_add_nick( $nick, "on $channel saying: $text" );
}
sub irc_quit {
my ( $nick, $text ) = @_[ ARG0 .. $#_ ];
my $msg = 'quitting';
$msg .= " with message '$text'" if defined $text;
_add_nick( $nick, $msg );
}
sub _add_nick {
my ( $nick, $msg ) = @_;
my $nick_parsed = parse_user($nick);
$SEEN->{ l_irc($nick_parsed) } = {
LAST_SEEN_DATE => time,
LAST_SEEN_MSG => $msg,
};
}
sub irc_botcmd_seen {
my ( $heap, $nick, $channel, $target ) = @_[ HEAP, ARG0 .. $#_ ];
my $privmsg;
if ( my $target_href = $SEEN->{ l_irc($target) } ) {
my $time = $target_href->{LAST_SEEN_DATE};
my $msg = $target_href->{LAST_SEEN_MSG};
my $date = localtime $time;
$privmsg = "I last saw $target at $date, $msg";
}
else {
$privmsg = "I haven't seen $target";
}
my $irc = $heap->{irc};
my $nick_parsed = parse_user($nick);
$irc->yield( privmsg => $channel, "$nick_parsed: $privmsg" );
}
__END__
XX Fix all this!
=head1 SYNOPSIS
Quick summary of what the module does.
Perhaps a little code snippet.
use Foo::Bar;
my $foo = Foo::Bar->new();
...
=head1 AUTHOR
Bruce Gray, C<< <bruce.gray at acm.org> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-foo-bar at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Foo-Bar>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Foo::Bar
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Foo-Bar>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Foo-Bar>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Foo-Bar>
=item * Search CPAN
L<http://search.cpan.org/dist/Foo-Bar/>
=back
=head1 TODO
XXX Scan for XXX
XXX Document - usage
XXX Move to GitHub
XXX Rename
let seen work with public, too
This version hoists spawn() out of _start().
Try POE::Component::IRC::Plugin::Console
Features - channel recovery, Seen, auto-op, logging. No karma yet
XXX Does CTCP action still work?
Enumerate ACKNOWLEDGEMENTS
Fix BUGS and SUPPORT sections
Extract most code into new POE::Component::IRC::Plugin:: modules, and contribute to CPAN.
Add tests based on IO::Socket::INET.
Add options (for testing) to select channels, and auth user lists.
Document that this does not work well on servers that do not enforce ircnick registration.
Add support for RSS feeds from our googlecode project
TODO:
Add user info to this message:
irc_join - can't op - Bot is not a channel op
Add a "Ops recovered" message
Status messages to console about which chanels we are in, etc?
Command-line script to dump opbot_seen.dat?
perl -MStorable -wle '%h = %{retrieve(shift)}; printf "%-15s\t%s\t%s\n", $_, scalar(localtime $h{$_}{LAST_SEEN_DATE}), $h{$_}{LAST_SEEN_MSG} for sort keys %h;' opbot_seen.dat
Need to store SEEN info on a per-channel basis
=head1 ACKNOWLEDGEMENTS
All the IRCing members of Atlanta.pm L<http://atlanta.pm.org/>
=head1 COPYRIGHT & LICENSE
Copyright 2009 Bruce Gray.
This program is free software; you can redistribute it and/or
modify it under the terms of either:
=over 4
=item * the GNU General Public License as published by the Free
Software Foundation; either version 1, or (at your option) any
later version, or
=item * the Artistic License version 2.0.
=back
=cut