[JDEV] From the crude hack department
Piers Harding
piers at ompa.net
Thu Apr 18 08:32:11 CDT 2002
Jarl is great! Perl is great!
The other day when I was trying to test out some headline viewing
integration with pubsub, I had the problem of clients not being able to
support new packet types etc.
So.... I modifeid Jarl!
In JEP 0024 the packet type for pushed subscription data is an IQ - but
the payload can be anything eg:
<iq type='set' to='pubsub component' id='p1'>
<query xmlns='pipetree:iq:pubsub'>
<publish xmlns='peerkat:home:weblogs:jabber:test'>
<item xmlns='peerkat:home:weblogs:jabber:test'>
<title>the title</title>
<link>http://the.link</link>
<description>this is the description</description>
</item>
</publish>
</query>
</iq>
Basically all I need t odo was to munge this dat aarround so that it
looked like a <message type="headline"/>. So using a combination of
Jabber::NodeFactory and Net::Jabber I only had to do two things.
(a) at arround line 712 of jarl I changed the receive callback to look
like:
$jabber{client}->SetCallBacks(message=>\&messageCB,
presence=>\&presenceCB,
iq=>\&iqCB,
send=>\&jarlDebug_AddSendXML,
receive=>\&my_iq_CB);
(b) I inserted my own custom callback to handle both debug packets and
whatever I want to do ( which is create headlines ).
The real bit of magic is this subroutine which takes a packet and
generates a headline- this to me is the real magic, as jarl has
subroutines for all these events, which means you can easily roll all
your own custom payload packets in.
&jarlHeadlineIF_AddMessage($mess);
Note: I have picked up the debugging of packets by mannually calling
&jarlDebug_AddReceiveXML( @_ );
sub my_iq_CB {
my ( $sid, $xml ) = @_;
# $xml is the string of xml
# make sure you still get debug
&jarlDebug_AddReceiveXML( @_ );
$Debug->Log1("my_iq_CB: start");
# parse the xml so that I can easily manipulate it
use Jabber::NodeFactory;
my $jcnf = new Jabber::NodeFactory(fromstr => 1);
my $n = $jcnf->newNodeFromStr($xml);
return unless $n->name() eq "iq";
my $query = $n->getTag('query');
return unless $query;
return unless $query->attr("xmlns") eq "jabber:iq:pubsub";
print STDERR "WE HAVE A PUBSUB: $xml ....";
# now create the headline packet and then call the magic
foreach my $pub ( $query->getChildren() ){
next unless $pub->name() eq "publish";
foreach my $item ( $pub->getChildren() ){
next unless $item->name() eq "item";
my $mess = new Net::Jabber::Message();
$mess->SetMessage(
to => $n->attr('to'),
from => $n->attr('from'),
type => 'headline',
subject => $item->getTag("title")->data(),
body => $item->getTag("link")->data()." -
".$item->getTag("description")->data(),
);
# this is the magic
&jarlHeadlineIF_AddMessage($mess);
}
}
$Debug->Log1("my_iq_CB: end");
}
Cheers.
More information about the JDev
mailing list