#!/usr/bin/perl -w # -*- perl -*- # Blosxom # Author: Brian St. Pierre # Version: 0.1 # Home/Docs/Licensing: http://bstpierre.org/Projects/blwip/ # --- Configurable variables ----- # Where are the aggregator's entries kept? my $aggdir = "c:/cygwin/home/brian/blog/blagg"; # Where to place new posts (WIP == work in process)? my $wipdir = "c:/cygwin/home/brian/blog/wip"; # Where to place published posts? (ie. where to blog posts live)? my $datadir = "c:/cygwin/home/brian/blog/posts"; # Where does the edit script live? my $editscript = "/home/brian/bin/edit"; # How many entries should I show on the home page? # (0 == infinite) my $num_entries = 0; # Where does this script live? my $blwip = "/cgi-bin/blwip.cgi"; # Where to view entries? my $blog = "/cgi-bin/blosxom.cgi"; # Unlink (1) or rename (0)? # blagg requires rename or it will keep retrieving same posts! my $aggunlink = 0; # -------------------------------- use strict; use FileHandle; use File::Find; use File::stat; use Time::localtime; use CGI qw/:standard :netscape/; my $aggdir_current = $aggdir; # Take a gander at HTTP's PATH_INFO for optional blog name, archive yr/mo/day my $url = url(); $url =~ s/^included:/http:/; # Fix for Server Side Includes (SSI) my @pi = split m{/}, path_info(); shift @pi; my $pi_bl = ''; #while ($pi[0] =~ /^[a-zA-Z]\w*$/) #{ # $pi_bl .= '/' . shift @pi; #} sub getpost() { my $postnum = $_; my $title; my $body; my %post; print "getpost: $postnum"; return %post; } sub newpost() { my $fh = new FileHandle; my $fn = param("new"); my $title; my $body; if (-T "$aggdir_current/$fn.txt" && $fh->open("< $aggdir_current/$fn.txt")) { chomp($title = <$fh>); chomp($body = join '', <$fh>); } $fh->close; my $postnum; # Get the post number. if (-T "$wipdir/nextpost.dat" && $fh->open("< $wipdir/nextpost.dat")) { chomp($postnum = <$fh>); } $fh->close; # Increment the post number. $fh->open("> $wipdir/nextpost.dat"); print($fh ($postnum + 1) . "\n"); $fh->close; # Initialize the post. my $postfile; $postfile = sprintf("%s/%08d.txt", $wipdir, $postnum); $fh->open("> $postfile"); print($fh $title . "\n"); print($fh $body . "\n"); $fh->close; &editpost($postnum); &showpost($postnum); } sub editpost() { my ($postnum) = @_; my $postfile = sprintf("%s/%08d.txt", $wipdir, $postnum); # Launch the editor. if($editscript) { my $pid = fork(); if($pid == 0) { `bash $editscript $postfile`; exit(0); } } } my $num_shown = 0; sub showpost() { my ($postnum) = @_; my $body; my $title; my $fh = new FileHandle; my $fn = sprintf("%s/%08d.txt", $wipdir, $postnum); return if($num_entries != 0 and $num_shown >= $num_entries); if($fn =~ /[0-9]+\.txt$/) { if (-T $fn && $fh->open("< $fn")) { chomp($title = <$fh>); chomp($body = join '', <$fh>); print "

$title

"; print "$body"; $postnum = sprintf("%08d", $postnum); my $puburl = "$blwip?mode=pub&pub=$postnum"; my $delurl = "$blwip?mode=wipdel&del=$postnum"; my $refreshurl = "$blwip?mode=show&show=$postnum"; print "[ publish $postnum ]"; print "[ delete $postnum ]"; print "[ refresh ]"; $num_shown++; } } } sub wiplistfind() { my $fn = $File::Find::name; if(/[0-9]+\.txt$/) { $fn =~ s/$wipdir\/([0-9]+).txt/$1/; &showpost($fn); } } sub wiplist() { # Are we listing or showing? $num_entries = param('list') if(param('list')); find(\&wiplistfind, $wipdir); if($num_shown == 0) { print "

No posts in progress

\n"; } } sub wipshow() { &showpost(param('show')); } sub dopub() { my $postnum = param('pub'); my $postfile = sprintf("%s/%08d.txt", $wipdir, $postnum); my $fh = new FileHandle; my $title; my $body; if(-T $postfile && $fh->open("< $postfile")) { chomp($title = <$fh>); chomp($body = join '', <$fh>); $fh->close; $fh->open("> " . sprintf("%s/%08d.txt", $datadir, $postnum)); print($fh "$title\n"); print($fh "$body\n"); $fh->close; unlink($postfile); print "

Published $postnum

\n"; } else { print "

Error: $postnum does not exist.

\n"; } &wiplist; } sub wipdel() { my $postnum = param('del'); my $postfile = sprintf("%s/%08d.txt", $wipdir, $postnum); if(-T $postfile && unlink($postfile)) { print "

Deleted $postnum from work-in-progress

\n"; } else { print "

Error: $postnum does not exist.

\n"; } &wiplist; } sub aggdel() { foreach my $postname (param()) { next if $postname =~ /^mode$/; $postname =~ s/cb_//; my $postfile = "$aggdir/$postname.txt"; if(-T $postfile) { if($aggunlink) { unlink($postfile); } else { rename($postfile, $postfile . "_del"); } print "

Deleted $postname from aggregator.

\n"; } else { print "

Error: $postfile does not exist.

\n"; } } &wiplist; } sub listsubs() { print "

Subscriptions

\n\n"; } print header("text/html"); my $fh = new FileHandle; if(-T "$wipdir/head.html") { $fh->open("< $wipdir/head.html"); print join '', <$fh>; $fh->close; } else { print "no head"; } my $mode = param('mode'); &newpost if($mode =~ 'new'); &wiplist if($mode =~ 'list'); &wipshow if($mode =~ 'show'); &dopub if($mode =~ 'pub'); &wipdel if($mode =~ 'wipdel'); &aggdel if($mode =~ 'aggdel'); &listsubs if($mode =~ 'subs'); if(-T "$wipdir/foot.html") { $fh->open("< $wipdir/foot.html"); print join '', <$fh>; $fh->close; } else { print "\n"; }