Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: jabber bot for monitoring a machine

by kirillm (Friar)
on Jan 07, 2008 at 13:26 UTC ( #660828=note: print w/replies, xml ) Need Help??


in reply to jabber bot for monitoring a machine

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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://660828]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2022-08-15 01:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?