jabber bot in perl
Hey everyone I am trying to make a jabber bot and I am having a little trouble handling messages. If the bot's jabber account receives any messages while it's offline when it goes back online it seems to get these messages and puts them in a queue and deals with each one one at a time (that part makes sense to me). The part that doesn't make sense is that I have registered a callback for the message received event and once it receives these offline messages it will fire an event for the first 2 then it will idle. If you send the bot a message at this point it will then fire an event but it will handle offline message #3 instead of the message I just sent it. I think my problem has something to do with the fact that I am calling the keepItGoing sub routine in side the chat sub routine, but I am not sure how else to get it to "idle" otherwise. If anyone could offer any sort of insight on this that would be awesome.
I should probably also mention that I have been testing this by running the script once then stopping it then sending 3 offline messages then running the script again.
print "Running Version 3\n";
use strict;
use warnings;
#Libraries
use Net::XMPP;
use DBI;
use DBD::mysql;
use Data::Dumper;
#--------------- Config Vars -----------------
# Jabber Client
my $jbrHostname = "removed";
my $jbrUserName = "removed";
my $jbrPassword = "removed";
my $jbrResource = "removed";
my $jbrBoss = new Net::XMPP::JID();
$jbrBoss->SetJID(userid=>"removed",server=>$jbrHostname);
# MySQL
my $dbHostname = "removed";
my $dbName = "removed";
my $dbUserName = "removed";
my $dbPassword = "removed";
#--------------- End Config -----------------
# connect to the db
my $dbh = DBI->connect("DBI:mysql:database=$dbName;host=$dbHostname",$dbUserName, $dbPassword, {RaiseError => 1}) or die "Couldn't connect to the database: $!\n";
# create a new jabber client and connect to server
my $jabberBot = Net::XMPP::Client->new();
my $status = $jabberBot->Connect(hostname=>$jbrHostname) or die "Cannot connect ($!)\n";
my @results = $jabberBot->AuthSend(username=>$jbrUserName,password=>$jbrPassword,resource=>$jbrResource);
if($results[0] ne "ok")
{
die "Jabber auth error @results\n";
}
# set jabber bot callbacks
$jabberBot->SetMessageCallBacks(chat=>\&chat);
$jabberBot->SetPresenceCallBacks(available=>\&welcome, unavailable=>\&killBot);
$jabberBot->SetCallBacks(iq=>\&gotIQ);
$jabberBot->PresenceSend(type=>"available");
$jabberBot->Process(1);
sub welcome
{
$jabberBot->MessageSend(to=>$jbrBoss->GetJID(),subject=>"",body=>"Hello There!",type=>"chat",priority=>10);
&keepItGoing;
}
#$jabberBot->MessageSend(to=>$jbrBoss->GetJID(),subject=>"",body=>"Hello There! Global...",type=>"chat",priority=>10);
#$jabberBot->Process(5);
&keepItGoing;
sub chat
{
print "Chat Called!\n";
print Dumper(@_);
my ($sessionID,$msg) = @_;
my $jbrCmd = &trimSpaces($msg->GetBody());
#if($msg->GetBody() ne 'logout')
#{
# print $msg->GetBody()."\n";
# &keepItGoing;
#}
#else
#{
# &killBot($msg);
#}
my $dbQry = $dbh->prepare("SELECT command,acknowledgement FROM commands WHERE message = '".lc($jbrCmd)."'");
$dbQry->execute();
if($dbQry->rows() > 0 && $jbrCmd !~ /^insert/si)
{
my $ref = $dbQry->fetchrow_hashref();
$dbQry->finish();
$jabberBot->MessageSend(to=>$msg->GetFrom(),subject=>"",body=>$ref->{'acknowledgement'},type=>"chat",priority=>10);
eval $ref->{'command'};
}
else
{
$jabberBot->MessageSend(to=>$msg->GetFrom(),subject=>"",body=>"You said: ".$jbrCmd,type=>"chat",priority=>10);
$dbQry->finish();
}
&keepItGoing;
}
sub gotIQ
{
print $_[1]->{TAG}."\n";
return;
if($_[1]->{TAG} ne 'iq')
{
&chat;
}
}
sub trimSpaces
{
my $string = $_[0];
$string =~ s/^\s+//; #remove leading spaces
$string =~ s/\s+$//; #remove trailing spaces
return $string;
}
sub keepItGoing
{
print "Movin' the chains!\n";
my $proc = $jabberBot->Process(1);
while(defined($proc) && $proc != 1)
{
$proc = $jabberBot->Process(1);
}
return;
}
sub killBot
{
print "kill\n";
$jabberBot->MessageSend(to=>$jbrBoss->GetJID(),subject=>"",body=>"Logging Out!",type=>"chat",priority=>10);
$jabberBot->Process(1);
$jabberBot->Disconnect();
exit;
}```