Scripts archive: Slashdot headlines

#
# slashdot.pl
#
# usage: !slashdot [number of headlines] (default: 1, max: 5)
#
# Note: requires LWP::Simple and XML::Simple.
#

use strict;
use LWP::Simple;
use XML::Simple;

my $version = "1.0";

# URL to slashdot XML
my $slashdotxml = "http://slashdot.org/slashdot.xml";

OerPerl::register_startup_callback("slashdot_news");
OerPerl::add_command_handler("slashdot", "slashdot_news_commandhandler");

sub slashdot_news
{
    if(OerPerl::DEBUG) {
        OerPerl::oer_debug(OerPerl::DEBUG_INFO,
            "slashdot_news()->slashdot.pl version $version\n");
    }
}

sub slashdot_news_commandhandler
{
    my($command, $target, $nick, $userhost, $args) = @_;
    my $tochan = OerPerl::is_valid_channel($target) ? 1 : 0;
    my $target_type = $tochan ? OerPerl::CHANNEL : OerPerl::USER;
    my $numheadlines = 1;
    my @articles = ( "", "", "", "", "");
    my $i = 0;
    my $title = "";

    my $xml = LWP::Simple::get($slashdotxml);
    # parse XML
    my $tree = XMLin($xml);

    if(defined $xml) {
        if($args eq "" || $args eq "(null)") {
            $numheadlines = 1;
        } elsif($args < 1) {
            $numheadlines = 1;
        } elsif($args > 5) {
            $numheadlines = 5;
        } else {
            $numheadlines = $args;
        }
        for($i = 0; $i < $numheadlines; $i++) {
            OerPerl::sendto($tochan ? $target : $nick,
                $target_type,
                "$tree->{story}->[$i]->{time}: \"$tree->{story}->[$i]->{title}\"
                 ($tree->{story}->[$i]->{comments} comments) --
                 $tree->{story}->[$i]->{url}");
        }
    }
}