Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

A comment about POE::Component::Jabber.

POE::Component::Jabber is in my opinion very good. The way version 1 handled network connections was a bit buggy, but version 2 is huge step forward and is really great.

POE::Component::Jabber gives you a flexible framework to build your XMPP client, but you will need to do a lot yourself. You are required to have read at least RFC3920 and RFC3921. In that reagrd, the documentation is very good.

For example to send a message you'd first create a POE::Filter::XML::Node object and then pass it to the output_handler event. Here's an example of how I send messages (send_message is my own event handler, but it sends to output_handler in it's last line):

package KM::POE::JabberClient; use Filter::Template; const XNode POE::Filter::XML::Node use strict; [...] sub send_message { my ($kernel, $heap, %attrs) = @_[KERNEL, HEAP, ARG0..$#_]; my $node = XNode->new('message'); my @types = qw(chat error groupchat headline normal); $attrs{'type'} = $types[0] if exists $attrs{'type'} and !grep { $attrs{'type'} eq $_ } @type +s; for my $i (qw(subject body thread)) { my $k = delete $attrs{$i}; $node->insert_tag($i)->data($k) if $k; } $node->insert_attrs([%attrs]) if %attrs; $kernel->post($heap->{'session'}, 'output_handler', $node); }

To receive messages (and other input) in your XMPP client you'd create your own event handler. It will receive a POE::Filter::XML::Node object that you can do whatever you want with. Example from my code:

sub input_hook { my ($kernel, $heap, $node) = @_[KERNEL, HEAP, ARG0]; my $name = $node->name; my $users = $heap->{'users'}; if ($name eq 'presence') { my $jid = Net::XMPP::JID->new($node->attr('from')); my $full = $jid->GetJID('full'); my $base = $jid->GetJID('base'); my $type = $node->attr('type') || 'available'; return if $full !~ /\@/; # do some stuff with tracking online/offline users etc } elsif ($name eq 'iq' and $node->attr('type') eq 'result') { my $kids = $node->get_children(); my $res = $kids->[0]; if ($res->attr('xmlns') eq 'jabber:iq:roster') { for my $kid (@{$res->get_sort_children()}) { my $jid = $kid->attr('jid'); $users->{$jid} = {} if !exists $users->{$jid}; } } } }

The above code does not handle incoming messags (since I wasn't interested in these), but read the mentioned RFC and you'll see what you'll need.

Good luck!


In reply to Re: jabber bot for monitoring a machine by kirillm
in thread jabber bot for monitoring a machine by spx2

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-23 14:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found