Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

jabber bot for monitoring a machine

by spx2 (Deacon)
on Jan 07, 2008 at 12:34 UTC ( [id://660812]=perlquestion: print w/replies, xml ) Need Help??

spx2 has asked for the wisdom of the Perl Monks concerning the following question:

I want to write a bot in perl to run on a machine and
to receive various commands,execute them,and have it
send back monitoring information.

I have seen that bots are usually beeing written in perl
using POE. I'd thought first to look at pidginto see if it has
some plugins to allow reading and writing messages
to a user so that I can write a monolithic(one-user only)
bot so I've read this doc wich describes Purple but it's very incomplete...
it just says to look at the .xs code for libpurple,wich
unfortunately I can't find where this xs code lies
altough I have found this and this wich should be
the closest thing to a documentation to Purple
On perlmonks I have seen one single node that talks about the use of Purple.
With Purple I read how I can send messages to other users
on jabber,but the documentation I've read doesn't describe
how to read what messages are beeing sent to me.

I've seen of two projects wich are online and probably do something similar
to what I want to write Jabber Perl Bot and Ambrosio.
I have also came across Net::Jabber
but the documentation has also been a little discouraging

Then I also came across POE::Component::Jabber wich seems to be okay but also it lacks docs.
My questions is if you have wrote a similar thing can you tell me
what module have you used ?
How should I start writing this ?
What should I read?
Thank you.


EDIT:

In the meantime I've found that wikipedia presents here all the available perl implementations of the jabber protocol.

Replies are listed 'Best First'.
Re: jabber bot for monitoring a machine
by kirillm (Friar) on Jan 07, 2008 at 13:26 UTC

    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!

Re: jabber bot for monitoring a machine
by rafl (Friar) on Jan 07, 2008 at 12:50 UTC

    I've once done something similar and used the Net::Jabber::Loudmouth module. The documentation isn't all that good. You get automatically generated API docs when you install it, however those don't say anything about the purpose of the methods. You'd need to look those up in the loudmouth documentation, which is quite good, itself.

    Another nice looking xmpp implementation is Net::XMPP2.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-16 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found