Coding a Jabber Bot in Perl

I finally got it to work and it is not so difficult with the right packages (Net::XMPP) and parameters …

#! /usr/bin/perl

use strict;
use warnings;

use Net::XMPP;

#################
# Config params #
#################

my $hostName = 'jaim.at';
my $portNumber = 5222;
my $componentName = 'jaim.at';
my $userName = 'totosk1008';
my $passWord = 'totosk1008';
my $resource = 'NiceLittleBot';
my $tls = 0;
my $connectionType = 'tcpip';

my $debugLevel = 0;

# Create the client
my $bot = new Net::XMPP::Client(debuglevel => $debugLevel);

$bot->SetCallBacks(
onconnect       => &connectedCB,       # gets called when connected
onauth          => &authedCB,          # when authenticated
ondisconnect    => &disconnectedCB,    # when disconnected
);

$bot->SetMessageCallBacks(                      # callback for messages
chat            => &messageCB,         # chat-type messages
);

$bot->Execute(                                          # entering the main loop
hostname        => $hostName,
port            => $portNumber,
tls                     => $tls,
username        => $userName,
password        => $passWord,
resource        => $resource,
register        => 0,
connectiontype  => $connectionType,
);

##############
# Callbacks  #
##############

sub messageCB {                 # call back implementing the echo
my $sid = shift;
my $msg = shift;

my $from = $msg->GetFrom;
my $to = $msg->GetTo;

my $name;
my $data;

print "From : ", $from, "\n",
"Subject : ", $msg->GetSubject, "\n",
$msg->GetBody, "\n";

$name = $msg->GetBody;

$data = $msg->GetBody;

print ">>$name<<\n";
print $data, "\n";

$bot->MessageSend(
to              => $from,
from    => $to,
resource        => 'Gaim',
type    => $msg->GetType,
subject => $msg->GetSubject,
body    => $data,
);
}

sub connectedCB {
print "Connected\n";
}

sub authedCB {
print "Authed\n";
$bot->PresenceSend;
}

sub disconnectedCB {
print "Disconnected\n";
}

me <- proud ! ;-)

I finally finished it (though I expect to have to make some changes in the future …) :
Now I can setup my home automation events in MisterHouse throught my Mozilla Calendar ! The code has been submited and accepted for being integrated in MisterHouse and will be in the next release 🙂
Code is here.

Parsing an ICal file with Perl

I finally almost got it …

Here is a code snippet for parsing and showing pieces of an ICal file :

use strict;
use warnings;

use Data::ICal;
use Data::ICal::DateTime;
use DateTime;

my $cal = Data::ICal->new(filename => 'holidays.ics');

my @events = $cal->events();

foreach my $event (@events) {
        print "summary:", $event->property('summary')->[0]->value, "n";
        print "dtstart:", $event->start, "n";
        print "dtend:", $event->end, "n";
}
print DateTime->now, "n";

What I want to do ? Drive my home automation events with my Mozilla Calendar !! I’ll soon be there, if I gather enough time to do it !

Some pointers :

  • DateTime perl module : a hell of a lot of computations with dates and times
  • Data::ICal perl modules : for all the parsing stuff
  • Data::ICal::DateTime perl module : to get it to work with DateTime