<?xml version="1.0" encoding="windows-1252"?>
<node id="118732" title="Jabber Bot For Chatterbox" created="2001-10-14 03:42:24" updated="2005-08-14 21:17:00">
<type id="121">
perlcraft</type>
<author id="117788">
Hutta</author>
<data>
<field name="doctext">
#!/usr/bin/perl

#===[ MonkBot ]===============================================================
#
# Jabber bot that serves as an IM gateway to the perlmonks.org chatterbox.
# Homepage: http://hutta.com/perl/jabberbots/monkbot.html
#
# Note that this requires Net::Jabber, Net::Jabber::Bot and PerlMonks::Chat
# http://download.jabber.org/perl/
# http://hutta.com/perl/jabberbots/
# http://www.cerias.purdue.edu/homes/zamboni/perlmonks.html
#
# My personal MonkBot is on jabber as MonkBot@jabber.icgcorp.net.
# Add him to your roster and you can test this out.
#
#===[ TODO ]==================================================================
#
# - Let MonkBot take user/pass to allow people to send messages as themselves.
# - More integration with PM... New nodes, etc, etc, etc.
#
#=============================================================================

use Net::Jabber::Bot;
use PerlMonks::Chat;

#===[ Initialize ]============================================================

my $pm = new PerlMonks::Chat;
$pm-&gt;add_cookies;

my $bot = new Net::Jabber::Bot(
        client  =&gt; 'MonkBot',
        verbos  =&gt; '2',
        logfile =&gt; '/tmp/monkbot.log',
        version =&gt; '1.0',
        status  =&gt; 'Meditating',
);

$bot-&gt;connect(
        hostname =&gt; 'jabber.com',
        port     =&gt; '5222'
) || die "Can't connect"; 

$bot-&gt;auth(
        username =&gt; 'MonkBot',
        password =&gt; 'passwordhere',
        resource =&gt; 'bot',
        digest   =&gt; '1'
);

$bot-&gt;send_presence();

#===[ Defining Callbacks ]====================================================

$bot-&gt;set_callback( "hello" =&gt; \&amp;SayHello );
$bot-&gt;set_callback( "hi"    =&gt; \&amp;SayHello );
$bot-&gt;set_callback( "help"  =&gt; \&amp;SayHello );
$bot-&gt;set_callback( "all"   =&gt; \&amp;SendAll );

sub SayHello {
        my $user = shift;
        $user-&gt;write("Greetings.");
        $user-&gt;write("Add me to your roster/buddylist and I'll send you every" . 
                     "thing that happens on the chatterbox automatically.  " .
                     "Send the command 'all' to get all lines currently " . 
                     "available.");
}

sub SendAll {
        my $user = shift;
        my ($lines) = ($pm-&gt;getalllines(1,1));
        foreach (@{$lines}) {
                $user-&gt;write("$_");
        }
}

#===[ The main loop. ]========================================================
#
# This is where the bot will spend most of its time.  Looping and looping,
# waiting for new chatterbox messages.  When we get them, we'll send them
# out to anyone subscribed to our presence.
#
#=============================================================================

while (1) {
        $bot-&gt;Process(5);

        foreach ($pm-&gt;getnewlines(1,1)) { 
                $bot-&gt;broadcast(body=&gt;"$_", type=&gt;'chat');
        }

        $bot-&gt;Connected() || die "Lost my connection!";
}
# Edited: Sun Oct 14 06:52:45 2001 (GMT), by [footpad]
# Fixed formatting by adding line line breaks.
</field>
</data>
</node>
