/*
** Copyright 1998 - 1999 S. Varshavchik.  See COPYING for
** distribution information.
*/


/*
*/
#include	"sqwebmail.h"
#include	"config.h"
#include	"token.h"
#include	"sqconfig.h"
#include	"cgi/cgi.h"
#include	<stdio.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif
#include	<time.h>
#if HAVE_SYS_TIME_H
#include	<sys/time.h>
#endif
#include <fstream>

void tokennew()
{
time_t	t;

	time(&t);
	printf("<input type=\"hidden\" name=\"msgtoken\" value=\"%ld-%ld\" />",
		(long)t, (long)getpid());
}

void tokennewget()
{
time_t	t;

	time(&t);
	printf("&amp;msgtoken=%ld-%ld", (long)t, (long)getpid());
}

int tokencheck()
{
const char *token=cgi("msgtoken");

	if (!token || token[0] == '\0')
		return(0);
	auto savedtoken=read_sqconfig(".", TOKENFILE, 0);

	if (savedtoken && token == *savedtoken)
		return (-1);
	return (0);
}

void tokensave()
{
	write_sqconfig(".", TOKENFILE, cgi("msgtoken"));
}
