#!/usr/bin/perl
####
#### Time Password
#### Created by Paul Dupuy, Jr.
####

# Use cgi-lib parser
require "cgi-lib.pl";
&ReadParse(*input);

# Who gets the "death" error message?
$mailerrorto = 'paul@hiwaay.net';


# open time password log
open (TIMEPASS, ">>timepasslog") || &death("Could not open timepasslog: $!");
$starttime = time;
print TIME "$starttime\n";


###
### Check for existing username and password
###

# Open yesterday data file
open (INPUT, "yesterday") || &death("Could not open yesterday: $!");

# Check yesterday data
while (<INPUT>)
{
	if ($_ =~ /\"$input{'username'}\"/)			## username exists
	{
		split;
		$password = $_[1];
		$userok = "yes";
		last;
	}
}
			
# Close yesterday data file
close (INPUT) || &death("Could not close yesterday: $!");




###
### If username is OK, email the password
###

if ($userok eq "yes")
{

	# Tell the user to check email
	&sentpassword;
	
} #endif username OK




###
### If username is *not* OK, generate an error message page
###

else
{
	# Display an Error message page
	&errormessage;

} #endelse username *not* OK



# close time password log
print TIMEPASS "$input{'username'} $password\n";
close (TIMEPASS) || &death("Could not close timepasslog: $!");






###
### Subroutines
###



##
## Password Sent page
##
sub sentpassword
{
	print "Content-type: text/html\n\n";
	
	print <<"AAA";
<HTML><HEAD><TITLE>Online Time Password Sent</TITLE></HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" 
ALINK="#C900D9" VLINK="#660099">

<!--Created by Paul Dupuy, Jr. @ Valley Internet-->

<CENTER><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" ALIGN="CENTER"><TR><TR><TD 
COLSPAN="3" HEIGHT="4"> </TD></TR><TD><IMG 
SRC="http://home.hiwaay.net/~paul/resume/time/head1.gif" 
WIDTH="133" HEIGHT="34" BORDER="0"
ALIGN="MIDDLE" ALT="Valley"></TD><TD WIDTH="13"></TD><TD><IMG
SRC="http://home.hiwaay.net/~paul/resume/time/head2.gif" 
WIDTH="179" HEIGHT="34" BORDER="0" 
ALIGN="MIDDLE" ALT="Internet"></TD></TR></TABLE>
<P>

<FONT SIZE="+2">Online Time Password</FONT><P><BR>

<FONT FACE="Arial, Helvetica" SIZE="+1">Check your email: your Time Password has been 
sent to you.</FONT><P><BR>

<A HREF="http://home.hiwaay.net/~paul/resume/time/time.html">Return 
to Online Time Request</A>

</CENTER></BODY></HTML>
AAA

} #endsub sentpassword



##
## Add new user to yesterday database
##
sub adduser
{
	# Open "yesterday" for appended output
	open (OUTPUT, '>>yesterday') || &death("Could not open yesterday: $!");

	# write the new user data
	print OUTPUT "\"$input{'username'}\" :: 0\n";
	
	# Close "yesterday"
	close (OUTPUT) || &death("Could not close yesterday: $!");

} #endsub adduser



##
## Make a password
##
sub makepassword
{
	$password = "";
	
	# Plant a random seed
	srand (time|$$);
	
	# Generation loop
	for ($x=0; $x < 8; $x++)
	{
		# Pick a number and make it an ASCII reference
		my $intval = int(rand(25)) + 97;		## a-z
		
		# Add the character to the password string
		$password .= sprintf ("%c", $intval);
	}

} #endsub makepassword



##
## Change the password
##
sub writepassword
{
	# rename "yesterday" to "another"
	rename ("yesterday","another") || &death("Could not rename yesterday to another: $!");
	
	# Open "another" for input
	open (INPUT, "another") || &death("Could not open another: $!");
	
	# Open "yesterday" for output
	open (OUTPUT, ">yesterday") || &death("Could not open yesterday: $!");

	# Read and write
	while (<INPUT>)
	{
		if ($_ =~ /\"$input{'username'}\"/)			## our user
		{
			# change the password
			s/:.*:/:$password:/;
		}
		
		print OUTPUT $_;
	}
	
	# close files
	close (INPUT) || &death("Could not close another: $!");
	close (OUTPUT) || &death("Could not close yesterday: $!");
	
	# delete "another"
	unlink "another" || &death("Could not remove another: $!");

} #endsub writepassword




##
## Error Message page
##
sub errormessage
{
	print "Content-type: text/html\n\n";
	
	print <<"AAA";
<HTML><HEAD><TITLE>Online Time Error</TITLE></HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" 
ALINK="#C900D9" VLINK="#660099">

<!--Created by Paul Dupuy, Jr. @ Valley Internet-->

<CENTER><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" ALIGN="CENTER"><TR><TR><TD 
COLSPAN="3" HEIGHT="4"> </TD></TR><TD><IMG 
SRC="http://home.hiwaay.net/~paul/resume/time/head1.gif" 
WIDTH="133" HEIGHT="34" BORDER="0"
ALIGN="MIDDLE" ALT="Valley"></TD><TD WIDTH="13"></TD><TD><IMG
SRC="http://home.hiwaay.net/~paul/resume/time/head2.gif" 
WIDTH="179" HEIGHT="34" BORDER="0" 
ALIGN="MIDDLE" ALT="Internet"></TD></TR></TABLE>
<P>

<FONT SIZE="+2">Online Time Error</FONT><P><BR>
<FONT SIZE="+1" FACE="Arial, Helvetica">
AAA

		print "The username you entered (\"$input{'username'}\") is not valid.<P>";
		
		print <<"AAA";
Please <A HREF="http://home.hiwaay.net/~paul/resume/time/timepass.html">try 
again</A>.</FONT><P></CENTER>

<!-- Sicut adhuc. -->

</BODY></HTML>

AAA

} #endsub errormessage




##
## Email the password
##
sub emailpassword
{
	open(MAIL, "|/usr/lib/sendmail -oi -t") || die ("Unable to open mail program.\n");

	print MAIL <<"AAA";
Subject: Online Time Password
From: Paul Dupuy, Jr. <Paul\@hiwaay.net>
To: $input{'username'}\@vallnet.com


     Valley Internet Online Time
     <http://home.hiwaay.net/~paul/resume/time/time.html>


     Your Time Password is: $password


     At Valley Internet, we are always looking for ways to better serve our 
customers and we hope you find this new feature helpful. If you have any 
questions or comments about our *Online Time* feature, please email us at 
<Paul\@hiwaay.net>.


Thank you,

Paul Dupuy, Jr.
Software & Web Developer

AAA

close(MAIL);

} #endsub emailpassword




##
## Handle system errors
##
sub death 
{
	$message = @_[0];
	
	#
	# Output an Error Page
	#
	print "Content-type: text/html\n\n";
		
	print <<"AAA";
<HTML><HEAD><TITLE>Online Time Not Available</TITLE></HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" 
ALINK="#C900D9" VLINK="#660099">

<!--Created by Paul Dupuy, Jr. @ Valley Internet-->

<CENTER><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" ALIGN="CENTER"><TR><TR><TD 
COLSPAN="3" HEIGHT="4"> </TD></TR><TD><IMG 
SRC="http://home.hiwaay.net/~paul/resume/time/head1.gif" 
WIDTH="133" HEIGHT="34" BORDER="0"
ALIGN="MIDDLE" ALT="Valley"></TD><TD WIDTH="13"></TD><TD><IMG
SRC="http://home.hiwaay.net/~paul/resume/time/head2.gif" 
WIDTH="179" HEIGHT="34" BORDER="0" 
ALIGN="MIDDLE" ALT="Internet"></TD></TR></TABLE>
<P>

<FONT SIZE="+2">Online Time is Not Available</FONT>
<P><BR>

<FONT SIZE="+1" FACE="Arial, Helvetica">Sorry, the Online Time program is 
not available right now.<P>
Please try again later.</FONT><P><BR>

<A HREF="http://home.hiwaay.net/~paul/resume/time/index.html">About Online Time</A><P></CENTER>

<!-- Sicut adhuc. -->

</BODY></HTML>
AAA
	

	#
	# Mail an Error Message
	#
	open(MAIL, "|/usr/lib/sendmail -oi -t") || die ("Unable to open mail program.\n");

	print MAIL <<"AAA";
Subject: Online Time Error
From: timepass.pl
To: $mailerrorto


     An error occurred during the execution of timepass.pl:

$message


http://fly.hiwaay.net:8000/~paul/resume/time/timepass.pl

AAA

	close(MAIL) || die ("Unable to close mail program.\n");

	# Quit
	exit;
	
} #endsub death


##
## Sicut adhuc.

1;