#!/usr/bin/perl -w
# Created by Ben Okopnik on Thu Jul 16 16:38:06 CDT 2009
use strict;

my ($loc, $seen, $name, %h);

# Where's the link file?
$loc = "/var/www/linuxgazette.net/cool_links.html";

# Because I'm lazy, I'll just reuse the 'mailparse' routine
while (<>) {
    chomp;
    $seen++ if /^$/;
    if ($seen){
        push @{$h{body}}, $_;
    }
    else {
        if (/^From\s+(.*)$/){
            $h{Envelope_From} = $1;
            next;
        }
        if (/^([^\s:]+):\s*(.*)$/){
            $name = $1;
            $h{$name} = $2;
        }
        else {
            s/^\s*(.*?)\s*$/ $1/;
            $h{$name} .= $_;
        }
    }
}

my ($link, $text);
for (@{$h{body}}){
	chomp;
	next if /^\s*$/;
	if (m#^\s*(f|ht)tp:#){
		$link = $_;
		last;
	}
	else {
		$text .= $_;
	}
}

open Fh, ">>$loc" or die "$loc: $!\n";
print Fh "<p><a href='$link'>$text</a></p>\n\n";
close Fh;