Follow us on Twitter!
Capitalism is an Island of wealth in a sea of poverty
Monday, February 20, 2017
 Need Help?
Members Online
Total Online: 59
Guests Online: 48
Members Online: 11

Registered Members: 98643
Newest Member: jkronos85
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming


jabber bot in perl


Your avatar

Joined: 01.01.70
Posted on 02-04-10 18:45
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;

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();

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->SetPresenceCallBacks(available=>\&welcome, unavailable=>\&killBot);


sub welcome
    $jabberBot->MessageSend(to=>$jbrBoss->GetJID(),subject=>"",body=>"Hello There!",type=>"chat",priority=>10);

#$jabberBot->MessageSend(to=>$jbrBoss->GetJID(),subject=>"",body=>"Hello There! Global...",type=>"chat",priority=>10);

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;
    #    &killBot($msg);
   my $dbQry = $dbh->prepare("SELECT command,acknowledgement FROM commands WHERE message = '".lc($jbrCmd)."'");
   if($dbQry->rows() > 0 && $jbrCmd !~ /^insert/si)
      my $ref = $dbQry->fetchrow_hashref();
      eval $ref->{'command'};
      $jabberBot->MessageSend(to=>$msg->GetFrom(),subject=>"",body=>"You said: ".$jbrCmd,type=>"chat",priority=>10);

sub gotIQ
    print $_[1]->{TAG}."\n";
    if($_[1]->{TAG} ne 'iq')

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);

sub killBot
   print "kill\n";
    $jabberBot->MessageSend(to=>$jbrBoss->GetJID(),subject=>"",body=>"Logging Out!",type=>"chat",priority=>10);