The RSS News Agent Script
The RSS news agent script (newsagent) is written in Perl and is shown here in Example 9-17.
my $NAME = 'RSS News Agent'; my $ID = 'rss.qmacro.dyndns.org'; my $VERSION = '0.1'; my $reg_file = 'registrations'; my %reg; my %cache; my %sources = ( 'jonudell' => 'http://udell.roninhouse.com/udell.rdf', 'slashdot' => 'http://slashdot.org/slashdot.rdf', # etc ... ); tie (%reg, 'MLDBM', $reg_file) or die "Cannot tie to $reg_file: $!\n"; my $c = new Jabber::Connection( server => 'localhost:5999', localname => $ID, ns => 'jabber:component:accept', ); unless ($c->connect()) { die "oops: ".$c->lastError; } $SIG{HUP} = $SIG{KILL} = $SIG{TERM} = $SIG{INT} = \&cleanup; debug("registering RSS beat"); $c->register_beat(1800, \&rss); debug("registering IQ handlers"); $c->register_handler('iq',\&iq_register); $c->register_handler('iq',\&iq_version); $c->register_handler('iq',\&iq_browse); $c->register_handler('iq',\&iq_notimpl); $c->auth('secret'); $c->start; sub iq_register { my $node = shift; debug("[iq_register]"); return unless my $query = $node->getTag('', NS_REGISTER); debug("--> registration request"); # Reg query if ($node->attr('type') eq IQ_GET) { $node = toFrom($node); $node->attr('type', IQ_RESULT); my $instructions = "Choose an RSS source from: ".join(", ", keys %sources); $query->insertTag('instructions')->data($instructions); $query->insertTag('text'); $c->send($node); } # Reg request if ($node->attr('type') ...
Get Programming Jabber now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.