]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/Slashdot3.pl
02d74d6c8519d24b42928d3383c3f84c7864a21e
[infobot.git] / src / Modules / Slashdot3.pl
1 #
2 # Slashdot.pl: Slashdot headline retrival
3 #      Author: Chris Tessone <tessone@imsa.edu>
4 #    Modified: xk <xk@leguin.openprojects.net>
5 #   Licensing: Artistic License (as perl itself)
6 #     Version: v0.4 (19991125)
7 #
8
9 ###
10 # fixed up to use XML'd /. backdoor 7/31 by richardh@rahga.com
11 # My only request if this gets included in infobot is that the
12 # other header gets trimmed to 2 lines, dump the fluff ;) -rah
13 #
14 # added a status message so people know to install LWP - oznoid
15 # also simplified the return code because it wasn't working.
16 ###
17
18 package Slashdot;
19
20 use strict;
21
22 sub slashdotParse {
23     my @list;
24
25     foreach (@_) {
26         next unless (/<title>(.*?)<\/title>/);
27         my $title = $1;
28         $title =~ s/&amp\;/&/g;
29         push(@list, $title);
30     }
31
32     return @list;
33 }
34
35 sub Slashdot {
36     my @results = &main::getURL("http://www.slashdot.org/slashdot.xml");
37     my $retval  = "i could not get the headlines.";
38
39     if (scalar @results) {
40         my $prefix      = "Slashdot Headlines ";
41         my @list        = &slashdotParse(@results);
42         $retval         = &main::formListReply(0, $prefix, @list);
43     }
44
45     &main::performStrictReply($retval);
46 }
47
48 sub slashdotAnnounce {
49     my $file = "Temp/slashdot.xml";
50     if (! -d "Temp/") {
51         &main::DEBUG("sdA: mking dir.");
52         mkdir "Temp", 0755;
53     }
54
55     my @Cxml = &main::getURL("http://www.slashdot.org/slashdot.xml");
56     if (!scalar @Cxml) {
57         &main::DEBUG("sdA: failure (Cxml == NULL).");
58         return;
59     }
60
61     if (! -e $file) {           # first time run.
62         open(OUT, ">$file");
63         foreach (@Cxml) {
64             print OUT "$_\n";
65         }
66         close OUT;
67
68         return;
69     }
70
71     my @Oxml;
72     open(IN, $file);
73     while (<IN>) {
74         chop;
75         push(@Oxml,$_);
76     }
77     close IN;
78
79     my @Chl = &slashdotParse(@Cxml);
80     my @Ohl = &slashdotParse(@Oxml);
81
82     my @new;
83     foreach (@Chl) {
84         last if ($_ eq $Ohl[0]);
85         push(@new, $_);
86     }
87
88     if (scalar @new == 0) {
89         &main::status("Slashdot: no new headlines.");
90         return;
91     }
92
93     if (scalar @new == scalar @Chl) {
94         &main::DEBUG("sdA: scalar(new) == scalar(Chl). bad?");
95     }
96
97     open(OUT,">$file");
98     foreach (@Cxml) {
99         print OUT "$_\n";
100     }
101     close OUT;
102
103     my $line    = "Slashdot: News for nerds, stuff that matters -- ".
104                         join(" \002::\002 ", @new);
105
106     my @chans = split(/[\s\t]+/, lc $main::param{'slashdotAnnounce'});
107     @chans    = keys(%main::channels) unless (scalar @chans);
108     foreach (@chans) {
109         next unless (&main::validChan($_));
110
111         &main::status("sending slashdot update to $_.");
112         &main::notice($_, $line);
113     }
114     sleep 1;    # just in case?
115 }
116
117 1;