]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/Freshmeat.pl
a09e810c929017c918bb94c8ff3056ed0d6c34fc
[infobot.git] / src / Modules / Freshmeat.pl
1 #
2 # Freshmeat.pl: Frontend to www.freshmeat.net
3 #       Author: dms
4 #      Version: v0.7d (20000923)
5 #      Created: 19990930
6 #
7
8 package Freshmeat;
9
10 use strict;
11
12 ### download compressed version instead?
13
14 my %urls = (
15         'public'  => 'http://www.freshmeat.net/backend/appindex.txt',
16         'private' => 'http://feed.freshmeat.net/appindex/appindex.txt',
17 );
18
19 ####
20 # Usage: &Freshmeat($string);
21 sub Freshmeat {
22     my $sstr    = lc($_[0]);
23     my $refresh = $main::param{'freshmeatRefreshInterval'} * 60 * 60;
24
25     my $last_refresh = &main::dbGet("freshmeat", "name","_","stable");
26     my $renewtable   = 0;
27
28     if (defined $last_refresh) {
29         $renewtable++ if (time() - $last_refresh > $refresh);
30     } else {
31         $renewtable++;
32     }
33     $renewtable++ if (&main::countKeys("freshmeat") < 10);
34
35     if ($renewtable and $$ == $main::bot_pid) {
36         &main::Forker("freshmeat", sub {
37                 &downloadIndex();
38                 &Freshmeat($sstr);
39         } );
40         # both parent/fork runs here, in case the following looks weird.
41         return if ($$ == $main::bot_pid);
42     }
43
44     if (!&showPackage($sstr)) {         # no exact match.
45         my $start_time = &main::timeget();
46         my %hash;
47
48         # search by key/NAME first.
49         foreach (&main::searchTable("freshmeat", "name","name",$sstr)) {
50             $hash{$_} = 1 unless exists $hash{$_};
51         }
52
53         # search by description line.
54         foreach (&main::searchTable("freshmeat", "name","oneliner", $sstr)) {
55             $hash{$_} = 1 unless exists $hash{$_};
56             last if (scalar keys %hash > 15);
57         }
58
59         my @list = keys %hash;
60         # search by value, if we have enough room to do it.
61         if (scalar @list == 1) {
62             &main::status("only one match found; showing full info.");
63             &showPackage($list[0]);
64             return;
65         }
66
67         # show how long it took.
68         my $delta_time = &main::timedelta($start_time);
69         &main::status(sprintf("freshmeat: %.02f sec to complete query.", $delta_time)) if ($delta_time > 0);
70
71         for (@list) {
72             tr/A-Z/a-z/;
73             s/([\,\;]+)/\037$1\037/g;
74         }
75
76         &main::performStrictReply( &main::formListReply(1, "Freshmeat ", @list) );
77     }
78 }
79
80 sub showPackage {
81     my ($pkg)   = @_;
82     my @fm      = &main::dbGet("freshmeat", "name",$pkg,"*");
83
84     if (scalar @fm) {           #1: perfect match of name.
85         my $retval;
86         $retval  = "$fm[0] \002(\002$fm[11]\002)\002, ";
87         $retval .= "section $fm[3], ";
88         $retval .= "is $fm[4]. ";
89         $retval .= "Stable: \002$fm[1]\002, ";
90         $retval .= "Development: \002$fm[2]\002. ";
91         $retval .= $fm[5] || $fm[6];             # fallback to 'download'.
92         $retval .= " deb: ".$fm[8] if ($fm[8] ne ""); # 'deb'.
93         &main::performStrictReply($retval);
94         return 1;
95     } else {
96         return 0;
97     }
98 }
99
100 sub downloadIndex {
101     my $start_time      = &main::timeget(); # set the start time.
102     my $idx             = "$main::param{tempDir}/fm_index.txt";
103
104     &main::msg($main::who, "Updating freshmeat index... please wait");
105
106     if (&main::isStale($idx, 1)) {
107         &main::status("Freshmeat: fetching data.");
108         foreach (keys %urls) {
109             my $retval = &main::getURLAsFile($urls{$_}, $idx);
110             next if ($retval =~ /^(403|500)$/);
111
112             &main::DEBUG("FM: last! retval => '$retval'.");
113             last;
114         }
115     } else {
116         &main::status("Freshmeat: local file hack.");
117     }
118
119     if (! -e $idx) {
120         &main::msg($main::who, "the freshmeat butcher is closed.");
121         return;
122     }
123
124     if ( -s $idx < 100000) {
125         &main::DEBUG("FM: index too small?");
126         unlink $idx;
127         &main::msg($main::who, "internal error?");
128         return;
129     }
130
131     if ($idx =~ /bz2$/) {
132         open(IN, "bzcat $idx |");
133     } elsif ($idx =~ /gz$/) {
134         open(IN, "gzcat $idx |");
135     } else {
136         open(IN, $idx);
137     }
138
139     # delete the table before we redo it.
140     &main::deleteTable("freshmeat");
141
142     ### lets get on with business.
143     # set the last refresh time. fixes multiple spawn bug.
144     &main::dbSet("freshmeat", "name","_","stable",time());
145
146     my $i = 0;
147     while (my $line = <IN>) {
148         chop $line;
149         $i++ if ($line eq "%%");
150         last if ($i == 2);
151     }
152
153     &main::dbRaw("LOCK", "LOCK TABLES freshmeat WRITE");
154     my @data;
155     my @done;
156     while (my $line = <IN>) {
157         chop $line;
158         if ($line ne "%%") {
159             push(@data,$line);
160             next;
161         }
162
163         if ($i % 200 == 0 and $i != 0) {
164             &main::DEBUG("FM: unlocking and locking.");
165             &main::dbRaw("UNLOCK", "UNLOCK TABLES");
166             ### another lame hack to "prevent" errors.
167             select(undef, undef, undef, 0.2);
168             &main::dbRaw("LOCK", "LOCK TABLES freshmeat WRITE");
169         }
170
171         if (grep /^\Q$data[0]\E$/, @done) {
172             &main::DEBUG("dupe? $data[0]");
173             @data = ();
174             next;
175         }
176
177         $i++;
178         pop @data;
179         $data[1] ||= "none";
180         $data[2] ||= "none";
181         &main::dbSetRow("freshmeat", @data);
182         push(@done,$data[0]);
183         @data = ();
184     }
185     close IN;
186     &main::DEBUG("FM: data ".scalar(@data) );
187     &main::dbRaw("UNLOCK", "UNLOCK TABLES");
188
189     my $delta_time = &main::timedelta($start_time);
190     &main::status(sprintf("Freshmeat: %.02f sec to complete.", $delta_time)) if ($delta_time > 0);
191
192     my $count = &main::countKeys("freshmeat");
193     &main::status("Freshmeat: $count entries loaded.");
194 }
195
196 sub freshmeatAnnounce {
197     my $file = "$main::param{tempDir}/fm_recent.txt";
198     my @old;
199
200     ### if file exists, lets read it.
201     if ( -f $file) {
202         open(IN, $file);
203         while (<IN>) {
204             chop;
205             push(@old,$_);
206         }
207         close IN;
208     }
209
210     my @array = &main::getURL("http://core.freshmeat.net/backend/recentnews.txt");
211     my @now;
212
213     while (@array) {
214         my($what,$date,$url) = splice(@array,0,3);
215         push(@now, $what);
216     }
217
218     ### if file does not exist, write new.
219     if (! -f $file) {
220         open(OUT, ">$file");
221         foreach (@now) {
222             print OUT "$_\n";
223         }
224         close OUT;
225
226         return;
227     }
228
229     my @new;
230     for(my $i=0; $i<scalar(@old); $i++) {
231         last if ($now[$i] eq $old[0]);
232         push(@new, $now[$i]);
233     }
234
235     if (!scalar @new) {
236         &main::DEBUG("fA: no new items.");
237         return;
238     }
239
240     my $chan;
241     my @chans = split(/[\s\t]+/, lc $main::param{'freshmeatAnnounce'});
242     @chans    = keys(%main::channels) unless (scalar @chans);
243
244     my $line = "Freshmeat update: ".join(" \002::\002 ", @new);
245     foreach (@chans) {
246         next unless (&main::validChan($_));
247
248         &main::status("sending freshmeat update to $_.");
249         &main::notice($_, $line);
250     }
251
252     ### output new file.
253     open(OUT, ">$file");
254     foreach (@now) {
255         print OUT "$_\n";
256     }
257     close OUT;
258 }
259
260 1;