On 06/15/2018 12:37 PM, hw wrote:

Hi,

using Net::XMPP::Client, I am able to send messages, but I don´t see a way to receive messages.  The documentation leaves me entirely in the dark as to how to do this, and I couldn´t find an example using this module to receive messages.

So how can I receive messages?


Below program seems to work (provided that a SRV record is set for the server).

But I don´t understand what the Execute() function of the client is for, which might make more sense to use because it also handles re-connecting: When I call Execute() instead of Process(), it seems to run only once, and the program is finished right away. It also doesn´t pass parameters to the callbacks it allows to set, so how would I get at the messages I´m trying to receive?


=== cut here ===

#!/bin/perl

use strict;
use Net::XMPP;
use feature 'say';


sub normal {
  my ($sid, $msg_o) = @_;
  unless(defined($sid)) {
    say 'WARNING: undefined session-ID';
  } else {
    say $sid;
  }
  unless(defined($msg_o)) {
    say 'ERROR: undefined message object';
    return;
  }

  my $from = $msg_o->GetFrom;
  my $body = $msg_o->GetBody;
  say "Normal from $from:\n";
  say $body;
  return;
}
sub chat {
  my ($sid, $msg_o) = @_;
  unless(defined($sid)) {
    say 'WARNING: undefined session-ID';
  } else {
    say $sid;
  }
  unless(defined($msg_o)) {
    say 'ERROR: undefined message object';
    return;
  }

  my $from = $msg_o->GetFrom;
  my $body = $msg_o->GetBody;
  say "Chat from $from:\n";
  say $body;
  return;
}
sub groupchat {
  my ($sid, $msg_o) = @_;
  return;
}
sub headline {
  my ($sid, $msg_o) = @_;
  return;
}
sub onerror {
  my ($sid, $msg_o) = @_;
  return;
}


# my $con = new Net::XMPP::Client(debuglevel => 3, debugfile => 'stdout');
my $con = new Net::XMPP::Client();
my $status = $con->Connect(
                           hostname => 'xmpp.example.com',
                           connectiontype => 'tcpip',
                           srv => 0,
                           tls => 1,
                           componentname => 'example.com'
                          );
die('ERROR: XMPP connection failed') if ! defined($status);

my @result = $con->AuthSend(
                            hostname => 'example.com',
                            username => 'test',
                            password => 'secret');
if($result[0] ne 'ok') {
  $con->Disconnect;
  die('ERROR: XMPP authentication failed');
}

#&normal('test');
$con->SetMessageCallBacks(normal => \&normal, chat => \&chat, groupchat => \&groupchat, headline => \&headline, error => \&onerror);
$con->PresenceSend;

say 'INFO: presence sent';

while($con->Process) {};
#$con->Execute;

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to