Debian Codegraph @

lib/POE/Component/IRC/Cookbook/BasicBot.pod

raw
1 =encoding utf8
2
3 =head1 NAME
4
5 POE::Component::IRC::Cookbook::BasicBot - A basic IRC bot
6
7 =head1 SYNOPSIS
8
9 This a very basic bot that connects to IRC, joins a few channels, and announces
10 its arrival.
11
12 =head1 DESCRIPTION
13
14 We start off quite simply:
15
16 #!/usr/bin/env perl
17
18 use strict;
19 use warnings;
20
21 Then we C<use> the stuff we're going to...well, use. C<::State> is a subclass
22 which keeps track of state information related to channels and nicknames. It is
23 needed by the C<AutoJoin> plugin which takes care of keeping us on our channels.
24
25 use POE;
26 use POE::Component::IRC::State;
27 use POE::Component::IRC::Plugin::AutoJoin;
28
29 Next up is our POE session. We create it and list our event handlers. We then
30 start the POE kernel.
31
32 POE::Session->create(
33 package_states => [
34 main => [ qw(_start irc_join) ]
35 ]
36 );
37
38 $poe_kernel->run();
39
40 Now all we have to do is write the handlers for C<_start> and C<irc_join>.
41 In C<_start>, we create our IRC component, add an C<AutoJoin> plugin, register
42 for the C<irc_join> event, and connect to the IRC server.
43
44 sub _start {
45 my $irc = POE::Component::IRC::State->spawn(
46 Nick => 'basic_bot',
47 Server => 'irc.freenode.net',
48 );
49
50 $irc->plugin_add('AutoJoin', POE::Component::IRC::Plugin::AutoJoin->new(
51 Channels => [ '#test_channel1', '#test_channel2' ]
52 ));
53
54 $irc->yield(register => 'join');
55 $irc->yield('connect');
56 }
57
58 Now comes our C<irc_join> event handler. We send a message to the channel
59 once we've joined it.
60
61 sub irc_join {
62 my $nick = (split /!/, $_[ARG0])[0];
63 my $channel = $_[ARG1];
64 my $irc = $_[SENDER]->get_heap();
65
66 # only send the message if we were the one joining
67 if ($nick eq $irc->nick_name()) {
68 $irc->yield(privmsg => $channel, 'Hi everybody!');
69 }
70 }
71
72 That's it!
73
74 =head1 AUTHOR
75
76 Hinrik E<Ouml>rn SigurE<eth>sson, [email protected]
77