Hi guys, My brain has got enough for today but I have got a problem. I was bored and decided I will write a simple bot which is going to welcome all guests joining the channel. Well... I thought it will be simple bot but I was a bit wrong ;). I don't want to use any MySQL databases or such a things. I just wanted to keep the code simple as possible. So the first problem I faced with was no proper status when someone joins the channel. The client only sends a presence to the channel so we know someone is available. So in effect bot was inviting everyone who's status was changed ;). So the idea was to join the channel by the bot, get a list of users, put that into the array and if someone will leave, remove it from the list, if someone will change the status check against the array if the JID is already in. Everything is fine, but... it doesn't want to work. I managed to create a list, eliminate duplicates (if exists) and remove JID when the user left. The problem is when anyone changes the status bot adds this user's JID again to the list. I put some "debugging" to check what is the status of $index variable which tells where the JID is in the array and if it doesn't exist, put that JID, however even if JID exists and $index is returned as 0 or other, condition !$index doesn't work. This is what I have got:
#!/usr/bin/perl + + use Net::Jabber; use Data::Dumper; use strict; use Fcntl qw(:flock); my @roster; my $server = "server.tld"; my $port = "5222"; my $username = "user"; my $password = "pass"; my $resource = "BOT"; my $Connection; my %roster=(); my $en_jid; startbot(); sub startbot() { #### PREVENT FORKING #### open (FILE, '>> bot.lock') or die "Cannot open file"; flock(FILE, LOCK_EX|LOCK_NB) or die "Process already running"; ######################### $Connection = new Net::Jabber::Client(); $Connection->SetCallBacks(presence=>\&InPresence, iq=>\&InIQ, mess +age=>\&InMessage); my $status = $Connection->Connect(hostname=>$server,port=>$port); + if (!(defined($status))) + { + print "ERROR: Jabber server is down or connection was not all +owed.\n"; print " ($!)\n"; + exit(0); + } my @result = $Connection->AuthSend( username=>$username, password=>$password, resource=>$resource); if ($result[0] ne "ok") { print "ERROR: Authorization failed: $result[0] - $result[1]\n" +; exit(0); + } + print "Logged in to $server:$port...\n"; + $Connection->MUCJoin( + room=>"test", + server=>"chat.server.tld", nick=>"Robot"); + while(defined($Connection->Process())) {} + } sub InMessage { my $sid = shift; my $message = shift; my $xml = $message->GetXML(); print "Message \n"; print $xml ."\n"; print "\n"; } sub InIQ { my $sid = shift; my $iq = shift; my $xml = $iq->GetXML(); print "IQ \n"; print $xml ."\n"; print "\n"; } sub InPresence { my $i; my $index; my @jid; my $jid; my $roster_size = $#roster; my $sid = shift; my $presence = shift; my $xml = $presence->GetXML(); my $type = $presence->GetType(); $jid = $xml =~ /jid='(.*)'/; @jid = split /\//, $1; $jid = $jid[0]; my $from = $presence->GetFrom(); my @from = split /\//, $from; $from=$from[0]; print "Presence \n"; print $xml ."\n"; print "Type: $type\n"; print "\n"; print "Roster size = $roster_size\n"; print "Index before for: $index\n"; for ($i = 0; $i < $roster_size+1; $i++) { if($roster[$i] =~ /^$jid$/) { $index = $i; print "JID $jid found, Index: $index\n"; print "Index inside for: $index\n"; last; } else { push @roster, $jid; } } print "Index after for: $index\n"; if (!$index and $type !~ /unavailable/) { print "Index in if: $index\n"; push @roster, $jid; } elsif ($type =~ /unavailable/) { print "Removing user $jid from Index $index\n"; splice @roster, $index, 1; } else { print "Nothing\n"; } print "Index after if: $index"; print "\n=================== ROSTER =====================\n"; print "@roster"; print "\n=================== ROSTER =====================\n"; }
I really appreciate your help. Thank you very much in advance. Regards, Jarek

In reply to Jabber MUC bot channel list problem. by Jarek

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.