#! /usr/bin/python

# $Id: meantime.py,v 1.3 2000/03/29 05:47:03 mbp Exp $
# Copyright (C) 2000 by Martin Pool.

#    This software is provided 'as-is', without any express or implied
#    warranty.  In no event will the authors be held liable for any damages
#    arising from the use of this software.

#    Permission is granted to anyone to use this software for any purpose,
#    including commercial applications, and to alter it and redistribute it
#    freely, subject to the following restrictions:

#    1. The origin of this software must not be misrepresented; you must not
#       claim that you wrote the original software. If you use this software
#       in a product, an acknowledgment in the product documentation would be
#       appreciated but is not required.
#    2. Altered source versions must be plainly marked as such, and must not be
#       misrepresented as being the original software.
#    3. This notice may not be removed or altered from any source distribution.


import cgi
from os import environ
import gdbm
from sys import stdout, stderr
from time import gmtime, asctime, ctime, time
from string import find

def nice_get(dict, key):
	if dict.has_key(key):
		return dict[key]
	else:
		return None

def log(str):
	stderr.write(str + '\n')

form = cgi.FieldStorage()

datafile = gdbm.open('mtdata', 'c')

method = nice_get(environ, 'REQUEST_METHOD')
if method == 'POST':
	ims = form['tag'].value
	newvalue = form['newvalue'].value

	datafile[ims] = newvalue
	value = newvalue
	log('updated key %s to %s' % (ims, value))
else: # method == 'GET' i hope
	ims = nice_get(environ, 'HTTP_IF_MODIFIED_SINCE')
	if ims:
		log('got ims "%s"' % ims)
		# there might be extra parameters after the date such as the
		# length, but we ignore them
		pos = find(ims, ';')
		if pos > 0:
			ims = ims[:pos]
		value = nice_get(datafile, ims)
		log('retrieved value %s for key %s' % (`value`, `ims`))
	else:
		next = int(nice_get(datafile, 'Next') or 0)
		next = next + 1
		datafile['Next'] = str(next)
		ims = asctime(gmtime(next))
		log('Generated new fake time %s' % ims)
		value = None

	if value is None:
		value = '%s:%s %s' % \
			(nice_get(environ, 'REMOTE_ADDR') or 'Unknown',
			 nice_get(environ, 'REMOTE_PORT') or 'Unknown',
			 asctime(gmtime(time())))
		datafile[ims] = value
		log('remembering value "%s" for key "%s"' % (value, ims))


count_key = ims + ';count'
count = nice_get(datafile, count_key)
if count is None:
	count = 1
else:
	count = int(count) + 1
datafile[count_key] = `count`

datafile.close()

stdout.write("HTTP/1.1 200 OK\r\n");
stdout.write("Content-Type: text/html\r\n");
stdout.write("Last-Modified: " + ims + "\r\n");
stdout.write("Cache-Control: private\r\n");
stdout.write("Cache-Control: must-revalidate\r\n");
stdout.write("\r\n");

stdout.write("""<p>Your browser's cache has just been tagged and tracked
by <a href="http://www.linuxcare.com.au/mbp/meantime/">meantime</a>.  You
have visited this resource <b>%d</b> times using this tag.

<form action="nph-meantime.cgi" method="POST">
<input type="hidden" name="tag" value="%s">
<input type="text" name="newvalue" size="80" value="%s">
<input type="submit" value="Remember me!">
</form>
""" % (count, ims, value))

cgi.print_environ()

