#!/usr/bin/perl

use LWP::Simple;
use CGI qw(:standard);
use URI::Escape;

# the path to the blog file
$blogfile = "/home/asisaid/data/blog.txt";
# the absolute URL to the blog.cgi file
$wwwblog = "http://asisaid.com/journal/";

#if ($ENV{'REMOTE_USER'} ne "tbutler") {
  print "Sorry, only Tim is allowed to write blog entries. :)\n";
#}
#else {
  $action = param('action');
  if ($action eq "" || $action eq "main") {
    print header;
    print <<HTML_QUOTE;
<html>
<head>
<title>Add a Blog Entry</title>
</head>
<body bgcolor="#C0C0FF">
<font face="Verdana, Arial, sans-serif" size="-1">
Please enter your blog entry below. Press 'Preview' to see how it will look.
<br><br>
<form method=post action="addblog.cgi">
<input type=hidden name=action value=preview>
<table>
<tr><td><font size="-2">Mood: </font></td><td><select name=mood>
<option value="Ecstatic">Ecstatic
<option value="Very happy">Very happy
<option selected value="Happy">Happy
<option value="Not happy, not sad">Not happy, not sad
<option value="Both happy and sad">Both happy and sad
<option value="Puzzled">Puzzled
<option value="Thoughtful">Thoughtful
<option value="Sad">Sad
<option value="Very sad">Very sad
<option value="Angry">Angry
<option value="Fuming">Fuming
</select>
</td><td><font size="-2">Location: </font></td><td><input name="location" type="text">

</td><td><font size="-2">Now playing: </font></td><td><input type=text name=nowplaying size=21 value="n/a">
</td></tr>
<tr><td><font size="-2">Subject: </font></td><td colspan=5><input type=text name=subject size=83>
<tr><td>Intro:</td><td colspan=5><textarea name=top rows=8 cols=80></textarea>
</td></tr>
<tr><td>Body:</td><td colspan=5><textarea name=entry rows=8 cols=80></textarea>
</td></tr>
<tr><td><font size="-2">Lyrics URL:</td><td colspan=5><input type=text name=lyrics size=83>
</td></tr>
<tr><td colspan=6><input type=submit value="Preview">
</td></tr>
</table>
</form>
</font>
</body>
</html>
HTML_QUOTE
  }
  if ($action eq "preview") {
    $mood = param('mood');
    $location = param('location');
    $nowplaying = param('nowplaying');
    $subject = param('subject');
    $lyrics = param('lyrics');
    $top = param('top');
    $entry = param('entry');

    $dispentry = $entry;
    $dispentry=~s/\r/<br>/g;

    $disptop = $top;
    $disptop=~s/\r/<br>/g;

    print header;
    print <<HTML_QUOTE;
<html>
<head>
<title>Blog Entry - Preview</title>
</head>
<body bgcolor="#C0C0FF">
<font face="Verdana, Arial, sans-serif" size="-1">
This is how your blog entry will look (the time may be different when you submit the entry):
<br><br>
<hr color="#9090C0" noshade>
HTML_QUOTE
    $mytime = localtime(time()  - 3600);
    $mytime=~s/.*? (.*) (.*):.* (.*)/$1, $3/;
    $timepart = $2;
    print "<font size=\"-1\"><b>$mytime</b>: <i>$subject</i></font><br>\n";
    if ($lyrics ne "") {
      $dispnowplaying = "<a href=\"$lyrics\">$nowplaying</a>";
    }
    else {
      $dispnowplaying = $nowplaying;
    }
    print <<HTML_QUOTE;
<font size="-2"><b>Time:</b> <i>$timepart</i> - <b>Mood:</b> <i>$mood</i> - <b>Location:</b> <i>$location</i> - <b>Now playing:</b> <i>$dispnowplaying</i></font>
<br><br>
<font size="-1">
$disptop<BR><BR>$dispentry
</font>
<hr color="#9090C0" noshade>
<br>
If you are happy with this, go ahead and click Submit below. If not, go back and edit your entry.
<br><br>
<form method=post action="addblog.cgi">
<input type=hidden name="action" value="post">
<input type=hidden name="subject" value="$subject">
<input type=hidden name="mood" value="$mood">
<input type=hidden name="location" value="$location">
<input type=hidden name="nowplaying" value="$nowplaying">
<input type=hidden name="lyrics" value="$lyrics">

HTML_QUOTE
    print "<input type=hidden name=\"entry\" value=\"" . uri_escape($entry) . "\">\n";
    print "<input type=hidden name=\"top\" value=\"" . uri_escape($top) . "\">\n";
    print <<HTML_QUOTE;
<input type=submit value="Submit">
</form>
</font>
</body>
</html>
HTML_QUOTE
  }
  if ($action eq "post") {
    $mood = param('mood');
    $location = param('location');
    $nowplaying = param('nowplaying');
    $subject = param('subject');
    $lyrics = param('lyrics');
    $top = param('top');
    $entry = param('entry');
    $now = time();

    $entry = "%20" unless ($entry);

    open(BLOG, ">>$blogfile");
    print BLOG "${now}:--:${subject}:--:${mood}:--:${location}:--:${nowplaying}:--:${lyrics}\n$top\n$entry\n";
    close(BLOG);
 
    $ping = get("http://newhome.weblogs.com/pingSiteForm?name=asisaid.com:+Tim%27s+Journal&url=http%3A%2F%2Fasisaid.com%2Fjournal%2F");
 
    $ping = get("http://ping.blo.gs/?url=http%3A%2F%2Funinetsolutions.com%2Ftbutler%2Fjournal%2F&name=asisaid.com:+Tim%27s%20Journal&rssUrl=http%3A%2F%2Fasisaid.com%2Fjournal%2Fjournal.rss");
    

          
    @pinglist = ("http://rpc.blogrolling.com/pinger/", "http://www.blogshares.com/rpc.php 1");
    
    foreach $pingitem (@pinglist) {
    	ping($pingitem);
    }
    
    print "Status: 302 Moved\nLocation: $wwwblog\n\n";
  }
#}


sub ping {

	$pingaddress = $_[0];
	
	# Create a user agent object
	use LWP::UserAgent;
	$ua = new LWP::UserAgent;
	$ua->agent("asisaid/0.1 " . $ua->agent);

	# Create a request
	my $req = new HTTP::Request POST =>$pingaddress;

	# put XML payload here.
	#

	$xmlcontent = <<"XMLEOT";
<?xml version="1.0"?>
<methodCall>
	<methodName>weblogUpdates.ping</methodName>
	<params>
		<param>
			<value>asisaid.com: Tim's Journal</value>
		</param>
		<param>
			<value>http://asisaid.com/journal/</value>
		</param>
	</params>
</methodCall>
XMLEOT

	$req->content_type('text/xml');
	$req->content($xmlcontent);

	# Pass request to the user agent and get a response back
	my $res = $ua->request($req);

	# Check the outcome of the response

	if ($res->is_success) {
	
		open (LOG, ">/home/asisaid/data/blogdir/bloglog.txt");
		print LOG "$pingaddress\n";
		print LOG $res->content;
		close (LOG);
		
		return 1;
	} else {
		open (LOG, ">/home/asisaid/data/blogdir/bloglog.txt");
		print LOG "didn't work!!!\n";
		close (LOG);	
	
		return 0;
	}	
}

