#!/usr/bin/perl
use FindBin;
use lib $FindBin::Bin;
use DBI;
use strict 'vars';
use Slash;
my ($basedir,$datadir,$rootdir)=getvars("basedir","datadir","rootdir");
sub slashdLog
{
open LOG, ">>$datadir/logs/slashd.log";
print LOG localtime()."\t",join("\t",@_),"\n";
close LOG;
}
sub getslashdot
{
my(@S)=split("%%",geturl("http://slashdot.org/ultramode.txt"));
shift @S;
shift @S;
my $stuff;
foreach my $ss (@S) {
my @s=split("\n",$ss);
$stuff.="
$s[1] ($s[7])\n" if $s[1];
}
if(@S > 2) {
setblock('slashdot',"$stuff Visit Slashdot");
}
}
sub copy
{
my($from,$to)=@_;
# Make sure that $to exists
my $t=$to;
$t=~s|(.*)/(.*)|$1|;
my @dirs=split("/",$t);
my $d;
foreach (@dirs) {
$d.="/$_";
slashdLog "mkdir $d";
mkdir $d,oct('0755');
}
open FROM,$from;
open TO,">>$to";
while() { print TO; }
close FROM;
close TO;
}
sub moveImages
{
if(opendir DIR,"/tmp/slash") {
my @files = readdir(DIR);
foreach my $old (@files) {
my $new=$basedir."/".$old;
$new=~s|~|/|g;
if(substr($old,0,1) ne ".") {
copy("/tmp/slash/$old",$new);
unlink("/tmp/slash/$old");
}
}
}
}
sub setblock
{
my ($bid, $block)=@_;
$block=$dbh->quote($block);
$dbh->do("UPDATE blocks SET block=$block WHERE bid='$bid'");
}
sub getminute
{
my ($sec,$min,$other) = gmtime(time);
return $min;
}
sub save2file
{
my ($f, $d)=@_;
open FH,">$f";
print FH $d;
close FH;
}
sub newmotd
{
my $t=`/usr/games/fortune -s`;
setblock('motd',$t);
}
sub sectionHeaders
{
my($section)=@_;
local *FH;
open FH,">$basedir/$section/slashhead.inc";
*STDOUT=*FH;
header("",$section,"thread");
close FH;
open FH,">$basedir/$section/slashfoot.inc";
*STDOUT=*FH;
footer();
close FH;
open FH,">$basedir/$section/slashhead_F.inc";
*STDOUT=*FH;
header("",$section,"flat");
close FH;
open FH,">$basedir/$section/slashfoot_F.inc";
*STDOUT=*FH;
footer();
close FH;
}
sub newfooter
{
local *FH;
local *SO=*STDOUT;
sectionHeaders("");
my $c=$dbh->prepare("SELECT section FROM sections");
$c->execute();
while(my ($section)=$c->fetchrow()) {
mkdir "$basedir/$section",oct('0755');
sectionHeaders($section);
}
$c->finish();
*STDOUT=*SO;
}
sub newultra()
{
my $c=$dbh->prepare("SELECT stories.sid,title,time,dept,aid,
alttext,image,commentcount,section
FROM stories,topics
WHERE stories.tid=topics.tid
AND writestatus >= 0
AND displaystatus = 0
ORDER BY time DESC");
$c->execute;
my $y=0;
my $um="Welcome to ultramode.txt, the spiffy back end to Slash.
%%
";
while(my($sid,$title,$time,$dept,$aid,$topic,$image,$commentcount,$section)
=$c->fetchrow and $y<11) {
$y++;
$um.="$title
$rootdir/$section/$sid.shtml
$time EDT
$aid
$dept
$topic
$commentcount
$section
$image
%%
";
}
$c->finish();
save2file("$basedir/ultramode.txt",$um);
}
sub makeDir
{
my ($section,$sid)=@_;
$sid =~ /(.*)\//;
my $y2k = length($1);
my($monthid, $yearid, $dayid);
if($y2k == 10) {
$monthid = substr($sid, 5, 2);
$yearid = substr($sid, 0, 4);
$dayid = substr($sid, 8, 2);
} else {
$monthid = substr($sid, 3, 2);
$yearid = substr($sid, 0, 2);
$dayid = substr($sid, 6, 2);
}
mkdir "$basedir/$section",oct('0755');
mkdir "$basedir/$section/$yearid",oct('0755');
mkdir "$basedir/$section/$yearid/$monthid",oct('0755');
mkdir "$basedir/$section/$yearid/$monthid/$dayid",oct('0755');
}
# This is the normal, every 60 seconds stuff
sub freshenup
{
moveImages();
my $c=$dbh->prepare("SELECT sid,title,section FROM stories WHERE
writestatus=1");
$c->execute;
my (%updates)="";
while(my ($sid,$title,$section)=$c->fetchrow) {
slashdLog "Updating $title $sid";
$updates{$section}=1;
makeDir($section,$sid);
if(prog2file("$basedir/article.pl sid=$sid\\\&ssi=yes\\\&mode=thread",
"$basedir/$section/$sid".".shtml")) {
prog2file("$basedir/article.pl sid=$sid\\\&ssi=yes\\\&mode=flat",
"$basedir/$section/$sid"."_F.shtml");
sqlUpdate("stories","sid=".$dbh->quote($sid),(writestatus=>0));
} else {
slashdLog "Trouble Updating $sid";
}
}
$c->finish();
$c=sqlSelectMany("sid,section","stories","writestatus=5");
# RM files from directories
my $x=0;
while(my ($sid,$section)=$c->fetchrow()) {
$x++;
$updates{$section}=1;
unlink "$basedir/$section/$sid.shtml";
unlink "rm $basedir/$section/$sid"."_F.shtml";
$dbh->do("DELETE from stories where sid='$sid'");
}
$c->finish();
my ($w)=getvar("writestatus");
if($updates{articles} ne "" or $w ne "0") {
setvar("writestatus","0");
prog2file("$basedir/index.pl ssi=yes",
"$basedir/index.shtml");
prog2file("$basedir/index.pl ssi=yes\\\&mode=flat",
"$basedir/index_F.shtml");
}
foreach my $key (keys %updates) {
prog2file("$basedir/index.pl ssi=yes\\\§ion=$key",
"$basedir/$key/index.shtml");
prog2file("$basedir/index.pl ssi=yes\\\&mode=flat\\\§ion=$key",
"$basedir/$key/index_F.shtml");
}
}
my $m=14;
slashdLog "Starting up Slashd";
while() {
$dbh||=sqlconnect();
freshenup();
$m++;
if($m%30 == 5) { # Freshmeat
getslashdot();
} elsif($m%60==20) {
prog2file("$basedir/hof.pl ssi=yes","$basedir/hof.shtml");
prog2file("$basedir/topics.pl ssi=yes","$basedir/topics.shtml");
} elsif($m%30==15) {
slashdLog "It's been a half hour... do my voodoo:";
nukeBlockCache();
newultra();
newfooter();
# If it's tomorrow, execute dailyStuff
my ($today)=sqlSelect("to_days(now())");
my ($yesterday)=getvars("today");
if($today ne $yesterday) {
setvar("today",$today);
slashdLog "It's Tomorrow:Run Slashd daily Voodoo";
system("$datadir/dailyStuff");
}
}
# Naptime
sleep(60);
}